<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import shutil <EOL> import xmlrpclib <EOL> from compass . deployment . installers . installer import OSInstaller <EOL> from compass . deployment . utils import constants as const <EOL> from compass . utils import setting_wrapper as compass_setting <EOL> from compass . utils import util <EOL> from copy import deepcopy <EOL> NAME = '<STR_LIT>' <EOL> class CobblerInstaller ( OSInstaller ) : <EOL> """<STR_LIT>""" <EOL> CREDENTIALS = "<STR_LIT>" <EOL> USERNAME = '<STR_LIT:username>' <EOL> PASSWORD = '<STR_LIT:password>' <EOL> INSTALLER_URL = "<STR_LIT>" <EOL> TMPL_DIR = '<STR_LIT>' <EOL> SYS_TMPL = '<STR_LIT>' <EOL> SYS_TMPL_NAME = '<STR_LIT>' <EOL> SYS_PROFILE_NAME = '<STR_LIT>' <EOL> PROFILE = '<STR_LIT>' <EOL> POWER_TYPE = '<STR_LIT>' <EOL> POWER_ADDR = '<STR_LIT>' <EOL> POWER_USER = '<STR_LIT>' <EOL> POWER_PASS = '<STR_LIT>' <EOL> def __init__ ( self , config_manager ) : <EOL> super ( CobblerInstaller , self ) . __init__ ( ) <EOL> self . config_manager = config_manager <EOL> installer_settings = self . config_manager . get_os_installer_settings ( ) <EOL> try : <EOL> username = installer_settings [ self . CREDENTIALS ] [ self . USERNAME ] <EOL> password = installer_settings [ self . CREDENTIALS ] [ self . PASSWORD ] <EOL> cobbler_url = installer_settings [ self . INSTALLER_URL ] <EOL> self . tmpl_dir = CobblerInstaller . get_tmpl_path ( ) <EOL> except KeyError as ex : <EOL> raise KeyError ( ex . message ) <EOL> self . remote = self . _get_cobbler_server ( cobbler_url ) <EOL> self . token = self . _get_token ( username , password ) <EOL> self . pk_installer_config = None <EOL> logging . debug ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def get_tmpl_path ( cls ) : <EOL> return os . path . join ( compass_setting . TMPL_DIR , '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , self . remote , self . token ) <EOL> def _get_cobbler_server ( self , cobbler_url ) : <EOL> if not cobbler_url : <EOL> logging . error ( "<STR_LIT>" ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return xmlrpclib . Server ( cobbler_url ) <EOL> def _get_token ( self , username , password ) : <EOL> if self . remote is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return self . remote . login ( username , password ) <EOL> def get_supported_oses ( self ) : <EOL> """<STR_LIT>""" <EOL> profiles = self . remote . get_profiles ( ) <EOL> oses = [ ] <EOL> for profile in profiles : <EOL> oses . append ( profile [ '<STR_LIT:name>' ] ) <EOL> return oses <EOL> def deploy ( self ) : <EOL> """<STR_LIT>""" <EOL> host_ids = self . config_manager . get_hosts_id_list_for_os_installation ( ) <EOL> if not host_ids : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return { } <EOL> os_version = self . config_manager . get_os_version ( ) <EOL> profile = self . _get_profile_from_server ( os_version ) <EOL> global_vars_dict = self . _get_cluster_tmpl_vars_dict ( ) <EOL> self . update_profile_config_to_cobbler ( profile , global_vars_dict ) <EOL> hosts_deploy_config = { } <EOL> for host_id in host_ids : <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> vars_dict = self . _get_host_tmpl_vars_dict ( host_id , <EOL> global_vars_dict , <EOL> hostname = hostname , <EOL> profile = profile ) <EOL> self . update_host_config_to_cobbler ( host_id , hostname , vars_dict ) <EOL> host_config = { } <EOL> host_config [ const . DEPLOYED_OS_CONFIG ] = vars_dict [ const . OS_CONFIG ] <EOL> hosts_deploy_config [ host_id ] = host_config <EOL> self . _sync ( ) <EOL> cluster_config = global_vars_dict . setdefault ( const . OS_CONFIG , { } ) <EOL> return { <EOL> const . CLUSTER : { <EOL> const . ID : self . config_manager . get_cluster_id ( ) , <EOL> const . DEPLOYED_OS_CONFIG : cluster_config <EOL> } , <EOL> const . HOSTS : hosts_deploy_config <EOL> } <EOL> def clean_progress ( self ) : <EOL> """<STR_LIT>""" <EOL> clusterhost_list = self . config_manager . get_host_id_list ( ) <EOL> log_dir_prefix = compass_setting . INSTALLATION_LOGDIR [ NAME ] <EOL> for host_id in clusterhost_list : <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> self . _clean_log ( log_dir_prefix , hostname ) <EOL> def redeploy ( self ) : <EOL> """<STR_LIT>""" <EOL> host_ids = self . config_manager . get_host_id_list ( ) <EOL> if not host_ids : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return <EOL> for host_id in host_ids : <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> sys_id = self . _get_create_system ( hostname ) <EOL> if sys_id : <EOL> self . _netboot_enabled ( sys_id ) <EOL> self . _sync ( ) <EOL> def set_package_installer_config ( self , package_configs ) : <EOL> """<STR_LIT>""" <EOL> self . pk_installer_config = package_configs <EOL> def _sync ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . remote . sync ( self . token ) <EOL> os . system ( '<STR_LIT>' ) <EOL> except Exception as ex : <EOL> logging . debug ( "<STR_LIT>" % ex ) <EOL> raise ex <EOL> def dump_system_info ( self , host_id ) : <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> if self . remote is None or not hostname : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return { } <EOL> return self . remote . get_system_as_rendered ( hostname ) <EOL> def _generate_system_config ( self , host_id , host_vars_dict ) : <EOL> """<STR_LIT>""" <EOL> os_version = self . config_manager . get_os_version ( ) <EOL> tmpl_path = os . path . join ( <EOL> os . path . join ( self . tmpl_dir , os_version ) , self . SYS_TMPL_NAME <EOL> ) <EOL> if not os . path . exists ( tmpl_path ) : <EOL> err_msg = "<STR_LIT>" % tmpl_path <EOL> logging . error ( err_msg ) <EOL> raise Exception ( err_msg ) <EOL> system_config = self . get_config_from_template ( tmpl_path , <EOL> host_vars_dict ) <EOL> if self . pk_installer_config and host_id in self . pk_installer_config : <EOL> pk_config = self . pk_installer_config [ host_id ] <EOL> ksmeta = system_config . setdefault ( "<STR_LIT>" , { } ) <EOL> util . merge_dict ( ksmeta , pk_config ) <EOL> system_config [ "<STR_LIT>" ] = ksmeta <EOL> return system_config <EOL> def _generate_profile_config ( self , cluster_vars_dict ) : <EOL> os_version = self . config_manager . get_os_version ( ) <EOL> tmpl_path = os . path . join ( <EOL> os . path . join ( self . tmpl_dir , os_version ) , self . SYS_PROFILE_NAME <EOL> ) <EOL> return self . get_config_from_template ( tmpl_path , cluster_vars_dict ) <EOL> def _get_profile_from_server ( self , os_version ) : <EOL> """<STR_LIT>""" <EOL> result = self . remote . find_profile ( { '<STR_LIT:name>' : os_version } ) <EOL> if not result : <EOL> raise Exception ( "<STR_LIT>" , os_version ) <EOL> profile = result [ <NUM_LIT:0> ] <EOL> return profile <EOL> def _get_create_system ( self , hostname ) : <EOL> """<STR_LIT>""" <EOL> sys_name = hostname <EOL> sys_id = None <EOL> system_info = self . remote . find_system ( { "<STR_LIT:name>" : hostname } ) <EOL> if not system_info : <EOL> sys_id = self . remote . new_system ( self . token ) <EOL> self . remote . modify_system ( sys_id , "<STR_LIT:name>" , hostname , self . token ) <EOL> logging . debug ( '<STR_LIT>' , sys_id , sys_name ) <EOL> else : <EOL> sys_id = self . remote . get_system_handle ( sys_name , self . token ) <EOL> return sys_id <EOL> def _get_profile_id ( self , profilename ) : <EOL> """<STR_LIT>""" <EOL> return self . remote . get_profile_handle ( profilename , self . token ) <EOL> def _clean_system ( self , hostname ) : <EOL> """<STR_LIT>""" <EOL> sys_name = hostname <EOL> try : <EOL> self . remote . remove_system ( sys_name , self . token ) <EOL> logging . debug ( '<STR_LIT>' , sys_name ) <EOL> except Exception : <EOL> logging . debug ( '<STR_LIT>' , sys_name ) <EOL> def _update_system_config ( self , sys_id , system_config ) : <EOL> """<STR_LIT>""" <EOL> for key , value in system_config . iteritems ( ) : <EOL> self . remote . modify_system ( sys_id , str ( key ) , value , self . token ) <EOL> self . remote . save_system ( sys_id , self . token ) <EOL> def _update_profile_config ( self , profile_id , profile_config ) : <EOL> for key , value in profile_config . iteritems ( ) : <EOL> self . remote . modify_profile ( profile_id , str ( key ) , value , self . token ) <EOL> self . remote . save_profile ( profile_id , self . token ) <EOL> def _netboot_enabled ( self , sys_id ) : <EOL> """<STR_LIT>""" <EOL> self . remote . modify_system ( sys_id , '<STR_LIT>' , True , self . token ) <EOL> self . remote . save_system ( sys_id , self . token ) <EOL> def _clean_log ( self , log_dir_prefix , system_name ) : <EOL> """<STR_LIT>""" <EOL> log_dir = os . path . join ( log_dir_prefix , system_name ) <EOL> shutil . rmtree ( log_dir , True ) <EOL> def update_host_config_to_cobbler ( self , host_id , hostname , host_vars_dict ) : <EOL> """<STR_LIT>""" <EOL> sys_id = self . _get_create_system ( hostname ) <EOL> system_config = self . _generate_system_config ( host_id , host_vars_dict ) <EOL> logging . debug ( '<STR_LIT>' , host_id , system_config ) <EOL> self . _update_system_config ( sys_id , system_config ) <EOL> self . _netboot_enabled ( sys_id ) <EOL> def update_profile_config_to_cobbler ( self , profilename , cluster_vars_dict ) : <EOL> """<STR_LIT>""" <EOL> profile_id = self . _get_profile_id ( profilename ) <EOL> profile_config = self . _generate_profile_config ( cluster_vars_dict ) <EOL> logging . debug ( <EOL> '<STR_LIT>' , profilename , profile_config <EOL> ) <EOL> self . _update_profile_config ( profile_id , profile_config ) <EOL> def delete_hosts ( self ) : <EOL> hosts_id_list = self . config_manager . get_host_id_list ( ) <EOL> logging . debug ( '<STR_LIT>' , hosts_id_list ) <EOL> for host_id in hosts_id_list : <EOL> self . delete_single_host ( host_id ) <EOL> self . _sync ( ) <EOL> def delete_single_host ( self , host_id ) : <EOL> """<STR_LIT>""" <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> try : <EOL> log_dir_prefix = compass_setting . INSTALLATION_LOGDIR [ NAME ] <EOL> self . _clean_system ( hostname ) <EOL> self . _clean_log ( log_dir_prefix , hostname ) <EOL> except Exception as ex : <EOL> logging . error ( "<STR_LIT>" , ex ) <EOL> logging . exception ( ex ) <EOL> def _get_host_tmpl_vars_dict ( self , host_id , global_vars_dict , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> vars_dict = { } <EOL> if global_vars_dict : <EOL> vars_dict = deepcopy ( global_vars_dict ) <EOL> host_baseinfo = self . config_manager . get_host_baseinfo ( host_id ) <EOL> vars_dict [ const . BASEINFO ] = host_baseinfo <EOL> if self . PROFILE in kwargs : <EOL> profile = kwargs [ self . PROFILE ] <EOL> else : <EOL> os_version = self . config_manager . get_os_version ( ) <EOL> profile = self . _get_profile_from_server ( os_version ) <EOL> vars_dict [ const . BASEINFO ] [ self . PROFILE ] = profile <EOL> metadata = self . config_manager . get_os_config_metadata ( ) <EOL> os_config = self . config_manager . get_host_os_config ( host_id ) <EOL> host_vars_dict = self . get_tmpl_vars_from_metadata ( metadata , os_config ) <EOL> util . merge_dict ( <EOL> vars_dict . setdefault ( const . OS_CONFIG , { } ) , host_vars_dict <EOL> ) <EOL> return vars_dict <EOL> def _get_cluster_tmpl_vars_dict ( self ) : <EOL> metadata = self . config_manager . get_os_config_metadata ( ) <EOL> os_config = self . config_manager . get_cluster_os_config ( ) <EOL> cluster_vas_dict = { } <EOL> cluster_vas_dict [ const . OS_CONFIG ] = self . get_tmpl_vars_from_metadata ( metadata , os_config ) <EOL> return cluster_vas_dict <EOL> def _check_and_set_system_impi ( self , host_id , sys_id ) : <EOL> if not sys_id : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return False <EOL> system = self . dump_system_info ( host_id ) <EOL> if system [ self . POWER_TYPE ] != '<STR_LIT>' or not system [ self . POWER_USER ] : <EOL> ipmi_info = self . config_manager . get_host_ipmi_info ( host_id ) <EOL> if not ipmi_info : <EOL> logging . info ( '<STR_LIT>' ) <EOL> return False <EOL> ipmi_ip , ipmi_user , ipmi_pass = ipmi_info <EOL> power_opts = { } <EOL> power_opts [ self . POWER_TYPE ] = '<STR_LIT>' <EOL> power_opts [ self . POWER_ADDR ] = ipmi_ip <EOL> power_opts [ self . POWER_USER ] = ipmi_user <EOL> power_opts [ self . POWER_PASS ] = ipmi_pass <EOL> self . _update_system_config ( sys_id , power_opts ) <EOL> return True <EOL> def poweron ( self , host_id ) : <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> sys_id = self . _get_create_system ( hostname ) <EOL> if not self . _check_and_set_system_impi ( sys_id ) : <EOL> return <EOL> self . remote . power_system ( sys_id , self . token , power = '<STR_LIT>' ) <EOL> logging . info ( "<STR_LIT>" % host_id ) <EOL> def poweroff ( self , host_id ) : <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> sys_id = self . _get_create_system ( hostname ) <EOL> if not self . _check_and_set_system_impi ( sys_id ) : <EOL> return <EOL> self . remote . power_system ( sys_id , self . token , power = '<STR_LIT>' ) <EOL> logging . info ( "<STR_LIT>" % host_id ) <EOL> def reset ( self , host_id ) : <EOL> hostname = self . config_manager . get_hostname ( host_id ) <EOL> sys_id = self . _get_create_system ( hostname ) <EOL> if not self . _check_and_set_system_impi ( sys_id ) : <EOL> return <EOL> self . remote . power_system ( sys_id , self . token , power = '<STR_LIT>' ) <EOL> logging . info ( "<STR_LIT>" % host_id ) </s>
