<s> __auther__ = "<STR_LIT>" <EOL> """<STR_LIT>""" <EOL> from Cheetah . Template import Template <EOL> from copy import deepcopy <EOL> import json <EOL> import logging <EOL> import os <EOL> import re <EOL> import shutil <EOL> import subprocess <EOL> from compass . deployment . installers . installer import PKInstaller <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> NAME = "<STR_LIT>" <EOL> def byteify ( input ) : <EOL> if isinstance ( input , dict ) : <EOL> return dict ( [ ( byteify ( key ) , byteify ( value ) ) <EOL> for key , value in input . iteritems ( ) ] ) <EOL> elif isinstance ( input , list ) : <EOL> return [ byteify ( element ) for element in input ] <EOL> elif isinstance ( input , unicode ) : <EOL> return input . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return input <EOL> class AnsibleInstaller ( PKInstaller ) : <EOL> INVENTORY_TMPL_DIR = '<STR_LIT>' <EOL> GROUPVARS_TMPL_DIR = '<STR_LIT>' <EOL> INVENTORY_PATCH_TEMPALTE_DIR = '<STR_LIT>' <EOL> ANSIBLE_DIR = '<STR_LIT>' <EOL> ANSIBLE_RUN_DIR = '<STR_LIT>' <EOL> LOG_FILE = '<STR_LIT>' <EOL> ANSIBLE_CONFIG = '<STR_LIT>' <EOL> INVENTORY = '<STR_LIT>' <EOL> GROUP_VARIABLE = '<STR_LIT>' <EOL> HOSTS_PATH = '<STR_LIT>' <EOL> RUNNER_DIRS = '<STR_LIT>' <EOL> def __init__ ( self , config_manager ) : <EOL> super ( AnsibleInstaller , self ) . __init__ ( ) <EOL> self . config_manager = config_manager <EOL> self . tmpl_name = self . config_manager . get_cluster_flavor_template ( ) <EOL> self . installer_settings = ( <EOL> self . config_manager . get_pk_installer_settings ( ) <EOL> ) <EOL> settings = self . installer_settings <EOL> self . ansible_dir = settings . setdefault ( self . ANSIBLE_DIR , None ) <EOL> self . ansible_run_dir = ( <EOL> settings . setdefault ( self . ANSIBLE_RUN_DIR , None ) <EOL> ) <EOL> self . log_file = settings . setdefault ( self . LOG_FILE , None ) <EOL> self . ansible_config = ( <EOL> settings . setdefault ( self . ANSIBLE_CONFIG , None ) <EOL> ) <EOL> self . inventory = settings . setdefault ( self . INVENTORY , None ) <EOL> self . group_variable = ( <EOL> settings . setdefault ( self . GROUP_VARIABLE , None ) <EOL> ) <EOL> self . hosts_path = ( <EOL> settings . setdefault ( self . HOSTS_PATH , None ) <EOL> ) <EOL> self . runner_dirs = ( <EOL> settings . setdefault ( self . RUNNER_DIRS , None ) <EOL> ) <EOL> self . playbook = self . tmpl_name . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . runner_files = [ self . playbook ] <EOL> adapter_name = self . config_manager . get_dist_system_name ( ) <EOL> self . tmpl_dir = AnsibleInstaller . get_tmpl_path ( adapter_name ) <EOL> self . adapter_dir = os . path . join ( self . ansible_dir , adapter_name ) <EOL> logging . debug ( '<STR_LIT>' , self ) <EOL> @ classmethod <EOL> def get_tmpl_path ( cls , adapter_name ) : <EOL> tmpl_path = os . path . join ( <EOL> os . path . join ( compass_setting . TMPL_DIR , '<STR_LIT>' ) , <EOL> adapter_name <EOL> ) <EOL> return tmpl_path <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , self . NAME , self . installer_url ) <EOL> def generate_installer_config ( self ) : <EOL> """<STR_LIT>""" <EOL> host_ids = self . config_manager . get_host_id_list ( ) <EOL> os_installer_configs = { } <EOL> for host_id in host_ids : <EOL> temp = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> os_installer_configs [ host_id ] = temp <EOL> return os_installer_configs <EOL> def get_env_name ( self , dist_sys_name , cluster_name ) : <EOL> return "<STR_LIT:->" . join ( ( dist_sys_name , cluster_name ) ) <EOL> def _get_cluster_tmpl_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> cluster_vars_dict = { } <EOL> cluster_baseinfo = self . config_manager . get_cluster_baseinfo ( ) <EOL> cluster_vars_dict [ const . BASEINFO ] = cluster_baseinfo <EOL> pk_metadata = self . config_manager . get_pk_config_meatadata ( ) <EOL> pk_config = self . config_manager . get_cluster_package_config ( ) <EOL> os_metadata = self . config_manager . get_os_config_metadata ( ) <EOL> os_config = self . config_manager . get_cluster_os_config ( ) <EOL> pk_meta_dict = self . get_tmpl_vars_from_metadata ( pk_metadata , pk_config ) <EOL> os_meta_dict = self . get_tmpl_vars_from_metadata ( os_metadata , os_config ) <EOL> util . merge_dict ( pk_meta_dict , os_meta_dict ) <EOL> cluster_vars_dict [ const . PK_CONFIG ] = pk_meta_dict <EOL> mapping = self . config_manager . get_cluster_roles_mapping ( ) <EOL> logging . info ( "<STR_LIT>" , mapping ) <EOL> cluster_vars_dict [ const . ROLES_MAPPING ] = mapping <EOL> hosts_ip_settings = self . config_manager . get_hosts_ip_settings ( <EOL> pk_meta_dict [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> pk_meta_dict [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> ) <EOL> logging . info ( "<STR_LIT>" , hosts_ip_settings ) <EOL> cluster_vars_dict [ "<STR_LIT>" ] = hosts_ip_settings <EOL> return byteify ( cluster_vars_dict ) <EOL> def _generate_inventory_attributes ( self , global_vars_dict ) : <EOL> inventory_tmpl_path = os . path . join ( <EOL> os . path . join ( self . tmpl_dir , self . INVENTORY_TMPL_DIR ) , <EOL> self . tmpl_name <EOL> ) <EOL> if not os . path . exists ( inventory_tmpl_path ) : <EOL> logging . error ( <EOL> "<STR_LIT>" , self . tmpl_name <EOL> ) <EOL> raise Exception ( "<STR_LIT>" % self . tmpl_name ) <EOL> return self . get_config_from_template ( <EOL> inventory_tmpl_path , global_vars_dict <EOL> ) <EOL> def _generate_group_vars_attributes ( self , global_vars_dict ) : <EOL> logging . info ( "<STR_LIT>" , global_vars_dict ) <EOL> group_vars_tmpl_path = os . path . join ( <EOL> os . path . join ( self . tmpl_dir , self . GROUPVARS_TMPL_DIR ) , <EOL> self . tmpl_name <EOL> ) <EOL> if not os . path . exists ( group_vars_tmpl_path ) : <EOL> logging . error ( "<STR_LIT>" , <EOL> self . tmpl_name ) <EOL> raise Exception ( "<STR_LIT>" % self . tmpl_name ) <EOL> return self . get_config_from_template ( <EOL> group_vars_tmpl_path , global_vars_dict <EOL> ) <EOL> def _generate_hosts_attributes ( self , global_vars_dict ) : <EOL> hosts_tmpl_path = os . path . join ( <EOL> os . path . join ( self . tmpl_dir , '<STR_LIT>' ) , self . tmpl_name <EOL> ) <EOL> if not os . path . exists ( hosts_tmpl_path ) : <EOL> logging . error ( "<STR_LIT>" , self . tmpl_name ) <EOL> raise Exception ( "<STR_LIT>" % self . tmpl_name ) <EOL> return self . get_config_from_template ( hosts_tmpl_path , global_vars_dict ) <EOL> def _generate_ansible_cfg_attributes ( self , global_vars_dict ) : <EOL> ansible_cfg_tmpl_path = os . path . join ( <EOL> os . path . join ( self . tmpl_dir , '<STR_LIT>' ) , self . tmpl_name <EOL> ) <EOL> if not os . path . exists ( ansible_cfg_tmpl_path ) : <EOL> logging . error ( "<STR_LIT>" , self . tmpl_name ) <EOL> raise Exception ( "<STR_LIT>" % self . tmpl_name ) <EOL> return self . get_config_from_template ( <EOL> ansible_cfg_tmpl_path , <EOL> global_vars_dict <EOL> ) <EOL> def get_config_from_template ( self , tmpl_path , vars_dict ) : <EOL> logging . debug ( "<STR_LIT>" , vars_dict ) <EOL> if not os . path . exists ( tmpl_path ) or not vars_dict : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return { } <EOL> searchList = [ ] <EOL> copy_vars_dict = deepcopy ( vars_dict ) <EOL> for key , value in vars_dict . iteritems ( ) : <EOL> if isinstance ( value , dict ) : <EOL> temp = copy_vars_dict [ key ] <EOL> del copy_vars_dict [ key ] <EOL> searchList . append ( temp ) <EOL> searchList . append ( copy_vars_dict ) <EOL> tmpl = Template ( file = open ( tmpl_path , "<STR_LIT:r>" ) , searchList = searchList ) <EOL> return tmpl . respond ( ) <EOL> def _create_ansible_run_env ( self , env_name , ansible_run_destination ) : <EOL> if os . path . exists ( ansible_run_destination ) : <EOL> shutil . rmtree ( ansible_run_destination , True ) <EOL> os . mkdir ( ansible_run_destination ) <EOL> dirs = self . runner_dirs <EOL> files = self . runner_files <EOL> for dir in dirs : <EOL> if not os . path . exists ( os . path . join ( self . ansible_dir , dir ) ) : <EOL> continue <EOL> os . system ( <EOL> "<STR_LIT>" % ( <EOL> os . path . join ( self . ansible_dir , dir ) , <EOL> ansible_run_destination <EOL> ) <EOL> ) <EOL> for file in files : <EOL> logging . info ( '<STR_LIT>' , file ) <EOL> shutil . copy ( <EOL> os . path . join ( self . adapter_dir , file ) , <EOL> os . path . join ( <EOL> ansible_run_destination , <EOL> file <EOL> ) <EOL> ) <EOL> def prepare_ansible ( self , env_name , global_vars_dict ) : <EOL> ansible_run_destination = os . path . join ( self . ansible_run_dir , env_name ) <EOL> if os . path . exists ( ansible_run_destination ) : <EOL> ansible_run_destination += "<STR_LIT>" <EOL> self . _create_ansible_run_env ( env_name , ansible_run_destination ) <EOL> inv_config = self . _generate_inventory_attributes ( global_vars_dict ) <EOL> inventory_dir = os . path . join ( ansible_run_destination , '<STR_LIT>' ) <EOL> vars_config = self . _generate_group_vars_attributes ( global_vars_dict ) <EOL> vars_dir = os . path . join ( ansible_run_destination , '<STR_LIT>' ) <EOL> hosts_config = self . _generate_hosts_attributes ( global_vars_dict ) <EOL> hosts_destination = os . path . join ( <EOL> ansible_run_destination , self . hosts_path <EOL> ) <EOL> cfg_config = self . _generate_ansible_cfg_attributes ( global_vars_dict ) <EOL> cfg_destination = os . path . join ( <EOL> ansible_run_destination , <EOL> self . ansible_config <EOL> ) <EOL> os . mkdir ( inventory_dir ) <EOL> os . mkdir ( vars_dir ) <EOL> inventory_destination = os . path . join ( inventory_dir , self . inventory ) <EOL> group_vars_destination = os . path . join ( vars_dir , self . group_variable ) <EOL> self . serialize_config ( inv_config , inventory_destination ) <EOL> self . serialize_config ( vars_config , group_vars_destination ) <EOL> self . serialize_config ( hosts_config , hosts_destination ) <EOL> self . serialize_config ( cfg_config , cfg_destination ) <EOL> def deploy ( self ) : <EOL> """<STR_LIT>""" <EOL> host_list = self . config_manager . get_host_id_list ( ) <EOL> if not host_list : <EOL> return { } <EOL> adapter_name = self . config_manager . get_adapter_name ( ) <EOL> cluster_name = self . config_manager . get_clustername ( ) <EOL> env_name = self . get_env_name ( adapter_name , cluster_name ) <EOL> global_vars_dict = self . _get_cluster_tmpl_vars ( ) <EOL> logging . info ( <EOL> '<STR_LIT>' , self . __class__ . __name__ , global_vars_dict <EOL> ) <EOL> self . prepare_ansible ( env_name , global_vars_dict ) <EOL> def patch ( self , patched_role_mapping ) : <EOL> adapter_name = self . config_manager . get_adapter_name ( ) <EOL> cluster_name = self . config_manager . get_clustername ( ) <EOL> env_name = self . get_env_name ( adapter_name , cluster_name ) <EOL> ansible_run_destination = os . path . join ( self . ansible_run_dir , env_name ) <EOL> inventory_dir = os . path . join ( ansible_run_destination , '<STR_LIT>' ) <EOL> patched_global_vars_dict = self . _get_cluster_tmpl_vars ( ) <EOL> mapping = self . config_manager . get_cluster_patched_roles_mapping ( ) <EOL> patched_global_vars_dict [ '<STR_LIT>' ] = mapping <EOL> patched_inv = self . _generate_inventory_attributes ( <EOL> patched_global_vars_dict ) <EOL> inv_file = os . path . join ( inventory_dir , '<STR_LIT>' ) <EOL> self . serialize_config ( patched_inv , inv_file ) <EOL> config_file = os . path . join ( <EOL> ansible_run_destination , self . ansible_config <EOL> ) <EOL> playbook_file = os . path . join ( ansible_run_destination , self . playbook ) <EOL> log_file = os . path . join ( ansible_run_destination , '<STR_LIT>' ) <EOL> cmd = "<STR_LIT>" % ( config_file , <EOL> inv_file , <EOL> playbook_file ) <EOL> with open ( log_file , '<STR_LIT:w>' ) as logfile : <EOL> subprocess . Popen ( cmd , shell = True , stdout = logfile , stderr = logfile ) <EOL> return patched_role_mapping <EOL> def cluster_os_ready ( self ) : <EOL> adapter_name = self . config_manager . get_adapter_name ( ) <EOL> cluster_name = self . config_manager . get_clustername ( ) <EOL> env_name = self . get_env_name ( adapter_name , cluster_name ) <EOL> ansible_run_destination = os . path . join ( self . ansible_run_dir , env_name ) <EOL> expansion_dir = ansible_run_destination + "<STR_LIT>" <EOL> if os . path . exists ( expansion_dir ) : <EOL> ansible_run_destination = expansion_dir <EOL> inventory_dir = os . path . join ( ansible_run_destination , '<STR_LIT>' ) <EOL> inventory_file = os . path . join ( inventory_dir , self . inventory ) <EOL> playbook_file = os . path . join ( ansible_run_destination , self . playbook ) <EOL> log_file = os . path . join ( ansible_run_destination , '<STR_LIT>' ) <EOL> config_file = os . path . join ( <EOL> ansible_run_destination , self . ansible_config <EOL> ) <EOL> cmd = "<STR_LIT>" % ( config_file , <EOL> inventory_file , <EOL> playbook_file ) <EOL> with open ( log_file , '<STR_LIT:w>' ) as logfile : <EOL> subprocess . Popen ( cmd , shell = True , stdout = logfile , stderr = logfile ) </s>
