-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprelim.py
More file actions
322 lines (278 loc) · 12.6 KB
/
prelim.py
File metadata and controls
322 lines (278 loc) · 12.6 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# prelim.py version 1.15 06-April 2025
import sys
import os
import glob
import logging
import datetime
import lsfunctions as lsf
from pathlib import Path
#####################################################
# 2601: Import Module & set Global variables
#####################################################
sys.path.append('/vpodrepo/2026-Labs/2601')
import functions.hol_functions as hol
# default logging level is WARNING (other levels are DEBUG, INFO, ERROR and CRITICAL)
logging.basicConfig(level=logging.DEBUG)
# Record whether this is a first run or a LabCheck execution
lsf.test_labcheck()
color = 'red'
if len(sys.argv) > 1:
lsf.start_time = datetime.datetime.now() - datetime.timedelta(seconds=int(sys.argv[1]))
if sys.argv[2] == "True":
lsf.labcheck = True
color = 'green'
lsf.write_output(f'{sys.argv[0]}: labcheck is {lsf.labcheck}', logfile=lsf.logfile)
else:
lsf.labcheck = False
# read the /hol/config.ini
lsf.init(router=False)
lsf.write_output(f'Running {sys.argv[0]}', logfile=lsf.logfile)
lsf.write_vpodprogress('PRELIM', 'GOOD-2', color=color)
#
# Copy this file to the /vpodrepo/202?-Labs/2??? folder for your vPod_SKU
# Make your changes after the Core Team code section
#
#
## Begin Core Team code (please do not edit)
#
if lsf.labtype == "HOL":
# copy the README.txt from the vpodrepo to the MC if byte count is different and newer
repo_readme = f'{lsf.vpod_repo}/README.txt'
mc_readme = f'{lsf.mcdesktop}/README.txt'
try:
if os.stat(repo_readme).st_size != os.stat(mc_readme).st_size:
readme_tdiff = os.path.getmtime(repo_readme) - os.path.getmtime(mc_readme)
if readme_tdiff > 0:
lsf.write_output('vPodrepo README is different and newer. Copying to Main Console...')
os.system(f'cp -p {repo_readme} {mc_readme}')
elif readme_tdiff < 0:
lsf.write_output('Changes detected on MC README. Please add to vPodrepo and check in.')
else:
lsf.write_output('vPodrepo README and MC have no differences. All good.')
except Exception as e:
lsf.write_output(f'Error updating README.txt: {e}')
# prevent the annoying Firefox banner if WMC
if lsf.WMC and lsf.labtype == 'HOL':
appdata = f'{lsf.mc}/Users/Administrator/AppData'
ffroamprofiles = f'{appdata}/Roaming/Mozilla/Firefox/Profiles/*.default-release'
fflocalprofiles = f'{appdata}/Local/Mozilla/Firefox/Profiles/*.default-release'
os.system(f'rm {ffroamprofiles}/parent.lock > /dev/null 2>&1')
os.system(f'rm -rf {ffroamprofiles}/storage > /dev/null 2>&1')
os.system(f'rm -rf {fflocalprofiles}/cache2 > /dev/null 2>&1')
# and apparently all this is not enough. must also create user.js with disableResetPrompt option
releasedir = glob.glob(ffroamprofiles)
resetprompt = 'user_pref("browser.disableResetPrompt", true);'
userpref = f'{releasedir[0]}/user.js'
prefneeded = True
if not os.path.isfile(userpref):
Path(userpref).touch()
with open(userpref, 'r') as fp:
prefs = fp.read()
for pref in prefs:
if pref.find('disableResetPrompt'):
prefneeded = False
break
fp.close()
if prefneeded:
lsf.write_output(f'need to add {resetprompt} in {userpref}')
with open(userpref, 'a') as fp:
fp.write(f'{resetprompt}\n')
fp.close()
# test an external url to be certain the connection is blocked. The last argument is the timeout.
if lsf.labtype != 'HOL':
lsf.write_output(f'NOT checking firewall for labtype {lsf.labtype}')
else:
# check the firewall from the Main Console
fwok = False
ctr = 0
maxctr = 20
lsf.write_output("Sleeping 60 seconds for holorouter firewall to come up...")
lsf.labstartup_sleep(60)
os.system(f'cp {lsf.holroot}/Tools/checkfw.py {lsf.mctemp}')
output = []
if lsf.LMC:
try:
fwout = lsf.ssh('/usr/bin/python3 /tmp/checkfw.py', 'holuser@console', lsf.password)
output = fwout.stdout
except Exception as e:
lsf.write_output(f'Error ruuning checkfw.py: {e}')
elif lsf.WMC:
try:
output = lsf.runwincmd('python C:\\Temp\\checkfw.py', 'console', 'Administrator', lsf.password, logfile=lsf.logfile)
except Exception as e:
lsf.write_output(f'Error ruuning checkfw.py: {e}')
while 'Good' not in output and ctr < maxctr:
if lsf.LMC:
fwout = lsf.ssh('/usr/bin/python3 /tmp/checkfw.py', 'holuser@console', lsf.password)
output = fwout.stdout
elif lsf.WMC:
output = lsf.runwincmd('python C:\\Temp\\checkfw.py', 'console', 'Administrator', lsf.password, logfile=lsf.logfile)
for line in output:
if 'Good' in line:
fwok = True
break
ctr += 1
if ctr == maxctr:
lsf.write_output('Firewall is OFF for the Main Console. Failing lab.')
lsf.labfail('OPEN FIREWALL')
lsf.write_output(f'Checking firewall on Main Console. Attempt: {ctr}')
lsf.write_output(f'firewall output: {output} {ctr}')
lsf.labstartup_sleep(lsf.sleep_seconds)
if fwok:
lsf.write_output('Firewall is on for the Main Console.', logfile=lsf.logfile)
#lsf.test_firewall('https://vmware.com', '<title>', 2)
lsf.run_command(f'rm {lsf.mctemp}/checkfw.py')
if lsf.WMC and lsf.labcheck == False:
# execute the WMCstart script on the WMC to address startup issues
# Windows console locking and DesktopInfo startup issue on Windows 2019
admindir = 'Users/Administrator'
os.system(f'cp {lsf.holroot}/Tools/WMCstartup.ps1 {lsf.mc}/{admindir}/')
# C:\Program Files\PowerShell\7\pwsh.exe
command = 'pwsh C:\\Users\\Administrator\\WMCstartup.ps1 > C:\\Users\\Administrator\\WMCstartup.log'
lsf.write_output('Running WMCstartup.ps1 on console. Please stand by...', logfile=lsf.logfile)
lsf.runwincmd(command, 'console', 'Administrator', lsf.password, logfile=lsf.logfile)
with open(f'{lsf.mc}/{admindir}/WMCstartup.log', 'r') as ologfile:
olog = ologfile.readlines()
ologfile.close()
for line in olog:
lsf.write_output(line.strip(), logfile=lsf.logfile)
os.system(f'rm {lsf.mc}/{admindir}/WMCstartup.ps1')
os.system(f'mv {lsf.mc}/{admindir}/WMCstartup.log /tmp')
# BEGIN ODYSSEY RESET CODE
#
# Odyssey Variables
#
# LMC or WMC
if lsf.WMC:
desktop = '/Users/Administrator/Desktop'
ody_shortcut = 'Play VMware Odyssey.lnk'
odyssey_app = 'odyssey-launcher.exe'
# legacy most likely not needed
# odysseyEXE = 'odyssey-launcher.exe'
odyssey_dst = '/Users/Administrator'
elif lsf.LMC:
desktop = '/home/holuser/Desktop'
ody_shortcut = 'launch_odyssey.desktop'
odyssey_app = 'odyssey-client-linux.AppImage'
odyssey_launcher = 'odyssey-launch.sh'
odyssey_dst = f'desktop-hol'
lmcuser = 'holuser@console'
# on initial boot remove the Odyssey files if present
if not lsf.labcheck:
if os.path.isfile(f'{lsf.mc}/{desktop}/{ody_shortcut}'):
lsf.write_output('Removing existing Odyssey desktop shortcut.', logfile=lsf.logfile)
os.remove(f'{lsf.mc}/{desktop}/{ody_shortcut}')
if os.path.isfile(f'{lsf.mc}/{odyssey_dst}/{odyssey_app}'):
lsf.write_output(f'Removing existing Odyssey application. {lsf.mc}/{odyssey_dst}/{odyssey_app}', logfile=lsf.logfile)
os.remove(f'{lsf.mc}/{odyssey_dst}/{odyssey_app}')
# remove the file locally (shouldn't be here since /tmp gets deleted with each boot)
if os.path.isfile(f'/tmp/{odyssey_app}'):
lsf.write_output('Removing existing Odyssey application.', logfile=lsf.logfile)
os.remove(f'/tmp/{odyssey_app}')
# doubt this is needed but will include
# we won't be able to do this. we can check but will need to run a command to remove (permissions)
legacy_odyssey_client = '/wmchol/Users/Administrator/VMware_Odyssey.exe'
if os.path.isfile(legacy_odyssey_client):
lsf.write_output('Removing legacy Odyssey application.', logfile=lsf.logfile)
os.remove(legacy_odyssey_client)
#
## End Core Team code
#
#
# Insert your code here using the file in your vPod_repo
#
# fail like this
#now = datetime.datetime.now()
#delta = now - lsf.start_time
#lsf.labfail('PRELIM ISSUE', delta)
#exit(1)
########################################################
# 26xx - Copy LabFiles from vPod Repo
########################################################
src="/vpodrepo/2026-labs/2601/labfiles"
dst="/home/holuser/"
pwd = lsf.password
if lsf.LMC:
lsf.write_vpodprogress('Updating labfiles folder', 'GOOD-2', color=color)
lsf.write_output(f"TASK: Removing existing labfiles folder...", logfile=lsf.logfile)
try:
lsf.ssh(f'rm -rf {dst}/labfiles', 'holuser@console', pwd)
except Exception as e:
lsf.write_output(f'INFO: {e}', logfile=lsf.logfile)
print(f'INFO: {e}')
lsf.write_output(f"TASK: Copying '{src}' to '{dst}'.", logfile=lsf.logfile)
try:
lsf.scp(src, f"holuser@console:{dst}", pwd, recurse='True')
except Exception as e:
lsf.write_output(f'INFO: {e}', logfile=lsf.logfile)
print(f'INFO: {e}')
########################################################
# 26xx - Copy GitLab Projects files to WMC
########################################################
# gitFqdn = "gitlab.vcf.sddc.lab"
# gitlabRepoSource = "/vpodrepo/2025-labs/2501/gitlab"
# gitlabRepoDestination = f"{lsf.mc}"
# sslVerify = False
# while True:
# if hol.isGitlabReady(gitFqdn, sslVerify) and hol.isGitlabLive(gitFqdn, sslVerify) and hol.isGitlabHealthy(gitFqdn, sslVerify):
# lsf.write_output(f'2501: Gitlab {gitFqdn} is in a Ready state!', logfile=lsf.logfile)
# print(f'2501: Gitlab {gitFqdn} is in a Ready state!')
# break
# else:
# print(f'2501: Gitlab {gitFqdn} is not Ready!')
# lsf.write_output(f'2501: Gitlab {gitFqdn} is not Ready!', logfile=lsf.logfile)
# lsf.labstartup_sleep(30)
# if lsf.WMC and os.path.exists(gitlabRepoDestination) and os.path.exists(gitlabRepoSource):
# try:
# hol.createFolder(f'{lsf.mc}', 'gitlab')
# lsf.write_output(f'2501: Copying repo files from {gitlabRepoSource} to {gitlabRepoDestination} folder.', logfile=lsf.logfile)
# print(f'2501: Copying repo files from {gitlabRepoSource} to {gitlabRepoDestination} folder.')
# os.system(f'cp -rfu {gitlabRepoSource} {gitlabRepoDestination}')
# lsf.write_output(f'2501: Copying repo files complete.', logfile=lsf.logfile)
# print(f'2501: Copying repo files complete.')
# except Exception as e:
# lsf.labfail(f'2501: {e}')
# print(f'2501: {e}')
# exit(1)
########################################################
# 26xx - GitLab Clean Up Script
########################################################
# if lsf.WMC:
# try:
# if os.path.isfile(f'{labfilesDestination}/gitlab/cleanup.ps1'):
# lsf.write_output(f'2501: Running Gitlab cleanup.ps1 on WMC.', logfile=lsf.logfile)
# print(f'2501: Running Gitlab cleanup.ps1 on WMC.')
# command = "powershell -NoProfile -ExecutionPolicy Bypass -File c:\gitlab\cleanup.ps1"
# lsf.runwincmd(command,'mainconsole','Administrator', 'VMware123!')
# else:
# lsf.labfail(f'2501: File: c:\gitlab\cleanup.ps1 not found')
# print(f'2501: File: c:\gitlab\cleanup.ps1 not found')
# exit(1)
# except Exception as e:
# lsf.labfail(f'2501: {e}')
# print(f'2501: {e}')
# exit(1)
########################################################
# 26xx - Commit Gitlab Projects files to Gitlab
########################################################
# if lsf.LMC:
# try:
# if os.path.isfile(f'{labfilesDestination}/gitlab/sync.ps1'):
# lsf.write_output(f'2501: Committing Repos to Gitlab.', logfile=lsf.logfile)
# print(f'2501: Committing Repos to Gitlab.')
# command = "powershell -NoProfile -ExecutionPolicy Bypass -File c:\gitlab\sync.ps1"
# lsf.runwincmd(command,'mainconsole','Administrator', 'VMware123!')
# else:
# lsf.labfail(f'2501: Failed to commit Repos to Gitlab')
# print(f'2501: Failed to commit Repos to Gitlab')
# exit(1)
# except Exception as e:
# lsf.labfail(f'2501: {e}')
# print(f'2501: {e}')
# exit(1)
# else:
# lsf.labfail(f'2501: Error committing repository files to Gitlab.')
# print(f'2501: Error committing repository files to Gitlab.')
# exit(1)
lsf.write_output(f'{sys.argv[0]} finished.', logfile=lsf.logfile)
exit(0)