-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.yml
282 lines (260 loc) · 8.09 KB
/
deploy.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
- hosts: builder
gather_facts: yes
connection: ssh
vars_files:
- vars.yaml
vars_prompt:
- name: github_token
prompt: "Enter your github personal access token?"
private: true
tasks:
- name: Install Maven
ansible.builtin.apt:
name: maven
state: present
become: true
- name: Checking out sources
ansible.builtin.include_role:
name: github-add-deploy-key
vars:
key_path: ~/gh_deploy_key
known_hosts_path: ~/.ssh/known_hosts
key_title: ansible_key
github_access_token: "{{ github_token }}"
clone_dest: "~/{{ item.name }}"
github_account_id: jrb-s2c-github
repo: "{{ item.git_repo }}"
branch: "{{ item.git_branch }}"
loop:
"{{ apps }}"
# # should this step take forever, SSH is prompting for user authorization and the remote host's public key should be added to /etc/ssh/ssh_known_hosts
## with key scan: ssh-keyscan -H remote_host.com >> /etc/ssh/ssh_known_host
# - name: Git checkouts
# ansible.builtin.git:
# repo: "https://github.com/jrb-s2c-github/{{ item.git_repo }}.git"
# dest: "~/{{ item.name }}"
# version: "{{ item.git_branch }}"
# loop:
# "{{ apps }}"
- name: Maven install
ansible.builtin.command: "mvn install"
args:
chdir: "~/{{ item.name }}"
register: mvn_out
# become: true
changed_when: mvn_out.rc == 0
loop:
"{{ git_repos }}"
- name: Run JIB builds
ansible.builtin.command: "mvn clean compile jib:buildTar -Dimage={{ item.name }}:{{ item.namespace }}"
args:
chdir: "~/{{ item.name }}/{{ item.jib_dir }}"
register: mvn_out
changed_when: mvn_out.rc == 0
# become: true
# changed_when: '"BUILD SUCCESS" in mvn_output'
loop:
"{{ apps }}"
#
# - name: Print result of JIB build
# ansible.builtin.debug:
# var: mvn_out
# https://blog.scottlowe.org/2020/01/25/manually-loading-container-images-with-containerd/
- name: Load images into containerd
ansible.builtin.command: ctr -n=k8s.io images import jib-image.tar
args:
chdir: "/home/ansible/{{ item.name }}/{{ item.jib_dir }}/target"
register: ctr_out
become: true
# changed_when: '"BUILD SUCCESS" in mvn_output'
loop:
"{{ apps }}"
- name: Print result of CTR Load
ansible.builtin.debug:
var: ctr_out
- hosts: masters
gather_facts: yes
connection: ssh
vars_files:
- vars.yaml
tasks:
- name: Install PIP
ansible.builtin.apt:
name: pip
state: present
become: true
- name: install Python pre-requisites for Kubernetes
ansible.builtin.pip:
name:
- openshift
- pyyaml
- kubernetes
- name: Remove all k8s namespaces
remote_user: ansible
kubernetes.core.k8s:
kubeconfig: /home/ansible/.kube/config
name: "{{ item.name }}"
api_version: v1
kind: Namespace
state: absent
loop:
"{{ namespaces }}"
- pause: seconds=30
- name: Create k8s namespaces
remote_user: ansible
kubernetes.core.k8s:
kubeconfig: /home/ansible/.kube/config
name: "{{ item.name }}"
api_version: v1
kind: Namespace
state: present
loop:
"{{ namespaces }}"
- name: Running the kubectl pre-commands
ansible.builtin.shell: "{{ item }}"
loop:
"{{ pre_k8s_cmds }}"
- name: Create application.property configmaps
kubernetes.core.k8s:
kubeconfig: /home/ansible/.kube/config
namespace: "{{ item.namespace }}"
state: present
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: "{{ item.name }}-cm"
data:
"{{ item.application_properties }}"
loop:
"{{ apps }}"
- name: Create deployments
kubernetes.core.k8s:
kubeconfig: /home/ansible/.kube/config
namespace: "{{ item.namespace }}"
state: present
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: "{{ item.name }}"
name: "{{ item.name }}"
spec:
selector:
matchLabels:
app: "{{ item.name }}"
strategy: { }
template:
metadata:
creationTimestamp: null
labels:
app: "{{ item.name }}"
spec:
containers:
- image: "{{ item.name }}:{{ item.namespace }}"
name: "{{ item.name }}"
resources: { }
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /config
name: config
volumes:
- configMap:
items:
- key: application.properties
path: application.properties
name: "{{ item.name }}-cm"
name: config
status: { }
loop:
"{{ apps }}"
- name: Scale relevant deployments
ansible.builtin.shell: "kubectl -n cc scale deployment {{ item.name }} --replicas {{ item.replicas }}"
when: item.replicas is defined
loop:
"{{ apps }}"
- name: Create services
kubernetes.core.k8s:
kubeconfig: /home/ansible/.kube/config
namespace: "{{ item.namespace }}"
state: present
definition:
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: "{{ item.name }}"
name: "{{ item.name }}"
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: "{{ item.name }}"
type: ClusterIP
status:
loadBalancer: {}
loop:
"{{ apps }}"
- name: Running the kubectl post-commands
ansible.builtin.shell: "{{ item }}"
loop:
"{{ post_k8s_cmds }}"
- name: Create ingress master
kubernetes.core.k8s:
kubeconfig: /home/ansible/.kube/config
namespace: default
state: present
definition:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-master
annotations:
nginx.org/mergeable-ingress-type: "master"
spec:
ingressClassName: nginx
rules:
- host: "{{ ingress.host }}"
- name: Create ingress minions
kubernetes.core.k8s:
kubeconfig: /home/ansible/.kube/config
namespace: "{{ item.namespace }}"
state: present
definition:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: " {{ item.service_path }} "
nginx.org/mergeable-ingress-type: "minion"
name: "ingress-{{ item.namespace }}"
spec:
ingressClassName: nginx
rules:
- host: "{{ ingress.host }}"
http:
paths:
- path: "{{ item.ingress_path }}"
pathType: Prefix
backend:
service:
name: "{{ item.service }}"
port:
number: 80
# - path: /bar
# pathType: Prefix
# backend:
# service:
# name: service2
# port:
# number: 8080
loop:
"{{ ingress.rules }}"