-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.py
executable file
·161 lines (118 loc) · 4.19 KB
/
runner.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
#!/usr/bin/env python
import base64
import json
from glob import glob
from os.path import basename, dirname, expanduser, isfile, join
from pathlib import Path
from typing import List, Union
import click
import requests
ostype = "*"
repository = "ukhydrographicoffice"
dockerfiles = glob(join(ostype, "**", "Dockerfile"))
def targets():
print(f"loading targets")
for dockerfile in dockerfiles:
directory = dirname(dockerfile)
print(f"getting target: {directory}")
versions = ["latest"]
print(f"base version: {versions}")
versions_script_path = join(directory, "versions")
print(f"path: {versions_script_path}")
if isfile(versions_script_path):
output = run(["bash", versions_script_path])
versions += output.splitlines()
print(f"versions: {versions}")
name = basename(directory)
print(f"targets: {name}")
for version in versions:
print(f"image: {name}:{version}")
docker_image = f"{repository}/{name}:{version}"
yield dockerfile, directory, docker_image
@click.group(chain=True)
@click.option("--win", is_flag=True)
@click.option("--linux", is_flag=True)
@click.option("--filter", default = "")
def cli(win,linux,filter):
global dockerfiles
ostype = "*"
if linux:
ostype = "linux"
if win:
ostype = "win"
dockerfiles = sorted(glob(join(ostype, f"*{filter}*", "Dockerfile")))
@cli.command()
def win():
global dockerfiles
ostype = "win"
dockerfiles = glob(join(ostype, "*", "Dockerfile"))
@cli.command()
def linux():
global dockerfiles
ostype = "linux"
dockerfiles = glob(join(ostype, "*", "Dockerfile"))
@cli.command()
def build():
for dockerfile, directory, docker_image in targets():
print(f"building: {docker_image}")
docker(f"build --rm --tag {docker_image} {directory}")
print(f"built: {docker_image}")
@cli.command()
def lint():
for dockerfile in dockerfiles:
print(f"linting: {dockerfile}")
docker(f"run --rm -i hadolint/hadolint < {dockerfile}")
print(f"linted: {dockerfile}")
@cli.command()
def ls():
for dockerfile, directory, docker_image in targets():
print(dockerfile, docker_image)
@cli.command()
def publish():
for dockerfile, directory, docker_image in targets():
if "aardvark" not in docker_image:
print(f"publishing: {docker_image}")
docker(f"push {docker_image}")
print(f"published: {docker_image}")
#readme = Path(join(directory, "README.md"))
#if readme.is_file():
# print(f"publishing: {readme}")
# update_readme(docker_image, readme.read_text())
# print(f"published: {readme}")
def update_readme(docker_image, readme_contents):
split = docker_image.split(":")[0]
response = requests.patch(
f"https://hub.docker.com/v2/repositories/{split}/",
data={"full_description": readme_contents},
headers={"Authorization": "JWT " + jwt()},
)
if not response.ok:
raise Exception(
f"Failed to update README for {split}:", response.reason, response.content
)
def jwt():
docker_config_path = expanduser(join("~", ".docker", "config.json"))
with open(docker_config_path, "r") as docker_config_json:
docker_config = json.load(docker_config_json)
token = docker_config["auths"]["https://index.docker.io/v1/"]["auth"]
[usr, pwd] = base64.b64decode(token).decode().split(":", 1)
response = requests.post(
"https://hub.docker.com/v2/users/login/",
data={"username": usr, "password": pwd},
)
return response.json()["token"]
def docker(command: str):
cmd = f"docker {command}"
print("$", cmd)
run(cmd)
def run(cmd: Union[str, List[str]]) -> str:
from subprocess import run as sub_run, STDOUT, PIPE
if type(cmd) == str:
p = sub_run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True, shell=True)
elif type(cmd) == list:
p = sub_run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
output = p.stdout
if p.returncode != 0:
print(output)
return output
cli()