-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate-AMI.yml
110 lines (100 loc) · 4.15 KB
/
create-AMI.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
- name: Create Amazon Machine Images
hosts: local
connection: local
vars_files:
- vars/AMI-template-instances.var.yml
pre_tasks:
- name: get basic_facts
set_fact:
basic_fact={{ item }}
when: item.Environment == Environment
with_items: "{{ basic_facts }}"
- name: set facts to environment from basic_fact
set_fact: "{{ item.key }}={{ item.value }}"
with_dict: "{{ basic_fact }}"
when: basic_fact is defined
tasks:
- name: Get stack name
set_fact:
stack_name: "{{ item.name }}"
when: item.template_parameters.Environment == Environment
with_items: "{{ cloudformation_stacks }}"
- name: Get current stack facts
cloudformation_facts:
region: "{{ region }}"
stack_name: "{{ stack_name }}"
stack_resources: true
register: opsworks_facts
- name: Set new instance facts
set_fact:
InstanceId: "{{ opsworks_facts.ansible_facts.cloudformation[stack_name].stack_outputs[layer + 'TemplateInstanceId'] }}"
InstanceName: "{{ Environment }}-{{ service_name_lower }}-{{ layer }}-image"
- name: Wait for instance startup
shell: |
STATUS=$(aws ec2 describe-instances --region {{ region }} --instance-ids {{ InstanceId }} --query "Reservations[].Instances[].State.Name" --output text) || return 1
echo "Instance {{ InstanceId }} status: $STATUS" >&2
for retry in `seq 1 6`; do
if [ "$STATUS" = "pending" ]; then
sleep 10
$(aws ec2 describe-instances --region {{ region }} --instance-ids {{ InstanceId }} --query "Reservations[].Instances[].State.Name" --output text) || return 1
echo "Instance {{ InstanceId }} status: $STATUS" >&2
else
break
fi
done
if [ "$STATUS" != "running" ]; then
echo "Failed to start {{ InstanceId }}, status $STATUS - aborting" >&2
exit 2
fi
- name: Wait for instance configuration
shell: |
DEPLOYMENT_ID=None
for retry in `seq 1 180`; do
if [ "$DEPLOYMENT_ID" = "None" ]; then
DEPLOYMENT_ID=$(aws ssm list-commands --region {{ region }} --instance-id {{ InstanceId }} \
--filters "key=DocumentName,value=AWS-ApplyChefRecipes" "key=InvokedAfter,value={{ timestamp }}" \
--query "Commands|[0].CommandId" --output text) || exit 1
fi
if [ "$DEPLOYMENT_ID" = "None" ]; then
echo "Instance {{ InstanceId }} has no deployment yet..." >&2
else
STATUS=$(aws ssm list-commands --region {{ region }} --command-id $DEPLOYMENT_ID \
--query "Commands|[0].Status" --output text) || exit 1
echo "{{ layer }} Instance {{ InstanceId }} deployment [$DEPLOYMENT_ID] status: $STATUS" >&2
if [ "$STATUS" != "Pending" ] && [ "$STATUS" != "InProgress" ] && [ "$STATUS" != "None" ]; then
break
fi
fi
sleep 20
done
if [ "$STATUS" != "Success" ]; then
echo "Failed to deploy to instance {{ InstanceId }}, status $STATUS - aborting" >&2
exit 2
fi
- name: Stop instance
ec2:
instance_ids:
- "{{ InstanceId }}"
region: "{{ region }}"
state: stopped
wait: True
- name: Create new ami
ec2_ami:
instance_id: "{{ InstanceId }}"
region: "{{ region }}"
wait: yes
name: "{{ InstanceName }}-{{ timestamp | replace(':', '-') }}"
description: "Base image for {{ Environment }} {{ service_name }} {{ layer }} layer"
tags:
Name: "{{ InstanceName }}-{{ timestamp | replace(':', '-') }}"
Environment: "{{ Environment }}"
Service: "{{ service_name }}"
Layer: "{{ layer | lower }}"
Division: "{{ Division }}"
Owner: "{{ Owner }}"
Version: "{{ image_version | default('1.0') }}"
register: new_image
- name: Record AMI ID
shell: |
aws ssm put-parameter --overwrite --type String --name "/config/CKAN/{{ Environment }}/app/{{ service_name_lower }}/{{ layer }}AmiId" --value "{{ new_image.image_id }}"