forked from trondhindenes/drone-makisu-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
176 lines (150 loc) · 5.12 KB
/
script.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
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
#!/usr/bin/env python3
import os
import json
import subprocess
import time
import sys
def execute_process(cmd, env_vars={}, fail_on_non_zero_exit=True, log_cmd=True, log_envvars=False, log_stdout=True, log_stderr=True):
proc_env_vars = os.environ.copy()
proc_env_vars = {**proc_env_vars, **env_vars}
if log_cmd:
print(f'Running {cmd}')
if log_envvars:
print('envvars')
print(proc_env_vars)
process = subprocess.Popen(cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr, env=proc_env_vars)
return_code = None
while True:
return_code = process.poll()
if return_code is not None:
break
else:
time.sleep(0.05)
if return_code != 0:
print(f'Process exited with return code {return_code}')
if fail_on_non_zero_exit:
raise ValueError('exiting')
return
def get_vars(vars_dict, vars_list, required=True):
for var_item in vars_list:
var_env_name = f'PLUGIN_{var_item}'.upper()
var_env_value = os.getenv(var_env_name, None)
if not var_env_value and required:
raise ValueError('required variable {var_item} not found')
vars_dict[var_item] = var_env_value
vars = {}
get_vars(vars, ['repo'])
get_vars(
vars,
['tags', 'build_args', 'build_args_from_env', 'registry',
'dockerfile', 'debug', 'storage', 'commit',
'username', 'password'
],
required=False
)
debug = False
if vars['debug'] and vars['debug'].lower() == 'true':
debug = True
commit = 'implicit'
if vars['commit'] and vars['commit'] == 'explicit':
commit = 'explicit'
storage_str = ''
if vars['storage']:
storage_str = '--storage=' + vars['storage'] + ' '
if not vars['dockerfile']:
vars['dockerfile'] = 'Dockerfile'
dockerfile = vars['dockerfile']
if not vars['tags'] and os.path.exists('.tags'):
with open('.tags', 'r') as stream:
tags_from_files = stream.read()
vars['tags'] = tags_from_files.replace(os.linesep, '')
print('read tags from .tags file:')
print(vars['tags'])
elif not vars['tags']:
vars['tags'] = 'latest'
if debug:
print(json.dumps(vars, indent=4, sort_keys=True))
can_push = False
registry = None
registry_is_ecr = False
registry_is_docker_hub = False
registry_config_str = ''
cmd_env = {}
replica_str = ''
build_args_str = ''
if vars['build_args']:
build_args = json.loads(vars['build_args'])
for build_arg in build_args.keys():
build_arg_value = build_args[build_arg]
arg_str = f'--build-arg {build_arg}={build_arg_value} '
build_args_str += arg_str
if vars['build_args_from_env']:
build_args_from_env = vars['build_args_from_env'].split(',')
for build_arg in build_args_from_env:
build_arg_value = os.getenv(build_arg)
arg_str = f'--build-arg {build_arg}={build_arg_value} '
build_args_str += arg_str
if '/' in vars['repo'] and not vars['registry']:
registry = vars['repo'].split('/')[0]
repo = '/'.join(vars['repo'].split('/')[1:])
can_push = True
elif vars['registry']:
registry = vars['registry']
repo = vars['repo']
can_push = True
else:
print('registry not specified, not able to push')
repo = vars['repo']
if not vars['tags']:
vars['tags'] = 'latest'
vars['tags_list'] = vars['tags'].split(',')
vars['tags_list'] = sorted(list(set(vars['tags_list'])))
first_tag = vars['tags_list'][0]
if can_push and len(vars['tags_list']) > 1:
replica_pushes = []
replica_str = ''
for tag in vars['tags_list'][1:]:
replica_str += f'--replica "{registry}/{repo}:{tag}" '
replica_str = replica_str[:-1] + ' '
full_name_first_img = repo + ':' + first_tag
if can_push and '.ecr.' in registry and '.amazonaws.com' in registry:
registry_is_ecr = True
cmd_env['AWS_SDK_LOAD_CONFIG'] = 'true'
elif can_push and vars['username'] and vars['password']:
registry_is_docker_hub = True
if can_push and registry_is_ecr:
registry_config = {
registry: {
'.*': {
'push_chunk': -1,
'security': {
'credsStore': 'ecr-login'
}
}
}
}
registry_config_str = f'--registry-config=\'{json.dumps(registry_config)}\' '
elif can_push and registry_is_docker_hub:
registry_config = {
'index.docker.io': {
'.*': {
'security': {
'tls': {
'client': {
'disabled' : False
}
},
'basic': {
'username': vars['username'],
'password': vars['password']
}
}
}
}
}
registry_config_str = f'--registry-config=\'{json.dumps(registry_config)}\' '
registry_param = ''
if can_push:
registry_param = f'--push {registry} '
cmd_line = f'/makisu-internal/makisu build -t {full_name_first_img} --commit={commit} --modifyfs=true --log-fmt=console -f {dockerfile} {storage_str}{registry_param}{registry_config_str}{replica_str}{build_args_str} .'
execute_process(cmd_line, env_vars=cmd_env, log_cmd=debug)