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
|
import subprocess
import sys
import os
import logging
import argparse
N = {'id': 0, 'status': 1, 'name': 2, 'cluster': 3, 'ip': 4, 'mac': 5,
'roles': 6, 'pending_roles': 7, 'online': 8}
E = {'id': 0, 'status': 1, 'name': 2, 'mode': 3, 'release_id': 4,
'changes': 5, 'pending_release_id': 6}
R = {'id': 0, 'name': 1, 'state': 2, 'operating_system': 3, 'version': 4}
RO = {'name': 0, 'conflicts': 1}
LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s')
out_handler = logging.StreamHandler(sys.stdout)
out_handler.setFormatter(formatter)
LOG.addHandler(out_handler)
out_handler = logging.FileHandler('autodeploy.log', mode='w')
out_handler.setFormatter(formatter)
LOG.addHandler(out_handler)
def exec_cmd(cmd, check=True):
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True)
response = process.communicate()[0].strip()
return_code = process.returncode
if check:
if return_code > 0:
err(response)
else:
return response
return response, return_code
def run_proc(cmd):
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True)
return process
def parse(printout):
parsed_list = []
lines = printout.splitlines()
for l in lines[2:]:
parsed = [e.strip() for e in l.split('|')]
parsed_list.append(parsed)
return parsed_list
def clean(lines):
parsed_list = []
parsed = []
for l in lines.strip().splitlines():
parsed = []
cluttered = [e.strip() for e in l.split(' ')]
for p in cluttered:
if p:
parsed.append(p)
parsed_list.append(parsed)
return parsed if len(parsed_list) == 1 else parsed_list
def err(message):
LOG.error('%s\n' % message)
sys.exit(1)
def check_file_exists(file_path):
if not os.path.isfile(file_path):
err('ERROR: File %s not found\n' % file_path)
def check_dir_exists(dir_path):
if not os.path.isdir(dir_path):
err('ERROR: Directory %s not found\n' % dir_path)
def create_dir_if_not_exists(dir_path):
if not os.path.isdir(dir_path):
log('Creating directory %s' % dir_path)
os.makedirs(dir_path)
def commafy(comma_separated_list):
l = [c.strip() for c in comma_separated_list.split(',')]
return ','.join(l)
def delete_file(file):
if os.path.exists(file):
os.remove(file)
def check_if_root():
r = exec_cmd('whoami')
if r != 'root':
err('You need be root to run this application')
def log(message):
LOG.debug('%s\n' % message)
class ArgParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('ERROR: %s\n' % message)
self.print_help()
sys.exit(2)
|