This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.py
252 lines (208 loc) · 7.37 KB
/
install.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
#!/usr/bin/env python
# Copyright 2018-2021 Synadia Communications, Inc
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This installer mostly inspired by
# https://github.com/denoland/deno_install/blob/master/install.py
from __future__ import print_function
import io
import os
import re
import sys
import time
import zipfile
import zlib
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
NGS_REPO_URL = "https://github.com/connecteverything/ngs-cli"
NGS_LATEST_RELEASE_URL = NGS_REPO_URL + "/releases/latest"
NGS_TAG_URL = NGS_REPO_URL + "/releases/tag/"
NGS_FILENAME_LOOKUP = {
"darwin": {
"amd64": "ngs-darwin-amd64.zip",
"arm64": "ngs-darwin-arm64.zip",
},
"linux": {
"amd64": "ngs-linux-amd64.zip",
"arm64": "ngs-linux-arm64.zip",
},
"win32": {
"amd64": "ngs-windows-amd64.zip",
},
}
NSC_REPO_URL = "https://github.com/nats-io/nsc"
NSC_LATEST_RELEASE_URL = NSC_REPO_URL + "/releases/latest"
# NSC_PROD_RELEASE_URL = NSC_REPO_URL + "/releases/tag/0.5.0"
NSC_TAG_URL = NSC_REPO_URL + "/releases/tag/"
NSC_FILENAME_LOOKUP = {
"darwin": {
"amd64": "nsc-darwin-amd64.zip",
"arm64": "nsc-darwin-arm64.zip",
},
"linux": {
"amd64": "nsc-linux-amd64.zip",
"arm64": "nsc-linux-arm64.zip",
},
"win32": {
"amd64": "nsc-windows-amd64.zip",
},
}
def ngs_release_url(platform, arch, tag):
try:
filename = NGS_FILENAME_LOOKUP[platform][arch]
except KeyError:
print("Unable to locate appropriate filename for", platform, "with archtecture", arch)
sys.exit(1)
url = NGS_TAG_URL + tag if tag else NGS_LATEST_RELEASE_URL
try:
html = urlopen(url).read().decode('utf-8')
except:
print("Unable to find release page for", tag)
sys.exit(1)
urls = re.findall(r'href=[\'"]?([^\'" >]+)', html)
matching = [u for u in urls if filename in u]
if len(matching) != 1:
print("Unable to find download url for", filename)
sys.exit(1)
return "https://github.com" + matching[0]
def nsc_release_url(platform, arch, tag):
try:
filename = NSC_FILENAME_LOOKUP[platform][arch]
except KeyError:
print("Unable to locate appropriate filename for", platform, "with archtecture", arch)
sys.exit(1)
url = NSC_TAG_URL + tag if tag else NSC_LATEST_RELEASE_URL
try:
html = urlopen(url).read().decode('utf-8')
except:
print("Unable to find release page for", tag)
sys.exit(1)
urls = re.findall(r'href=[\'"]?([^\'" >]+)', html)
matching = [u for u in urls if filename in u]
if len(matching) != 1:
print("Unable to find download url for", filename)
sys.exit(1)
return "https://github.com" + matching[0]
def download_with_progress(msg, url):
print(msg, url)
remote_file = urlopen(url)
total_size = int(remote_file.headers['Content-Length'].strip())
data = []
bytes_read = 0.0
while True:
d = remote_file.read(8192)
if not d:
print()
break
bytes_read += len(d)
data.append(d)
sys.stdout.write('\r%2.2f%% downloaded' % (bytes_read / total_size * 100))
sys.stdout.flush()
return b''.join(data)
def mkdir(d):
if not os.path.exists(d):
os.mkdir(d)
def add_env(bin_dir):
home = os.path.expanduser("~")
ngs_home = os.path.join(home, ".nsc")
env = os.path.join(ngs_home, "env")
env_cmd = 'export PATH=' + bin_dir + ':$PATH #Add NGS utility to the path'
with open(env, 'w+') as env_file:
env_file.write(env_cmd + '\n')
os.chmod(env, 0o744)
def make_bin_dir():
home = os.path.expanduser("~")
toolhome = os.path.join(home, ".nsc")
mkdir(toolhome)
bin_dir = os.path.join(toolhome, "bin")
mkdir(bin_dir)
return bin_dir
def main():
platform = sys.platform
if "linux" in platform:
# convert any linux regardless of version reported to "linux"
platform = "linux"
arch = os.uname()[4]
if arch == "x86_64":
arch = "amd64"
elif arch == "aarch64":
arch = "arm64"
print()
print("Installing NGS tools for platform: " + platform + " with architecture: " + arch)
url = ngs_release_url(platform, arch, sys.argv[1] if len(sys.argv) > 1 else None)
bin_dir = make_bin_dir()
ngs_exe_path = os.path.join(bin_dir, "ngs")
if "windows" in url:
ngs_exe_path = os.path.join(bin_dir, "ngs.exe")
compressed = download_with_progress("Downloading NGS installer: ", url)
if url.endswith(".zip"):
with zipfile.ZipFile(io.BytesIO(compressed), 'r') as z:
with open(ngs_exe_path, 'wb+') as exe:
if "windows" not in url:
exe.write(z.read('ngs'))
else:
exe.write(z.read('ngs.exe'))
else:
# Note: gzip.decompress is not available in python2.
content = zlib.decompress(compressed, 15 + 32)
with open(ngs_exe_path, 'wb+') as exe:
exe.write(content)
os.chmod(ngs_exe_path, 0o744)
nsc_exe_path = os.path.join(bin_dir, "nsc")
if "windows" in url:
nsc_exe_path = os.path.join(bin_dir, "nsc.exe")
url = nsc_release_url(platform, arch, sys.argv[1] if len(sys.argv) > 1 else None)
compressed = download_with_progress("Downloading NSC installer: ", url)
if url.endswith(".zip"):
with zipfile.ZipFile(io.BytesIO(compressed), 'r') as z:
with open(nsc_exe_path, 'wb+') as exe:
if "windows" not in url:
exe.write(z.read('nsc'))
else:
exe.write(z.read('nsc.exe'))
else:
# Note: gzip.decompress is not available in python2.
content = zlib.decompress(compressed, 15 + 32)
with open(nsc_exe_path, 'wb+') as exe:
exe.write(content)
os.chmod(nsc_exe_path, 0o744)
# Add the env helper.
add_env(bin_dir)
env_tool = os.path.join('$HOME', '.nsc', 'env')
print()
print("The NSC and NGS tools have been installed.")
print()
print(nsc_exe_path + " is used to edit, view and deploy NATS security JWTS.")
print(ngs_exe_path + " can be used to signup for the Synadia global service and manage your billing plan.")
print()
print("To add these commands to your $PATH")
print()
print("Bash:")
print(" echo 'export PATH=\"$PATH:%s\"' >> $HOME/.bash_profile" % bin_dir)
print(" source $HOME/.bash_profile")
print()
print("zsh:")
print(" echo 'export PATH=\"$PATH:%s\"' >> $HOME/.zshrc" % bin_dir)
print(" source $HOME/.zshrc")
print()
print("windows:")
print(" setx path %%path;\"%s\"" % bin_dir)
print()
print("If successful, 'ngs -h' and 'nsc -h' will show the help options.")
print()
print("Learn more about signing up at synadia.com.")
print()
if __name__ == '__main__':
main()