Merge "Corrected links associated with release docs. To be updated along with the...
[genesis.git] / fuel / deploy / deploy.py
1 import os
2 import shutil
3 import io
4 import re
5 import sys
6 import netaddr
7 import uuid
8 import yaml
9
10 from dea import DeploymentEnvironmentAdapter
11 from dha import DeploymentHardwareAdapter
12 from install_fuel_master import InstallFuelMaster
13 from deploy_env import CloudDeploy
14 from setup_execution_environment import ExecutionEnvironment
15 import common
16
17 log = common.log
18 exec_cmd = common.exec_cmd
19 err = common.err
20 check_file_exists = common.check_file_exists
21 check_dir_exists = common.check_dir_exists
22 create_dir_if_not_exists = common.create_dir_if_not_exists
23 check_if_root = common.check_if_root
24 ArgParser = common.ArgParser
25
26 FUEL_VM = 'fuel'
27 PATCH_DIR = 'fuel_patch'
28 WORK_DIR = 'deploy'
29 CWD = os.getcwd()
30
31 class cd:
32     def __init__(self, new_path):
33         self.new_path = os.path.expanduser(new_path)
34
35     def __enter__(self):
36         self.saved_path = CWD
37         os.chdir(self.new_path)
38
39     def __exit__(self, etype, value, traceback):
40         os.chdir(self.saved_path)
41
42
43 class AutoDeploy(object):
44
45     def __init__(self, without_fuel, storage_dir, pxe_bridge, iso_file,
46                  dea_file, dha_file):
47         self.without_fuel = without_fuel
48         self.storage_dir = storage_dir
49         self.pxe_bridge = pxe_bridge
50         self.iso_file = iso_file
51         self.dea_file = dea_file
52         self.dha_file = dha_file
53         self.dea = DeploymentEnvironmentAdapter(dea_file)
54         self.dha = DeploymentHardwareAdapter(dha_file)
55         self.fuel_conf = {}
56         self.fuel_node_id = self.dha.get_fuel_node_id()
57         self.fuel_username, self.fuel_password = self.dha.get_fuel_access()
58         self.tmp_dir = None
59
60     def modify_ip(self, ip_addr, index, val):
61         ip_str = str(netaddr.IPAddress(ip_addr))
62         decimal_list = map(int, ip_str.split('.'))
63         decimal_list[index] = val
64         return '.'.join(map(str, decimal_list))
65
66     def collect_fuel_info(self):
67         self.fuel_conf['ip'] = self.dea.get_fuel_ip()
68         self.fuel_conf['gw'] = self.dea.get_fuel_gateway()
69         self.fuel_conf['dns1'] = self.dea.get_fuel_dns()
70         self.fuel_conf['netmask'] = self.dea.get_fuel_netmask()
71         self.fuel_conf['hostname'] = self.dea.get_fuel_hostname()
72         self.fuel_conf['showmenu'] = 'yes'
73
74     def install_fuel_master(self):
75         log('Install Fuel Master')
76         new_iso = '%s/deploy-%s' \
77                   % (self.tmp_dir, os.path.basename(self.iso_file))
78         self.patch_iso(new_iso)
79         self.iso_file = new_iso
80         self.install_iso()
81
82     def install_iso(self):
83         fuel = InstallFuelMaster(self.dea_file, self.dha_file,
84                                  self.fuel_conf['ip'], self.fuel_username,
85                                  self.fuel_password, self.fuel_node_id,
86                                  self.iso_file, WORK_DIR)
87         fuel.install()
88
89     def patch_iso(self, new_iso):
90         tmp_orig_dir = '%s/origiso' % self.tmp_dir
91         tmp_new_dir = '%s/newiso' % self.tmp_dir
92         self.copy(tmp_orig_dir, tmp_new_dir)
93         self.patch(tmp_new_dir, new_iso)
94
95     def copy(self, tmp_orig_dir, tmp_new_dir):
96         log('Copying...')
97         os.makedirs(tmp_orig_dir)
98         os.makedirs(tmp_new_dir)
99         exec_cmd('fuseiso %s %s' % (self.iso_file, tmp_orig_dir))
100         with cd(tmp_orig_dir):
101             exec_cmd('find . | cpio -pd %s' % tmp_new_dir)
102         with cd(tmp_new_dir):
103             exec_cmd('fusermount -u %s' % tmp_orig_dir)
104         shutil.rmtree(tmp_orig_dir)
105         exec_cmd('chmod -R 755 %s' % tmp_new_dir)
106
107     def patch(self, tmp_new_dir, new_iso):
108         log('Patching...')
109         patch_dir = '%s/%s' % (CWD, PATCH_DIR)
110         ks_path = '%s/ks.cfg.patch' % patch_dir
111
112         with cd(tmp_new_dir):
113             exec_cmd('cat %s | patch -p0' % ks_path)
114             shutil.rmtree('.rr_moved')
115             isolinux = 'isolinux/isolinux.cfg'
116             log('isolinux.cfg before: %s'
117                 % exec_cmd('grep netmask %s' % isolinux))
118             self.update_fuel_isolinux(isolinux)
119             log('isolinux.cfg after: %s'
120                 % exec_cmd('grep netmask %s' % isolinux))
121
122             iso_linux_bin = 'isolinux/isolinux.bin'
123             exec_cmd('mkisofs -quiet -r -J -R -b %s '
124                      '-no-emul-boot -boot-load-size 4 '
125                      '-boot-info-table -hide-rr-moved '
126                      '-x "lost+found:" -o %s .'
127                      % (iso_linux_bin, new_iso))
128
129     def update_fuel_isolinux(self, file):
130         with io.open(file) as f:
131             data = f.read()
132         for key, val in self.fuel_conf.iteritems():
133             pattern = r'%s=[^ ]\S+' % key
134             replace = '%s=%s' % (key, val)
135             data = re.sub(pattern, replace, data)
136         with io.open(file, 'w') as f:
137             f.write(data)
138
139     def deploy_env(self):
140         dep = CloudDeploy(self.dha, self.fuel_conf['ip'], self.fuel_username,
141                           self.fuel_password, self.dea_file, WORK_DIR)
142         return dep.deploy()
143
144     def setup_execution_environment(self):
145         exec_env = ExecutionEnvironment(self.storage_dir, self.pxe_bridge,
146                                         self.dha_file, self.dea)
147         exec_env.setup_environment()
148
149     def create_tmp_dir(self):
150         self.tmp_dir = '%s/fueltmp-%s' % (CWD, str(uuid.uuid1()))
151         os.makedirs(self.tmp_dir)
152
153     def deploy(self):
154         check_if_root()
155         self.collect_fuel_info()
156         if not self.without_fuel:
157             self.setup_execution_environment()
158             self.create_tmp_dir()
159             self.install_fuel_master()
160             shutil.rmtree(self.tmp_dir)
161         return self.deploy_env()
162
163 def check_bridge(pxe_bridge, dha_path):
164     with io.open(dha_path) as yaml_file:
165         dha_struct = yaml.load(yaml_file)
166     if dha_struct['adapter'] != 'libvirt':
167         log('Using Linux Bridge %s for booting up the Fuel Master VM'
168             % pxe_bridge)
169         r = exec_cmd('ip link show %s' % pxe_bridge)
170         if pxe_bridge in r and 'state UP' not in r:
171             err('Linux Bridge {0} is not Active, '
172                 'bring it UP first: [ip link set dev {0} up]' % pxe_bridge)
173
174 def parse_arguments():
175     parser = ArgParser(prog='python %s' % __file__)
176     parser.add_argument('-nf', dest='without_fuel', action='store_true',
177                         default=False,
178                         help='Do not install Fuel Master (and Node VMs when '
179                              'using libvirt)')
180     parser.add_argument('iso_file', nargs='?', action='store',
181                         default='%s/OPNFV.iso' % CWD,
182                         help='ISO File [default: OPNFV.iso]')
183     parser.add_argument('dea_file', action='store',
184                         help='Deployment Environment Adapter: dea.yaml')
185     parser.add_argument('dha_file', action='store',
186                         help='Deployment Hardware Adapter: dha.yaml')
187     parser.add_argument('-s', dest='storage_dir', action='store',
188                         default='%s/images' % CWD,
189                         help='Storage Directory [default: images]')
190     parser.add_argument('-b', dest='pxe_bridge', action='store',
191                         default='pxebr',
192                         help='Linux Bridge for booting up the Fuel Master VM '
193                              '[default: pxebr]')
194
195     args = parser.parse_args()
196     log(args)
197
198     check_file_exists(args.dea_file)
199     check_file_exists(args.dha_file)
200
201     if not args.without_fuel:
202         log('Using OPNFV ISO file: %s' % args.iso_file)
203         check_file_exists(args.iso_file)
204         log('Using image directory: %s' % args.storage_dir)
205         create_dir_if_not_exists(args.storage_dir)
206         check_bridge(args.pxe_bridge, args.dha_file)
207
208     return (args.without_fuel, args.storage_dir, args.pxe_bridge,
209             args.iso_file, args.dea_file, args.dha_file)
210
211
212 def main():
213     without_fuel, storage_dir, pxe_bridge, iso_file, dea_file, dha_file = \
214         parse_arguments()
215
216     d = AutoDeploy(without_fuel, storage_dir, pxe_bridge, iso_file,
217                    dea_file, dha_file)
218     sys.exit(d.deploy())
219
220 if __name__ == '__main__':
221     main()