-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_tempus.py
executable file
·217 lines (190 loc) · 7.31 KB
/
update_tempus.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
#!/usr/bin/python
import os
import requests
import yaml
import shutil
import subprocess
import errno
from zipfile import ZipFile
from tempfile import TemporaryFile, mkdtemp
class NoBuild(Exception): pass
def log(s):
print '[Tempus] {}'.format(s)
def downloadSPBuildMirror(version):
url = 'https://sourcepython.tempus.xyz/tf2/sourcepython_{}.zip'.format(version)
response = requests.get(url, stream=True)
if response.status_code >= 400:
raise NoBuild('Could not download build at mirror: {}'.format(url))
log('Fetching SP version {}: {}...'.format(version, url))
return response
def downloadSPBuild(version):
try:
fileName = getSPBuildFileName(version)
except ValueError:
return downloadSPBuildMirror(version)
url = 'http://build.affecta.net/job/Source.Python/{}/artifact/release/{}'.format(version, fileName)
response = requests.get(url, stream=True)
if response.status_code >= 400:
return downloadSPBuildMirror(version)
log('Fetching SP version {}: {}...'.format(version, url))
return response
def getSPBuildFileName(version):
url = 'http://build.affecta.net/job/Source.Python/{}/api/json'.format(version)
result = requests.get(url).json()
for a in result['artifacts']:
if a['fileName'].startswith('source-python-tf2-'):
return a['fileName']
else:
raise RuntimeError('TF2 SP build for version \'{}\' not found.'.format(version))
def main():
SRCDS_PATH = '/srv/srcds/tf'
ADDONS_PATH = os.path.join(SRCDS_PATH, 'addons')
SP_PATH = os.path.join(ADDONS_PATH, 'source-python')
TEMPUS_PATH = os.path.join(
ADDONS_PATH, 'source-python', 'plugins', 'tempus_loader')
CFG_PATH = os.path.join(TEMPUS_PATH, 'local', 'cfg', 'api.yml')
if os.path.exists(CFG_PATH):
with open(CFG_PATH, 'rb') as f:
config = yaml.safe_load(f)
cfgUrl = config['hostname']
cfgDeployment = config['deployment']
cfgUsername = config['username']
cfgPassword = config['password']
else:
CFG_PATH = os.path.join(TEMPUS_PATH, 'local', 'cfg', 'tempus.yml')
with open(CFG_PATH, 'rb') as f:
config = yaml.safe_load(f)['api']
cfgUrl = config['url']
cfgDeployment = config['deployment']
cfgUsername = config['username']
cfgPassword = config['password']
if not os.path.exists(CFG_PATH):
print log('API CONFIGURATION MISSING')
TEMPUS_VERSION_PATH = os.path.join(TEMPUS_PATH, 'tempus', 'tempus', 'version')
SP_VERSION_PATH = os.path.join(
ADDONS_PATH, 'source-python', 'packages', 'source-python', 'core',
'version.py')
tempusFullInstall = False
if os.path.exists(os.path.realpath(TEMPUS_VERSION_PATH)):
with open(TEMPUS_VERSION_PATH, 'rb') as f:
tempusVersion = int(f.read().strip())
else:
tempusFullInstall = True
spFullInstall = False
if os.path.exists(SP_VERSION_PATH):
with open(SP_VERSION_PATH, 'rb') as f:
for line in f.readlines():
if line.startswith('VERSION ='):
spVersion = int(line[10:].strip().strip("'"))
break
else:
raise RuntimeError('Could not find SP version.')
else:
spFullInstall = True
url = '{}{}/'.format(cfgUrl, cfgDeployment)
auth = (cfgUsername, cfgPassword)
log('Checking for updates...')
r = requests.get(url + 'tempus_builds/latest_version', auth=auth,
headers={'Accept': 'application/json'})
latestVersion = int(r.json()['version'])
if not tempusFullInstall and latestVersion == tempusVersion:
log('Already up to date.')
return
tfd = TemporaryFile()
r = requests.get(url + 'tempus_builds/files/tempus{}.zip'.format(latestVersion),
auth=auth, stream=True,
headers={'Accept': 'application/json'})
if not r.ok:
raise RuntimeError('Failed to fetch Tempus.')
for block in r.iter_content(1024):
tfd.write(block)
tTempusPath = mkdtemp()
with ZipFile(tfd, 'r') as z:
z.extractall(tTempusPath)
with open(os.path.join(tTempusPath, 'tempus_loader', 'tempus', 'tempus', 'SP_VERSION')) as f:
requiredSPVersion = int(f.read().strip())
needSPUpdate = False
if spFullInstall is True or requiredSPVersion != spVersion:
needSPUpdate = True
spfd = TemporaryFile()
r = downloadSPBuildMirror(requiredSPVersion)
for block in r.iter_content(1024):
spfd.write(block)
tSPPath = mkdtemp()
with ZipFile(spfd, 'r') as z:
z.extractall(tSPPath)
if spFullInstall:
log('Installing SP from scratch...')
subprocess.call([
'rsync',
'-r',
os.path.join(tSPPath) + '/',
SRCDS_PATH
])
shutil.rmtree(tSPPath)
elif needSPUpdate:
log('Updating SP...')
for x in ['source-python.vdf', 'source-python.so']:
dest = os.path.join(ADDONS_PATH, x)
try:
os.remove(dest)
except OSError:
pass
shutil.copyfile(os.path.join(tSPPath, 'addons', x), dest)
for d in ['bin', 'data', 'packages', 'Python3']:
dest = os.path.join(SP_PATH, d)
try:
shutil.rmtree(dest)
# path does not exist
except OSError:
pass
shutil.copytree(os.path.join(tSPPath, 'addons', 'source-python', d), dest)
for d in ['resource', 'sound']:
dest = os.path.join(SRCDS_PATH, d, 'source-python')
try:
shutil.rmtree(dest)
# path does not exist
except OSError:
pass
shutil.copytree(os.path.join(tSPPath, d, 'source-python'), dest)
shutil.rmtree(tSPPath)
if tempusFullInstall:
log('Installing Tempus from scratch...')
subprocess.call([
'rsync',
'-r',
os.path.join(tTempusPath, 'tempus_loader/'),
os.path.join(SP_PATH, 'plugins/tempus_loader')
])
else:
log('Updating Tempus...')
up = os.path.join(TEMPUS_PATH, 'local', 'updates')
if not os.path.exists(up):
os.makedirs(up)
tUpdatePath = os.path.join(up, str(latestVersion))
if os.path.exists(tUpdatePath):
shutil.rmtree(tUpdatePath)
shutil.copytree(tTempusPath, tUpdatePath)
tempusLibsPath = os.path.join(TEMPUS_PATH, 'tempus')
try:
os.unlink(tempusLibsPath)
except OSError as e:
if e.errno == errno.ENOENT:
pass
elif e.errno == errno.EISDIR:
# if tempus has not been updated since initial deploy,
# `tempus_loader/tempus` will be a dir instead of a symlink
shutil.rmtree(tempusLibsPath)
else:
raise
os.symlink(os.path.join(tUpdatePath, 'tempus_loader', 'tempus'),
tempusLibsPath)
subprocess.call([
'rsync',
'-r',
os.path.join(tUpdatePath, 'tempus_loader/'),
os.path.join(SP_PATH, 'plugins/tempus_loader')
])
shutil.rmtree(tTempusPath)
log('Update complete.')
main()