def execute(self, service): user_name = CONF.username osutils = osutils_factory.OSUtilsFactory().get_os_utils() password = self._get_password(service, osutils) if osutils.user_exists(user_name): LOG.info('Setting password for existing user "%s"' % user_name) osutils.set_user_password(user_name, password) else: LOG.info('Creating user "%s" and setting password' % user_name) osutils.create_user(user_name, password) # Create a user profile in order for other plugins # to access the user home, etc token = osutils.create_user_logon_session(user_name, password, True) osutils.close_user_logon_session(token) for group_name in CONF.groups: try: osutils.add_user_to_local_group(user_name, group_name) except Exception as ex: LOG.exception(ex) LOG.error('Cannot add user to group "%s"' % group_name) return (base.PLUGIN_EXECUTION_DONE, False)
def execute(self, service): meta_data = service.get_meta_data('openstack') if not 'public_keys' in meta_data: return (base.PLUGIN_EXECUTION_DONE, False) username = CONF.username osutils = osutils_factory.OSUtilsFactory().get_os_utils() user_home = osutils.get_user_home(username) if not user_home: raise Exception("User profile not found!") LOG.debug("User home: %s" % user_home) user_ssh_dir = os.path.join(user_home, '.ssh') if not os.path.exists(user_ssh_dir): os.makedirs(user_ssh_dir) authorized_keys_path = os.path.join(user_ssh_dir, "authorized_keys") with open(authorized_keys_path, 'w') as f: public_keys = meta_data['public_keys'] for k in public_keys: f.write(public_keys[k]) return (base.PLUGIN_EXECUTION_DONE, False)
def configure_host(self): osutils = osutils_factory.OSUtilsFactory().get_os_utils() osutils.wait_for_boot_completion() mdsf = metadata_factory.MetadataServiceFactory() service = mdsf.get_metadata_service() LOG.info('Metadata service loaded: \'%s\'' % service.get_name()) plugins = plugins_factory.PluginFactory().load_plugins() reboot_required = False try: for plugin in plugins: if self._check_plugin_os_requirements(osutils, plugin): if self._exec_plugin(osutils, service, plugin): reboot_required = True break finally: service.cleanup() if reboot_required: try: osutils.reboot() except Exception, ex: LOG.error('reboot failed with error \'%s\'' % ex)
def _check_metadata_ip_route(self): ''' Workaround for: https://bugs.launchpad.net/quantum/+bug/1174657 ''' osutils = osutils_factory.OSUtilsFactory().get_os_utils() os_major_version = int(osutils.get_os_version().split('.')[0]) if os_major_version >= 6: # 169.254.x.x addresses are not getting routed starting from # Windows Vista / 2008 metadata_netloc = urlparse.urlparse(CONF.metadata_base_url).netloc metadata_host = metadata_netloc.split(':')[0] if metadata_host.startswith("169.254."): if not osutils.check_static_route_exists(metadata_host): (interface_index, gateway) = osutils.get_default_gateway() if gateway: try: osutils.add_static_route(metadata_host, "255.255.255.255", gateway, interface_index, 10) except Exception, ex: # Ignore it LOG.exception(ex)
def process(self, part): osutils = osutils_factory.OSUtilsFactory().get_os_utils() file_name = part.get_filename() target_path = os.path.join(tempfile.gettempdir(), file_name) if file_name.endswith(".cmd"): args = [target_path] shell = True elif file_name.endswith(".sh"): args = ['bash.exe', target_path] shell = False elif file_name.endswith(".ps1"): args = [ 'powershell.exe', '-ExecutionPolicy', 'RemoteSigned', '-NonInteractive', target_path ] shell = False else: # Unsupported LOG.warning('Unsupported shell format') return False try: with open(target_path, 'wb') as f: f.write(part.get_payload()) (out, err, ret_val) = osutils.execute_process(args, shell) LOG.info('User_data script ended with return code: %d' % ret_val) LOG.debug('User_data stdout:\n%s' % out) LOG.debug('User_data stderr:\n%s' % err) except Exception, ex: LOG.warning( 'An error occurred during user_data execution: \'%s\'' % ex)
def execute(self, service): meta_data = service.get_meta_data('openstack') if 'hostname' not in meta_data: LOG.debug('Hostname not found in metadata') return (base.PLUGIN_EXECUTION_DONE, False) osutils = osutils_factory.OSUtilsFactory().get_os_utils() new_host_name = meta_data['hostname'].split('.', 1)[0] reboot_required = osutils.set_host_name(new_host_name) return (base.PLUGIN_EXECUTION_DONE, reboot_required)
def execute(self, service): meta_data = service.get_meta_data('openstack') if 'network_config' not in meta_data: return (base.PLUGIN_EXECUTION_DONE, False) network_config = meta_data['network_config'] if 'content_path' not in network_config: return (base.PLUGIN_EXECUTION_DONE, False) content_path = network_config['content_path'] content_name = content_path.rsplit('/', 1)[-1] debian_network_conf = service.get_content('openstack', content_name) LOG.debug('network config content:\n%s' % debian_network_conf) # TODO (alexpilotti): implement a proper grammar m = re.search( r'iface eth0 inet static\s+' r'address\s+(?P<address>[^\s]+)\s+' r'netmask\s+(?P<netmask>[^\s]+)\s+' r'broadcast\s+(?P<broadcast>[^\s]+)\s+' r'gateway\s+(?P<gateway>[^\s]+)\s+' r'dns\-nameservers\s+(?P<dnsnameservers>[^\r\n]+)\s+', debian_network_conf) if not m: raise Exception("network_config format not recognized") address = m.group('address') netmask = m.group('netmask') broadcast = m.group('broadcast') gateway = m.group('gateway') dnsnameservers = m.group('dnsnameservers').strip().split(' ') osutils = osutils_factory.OSUtilsFactory().get_os_utils() network_adapter_name = CONF.network_adapter if not network_adapter_name: # Get the first available one available_adapters = osutils.get_network_adapters() if not len(available_adapters): raise Exception("No network adapter available") network_adapter_name = available_adapters[0] LOG.info('Configuring network adapter: \'%s\'' % network_adapter_name) reboot_required = osutils.set_static_network_config( network_adapter_name, address, netmask, broadcast, gateway, dnsnameservers) return (base.PLUGIN_EXECUTION_DONE, reboot_required)
def _get_config_drive_cdrom_mount_point(self): osutils = osutils_factory.OSUtilsFactory().get_os_utils() conn = wmi.WMI(moniker='//./root/cimv2') q = conn.query('SELECT Drive FROM Win32_CDROMDrive WHERE ' 'MediaLoaded = True') for r in q: label = osutils.get_volume_label(r.Drive) if label == "config-2" and \ os.path.exists(os.path.join(r.Drive, 'openstack\\latest\\' 'meta_data.json')): return r.Drive + "\\" return None
def handle(user_data): osutils = osutils_factory.OSUtilsFactory().get_os_utils() target_path = os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) if re.search(r'^rem cmd\s', user_data, re.I): target_path += '.cmd' args = [target_path] shell = True elif re.search(r'^#!', user_data, re.I): target_path += '.sh' args = ['bash.exe', target_path] shell = False elif re.search(r'^#ps1\s', user_data, re.I): target_path += '.ps1' args = ['powershell.exe', '-ExecutionPolicy', 'RemoteSigned', '-NonInteractive', target_path] shell = False elif re.search(r'^#ps1_sysnative\s', user_data, re.I): if os.path.isdir(os.path.expandvars('%windir%\\sysnative')): target_path += '.ps1' args = [os.path.expandvars('%windir%\\sysnative\\' 'WindowsPowerShell\\v1.0\\' 'powershell.exe'), '-ExecutionPolicy', 'RemoteSigned', '-NonInteractive', target_path] shell = False else: # Unable to validate sysnative presence LOG.warning('Unable to validate sysnative folder presence. ' 'If Target OS is Server 2003, please ensure you ' 'have KB942589 installed') return (base.PLUGIN_EXECUTION_DONE, False) else: # Unsupported LOG.warning('Unsupported user_data format') return (base.PLUGIN_EXECUTION_DONE, False) try: with open(target_path, 'wb') as f: f.write(user_data) (out, err, ret_val) = osutils.execute_process(args, shell) LOG.info('User_data script ended with return code: %d' % ret_val) LOG.debug('User_data stdout:\n%s' % out) LOG.debug('User_data stderr:\n%s' % err) except Exception, ex: LOG.warning('An error occurred during user_data execution: \'%s\'' % ex)
def execute(self, service): user_name = CONF.username if not service.can_post_password: LOG.info('Cannot set the password in the metadata as it is ' 'not supported by this service') return (base.PLUGIN_EXECUTION_DONE, False) if service.is_password_set(self._post_password_md_ver): LOG.debug('User\'s password already set in the instance metadata') else: osutils = osutils_factory.OSUtilsFactory().get_os_utils() if osutils.user_exists(user_name): password = self._set_password(osutils, user_name) self._set_metadata_password(password, service) return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)