Class: VmShepherd::AwsManager
- Inherits:
-
Object
- Object
- VmShepherd::AwsManager
show all
- Includes:
- RetryHelper
- Defined in:
- lib/vm_shepherd/aws_manager.rb
Constant Summary
collapse
- OPS_MANAGER_INSTANCE_TYPE =
't2.medium'
- DO_NOT_TERMINATE_TAG_KEY =
'do_not_terminate'
- ELB_SECURITY_GROUP_NAME =
'ELB Security Group'
- CREATE_IN_PROGRESS =
'CREATE_IN_PROGRESS'
- CREATE_COMPLETE =
'CREATE_COMPLETE'
- UPDATE_IN_PROGRESS =
'UPDATE_IN_PROGRESS'
- UPDATE_COMPLETE =
'UPDATE_COMPLETE'
- UPDATE_COMPLETE_CLEANUP_IN_PROGRESS =
'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS'
- ROLLBACK_IN_PROGRESS =
'ROLLBACK_IN_PROGRESS'
- ROLLBACK_COMPLETE =
'ROLLBACK_COMPLETE'
- DELETE_IN_PROGRESS =
'DELETE_IN_PROGRESS'
- DELETE_COMPLETE =
'DELETE_COMPLETE'
- DELETE_FAILED =
'DELETE_FAILED'
Constants included
from RetryHelper
RetryHelper::RETRY_INTERVAL, RetryHelper::RETRY_LIMIT
Instance Method Summary
collapse
#retry_until
Constructor Details
#initialize(env_config:, logger:) ⇒ AwsManager
Returns a new instance of AwsManager.
25
26
27
28
29
30
31
32
33
|
# File 'lib/vm_shepherd/aws_manager.rb', line 25
def initialize(env_config:, logger:)
AWS.config(
access_key_id: env_config.fetch('aws_access_key'),
secret_access_key: env_config.fetch('aws_secret_key'),
region: env_config.fetch('region'),
)
@env_config = env_config
@logger = logger
end
|
Instance Method Details
#clean_environment ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/vm_shepherd/aws_manager.rb', line 124
def clean_environment
env_config.fetch('outputs').fetch('subnets').each do |aws_subnet_id|
clear_subnet(aws_subnet_id) if aws_subnet_id
end
env_config.fetch('elbs', []).each do |elb_config|
delete_elb(elb_config['name'])
end
bucket_names = env_config.fetch('outputs', {}).fetch('s3_bucket_names', []).compact
bucket_names.each do |bucket_name|
next if bucket_name.empty?
bucket = AWS::S3.new.buckets[bucket_name]
if bucket && bucket.exists?
logger.info("clearing bucket: #{bucket_name}")
bucket.clear!
end
end
delete_stack(env_config.fetch('stack_name'))
end
|
#deploy(ami_file_path:, vm_config:) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/vm_shepherd/aws_manager.rb', line 87
def deploy(ami_file_path:, vm_config:)
logger.info('Starting AMI Instance creation')
instance =
retry_until do
begin
AWS.ec2.instances.create(instance_create_params(ami_file_path, vm_config))
rescue AWS::EC2::Errors::InvalidIPAddress::InUse
false
end
end
logger.info('waiting until the instance status is running')
retry_until do
begin
status = instance.status
logger.info("current status: #{status}")
status == :running
rescue AWS::EC2::Errors::InvalidInstanceID::NotFound
false
end
end
vm_ip_address = vm_config.fetch('vm_ip_address', nil)
if vm_ip_address
logger.info('Associating existing IP to the instance')
elastic_ip = AWS::EC2::ElasticIp.new(vm_ip_address)
else
logger.info('Creating an Elastic IP and assigning it to the instance')
elastic_ip = AWS.ec2.elastic_ips.create(vpc: true)
retry_until do
elastic_ip.exists?
end
end
instance.associate_elastic_ip(elastic_ip)
instance.add_tag('Name', value: vm_config.fetch('vm_name'))
end
|
#destroy(vm_config) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/vm_shepherd/aws_manager.rb', line 146
def destroy(vm_config)
AWS.ec2.instances.with_tag('Name', vm_config.fetch('vm_name')).each do |instance|
if instance.tags['Name'] == vm_config.fetch('vm_name')
vm_ip_address = vm_config.fetch('vm_ip_address', nil)
elastic_ip = instance.elastic_ip unless vm_ip_address
if elastic_ip
elastic_ip.disassociate
elastic_ip.delete
end
instance.terminate
end
end
end
|
#prepare_environment(cloudformation_template_file) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/vm_shepherd/aws_manager.rb', line 35
def prepare_environment(cloudformation_template_file)
template = File.read(cloudformation_template_file)
cfm = AWS::CloudFormation.new
stack_name = env_config.fetch('stack_name')
stack = cfm.stacks[stack_name]
if stack and env_config.fetch('update_existing', false)
logger.info('Found existing stack - upgrading it')
stack.update(:template => template, parameters: env_config.fetch('parameters'), capabilities: ['CAPABILITY_IAM'])
waiting_for_status = UPDATE_COMPLETE
else
logger.info('Starting CloudFormation Stack Creation')
stack = cfm.stacks.create(stack_name, template, parameters: env_config.fetch('parameters'), capabilities: ['CAPABILITY_IAM'])
waiting_for_status = CREATE_COMPLETE
end
logger.info("Waiting for status [#{waiting_for_status}] on stack [#{stack.name}]")
retry_until(retry_limit: 60, retry_interval: 30) do
status = stack.status
logger.info("current stack status: #{status}")
case status
when CREATE_COMPLETE
true
when UPDATE_COMPLETE
true
when CREATE_IN_PROGRESS
false
when UPDATE_IN_PROGRESS
false
when UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
false
when ROLLBACK_IN_PROGRESS
false
else
if ROLLBACK_COMPLETE == status
logger.info("Rolling back stack [#{stack.name}]; events listed below")
stack.events.select { |event| event.resource_status_reason }.each do |failed_event|
logger.info("#{failed_event.resource_properties} #{failed_event.resource_status_reason}")
end
stack.delete
end
raise "Unexpected status for stack #{env_config.fetch('stack_name')} : #{status}"
end
end
env_config.fetch('elbs', []).each do |elb_config|
create_elb(stack, elb_config)
end
end
|