-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcoinbootmaker_helper
executable file
·262 lines (214 loc) · 8.02 KB
/
coinbootmaker_helper
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
#!/usr/bin/env python3
# Copyright (C) 2021 Gunter Miegel coinboot.io
#
# This file is part of Coinboot.
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
#
# Please notice even while this script is licensed with the
# MIT license the software packaged by this script may be licensed by
# an other license with different terms and conditions.
# You have to agree to the license of the packaged software to use it.
"""
Usage:
coinbootmaker_helper create plugin <path> [--upload]
coinbootmaker_helper create readme
"""
import re
import os
import subprocess
import fileinput
import logging
import boto3
from tabulate import tabulate
from docopt import docopt
from botocore.exceptions import ClientError
from strictyaml import load, Map, Str, Int, Seq, YAMLError
def call_coinbootmaker(script_name):
"""Run plugin creation script with coinbootmaker
:param script_name: name of the plugin creation script
:returns: output of running the plugin creation script
"""
coinbootmaker_output = subprocess.run(
["./coinbootmaker", "-p", script_name], stdout=subprocess.PIPE, check=True
)
return coinbootmaker_output
def extract_full_archive_name(subprocess_output):
"""Get composed full name of created Coinboot plugin archive
:param subprocess_output: output of subprocess call
:returns: filename of created Coinboot plugin archive
"""
# We look for a line looking like that:
# 'Created Coinboot Plugin: coinboot_ethminer_v0.18.0_20210603.1555.tar.gz'
for line in subprocess_output.stdout.decode("utf-8").split("\n"):
match = re.search("Created Coinboot Plugin: (.*)", line)
if match:
fullname_created_archive = match.group(1)
return fullname_created_archive
return None
def detect_kernel(subprocess_output):
"""Determine kernel version of coinbootmaker built environment
:param subprocess_output: output of calling coinbootmaker
:returns: detected kernel version else None
"""
for line in subprocess_output.stdout.decode("utf-8").split("\n"):
match = re.search("lib\/modules\/(.*-generic)\/.*$", line)
if match:
kernel = match.group(1)
return kernel
return None
def upload_file(
s3_client, file_name, bucket, coinbootmaker_file, kernel, object_name=None
):
"""Upload a file to an S3 bucket
:param file_name: file to upload
:param bucket: destination bucket
:param object_name: S3 object name. If not specified then file_name is used (Default value = None)
:param s3_client: S3-client instance created by boto
:param coinbootmaker_file: dict holding metadata and script of Coinbootmaker file
:returns: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name
if kernel is None:
kernel = "all"
# Upload the file
try:
response = s3_client.upload_file(
file_name,
bucket,
object_name,
# Metadata prefix 'x-amz-meta-' is added automatically.
ExtraArgs={
"ACL": "public-read",
"Metadata": {
"archive_name": coinbootmaker_file["archive_name"],
"description": coinbootmaker_file["description"],
"maintainer": coinbootmaker_file["maintainer"],
"plugin": coinbootmaker_file["plugin"],
"source": coinbootmaker_file["source"],
"version": coinbootmaker_file["version"],
"kernel": kernel,
},
},
)
except ClientError as e:
logging.error(e)
return False
return True
def create_markdown_table_of_plugins(s3_client, bucket):
"""Create a list of pluings present at a defined S3 bucket
:param s3_client: S3-client instance created by boto
:param bucket: destination bucket
"""
metadata_keys_sorted = [
"plugin",
"version",
"kernel",
"description",
"maintainer",
"source",
]
header_keys_sorted = [
metadata_key.capitalize() for metadata_key in metadata_keys_sorted
]
header_keys_sorted.append("URL")
list_of_plugins = []
list_of_plugins.append(header_keys_sorted)
# TODO: Error handling for S3 API call - failing API or objects, metadata missing
response = s3_client.list_objects(Bucket=bucket)
if "Contents" in response:
response_contents = response["Contents"]
for object in response_contents:
metadata = s3_client.head_object(Bucket=bucket, Key=object["Key"])[
"Metadata"
]
line = []
for key in metadata_keys_sorted:
line.append(metadata.get(key))
line.append(
"https://s3.eu-central-1.wasabisys.com/coinboot/" + object["Key"]
)
list_of_plugins.append(line)
markdown_formatted = tabulate(
list_of_plugins, headers="firstrow", tablefmt="github"
)
return markdown_formatted.split("\n")
else:
print("Bucket is empty")
return None
def concat_with_readme(readme_template_file, readme_file, list_of_plugins):
"""Create a markdown table in the README file with the provided list of plugins
:param readme_template_file: template file for the README
:param readme_file: the final README file
:param list_of_plugins: list of plugins to create a markdown table from
"""
readme_file_content = []
for line in fileinput.input(readme_template_file):
line = line.rstrip("\r\n")
if line == "<!-- PLACEHOLDER FOR MARKDOWN PLUGIN TABLE -->":
readme_file_content.extend(list_of_plugins)
else:
readme_file_content.append(line)
with open(readme_file, "w") as f:
f.write("\n".join(readme_file_content))
def setup_s3_client():
"""Setup the S3 client
:returns: S3 client object
"""
s3_client = boto3.client(
"s3",
endpoint_url="https://s3.eu-central-1.wasabisys.com",
aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
)
return s3_client
def main():
""" """
args = docopt(__doc__)
if args["plugin"]:
path = args["<path>"].replace("plugins/", "")
schema = Map(
{
"plugin": Str(),
"archive_name": Str(),
"version": Str(),
"description": Str(),
"maintainer": Str(),
"source": Str(),
"run": Str(),
}
)
with open(path, "r") as f:
try:
coinbootmaker_file = load(f.read(), schema)
script_name = path.replace("src", "")
coinbootmaker_output = call_coinbootmaker(script_name)
archive_name = extract_full_archive_name(coinbootmaker_output)
print(archive_name)
except YAMLError as error:
print(path + ": " + str(error))
if args["--upload"]:
s3_client = setup_s3_client()
kernel = detect_kernel(coinbootmaker_output)
if kernel:
prefix = kernel
else:
prefix = "all"
print("Bucket prefix is: " + prefix)
upload_file(
s3_client,
"build/" + archive_name,
"coinboot",
coinbootmaker_file.data,
kernel,
prefix + "/" + archive_name,
)
if args["readme"]:
s3_client = setup_s3_client()
table_of_plugins = create_markdown_table_of_plugins(s3_client, "coinboot")
if table_of_plugins:
concat_with_readme("README_template.md", "README.md", table_of_plugins)
if __name__ == "__main__":
main()