forked from Juniper/nita-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_ansible_job_k8s.py
52 lines (47 loc) · 1.25 KB
/
create_ansible_job_k8s.py
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
#!/usr/bin/python3
import yaml
import os
import sys
# insist on the user providing the name of the job
if len(sys.argv) < 2:
print("Must provide job name as the first argument")
sys.exit(1)
job_name = sys.argv[1]
# insist on the user providing the image name as the second argument
if len(sys.argv) < 3:
print("Must provide image name as the second argument")
sys.exit(1)
image_name = sys.argv[2]
# get path that is to be used inside the job for the working directory
path = os.getcwd()
# yaml file to be used as the starting point for creating a new job
job_yaml = f"""
apiVersion: batch/v1
kind: Job
metadata:
namespace: nita
name: {job_name}
spec:
backoffLimit: 0
ttlSecondsAfterFinished: 120
template:
spec:
serviceAccountName: internal-jenknis-pod
containers:
- name: nita-ansible
image: {image_name}
workingDir: {path}
command: ["/bin/bash"]
args: ["-c", "bash {job_name}.sh", "/tmp"]
volumeMounts:
- name: default
mountPath: /project/
volumes:
- name: default
hostPath:
type: DirectoryOrCreate
path: /var/nita_project
restartPolicy: Never
"""
with open(f'{job_name}.yaml', 'w') as file:
print(job_yaml, file=file)