def console_log(self, server_id): """ Return the server console log """ console_log = os.path.join(CONF.instances_dir, server_id, 'console.log') # Make the console log readable utils.execute(['chown', os.getuid(), console_log], run_as_root=True) with open(console_log) as fh: data = fh.read() return data
def _create_config_drive(server, keypair): """ Create the config drive for the server """ if not (CONF.force_config_drive or server['config_drive'] == 'True'): return # Config drive data config_data = { 'vendor_data': {}, 'meta_data': { 'availability_zone': 'dwarf', 'hostname': '%s.dwarflocal' % server['name'], 'launch_index': 0, 'name': server['name'], 'uuid': server['id'], }, } # Add the SSH keypair info if keypair is not None: config_data['meta_data']['public_keys'] = { keypair['name']: keypair['public_key'] } # Create a temporary directory containing the config drive data config_dir = os.path.join('/tmp', 'dwarf-config-drive-%s' % server['id']) if os.path.exists(config_dir): shutil.rmtree(config_dir) for version in ['latest', '2013-10-17']: data_dir = os.path.join(config_dir, 'openstack', version) os.makedirs(data_dir) for data in ['vendor_data', 'meta_data']: data_file = os.path.join(data_dir, '%s.json' % data) with open(data_file, 'w') as fh: json.dump(config_data[data], fh) # Create the server config drive at: # instances/<server_id>/disk.config server_disk_config = os.path.join(CONF.instances_dir, server['id'], 'disk.config') utils.execute([ 'genisoimage', '-o', server_disk_config, '-ldots', '-allow-lowercase', '-allow-multidot', '-l', '-quiet', '-J', '-r', '-V', 'config-2', config_dir ]) # Remove the temporary directory shutil.rmtree(config_dir)
def console_log(self, server_id): """ Return the server console log """ LOG.info('console_log(server_id=%s)', server_id) console_log = os.path.join(CONF.instances_dir, server_id, 'console.log') # Make the console log readable utils.execute(['chown', os.getuid(), console_log], run_as_root=True) with open(console_log) as fh: data = fh.read() return unicode(data, errors='ignore')
def _create_config_drive(server, keypair): """ Create the config drive for the server """ if not CONF.force_config_drive and not server['config_drive']: return # Config drive data config_data = { 'vendor_data': { }, 'meta_data': { 'availability_zone': 'dwarf', 'hostname': '%s.dwarflocal' % server['name'], 'launch_index': 0, 'name': server['name'], 'public_keys': { server['key_name']: keypair['public_key'], }, 'uuid': server['id'], }, } # Create a temporary directory containing the config drive data config_dir = os.path.join('/tmp', 'dwarf-config-drive-%s' % server['id']) if os.path.exists(config_dir): shutil.rmtree(config_dir) for version in ['latest', '2013-10-17']: data_dir = os.path.join(config_dir, 'openstack', version) os.makedirs(data_dir) for data in ['vendor_data', 'meta_data']: data_file = os.path.join(data_dir, '%s.json' % data) with open(data_file, 'w') as fh: json.dump(config_data[data], fh) # Create the server config drive at: # instances/<server_id>/disk.config server_disk_config = os.path.join(CONF.instances_dir, server['id'], 'disk.config') utils.execute(['genisoimage', '-o', server_disk_config, '-ldots', '-allow-lowercase', '-allow-multidot', '-l', '-quiet', '-J', '-r', '-V', 'config-2', config_dir]) # Remove the temporary directory shutil.rmtree(config_dir)
def _add_ec2metadata_route(ip, port): """ Add the iptables route for the Ec2 metadata service """ LOG.info('_add_ec2metadata_route(ip=%s, port=%s)', ip, port) # Add the route utils.execute(['iptables', '-t', 'nat', '-A', 'PREROUTING', '-s', ip, '-d', '169.254.169.254/32', '-p', 'tcp', '-m', 'tcp', '--dport', 80, '-j', 'REDIRECT', '--to-port', port], run_as_root=True)
def _delete_ec2metadata_route(ip, port): """ Delete a (compute) server from the metadata server """ LOG.info('_delete_ec2metadata_route(ip=%s, port=%s)', ip, port) # Delete the route utils.execute(['iptables', '-t', 'nat', '-D', 'PREROUTING', '-s', ip, '-d', '169.254.169.254/32', '-p', 'tcp', '-m', 'tcp', '--dport', 80, '-j', 'REDIRECT', '--to-port', port], run_as_root=True, check_exit_code=False)
def create_network(self): """ Create the network """ LOG.info('create_network()') self._connect() try: # Check if the network already exists net = self.libvirt.networkLookupByName('dwarf') except libvirt.libvirtError as e: if e.get_error_code() != libvirt.VIR_ERR_NO_NETWORK: # Unexpected error raise # Define the network xml = _create_net_xml() net = self.libvirt.networkDefineXML(xml) # Configure the network to automatically start on host boot net.setAutostart(1) # Create (start) the network if net.isActive() == 0: net.create() # Add the iptables rule for the Ec2 metadata service rule = ['PREROUTING', '-t', 'nat', '-s', '%s/24' % CONF.libvirt_bridge_ip, '-d', '169.254.169.254/32', '-p', 'tcp', '-m', 'tcp', '--dport', 80, '-j', 'REDIRECT', '--to-port', CONF.ec2_metadata_port] try: utils.execute(['iptables', '-C'] + rule, run_as_root=True, check_exit_code=0) except exception.CommandExecutionError: utils.execute(['iptables', '-A'] + rule, run_as_root=True, check_exit_code=0)
def _create_disks(server): """ Create the base (backing) and server disk images """ server_id = server['id'] image_id = server['image']['id'] image_file = server['image']['location'][7:] # remove 'file://' disk_size = server['flavor']['disk'] disk_local_size = server['flavor'].get('ephemeral', 10) # Create the base boot disk at: # instances/_base/<image_id>_<disk_size> base_disk = os.path.join(CONF.instances_base_dir, '%s_%s' % (image_id, disk_size)) if not os.path.exists(base_disk): try: utils.execute(['qemu-img', 'convert', '-O', 'raw', image_file, base_disk]) utils.execute(['qemu-img', 'resize', base_disk, '%sG' % disk_size]) except: if os.path.exists(base_disk): os.remove(base_disk) raise # Create the base ephemeral disk at: # instances/_base/ephemeral_<disk_local_size> base_disk_local = os.path.join(CONF.instances_base_dir, 'ephemeral_%s' % disk_local_size) if not os.path.exists(base_disk_local): try: utils.execute(['qemu-img', 'create', '-f', 'raw', base_disk_local, '%sG' % disk_local_size]) utils.execute(['mkfs.ext3', '-F', '-L', 'ephemeral0', base_disk_local]) except: if os.path.exists(base_disk_local): os.remove(base_disk_local) raise # Create the server disk at: # instances/<server_id>/disk server_disk = os.path.join(CONF.instances_dir, server_id, 'disk') utils.execute(['qemu-img', 'create', '-f', 'qcow2', '-o', 'cluster_size=2M,backing_file=%s' % base_disk, server_disk]) # Create the server ephemeral disk at: # instances/<server_id>/disk_local server_disk_local = os.path.join(CONF.instances_dir, server_id, 'disk.local') utils.execute(['qemu-img', 'create', '-f', 'qcow2', '-o', 'cluster_size=2M,backing_file=%s' % base_disk_local, server_disk_local])