modified to accomodate the MAAS 2.0
[joid.git] / ci / depMAAS.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 This script generates a maas deployer config based on lab config file.
6
7 Parameters:
8  -l, --lab      : lab config file
9 """
10
11 from optparse import OptionParser
12 from jinja2 import Environment, FileSystemLoader
13 from distutils.version import LooseVersion, StrictVersion
14 import os
15 import subprocess
16 import yaml
17 from pprint import pprint as pp
18 import socket
19 import fcntl
20 import struct
21
22 #
23 # Parse parameters
24 #
25
26 parser = OptionParser()
27 parser.add_option("-l", "--lab", dest="lab", help="lab config file")
28 (options, args) = parser.parse_args()
29 labconfig_file = options.lab
30
31 #
32 # Set Path and configs path
33 #
34
35 # Capture our current directory
36 jujuver = subprocess.check_output(["juju", "--version"])
37
38 if LooseVersion(jujuver) >= LooseVersion('2'):
39     TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/maas_tpl'
40 else:
41     TPL_DIR = os.path.dirname(os.path.abspath(__file__))+'/config_tpl/maas2/maas_tpl'
42
43 HOME = os.environ['HOME']
44 USER = os.environ['USER']
45
46 #
47 # Prepare variables
48 #
49
50 # Prepare a storage for passwords
51 passwords_store = dict()
52
53 #
54 # Local Functions
55 #
56
57
58 def load_yaml(filepath):
59     """Load YAML file"""
60     with open(filepath, 'r') as stream:
61         try:
62             return yaml.load(stream)
63         except yaml.YAMLError as exc:
64             print(exc)
65
66
67 def get_ip_address(ifname):
68     """Get local IP"""
69     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
70     return socket.inet_ntoa(fcntl.ioctl(
71         s.fileno(),
72         0x8915,  # SIOCGIFADDR
73         struct.pack('256s', bytes(ifname.encode('utf-8')[:15]))
74     )[20:24])
75
76
77 #
78 # Config import
79 #
80
81 def installMAAS():
82     subprocess.call(["echo", i], shell=True)
83
84 def configMAAS():
85     
86     
87 # Create the jinja2 environment.
88 env = Environment(loader=FileSystemLoader(TPL_DIR),
89                   trim_blocks=True)
90 template = env.get_template('deployment.yaml')
91
92 # Render the template
93 output = template.render(**config)
94
95 # Check output syntax
96 try:
97     yaml.load(output)
98 except yaml.YAMLError as exc:
99     print(exc)
100
101 # print output
102 print(output)