-
Notifications
You must be signed in to change notification settings - Fork 105
/
deploy.yaml
178 lines (156 loc) · 4.62 KB
/
deploy.yaml
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Provisions a new Webserver
- hosts: localhost
gather_facts: no
vars:
region: us-west-2
keypair_name: docker-course-keypair
instance_type: t2.nano
image_name: ami-6df1e514
live_ip_address: 34.212.44.64
tasks:
- name : Setup variables for later
set_fact:
region: "{{ region }}"
live_ip_address: "{{ live_ip_address }}"
- name: Setup security group
ec2_group:
vpc_id: vpc-ec212388
name: http-server
description: Security rules for HTTP Server
region: "{{ region }}"
rules_egress:
- proto: tcp
from_port: 0
to_port: 65535
cidr_ip: 0.0.0.0/0
rules:
- proto: tcp
from_port: 8080
to_port: 8080
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- name: Provision an instance
ec2:
vpc_subnet_id: subnet-5c18572a
key_name: "{{ keypair_name }}"
group: http-server
instance_type: "{{ instance_type }}"
image: "{{ image_name }}"
wait: true
region: "{{region}}"
exact_count: 1
count_tag:
Name: Webserver
Deploy: Blue
instance_tags:
Name: Webserver
Deploy: Blue
register: ec2
- name: Add all instance public IPs to host group
add_host: hostname={{ item.public_ip }} groups=ec2hosts
with_items: "{{ec2.instances}}"
- name: Wait for SSH Server to start responding
wait_for:
port: 22
host: "{{item.public_ip}}"
state: started
with_items: "{{ec2.instances}}"
- hosts: ec2hosts
tasks:
- name: upgrade all packages
yum: name=* state=latest
become: true
- name: Install JDK
yum:
name: java-1.8.0-openjdk-devel
state: latest
become: true
- name: Use Java8 for Runtime Java
alternatives:
name: java
path: /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java
link: /usr/bin/java
become: true
- name: Send the deployment across to the new instance
copy:
src: target/fleetman-0.0.1-SNAPSHOT.war
dest: /home/ec2-user/webapp/target/
mode: 0700
- name: Build a link to the executable so it becomes a service
file:
src: /home/ec2-user/webapp/target/fleetman-0.0.1-SNAPSHOT.war
dest: /etc/init.d/webapp
state: link
become: true
- name: Send a config file to ensure the service starts up with production environment variable
copy:
content: JAVA_OPTS="-Dspring.profiles.active=production"
dest: /home/ec2-user/webapp/target/fleetman-0.0.1-SNAPSHOT.conf
mode: 0600
- name: Install Config Server as a startup service
service:
name: webapp
state: started
enabled: yes
become: true
- name: Wait for the new webserver to begin responding
wait_for:
host: localhost
port: 8080
state: started
# Series of steps to implement a Green -> Blue switchover
- hosts: localhost
tasks:
- name: Gather facts about the new blue server
ec2_remote_facts:
filters:
"tag:Deploy": Blue
"tag:Name": Webserver
instance-state-name: running
region: "{{ region }}"
register: new_server
- name: Get the id of the old, soon to be dead, green server
ec2_remote_facts:
filters:
"tag:Deploy": Green
"tag:Name": Webserver
instance-state-name: running
region: "{{ region }}"
register: old_green
- name: Deallocate the EIP
ec2_eip:
device_id: "{{ old_green.instances[0].id }}"
ip: "{{ live_ip_address }}"
state: absent
region: "{{ region }}"
when: old_green.instances|length > 0
- name: Allocate the EIP to the new instance
ec2_eip:
device_id: "{{ new_server.instances[0].id }}"
ip: "{{ live_ip_address }}"
region: "{{ region }}"
- name: Terminate the existing Green Server.
ec2:
exact_count: 0
count_tag:
Deploy: Green
Name: Webserver
region: "{{ region }}"
image: notneeded
- name: All done, set new server to Green status
ec2_tag:
region: "{{ region }}"
resource: "{{ new_server.instances[0].id }}"
tags:
Deploy: Green
state: present
- name: Remove Blue Tag
ec2_tag:
region: "{{ region }}"
resource: "{{ new_server.instances[0].id }}"
tags:
Deploy: Blue
state: absent