-
Notifications
You must be signed in to change notification settings - Fork 52
/
ami.yml
132 lines (115 loc) · 5.01 KB
/
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
---
- name: ensure we have the parameters necessary to package the VM image
hosts: 'localhost'
connection: 'local'
become: no
gather_facts: no
pre_tasks:
- name: ensure all required variables are set
fail:
msg: 'This playbook requires {{ item }} to be set.'
when: item not in vars and item not in hostvars[inventory_hostname]
with_items:
- origin_ci_aws_stage_strategy
- origin_ci_inventory_dir
- origin_ci_aws_region
- origin_ci_hosts
- name: ensure only one AWS instance is running
fail:
msg: 'Packaging AMIs with more than one AWS EC2 instance up is not supported.'
when: "groups['{{ origin_ci_hosts }}'] | length | int > 1"
- name: package the VM image
hosts: 'localhost'
connection: 'local'
become: no
gather_facts: no
tasks:
- name: determine the inventory hostname for the host we are packaging
set_fact:
origin_ci_aws_hostname: '{{ groups[origin_ci_hosts][0] }}'
- name: determine AWS EC2 image metadata
set_fact:
origin_ci_aws_ami_os: "{{ hostvars[origin_ci_aws_hostname]['origin_ci_aws_ami_tags']['operating_system'] }}"
origin_ci_aws_ami_stage: "{{ hostvars[origin_ci_aws_hostname]['origin_ci_aws_ami_tags']['image_stage'] }}"
origin_ci_aws_instance_id: "{{ hostvars[origin_ci_aws_hostname]['origin_ci_aws_instance_id'] }}"
origin_ci_aws_instance_name: "{{ hostvars[origin_ci_aws_hostname]['origin_ci_aws_instance_name'] }}"
- name: register stage upgrades
set_fact:
origin_ci_image_upgrades:
bare: base
base: build
build: install
- name: update image stage if we are upgrading stages
set_fact:
origin_ci_aws_ami_stage: '{{ origin_ci_image_upgrades[origin_ci_aws_ami_stage] }}'
when: origin_ci_aws_stage_strategy == 'next'
- name: update image stage if we are upgrading stages
set_fact:
origin_ci_aws_ami_stage: '{{ origin_ci_aws_stage_strategy }}'
when: origin_ci_aws_stage_strategy != 'next'
- name: package the Amazon machine image from the running instance
ec2_ami:
instance_id: '{{ origin_ci_aws_instance_id }}'
region: '{{ origin_ci_aws_region }}'
state: present
name: '{{ origin_ci_aws_instance_name }}'
description: 'OpenShift Origin development AMI on {{ origin_ci_aws_ami_os }} at the {{ origin_ci_aws_ami_stage }} stage.'
tags:
Name: '{{ origin_ci_aws_instance_name }}'
operating_system: '{{ origin_ci_aws_ami_os }}'
image_stage: '{{ origin_ci_aws_ami_stage }}'
ready: 'no'
wait: yes
wait_timeout: 3600
- name: determine the host variables file for the AWS EC2 host
set_fact:
origin_ci_aws_host_vars: '{{ origin_ci_inventory_dir }}/host_vars/{{ origin_ci_aws_hostname }}.yml'
- name: generate a new set of host variables with the stage updated
command: "/usr/bin/jq '.origin_ci_aws_ami_stage = \"{{ origin_ci_aws_ami_stage }}\"' {{ origin_ci_aws_host_vars }}"
register: origin_ci_aws_updated_host_vars
- name: update the image stage in the host variables for the instance
copy:
content: '{{ origin_ci_aws_updated_host_vars.stdout }}'
dest: '{{ origin_ci_aws_host_vars }}'
- name: Set AMI ID for AMI snapshotted from current instance
block:
- name: determine AWS EC2 AMI name
set_fact:
origin_ci_aws_ami_name: "{{ hostvars[origin_ci_aws_hostname]['origin_ci_aws_instance_name'] }}"
- name: search for an AMI to update
ec2_ami_find:
region: '{{ origin_ci_aws_region }}'
ami_tags:
Name: '{{ origin_ci_aws_ami_name }}'
sort: 'creationDate'
sort_order: descending
sort_end: 1
no_result_action: fail
register: ami_facts
retries: 5
delay: 10
until: ami_facts.results is defined
- name: determine which AMI to update
set_fact:
origin_ci_aws_ami_id: '{{ ami_facts.results[0].ami_id }}'
when: origin_ci_aws_source_ami is not defined or not origin_ci_aws_source_ami
- name: Verify AMI ID for source AMI of current instance
block:
- name: determine AWS EC2 source AMI ID
set_fact:
origin_ci_aws_ami_id: "{{ hostvars[origin_ci_aws_hostname]['origin_ci_aws_ami_id'] }}"
- name: ensure AMI ID variable is set
fail:
msg: '{{ item }} must be set when updating source AMI.'
when: item not in vars and item not in hostvars[inventory_hostname]
with_items:
- origin_ci_aws_ami_id
when: origin_ci_aws_source_ami is defined and origin_ci_aws_source_ami
- name: add any additional tags
ec2_tag:
region: '{{ origin_ci_aws_region }}'
resource: '{{ origin_ci_aws_ami_id }}'
tags: "{ '{{ item.key }}': '{{ item.value }}' }"
state: present
with_dict: '{{ origin_ci_aws_additional_tags }}'
when: "origin_ci_aws_additional_tags | length > 0"