30 @defgroup CTX VM Exceptions
41 raise Exception(
"The request must contain the parameter %s" % param)
50 raise Exception(
"There was problem during function execution: %s" % error)
60 raise Exception(
"Build in command %s failed with exit code: %s" % (cmd, code))
71 if hosts_list
is None:
74 f = open(
"/etc/hosts",
"r")
79 lines[:] = [line
for line
in lines
if not line.find(
"farm") != -1]
81 known_hosts = open(os.path.expanduser(
'~%s/.ssh/known_hosts' % user),
"a")
83 for host
in hosts_list:
84 lines.append(
"%s\t%s\n" % (host[
"ip"], host[
"host_name"]))
87 f = open(
"/etc/hosts",
'w')
94 for host
in hosts_list:
95 r = subprocess.call([
'ssh-keyscan', host[
"ip"]], stdout=known_hosts)
98 r = subprocess.call([
'ssh-keyscan', host[
"host_name"]], stdout=known_hosts)
110 r = subprocess.call([
'hostname', hostname])
120 return subprocess.call([
"which", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
128 r =
'not working cmd'
130 r = subprocess.call(
'sleep 5 && poweroff &', shell=
True)
132 r = subprocess.call(
'sleep 5 && shutdown -h now', shell=
True)
142 r =
'not working cmd'
144 r = subprocess.call([
'reboot'])
146 r = subprocess.call([
'shutdown',
'-r',
'now'])
159 chars = string.ascii_letters + string.digits
161 random.seed = (os.urandom(1024))
162 password =
''.join(random.choice(chars)
for unused
in range(12))
163 p = subprocess.Popen((
'openssl',
'passwd',
'-1', password), stdout=subprocess.PIPE)
164 shadow_password = p.communicate()[0].strip()
166 if p.returncode != 0:
171 p = subprocess.Popen([
'chpasswd',
'-e'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
172 r = p.communicate(
'%s:%s\n' % (user, shadow_password))
175 if p.returncode != 0:
192 uid = pwd.getpwnam(user).pw_uid
193 gid = grp.getgrnam(user).gr_gid
194 keys_path = os.path.expanduser(
'~%s/.ssh/authorized_keys' % user)
195 if not os.path.exists(keys_path):
196 d = os.path.dirname(os.path.expanduser(
'~%s/.ssh/' % user))
197 if not os.path.exists(d):
199 os.chown(d, uid, gid)
200 fp = file(keys_path,
'w')
203 fp = file(keys_path,
'a')
204 fp.write(
'\n%s\n' % ssh_key)
206 os.chown(keys_path, uid, gid)
208 ssh_conf = open(
"/etc/ssh/ssh_config",
'r')
209 lines = ssh_conf.readlines()
212 lines[:] = [line for line
in lines
if line.find(
"StrictHostKeyChecking") == -1]
213 lines.append(
"StrictHostKeyChecking no")
214 ssh_conf = open(
"/etc/ssh/ssh_config",
'w')
215 ssh_conf.writelines(lines)
230 key_name = os.path.expanduser(
'~%s/.ssh/' % user) + key_name
231 if not os.path.exists(key_name):
232 subprocess.call([
"ssh-keygen",
"-q",
"-N",
"",
"-f", key_name])
235 f = open(
"%s.pub" % key_name,
"r")
236 key = f.read().strip()