Merge "Corrected links associated with release docs. To be updated along with the...
[genesis.git] / fuel / deploy / common.py
1 import subprocess
2 import sys
3 import os
4 import logging
5 import argparse
6
7 N = {'id': 0, 'status': 1, 'name': 2, 'cluster': 3, 'ip': 4, 'mac': 5,
8      'roles': 6, 'pending_roles': 7, 'online': 8}
9 E = {'id': 0, 'status': 1, 'name': 2, 'mode': 3, 'release_id': 4,
10      'changes': 5, 'pending_release_id': 6}
11 R = {'id': 0, 'name': 1, 'state': 2, 'operating_system': 3, 'version': 4}
12 RO = {'name': 0, 'conflicts': 1}
13
14 LOG = logging.getLogger(__name__)
15 LOG.setLevel(logging.DEBUG)
16 formatter = logging.Formatter('%(message)s')
17 out_handler = logging.StreamHandler(sys.stdout)
18 out_handler.setFormatter(formatter)
19 LOG.addHandler(out_handler)
20 out_handler = logging.FileHandler('autodeploy.log', mode='w')
21 out_handler.setFormatter(formatter)
22 LOG.addHandler(out_handler)
23
24 def exec_cmd(cmd, check=True):
25     process = subprocess.Popen(cmd,
26                                stdout=subprocess.PIPE,
27                                stderr=subprocess.STDOUT,
28                                shell=True)
29     response = process.communicate()[0].strip()
30     return_code = process.returncode
31     if check:
32         if return_code > 0:
33             err(response)
34         else:
35             return response
36     return response, return_code
37
38 def run_proc(cmd):
39     process = subprocess.Popen(cmd,
40                                stdout=subprocess.PIPE,
41                                stderr=subprocess.STDOUT,
42                                shell=True)
43     return process
44
45 def parse(printout):
46     parsed_list = []
47     lines = printout.splitlines()
48     for l in lines[2:]:
49          parsed = [e.strip() for e in l.split('|')]
50          parsed_list.append(parsed)
51     return parsed_list
52
53 def clean(lines):
54     parsed_list = []
55     parsed = []
56     for l in lines.strip().splitlines():
57         parsed = []
58         cluttered = [e.strip() for e in l.split(' ')]
59         for p in cluttered:
60             if p:
61                 parsed.append(p)
62         parsed_list.append(parsed)
63     return parsed if len(parsed_list) == 1 else parsed_list
64
65 def err(message):
66     LOG.error('%s\n' % message)
67     sys.exit(1)
68
69 def check_file_exists(file_path):
70     if not os.path.isfile(file_path):
71         err('ERROR: File %s not found\n' % file_path)
72
73 def check_dir_exists(dir_path):
74     if not os.path.isdir(dir_path):
75         err('ERROR: Directory %s not found\n' % dir_path)
76
77 def create_dir_if_not_exists(dir_path):
78     if not os.path.isdir(dir_path):
79         log('Creating directory %s' % dir_path)
80         os.makedirs(dir_path)
81
82 def commafy(comma_separated_list):
83     l = [c.strip() for c in comma_separated_list.split(',')]
84     return ','.join(l)
85
86 def delete_file(file):
87     if os.path.exists(file):
88         os.remove(file)
89
90 def check_if_root():
91     r = exec_cmd('whoami')
92     if r != 'root':
93         err('You need be root to run this application')
94
95 def log(message):
96     LOG.debug('%s\n' % message)
97
98 class ArgParser(argparse.ArgumentParser):
99     def error(self, message):
100         sys.stderr.write('ERROR: %s\n' % message)
101         self.print_help()
102         sys.exit(2)
103