-
Notifications
You must be signed in to change notification settings - Fork 14
/
tbupgrade
executable file
·167 lines (119 loc) · 4.45 KB
/
tbupgrade
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
#!/usr/bin/python
import requests, subprocess, sys, argparse, tempfile, os, shutil
def log_header(text):
print 'tbupgrade: ' + text
def log(text):
print 'tbupgrade: ' + text
def get_installed_version():
package_info = subprocess.check_output(['dpkg', '-s', 'tingbot-os'])
version = None
for line in package_info.splitlines():
if line.startswith('Version:'):
_, version = line.split(': ')
assert version is not None, 'Version not found in dpkg output'
return version
def get_latest_version_info():
'''
Returns a tuple of (version, deb_url)
e.g. ('0.3', 'http://.../tingbot-os.deb')
'''
r = requests.get('https://api.github.com/repos/tingbot/tingbot-os/releases/latest')
r.raise_for_status()
release = r.json()
assets = release['assets']
deb_url = None
for asset in assets:
if asset['name'].endswith('.deb'):
deb_url = asset['browser_download_url']
assert deb_url is not None, 'Deb file not found on latest Github release'
version = release['tag_name']
if version.startswith('v'):
# trim the first 'v' from the tag name
version = version[1:]
return version, deb_url
def download(url, filename):
with open(filename, "wb") as f:
response = requests.get(url, stream=True)
response.raise_for_status()
total_size = response.headers.get('content-length')
if total_size is None: # no content length header
f.write(response.content)
else:
downloaded = 0
total_size = int(total_size)
for data in response.iter_content(1024):
downloaded += len(data)
f.write(data)
sys.stdout.write(' %.0i%%\r' % ((100 * downloaded) / total_size))
sys.stdout.flush()
def install_deb(deb_path):
subprocess.check_call(['gdebi', '--non-interactive', deb_path])
def install_deb_from_url(deb_url):
download_dir = tempfile.mkdtemp()
try:
download_file = os.path.join(download_dir, 'tingbot-os.deb')
log_header('Downloading new tingbot-os.deb...')
download(url=deb_url, filename=download_file)
log('Download complete.')
log_header('Installing with dpkg...')
install_deb(download_file)
log('Install complete.')
finally:
shutil.rmtree(download_dir)
def parse_arguments():
parser = argparse.ArgumentParser(description='Tingbot OS update utility',
epilog='Returns 0 on success, 2 if update is not required.')
parser.add_argument('--check-only',
action='store_true',
help='Don\'t update, just check and report if update is required')
parser.add_argument('--deb-url',
action='store',
help='Don\'t check, just download and install the deb at the URL')
parser.add_argument('-y', '--yes',
action='store_true',
help='Don\'t ask for confirmation when updating (useful for scripts)')
return parser.parse_args()
args = None
def main():
global args
args = parse_arguments()
try:
if args.deb_url:
deb_url = args.deb_url
log_header('Installing deb from URL')
log(deb_url)
else:
log_header('Checking installed version...')
installed_version = get_installed_version()
log('Installed version: ' + installed_version)
log_header('Checking latest version on Github...')
latest_version, deb_url = get_latest_version_info()
log('Latest version: ' + latest_version)
if installed_version == latest_version:
log_header('You\'re up to date!')
sys.exit(2)
log_header('Update available.')
if args.check_only:
sys.exit()
if sys.stdin.isatty() and not args.yes:
while True:
sys.stdout.write('\nAre you sure you want to update? [y/n] ')
response = raw_input()
if response in ['n', 'no']:
sys.exit(1)
elif response in ['y', 'ye', 'yes']:
break
install_deb_from_url(deb_url)
log_header('Update complete.')
except Exception as e:
import traceback
print ''
print e
print ''
traceback.print_exc()
print ''
log_header('Upgrade failed.')
print ''
sys.exit(1)
if __name__ == '__main__':
main()