365f6fb70066fc9afcaa68c2aa1d8a0ab7c60890
[genesis.git] / fuel / deploy / cloud_deploy / cloud / common.py
1 import subprocess
2 import sys
3 import os
4 import logging
5
6 N = {'id': 0, 'status': 1, 'name': 2, 'cluster': 3, 'ip': 4, 'mac': 5,
7      'roles': 6, 'pending_roles': 7, 'online': 8}
8 E = {'id': 0, 'status': 1, 'name': 2, 'mode': 3, 'release_id': 4,
9      'changes': 5, 'pending_release_id': 6}
10 R = {'id': 0, 'name': 1, 'state': 2, 'operating_system': 3, 'version': 4}
11 RO = {'name': 0, 'conflicts': 1}
12
13 LOG = logging.getLogger(__name__)
14 LOG.setLevel(logging.DEBUG)
15 formatter = logging.Formatter('%(message)s')
16 out_handler = logging.StreamHandler(sys.stdout)
17 out_handler.setFormatter(formatter)
18 LOG.addHandler(out_handler)
19 out_handler = logging.FileHandler('autodeploy.log', mode='w')
20 out_handler.setFormatter(formatter)
21 LOG.addHandler(out_handler)
22
23 def exec_cmd(cmd):
24     process = subprocess.Popen(cmd,
25                                stdout=subprocess.PIPE,
26                                stderr=subprocess.STDOUT,
27                                shell=True)
28     return process.communicate()[0], process.returncode
29
30 def run_proc(cmd):
31     process = subprocess.Popen(cmd,
32                                stdout=subprocess.PIPE,
33                                stderr=subprocess.STDOUT,
34                                shell=True)
35     return process
36
37 def parse(printout, *args):
38     parsed_list = []
39     lines = printout[0].splitlines()
40     for l in lines[2:]:
41          parsed = [e.strip() for e in l.split('|')]
42          parsed_list.append(parsed)
43     return parsed_list
44
45 def err(error_message):
46     LOG.error(error_message)
47     sys.exit(1)
48
49 def check_file_exists(file_path):
50     if not os.path.isfile(file_path):
51         err('ERROR: File %s not found\n' % file_path)