forked from xcp-ng/host-installer_old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.py
More file actions
60 lines (49 loc) · 1.88 KB
/
scripts.py
File metadata and controls
60 lines (49 loc) · 1.88 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
# Copyright (c) Citrix Systems 2009. All rights reserved.
# Xen, the Xen logo, XenCenter, XenMotion are trademarks or registered
# trademarks of Citrix Systems, Inc., in the United States and other
# countries.
import constants
import os
import stat
import tempfile
import util
import xelogging
script_dict = {}
def add_script(stage, url):
if stage not in script_dict:
script_dict[stage] = []
script_dict[stage].append(url)
def run_scripts(stage, *args):
if stage not in script_dict:
return
for script in script_dict[stage]:
run_script(script, stage, *args)
def run_script(script, stage, *args):
xelogging.log("Running script for stage %s: %s %s" % (stage, script, ' '.join(args)))
util.assertDir(constants.SCRIPTS_DIR)
fd, local_name = tempfile.mkstemp(prefix = stage, dir = constants.SCRIPTS_DIR)
try:
util.fetchFile(script, local_name)
# check the interpreter
fh = os.fdopen(fd)
fh.seek(0)
line = fh.readline(40)
fh.close()
except:
raise RuntimeError, "Unable to fetch script %s" % script
if not line.startswith('#!'):
raise RuntimeError, "Missing interpreter in %s." % script
interp = line[2:].split()
if interp[0] == '/usr/bin/env':
if len (interp) < 2 or interp[1] not in ['python']:
raise RuntimeError, "Invalid interpreter %s in %s." % (interp[1], script)
elif interp[0] not in ['/bin/sh', '/bin/bash', '/usr/bin/python']:
raise RuntimeError, "Invalid interpreter %s in %s." % (interp[0], script)
cmd = [local_name]
cmd.extend(args)
os.chmod(local_name, stat.S_IRUSR | stat.S_IXUSR)
os.environ['XS_STAGE'] = stage
rc, out, err = util.runCmd2(cmd, with_stdout = True, with_stderr = True)
xelogging.log("Script returned %d" % rc)
# keep script, will be collected in support tarball
return rc, out, err