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