This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
bootfabtasks.py
161 lines (137 loc) · 4.49 KB
/
bootfabtasks.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
import shutil
import urllib
import os
from fabric.api import env, run, put, cd, get, local
from cloudinitd.statics import *
def _iftar(filename):
"""Return base filename if filename ends with .tar.gz (and does not simply equal that suffix)
Otherwise return None
"""
if not filename:
return None
idx = filename.rfind('.tar.gz')
if idx > 0:
return filename[:idx]
else:
return None
def _tartask(directory, basename, tarball, run_pgm=run):
"""Expand the tarball, ensure it contains a directory with the basename.
Ensure run.sh exists inside.
Return path to the run.sh file.
"""
with cd(directory):
run_pgm("tar -xvzf %s" % tarball)
tardir = os.path.join(directory, basename)
try:
run_pgm("test -d %s" % tardir)
except:
raise Exception("The tarball does not expand to a directory of the same name: %s" % tardir)
destpgm = os.path.join(tardir, "run.sh")
try:
run_pgm("test -f %s" % destpgm)
except:
raise Exception("The tarball does contain a 'run.sh' file: %s" % tarball)
# In case they forgot:
run_pgm("chmod +x %s" % destpgm)
return destpgm
def _make_ssh(pgm, args="", local_exe=None):
if local_exe:
cmd = "%s %s" % (pgm, args)
return cmd
ssh_opts = "-A -n -T -o BatchMode=yes -o StrictHostKeyChecking=no -o PasswordAuthentication=no"
try:
if 'CLOUDINITD_SSH_OPTS' in os.environ:
ssh_opts = os.environ['CLOUDINITD_SSH_OPTS']
except:
pass
sshexec = "ssh"
try:
if os.environ['CLOUDINITD_SSH']:
sshexec = os.environ['CLOUDINITD_SSH']
except:
pass
args = urllib.unquote(args)
user = ""
if env.user:
user = env.user + '@'
port = ""
if env.port:
port = "-p " + env.port
key = ""
if env.key_filename and env.key_filename[0]:
key = "-i " + env.key_filename[0]
cmd = "%s %s %s %s %s%s '%s %s'" % (sshexec, port, ssh_opts, key, user, env.host, pgm, args)
return cmd
def readypgm(pgm=None, args=None, stagedir=None, local_exe=None):
local_exe = str(local_exe).lower() == 'true'
pgm_to_use = run
put_pgm = put
if local_exe:
pgm_to_use = local
put_pgm = shutil.copy
args = urllib.unquote(args)
env.warn_only = True
pgm_to_use('mkdir -p %s' % stagedir)
relpgm = os.path.basename(pgm)
destpgm = "%s/%s" % (stagedir, relpgm)
if local_exe:
os.chdir(stagedir)
put_pgm(pgm, destpgm)
os.chmod(destpgm, 0755)
else:
put_pgm(pgm, destpgm, mode=0755)
tarname = _iftar(relpgm)
if tarname:
destpgm = _tartask(stagedir, tarname, destpgm)
env.warn_only = False
destpgm = destpgm + " " + args
with cd(stagedir):
pgm_to_use(destpgm)
def cleanup_dirs(stagedir=None, local_exe=None):
cmd = _make_ssh("rm -rf %s" %(stagedir), local_exe=local_exe)
local(cmd)
def bootpgm(pgm=None, args=None, conf=None, env_conf=None, output=None, stagedir=None, remotedir=None, local_exe=None):
local_exe = str(local_exe).lower() == 'true'
pgm_to_use = run
put_pgm = put
if local_exe:
pgm_to_use = local
put_pgm = shutil.copy
args = urllib.unquote(args)
pgm_to_use('mkdir %s;chmod 777 %s' % (remotedir, remotedir))
pgm_to_use('mkdir -p %s' % stagedir)
relpgm = os.path.basename(pgm)
destpgm = "%s/%s" % (stagedir, relpgm)
if local_exe:
os.chdir(stagedir)
put_pgm(pgm, destpgm)
os.chmod(destpgm, 0755)
else:
put_pgm(pgm, destpgm, mode=0755)
tarname = _iftar(relpgm)
if tarname:
destpgm = _tartask(stagedir, tarname, destpgm, run_pgm=pgm_to_use)
if conf and conf != "None":
destconf = "%s/bootconf.json" % stagedir
put_pgm(conf, destconf)
os.remove(conf)
if env_conf and env_conf != "None":
destenv = "%s/bootenv.sh" % stagedir
put_pgm(env_conf, destenv)
os.remove(env_conf)
destpgm = destpgm + " " + args
local_cmd = _make_ssh("cd %s;%s" %(stagedir, destpgm), local_exe=local_exe)
if local_cmd:
local(local_cmd)
with cd(stagedir):
#run(destpgm)
try:
fetch_conf(output=output, stagedir=stagedir, local_exe=local_exe)
except:
pass
def fetch_conf(output=None, stagedir=None, local_exe=None):
remote_output = "%s/bootout.json" % (stagedir)
if local_exe:
shutil.copy(remote_output, output)
else:
get(remote_output, output)