-
Notifications
You must be signed in to change notification settings - Fork 37
/
tasks.py
278 lines (246 loc) · 9.7 KB
/
tasks.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
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
import json
import glob
import shutil
import hashlib
import platform
import os
import urllib.request
from pathlib import Path
from invoke import task, Collection
PROJECT_HOME = os.path.dirname(__file__)
PROJECT_CODE = "container-desktop"
PROJECT_VERSION = (
Path(os.path.join(PROJECT_HOME, "VERSION")).read_text(encoding="utf-8").strip()
)
NODE_ENV = os.environ.get("NODE_ENV", "development")
ENVIRONMENT = os.environ.get("ENVIRONMENT", NODE_ENV)
APP_PROJECT_VERSION = PROJECT_VERSION
TARGET = os.environ.get("TARGET", "linux")
PORT = int(os.environ.get("PORT", str(3000)))
PTY = os.name != "nt"
def url_download(url, path):
url = url
output_file = path
with urllib.request.urlopen(url) as response, open(output_file, "wb") as out_file:
shutil.copyfileobj(response, out_file)
def get_env():
return {
"BROWSER": "none",
"PORT": str(PORT),
"PROJECT_HOME": PROJECT_HOME,
"PROJECT_CODE": PROJECT_CODE,
"PROJECT_VERSION": PROJECT_VERSION,
"NODE_ENV": NODE_ENV,
"TARGET": TARGET,
"PUBLIC_URL": ".",
# "DEBUG": "electron-builder"
# Global
"ENVIRONMENT": ENVIRONMENT,
"APP_PROJECT_VERSION": APP_PROJECT_VERSION,
}
def run_env(ctx, cmd, env=None):
cmd_env = {**get_env(), **({} if env is None else env)}
nvm_dir = os.getenv("NVM_DIR", str(Path.home().joinpath(".nvm")))
nvm_sh = os.path.join(nvm_dir, "nvm.sh")
# print("ENVIRONMENT", cmd_env)
if os.path.exists(nvm_sh):
with ctx.prefix(f'source "{nvm_dir}/nvm.sh"'):
nvm_rc = os.path.join(ctx.cwd, ".nvmrc")
if os.path.exists(nvm_rc):
with ctx.prefix("nvm use"):
ctx.run(cmd, env=cmd_env, pty=PTY)
else:
ctx.run(cmd, env=cmd_env, pty=PTY)
else:
ctx.run(cmd, env=cmd_env, pty=PTY)
@task
def uninstall_self_signed_appx(ctx):
appx_list_process = ctx.run(
'powershell.exe -Command "(Get-AppxPackage | Select Name, PackageFullName | ConvertTo-Json)"',
warn=False,
echo=False,
)
try:
appx_list = json.loads(appx_list_process.stdout or "[]")
for app in appx_list:
if "ContainerDesktop" in app["Name"]:
print(
f"Appx already installed: {app['Name']} - removing {app['PackageFullName']}"
)
ctx.run(
f'powershell.exe -Command "Remove-AppxPackage -Package \\"{app["PackageFullName"]}\\""'
)
except:
print("Unable to parse appx list")
@task
def install_self_signed_appx(ctx):
# See https://ebourg.github.io for jsign-6.0.jar
# See https://gist.github.com/steve981cr/52ca0ae39403dba73a7dbdbe5d231bbf
# See https://gist.github.com/steve981cr/4d592c5cc0f4600d2dc11b1b55aa62a7
# See https://www.briggsoft.com/signgui.htm
# Create self-signed certificate
# New-SelfSignedCertificate -Type CodeSigning -Subject "CN=52408AA8-2ECC-4E48-9A2C-6C1F69841C79" -KeyUsage DigitalSignature -FriendlyName "Container Desktop" -CertStoreLocation "Cert:\CurrentUser\My" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}")
# Export without password
# $cert = @(Get-ChildItem -Path 'Cert:\CurrentUser\My\821E07AB166C20273197EF17569D4613ACE31E4E')[0]; $certBytes = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx); [System.IO.File]::WriteAllBytes('ContainerDesktop.pfx', $certBytes)
# Find if appx is already installed
uninstall_self_signed_appx(ctx)
# Generate and import certificate if not found
path = Path(PROJECT_HOME)
pfx_path = os.path.join(path, "temp/self-signed.pfx")
if not os.path.exists(pfx_path):
print("Certificate not found - generating")
# ctx.run(f'powershell.exe -Command "{command_gen_cert} | ConvertTo-Json"')
cert_config_path = os.path.join(path, "support/openssl.conf")
private_key_path = os.path.join(path, "temp/self-signed-private.key")
if not os.path.exists(private_key_path):
print(f"Private key not found at {private_key_path} - generating")
ctx.run(f"openssl genrsa -out {private_key_path} 2048")
# Generate CSR
os.makedirs(os.path.join(path, "temp"), exist_ok=True)
csr_path = os.path.join(path, "temp/self-signed.csr")
if not os.path.exists(csr_path):
print(f"CSR not found at {csr_path} - generating")
ctx.run(
f"openssl req -new -key {private_key_path} -out {csr_path} -config {cert_config_path}"
)
# Self sign
cert_path = os.path.join(path, "temp/self-signed.crt")
if not os.path.exists(cert_path):
print(f"Certificate not found at {cert_path} - generating")
ctx.run(
f"openssl x509 -req -in {csr_path} -signkey {private_key_path} -out {cert_path} -days 365 -extensions v3_req -extfile {cert_config_path}"
)
# Create pfx
if not os.path.exists(pfx_path):
print(f"PFX not found at {pfx_path} - generating")
ctx.run(
f'openssl pkcs12 -export -out {pfx_path} -inkey {private_key_path} -in {cert_path} -name "Container Desktop" -passout pass:123456'
)
# Signing the bundles
jar_path = os.path.join(path, "temp/jsign-6.0.jar")
ts_url = ",".join(
[
# Add more if needed
"http://timestamp.sectigo.com/rfc3161",
"http://timestamp.globalsign.com/scripts/timstamp.dll",
"http://timestamp.comodoca.com/authenticode",
"http://sha256timestamp.ws.symantec.com/sha256/timestamp",
]
)
exe_path = os.path.join(
path, "release", f"container-desktop-x64-{PROJECT_VERSION}.exe"
)
appx_path = os.path.join(
path, "release", f"container-desktop-x64-{PROJECT_VERSION}.appx"
)
with ctx.cd(path):
if os.path.exists(exe_path):
print(f"Signing {exe_path}")
shutil.copy(exe_path, f"{exe_path}.unsigned")
run_env(
ctx,
f'java -jar "{jar_path}" --keystore {pfx_path} --storetype PKCS12 --storepass 123456"" --tsaurl "{ts_url}" "{exe_path}"',
)
if os.path.exists(appx_path):
print(f"Signing {appx_path}")
shutil.copy(exe_path, f"{appx_path}.unsigned")
run_env(
ctx,
f'java -jar "{jar_path}" --keystore {pfx_path} --storetype PKCS12 --storepass 123456"" --tsaurl "{ts_url}" "{appx_path}"',
)
@task
def build(ctx, env=None):
path = Path(PROJECT_HOME)
with ctx.cd(path):
shutil.rmtree("build", ignore_errors=True)
run_env(ctx, "yarn build", env)
for file in glob.glob("./src/resources/icons/appIcon*"):
shutil.copy(file, "./build")
for file in glob.glob("./src/resources/icons/trayIcon*"):
shutil.copy(file, "./build")
@task
def build_relay(ctx, env=None):
path = Path(PROJECT_HOME)
relay_dir = os.path.join(path, "support/container-desktop-relay")
with ctx.cd(relay_dir):
os.makedirs(os.path.join(PROJECT_HOME, "bin"), exist_ok=True)
system = platform.system()
print(f"Building relay on {system}")
if system == "Linux":
run_env(ctx, f'cd "{relay_dir}" && ./relay-build.sh', env)
elif system == "Windows":
run_env(ctx, "relay-build.cmd", env)
for file in glob.glob(os.path.join(relay_dir, "bin", "**")):
shutil.copy(file, os.path.join(path, "bin"))
else:
raise Exception(f"Unsupported system: {system}")
@task
def bundle(ctx, env=None):
system = platform.system()
path = Path(PROJECT_HOME)
with ctx.cd(path):
env["DEBUG"] = "*"
if system == "Darwin":
run_env(ctx, "yarn package:mac_x86", env)
run_env(ctx, "yarn package:mac_arm", env)
elif system == "Linux":
run_env(ctx, "yarn package:linux_x86", env)
run_env(ctx, "yarn package:linux_arm", env)
else:
build_relay(ctx, env)
run_env(ctx, "yarn package:win_x86", env)
@task
def checksums(ctx, env=None):
items = glob.glob(os.path.join(PROJECT_HOME, "release", "container-desktop-*"))
for installer_path in items:
if installer_path.endswith(".sha256"):
continue
checksum_path = f"{installer_path}.sha256"
print(f"Creating checksum for {installer_path}")
file_contents = open(installer_path, "rb").read()
checksum = hashlib.sha256(file_contents).hexdigest()
with open(checksum_path, "w", encoding="utf-8") as fp:
fp.write(checksum)
@task(default=True)
def help(ctx):
ctx.run("invoke --list")
@task
def prepare(ctx, docs=False):
# Install infrastructure dependencies
with ctx.cd(PROJECT_HOME):
run_env(ctx, "npm install -g yarn@latest rimraf@latest")
run_env(ctx, "yarn install")
@task
def release(ctx, docs=False):
env = {
"NODE_ENV": "production",
"ENVIRONMENT": "production",
}
build(ctx, env)
bundle(ctx, env)
checksums(ctx, env)
@task
def clean(c, docs=False):
path = Path(PROJECT_HOME)
with c.cd(os.path.dirname(path)):
shutil.rmtree("node_modules", ignore_errors=True)
shutil.rmtree("bin", ignore_errors=True)
shutil.rmtree("build", ignore_errors=True)
shutil.rmtree("release", ignore_errors=True)
@task
def start(ctx, docs=False):
path = Path(PROJECT_HOME)
with ctx.cd(path):
run_env(ctx, "yarn dev")
namespace = Collection(
clean,
prepare,
build,
build_relay,
bundle,
release,
start,
checksums,
install_self_signed_appx,
uninstall_self_signed_appx,
)