8 from dea import DeploymentEnvironmentAdapter
9 from dha import DeploymentHardwareAdapter
10 from install_fuel_master import InstallFuelMaster
11 from deploy_env import CloudDeploy
15 exec_cmd = common.exec_cmd
17 check_file_exists = common.check_file_exists
18 check_if_root = common.check_if_root
21 TMP_DIR = '%s/fueltmp' % os.getenv('HOME')
22 PATCH_DIR = 'fuel_patch'
26 def __init__(self, new_path):
27 self.new_path = os.path.expanduser(new_path)
30 self.saved_path = os.getcwd()
31 os.chdir(self.new_path)
33 def __exit__(self, etype, value, traceback):
34 os.chdir(self.saved_path)
37 class AutoDeploy(object):
39 def __init__(self, without_fuel, iso_file, dea_file, dha_file):
40 self.without_fuel = without_fuel
41 self.iso_file = iso_file
42 self.dea_file = dea_file
43 self.dha_file = dha_file
44 self.dea = DeploymentEnvironmentAdapter(dea_file)
45 self.dha = DeploymentHardwareAdapter(dha_file)
47 self.fuel_node_id = self.dha.get_fuel_node_id()
48 self.fuel_custom = self.dha.use_fuel_custom_install()
49 self.fuel_username, self.fuel_password = self.dha.get_fuel_access()
51 def setup_dir(self, dir):
55 def cleanup_dir(self, dir):
56 if os.path.isdir(dir):
59 def power_off_blades(self):
60 node_ids = self.dha.get_all_node_ids()
61 node_ids = list(set(node_ids) - set([self.fuel_node_id]))
62 for node_id in node_ids:
63 self.dha.node_power_off(node_id)
65 def modify_ip(self, ip_addr, index, val):
66 ip_str = str(netaddr.IPAddress(ip_addr))
67 decimal_list = map(int, ip_str.split('.'))
68 decimal_list[index] = val
69 return '.'.join(map(str, decimal_list))
71 def collect_fuel_info(self):
72 self.fuel_conf['ip'] = self.dea.get_fuel_ip()
73 self.fuel_conf['gw'] = self.dea.get_fuel_gateway()
74 self.fuel_conf['dns1'] = self.dea.get_fuel_dns()
75 self.fuel_conf['netmask'] = self.dea.get_fuel_netmask()
76 self.fuel_conf['hostname'] = self.dea.get_fuel_hostname()
77 self.fuel_conf['showmenu'] = 'yes'
79 def install_fuel_master(self):
81 log('Not Installing Fuel Master')
83 log('Install Fuel Master')
84 new_iso = '%s/deploy-%s' % (TMP_DIR, os.path.basename(self.iso_file))
85 self.patch_iso(new_iso)
86 self.iso_file = new_iso
89 def install_iso(self):
90 fuel = InstallFuelMaster(self.dea_file, self.dha_file,
91 self.fuel_conf['ip'], self.fuel_username,
92 self.fuel_password, self.fuel_node_id,
93 self.iso_file, WORK_DIR)
95 log('Custom Fuel install')
98 log('Ordinary Fuel install')
101 def patch_iso(self, new_iso):
102 tmp_orig_dir = '%s/origiso' % TMP_DIR
103 tmp_new_dir = '%s/newiso' % TMP_DIR
104 self.copy(tmp_orig_dir, tmp_new_dir)
105 self.patch(tmp_new_dir, new_iso)
107 def copy(self, tmp_orig_dir, tmp_new_dir):
109 self.setup_dir(tmp_orig_dir)
110 self.setup_dir(tmp_new_dir)
111 exec_cmd('fuseiso %s %s' % (self.iso_file, tmp_orig_dir))
112 with cd(tmp_orig_dir):
113 exec_cmd('find . | cpio -pd %s' % tmp_new_dir)
114 with cd(tmp_new_dir):
115 exec_cmd('fusermount -u %s' % tmp_orig_dir)
116 shutil.rmtree(tmp_orig_dir)
117 exec_cmd('chmod -R 755 %s' % tmp_new_dir)
119 def patch(self, tmp_new_dir, new_iso):
121 patch_dir = '%s/%s' % (os.getcwd(), PATCH_DIR)
122 ks_path = '%s/ks.cfg.patch' % patch_dir
124 with cd(tmp_new_dir):
125 exec_cmd('cat %s | patch -p0' % ks_path)
126 shutil.rmtree('.rr_moved')
127 isolinux = 'isolinux/isolinux.cfg'
128 log('isolinux.cfg before: %s'
129 % exec_cmd('grep netmask %s' % isolinux))
130 self.update_fuel_isolinux(isolinux)
131 log('isolinux.cfg after: %s'
132 % exec_cmd('grep netmask %s' % isolinux))
134 iso_linux_bin = 'isolinux/isolinux.bin'
135 exec_cmd('mkisofs -quiet -r -J -R -b %s '
136 '-no-emul-boot -boot-load-size 4 '
137 '-boot-info-table -hide-rr-moved '
138 '-x "lost+found:" -o %s .'
139 % (iso_linux_bin, new_iso))
141 def update_fuel_isolinux(self, file):
142 with io.open(file) as f:
144 for key, val in self.fuel_conf.iteritems():
145 pattern = r'%s=[^ ]\S+' % key
146 replace = '%s=%s' % (key, val)
147 data = re.sub(pattern, replace, data)
148 with io.open(file, 'w') as f:
151 def deploy_env(self):
152 dep = CloudDeploy(self.dha, self.fuel_conf['ip'], self.fuel_username,
153 self.fuel_password, self.dea_file, WORK_DIR)
158 self.setup_dir(TMP_DIR)
159 self.collect_fuel_info()
160 self.power_off_blades()
161 self.install_fuel_master()
162 self.cleanup_dir(TMP_DIR)
168 python deploy.py [-nf] <isofile> <deafile> <dhafile>
171 -nf Do not install Fuel master
174 def parse_arguments():
175 if (len(sys.argv) < 4 or len(sys.argv) > 5
176 or (len(sys.argv) == 5 and sys.argv[1] != '-nf')):
177 log('Incorrect number of arguments')
181 if len(sys.argv) == 5 and sys.argv[1] == '-nf':
183 iso_file = sys.argv[-3]
184 dea_file = sys.argv[-2]
185 dha_file = sys.argv[-1]
186 check_file_exists(iso_file)
187 check_file_exists(dea_file)
188 check_file_exists(dha_file)
189 return (without_fuel, iso_file, dea_file, dha_file)
193 without_fuel, iso_file, dea_file, dha_file = parse_arguments()
195 d = AutoDeploy(without_fuel, iso_file, dea_file, dha_file)
198 if __name__ == '__main__':