Class: Roark::Ami

Inherits:
Object
  • Object
show all
Defined in:
lib/roark/ami.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Ami

Returns a new instance of Ami.



6
7
8
9
10
11
12
13
# File 'lib/roark/ami.rb', line 6

def initialize(args)
  @aws    = args[:aws]
  @ami_id = args[:ami_id]
  @name   = args[:name]

  @region   = @aws.region
  @logger   = Roark.logger
end

Instance Attribute Details

#ami_idObject

Returns the value of attribute ami_id.



4
5
6
# File 'lib/roark/ami.rb', line 4

def ami_id
  @ami_id
end

#awsObject

Returns the value of attribute aws.



4
5
6
# File 'lib/roark/ami.rb', line 4

def aws
  @aws
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/roark/ami.rb', line 4

def name
  @name
end

Instance Method Details

#add_tags(tags) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/roark/ami.rb', line 113

def add_tags(tags)
  begin
    tag tags
  rescue AWS::Errors::Base => e
    return Response.new :code => 1, :message => e.message
  end
  Response.new :code => 0, :message => 'Tagging completed successfully.'
end

#authorize_account_ids(account_ids) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/roark/ami.rb', line 122

def ()
  begin
    authorize 
  rescue AWS::Errors::Base => e
    return Response.new :code => 1, :message => e.message
  end
  Response.new :code => 0, :message => 'Authorizations completed successfully.'
end

#available?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/roark/ami.rb', line 131

def available?
  state == :available
end

#create_amiObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/roark/ami.rb', line 28

def create_ami
  @logger.info "Creating AMI '#{@name}' from Instance '#{instance_id}'."
  begin
    ami = instance.create_ami_from_instance
    @ami_id = ami.id
  rescue AWS::Errors::Base => e
    return Response.new :code => 1, :message => e.message
  end
  @logger.info "AMI '#{@ami_id}' created."
  Response.new :code => 0, :message => "AMI '#{@ami_id}' created."
end

#create_instance(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/roark/ami.rb', line 15

def create_instance(args)
  @logger.info "Creating instance in '#{@region}'."
  begin
    instance.create :parameters => args[:parameters],
                    :template   => args[:template]
  rescue AWS::Errors::Base => e
    return Response.new :code => 1, :message => e.message
  end

  @logger.info "Instance created."
  Response.new :code => 0, :message => 'Instance created.'
end

#destroyObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/roark/ami.rb', line 60

def destroy
  @logger.info "Destroying AMI '#{@ami_id}'."
  begin
    ec2_destroy_ami.destroy @ami_id
  rescue AWS::Errors::Base, AWS::Core::Resource::NotFound => e
    return Response.new :code => 1, :message => e.message
  end
  @logger.info "Destroy completed succesfully."
  Response.new :code => 0, :message => "Destroy completed succesfully."
end

#destroy_instanceObject



50
51
52
53
54
55
56
57
58
# File 'lib/roark/ami.rb', line 50

def destroy_instance
  begin
    instance.destroy
  rescue AWS::Errors::Base => e
    return Response.new :code => 1, :message => e.message
  end
  @logger.info "Instance destroyed."
  Response.new :code => 0, :message => "Instance destroyed."
end

#exists?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/roark/ami.rb', line 139

def exists?
  ec2_ami_state.exists? @ami_id
end

#instance_idObject



147
148
149
# File 'lib/roark/ami.rb', line 147

def instance_id
  instance.instance_id
end

#pending?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/roark/ami.rb', line 135

def pending?
  state == :pending
end

#stateObject



143
144
145
# File 'lib/roark/ami.rb', line 143

def state
  ec2_ami_state.state @ami_id
end

#stop_instanceObject



40
41
42
43
44
45
46
47
48
# File 'lib/roark/ami.rb', line 40

def stop_instance
  @logger.info "Stopping instance."
  begin
    instance.stop
  rescue AWS::Errors::Base => e
    return Response.new :code => 1, :message => e.message
  end
  Response.new :code => 0, :message => "Instance stopped."
end

#wait_for_amiObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/roark/ami.rb', line 96

def wait_for_ami
  while !exists? || pending?
    @logger.info "Waiting for AMI creation to complete. Current state: '#{state}'."
    sleep 15
  end

  if available?
    msg = "AMI completed succesfully."
    @logger.info msg
    Response.new :code => 0, :message => msg
  else
    msg = "AMI did not complete succesfully."
    @logger.info msg
    Response.new :code => 1, :message => msg
  end
end

#wait_for_instanceObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/roark/ami.rb', line 71

def wait_for_instance
  while instance.in_progress? || !instance.exists?
    @logger.info "Waiting for instance to come online."
    sleep 60
  end

  if instance.success?
    msg = "Instance '#{instance_id}' completed succesfully."
    @logger.info msg
    Response.new :code => 0, :message => msg
  else
    msg = "Instance did not complete succesfully."
    @logger.info msg
    Response.new :code => 1, :message => msg
  end
end

#wait_for_instance_to_stopObject



88
89
90
91
92
93
94
# File 'lib/roark/ami.rb', line 88

def wait_for_instance_to_stop
  while instance.status != :stopped
    @logger.info "Waiting for instance '#{instance_id}' to stop. Current state: '#{instance.status}'."
    sleep 15
  end
  Response.new :code => 0, :message => "Instance stopped."
end