Class: Aws::Controller::AwsController

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vpc_id, security_groups, subnet_id, key_pair_name) ⇒ AwsController

Returns a new instance of AwsController.



10
11
12
13
14
15
16
17
18
# File 'lib/aws-controller.rb', line 10

def initialize(vpc_id, security_groups, subnet_id, key_pair_name)
  @ec2 = AWS::EC2.new(
    :access_key_id => Aws::Controller::Access_key_id,
    :secret_access_key => Aws::Controller::Secret_access_key)
  @vpc = @ec2.vpcs[vpc_id]
  @security_groups = security_groups
  @subnet_id = subnet_id
  @key_pair = @ec2.key_pairs[key_pair_name]
end

Instance Attribute Details

#ec2Object

Returns the value of attribute ec2.



8
9
10
# File 'lib/aws-controller.rb', line 8

def ec2
  @ec2
end

#key_pairObject

Returns the value of attribute key_pair.



8
9
10
# File 'lib/aws-controller.rb', line 8

def key_pair
  @key_pair
end

#security_groupsObject

Returns the value of attribute security_groups.



8
9
10
# File 'lib/aws-controller.rb', line 8

def security_groups
  @security_groups
end

#subnet_idObject

Returns the value of attribute subnet_id.



8
9
10
# File 'lib/aws-controller.rb', line 8

def subnet_id
  @subnet_id
end

#vpcObject

Returns the value of attribute vpc.



8
9
10
# File 'lib/aws-controller.rb', line 8

def vpc
  @vpc
end

Instance Method Details

#create_image(instance_id, name) ⇒ Object

create image from instance Input: instance_id, name



22
23
24
25
# File 'lib/aws-controller.rb', line 22

def create_image(instance_id, name)
  @ec2.images.create(:instance_id => instance_id, :name => name)
  p 'creating image %s from instance %s' % [name, instance_id]
end

#create_instance(image_id, app, stage) ⇒ Object

create instance from image Input: image_id, app, stage



29
30
31
32
33
34
35
36
37
38
# File 'lib/aws-controller.rb', line 29

def create_instance(image_id, app, stage)
  instance = @ec2.instances.create(:image_id => image_id,
                       :subnet => @subnet_id,
                       :security_groups => @security_groups,
                       :key_pair => @key_pair)
  instance.tags.app = app
  instance.tags.stage = stage
  eip = @ec2.elastic_ips.create(:vpc => @vpc)
  instance.associate_elastic_ip(eip)
end

#get_instances(app, stage) ⇒ Object

get all instances of given app & stage Input: app, stage Output: Hash of all app & stage instances with status(“i-1bt6e5bd”=>“running”)



90
91
92
93
94
95
96
97
98
99
# File 'lib/aws-controller.rb', line 90

def get_instances(app , stage)
  p 'getting instances for app: %s, stage: %s' % [app, stage]
  instances = {}
  @ec2.instances.each do |instance|
    if instance.tags.stage == stage and instance.tags.app == app
      instances[instance.id] = instance.status.to_s
    end
  end
  instances
end

#reboot(app, stage) ⇒ Object

reboot all instances of given app & stage Input: app, stage



67
68
69
70
71
72
73
74
# File 'lib/aws-controller.rb', line 67

def reboot(app, stage)
  get_instances(app, stage).each do |instance|
    eip = instance.elastic_ip
    eip.disassociate
    instance.reboot
    instance.associate_elastic_ip(eip)
  end
end

#start(app, stage) ⇒ Object

start all stopped instances of given app & stage Input: app, stage



55
56
57
58
59
60
61
62
63
# File 'lib/aws-controller.rb', line 55

def start(app, stage)
  get_instances(app, stage).each do |instance, status|
    if status == 'stopped'
      eip = @ec2.elastic_ips.create(:vpc => @vpc)
      instance.associate_elastic_ip(eip)
      instance.start
    end
  end
end

#stop(app, stage) ⇒ Object

stop all running instances of given app & stage Input: app, stage



42
43
44
45
46
47
48
49
50
51
# File 'lib/aws-controller.rb', line 42

def stop(app, stage)
  get_instances(app, stage).each do |instance|
    if status == 'running'
      eip = instance.elastic_ip
      eip.disassociate
      eip.release
      instance.stop
    end
  end
end

#terminate(app, stage) ⇒ Object

terminate all instances of given app & stage Input: app, stage



78
79
80
81
82
83
84
85
# File 'lib/aws-controller.rb', line 78

def terminate(app, stage)
  get_instances(app, stage).each do |instance|
    eip = instance.elastic_ip
    eip.disassociate
    eip.release
    instance.terminate
  end
end