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 =
'm3.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'
- ROLLBACK_IN_PROGRESS =
'ROLLBACK_IN_PROGRESS'
- ROLLBACK_COMPLETE =
'ROLLBACK_COMPLETE'
- DELETE_IN_PROGRESS =
'DELETE_IN_PROGRESS'
- DELETE_COMPLETE =
'DELETE_COMPLETE'
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.
19
20
21
22
23
24
25
26
27
|
# File 'lib/vm_shepherd/aws_manager.rb', line 19
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/vm_shepherd/aws_manager.rb', line 103
def clean_environment
['public_subnet_id', 'private_subnet_id'].each do |subnet_id|
aws_subnet_id = env_config.fetch('outputs').fetch(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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/vm_shepherd/aws_manager.rb', line 58
def deploy(ami_file_path:, vm_config:)
image_id = read_ami_id(ami_file_path)
logger.info('Starting AMI Instance creation')
instance =
retry_until do
begin
AWS.ec2.instances.create(
image_id: image_id,
key_name: env_config.fetch('outputs').fetch('ssh_key_name'),
security_group_ids: [env_config.fetch('outputs').fetch('security_group')],
subnet: env_config.fetch('outputs').fetch('public_subnet_id'),
instance_type: OPS_MANAGER_INSTANCE_TYPE
)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/vm_shepherd/aws_manager.rb', line 126
def destroy(vm_config)
AWS.ec2.instances.each do |instance|
if instance.tags.to_h['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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/vm_shepherd/aws_manager.rb', line 29
def prepare_environment(cloudformation_template_file)
template = File.read(cloudformation_template_file)
cfm = AWS::CloudFormation.new
logger.info('Starting CloudFormation Stack Creation')
stack = cfm.stacks.create(env_config.fetch('stack_name'), template, parameters: env_config.fetch('parameters'), capabilities: ['CAPABILITY_IAM'])
logger.info("Waiting for status: #{CREATE_COMPLETE}")
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 CREATE_IN_PROGRESS
false
when ROLLBACK_IN_PROGRESS
false
else
stack.delete if status == ROLLBACK_COMPLETE
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
|