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
18 exec_cmd = common.exec_cmd
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
27 PATCH_DIR = 'fuel_patch'
32 def __init__(self, new_path):
33 self.new_path = os.path.expanduser(new_path)
37 os.chdir(self.new_path)
39 def __exit__(self, etype, value, traceback):
40 os.chdir(self.saved_path)
43 class AutoDeploy(object):
45 def __init__(self, without_fuel, storage_dir, pxe_bridge, iso_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)
56 self.fuel_node_id = self.dha.get_fuel_node_id()
57 self.fuel_username, self.fuel_password = self.dha.get_fuel_access()
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))
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'
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
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)
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)
95 def copy(self, tmp_orig_dir, tmp_new_dir):
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)
107 def patch(self, tmp_new_dir, new_iso):
109 patch_dir = '%s/%s' % (CWD, PATCH_DIR)
110 ks_path = '%s/ks.cfg.patch' % patch_dir
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))
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))
129 def update_fuel_isolinux(self, file):
130 with io.open(file) as f:
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:
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)
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()
149 def create_tmp_dir(self):
150 self.tmp_dir = '%s/fueltmp-%s' % (CWD, str(uuid.uuid1()))
151 os.makedirs(self.tmp_dir)
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()
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'
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)
174 def parse_arguments():
175 parser = ArgParser(prog='python %s' % __file__)
176 parser.add_argument('-nf', dest='without_fuel', action='store_true',
178 help='Do not install Fuel Master (and Node VMs when '
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('storage_dir', nargs='?', action='store',
188 default='%s/images' % CWD,
189 help='Storage Directory [default: images]')
190 parser.add_argument('pxe_bridge', nargs='?', action='store',
192 help='Linux Bridge for booting up the Fuel Master VM '
195 args = parser.parse_args()
197 check_file_exists(args.dea_file)
198 check_file_exists(args.dha_file)
200 if not args.without_fuel:
201 log('Using OPNFV ISO file: %s' % args.iso_file)
202 check_file_exists(args.iso_file)
203 log('Using image directory: %s' % args.storage_dir)
204 create_dir_if_not_exists(args.storage_dir)
205 log('Using bridge %s to boot up Fuel Master VM on it'
207 check_bridge(args.pxe_bridge, args.dha_file)
209 return (args.without_fuel, args.storage_dir, args.pxe_bridge,
210 args.iso_file, args.dea_file, args.dha_file)
214 without_fuel, storage_dir, pxe_bridge, iso_file, dea_file, dha_file = \
217 d = AutoDeploy(without_fuel, storage_dir, pxe_bridge, iso_file,
221 if __name__ == '__main__':