<s> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> import itertools <EOL> import json <EOL> import netaddr <EOL> import os <EOL> import re <EOL> import requests <EOL> from restful import Client <EOL> import socket <EOL> import sys <EOL> import time <EOL> import yaml <EOL> ROLE_UNASSIGNED = True <EOL> ROLE_ASSIGNED = False <EOL> import log as logging <EOL> LOG = logging . getLogger ( __name__ ) <EOL> from oslo_config import cfg <EOL> CONF = cfg . CONF <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> opts = [ <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . IntOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT:10> <EOL> ) , <EOL> cfg . IntOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT:10> <EOL> ) , <EOL> cfg . BoolOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = True <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = r'<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . IntOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> <EOL> ) , <EOL> cfg . IntOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> <EOL> ) , <EOL> cfg . IntOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = r'<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT:true>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> ) , <EOL> ] <EOL> CONF . register_cli_opts ( opts ) <EOL> def is_role_unassigned ( role ) : <EOL> return role <EOL> def _load_config ( config_filename ) : <EOL> if not config_filename : <EOL> return { } <EOL> with open ( config_filename ) as config_file : <EOL> content = config_file . read ( ) <EOL> return json . loads ( content ) <EOL> class CompassClient ( object ) : <EOL> def __init__ ( self ) : <EOL> LOG . info ( "<STR_LIT>" % CONF . compass_server ) <EOL> self . client = Client ( CONF . compass_server ) <EOL> self . subnet_mapping = { } <EOL> self . role_mapping = { } <EOL> self . host_mapping = { } <EOL> self . host_ips = defaultdict ( list ) <EOL> self . host_roles = { } <EOL> self . login ( ) <EOL> def is_ok ( self , status ) : <EOL> if status < <NUM_LIT> and status >= <NUM_LIT:200> : <EOL> return True <EOL> def login ( self ) : <EOL> status , resp = self . client . get_token ( <EOL> CONF . compass_user_email , <EOL> CONF . compass_user_password <EOL> ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> status , resp <EOL> ) <EOL> if self . is_ok ( status ) : <EOL> return resp [ "<STR_LIT>" ] <EOL> else : <EOL> raise Exception ( <EOL> '<STR_LIT>' , <EOL> CONF . compass_server , <EOL> CONF . compass_user_email <EOL> ) <EOL> def get_machines ( self ) : <EOL> status , resp = self . client . list_machines ( ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , status , resp ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> machines_to_add = list ( set ( [ <EOL> machine for machine in CONF . machines . split ( '<STR_LIT:U+002C>' ) <EOL> if machine <EOL> ] ) ) <EOL> LOG . info ( '<STR_LIT>' , machines_to_add ) <EOL> machines_db = [ str ( m [ "<STR_LIT>" ] ) for m in resp ] <EOL> LOG . info ( '<STR_LIT>' , machines_db ) <EOL> assert ( set ( machines_db ) == set ( machines_to_add ) ) <EOL> return [ m [ "<STR_LIT:id>" ] for m in resp ] <EOL> def get_adapter ( self ) : <EOL> """<STR_LIT>""" <EOL> status , resp = self . client . list_adapters ( name = CONF . adapter_name ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> status , resp <EOL> ) <EOL> if not self . is_ok ( status ) or not resp : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> os_re = re . compile ( CONF . adapter_os_pattern ) <EOL> flavor_re = re . compile ( CONF . adapter_flavor_pattern ) <EOL> adapter_id = None <EOL> os_id = None <EOL> flavor_id = None <EOL> adapter = None <EOL> adapter = resp [ <NUM_LIT:0> ] <EOL> adapter_id = adapter [ '<STR_LIT:id>' ] <EOL> for supported_os in adapter [ '<STR_LIT>' ] : <EOL> if not os_re or os_re . match ( supported_os [ '<STR_LIT:name>' ] ) : <EOL> os_id = supported_os [ '<STR_LIT>' ] <EOL> break <EOL> if '<STR_LIT>' in adapter : <EOL> for flavor in adapter [ '<STR_LIT>' ] : <EOL> if not flavor_re or flavor_re . match ( flavor [ '<STR_LIT:name>' ] ) : <EOL> flavor_id = flavor [ '<STR_LIT:id>' ] <EOL> break <EOL> assert ( os_id and flavor_id ) <EOL> return ( adapter_id , os_id , flavor_id ) <EOL> def add_subnets ( self ) : <EOL> subnets = [ <EOL> subnet for subnet in CONF . subnets . split ( '<STR_LIT:U+002C>' ) <EOL> if subnet <EOL> ] <EOL> assert ( subnets ) <EOL> subnet_mapping = { } <EOL> for subnet in subnets : <EOL> try : <EOL> netaddr . IPNetwork ( subnet ) <EOL> except Exception : <EOL> raise RuntimeError ( '<STR_LIT>' % subnet ) <EOL> status , resp = self . client . add_subnet ( subnet ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> subnet , <EOL> status , <EOL> resp <EOL> ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( '<STR_LIT>' % subnet ) <EOL> subnet_mapping [ resp [ '<STR_LIT>' ] ] = resp [ '<STR_LIT:id>' ] <EOL> self . subnet_mapping = subnet_mapping <EOL> def add_cluster ( self , adapter_id , os_id , flavor_id ) : <EOL> """<STR_LIT>""" <EOL> cluster_name = CONF . cluster_name <EOL> assert ( cluster_name ) <EOL> status , resp = self . client . add_cluster ( <EOL> cluster_name , adapter_id , <EOL> os_id , flavor_id ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> cluster_name , <EOL> status , <EOL> resp <EOL> ) <EOL> if isinstance ( resp , list ) : <EOL> cluster = resp [ <NUM_LIT:0> ] <EOL> else : <EOL> cluster = resp <EOL> cluster_id = cluster [ '<STR_LIT:id>' ] <EOL> flavor = cluster . get ( '<STR_LIT>' , { } ) <EOL> roles = flavor . get ( '<STR_LIT>' , [ ] ) <EOL> for role in roles : <EOL> if role . get ( '<STR_LIT>' , False ) : <EOL> self . role_mapping [ role [ '<STR_LIT:name>' ] ] = ROLE_ASSIGNED <EOL> else : <EOL> self . role_mapping [ role [ '<STR_LIT:name>' ] ] = ROLE_UNASSIGNED <EOL> return cluster_id <EOL> def add_cluster_hosts ( self , cluster_id , machines ) : <EOL> hostnames = [ <EOL> hostname for hostname in CONF . hostnames . split ( '<STR_LIT:U+002C>' ) <EOL> if hostname <EOL> ] <EOL> assert ( len ( machines ) == len ( hostnames ) ) <EOL> machines_dict = [ ] <EOL> for machine_id , hostname in zip ( machines , hostnames ) : <EOL> machines_dict . append ( { <EOL> '<STR_LIT>' : machine_id , <EOL> '<STR_LIT:name>' : hostname <EOL> } ) <EOL> status , resp = self . client . add_hosts_to_cluster ( <EOL> cluster_id , <EOL> { '<STR_LIT>' : machines_dict } ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> machines_dict , <EOL> cluster_id , <EOL> status , <EOL> resp <EOL> ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> for host in resp [ '<STR_LIT>' ] : <EOL> self . host_mapping [ host [ '<STR_LIT>' ] ] = host [ '<STR_LIT:id>' ] <EOL> assert ( len ( self . host_mapping ) == len ( machines ) ) <EOL> def set_cluster_os_config ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> os_config = { } <EOL> language = CONF . language <EOL> timezone = CONF . timezone <EOL> http_proxy = CONF . http_proxy <EOL> https_proxy = CONF . https_proxy <EOL> local_repo_url = CONF . local_repo_url <EOL> repo_name = CONF . repo_name <EOL> deploy_type = CONF . deploy_type <EOL> if not https_proxy and http_proxy : <EOL> https_proxy = http_proxy <EOL> no_proxy = [ <EOL> no_proxy for no_proxy in CONF . no_proxy . split ( '<STR_LIT:U+002C>' ) <EOL> if no_proxy <EOL> ] <EOL> compass_server = CONF . compass_server <EOL> if http_proxy : <EOL> for hostname , ips in self . host_ips . items ( ) : <EOL> no_proxy . append ( hostname ) <EOL> no_proxy . extend ( ips ) <EOL> ntp_server = CONF . ntp_server or compass_server <EOL> dns_servers = [ <EOL> dns_server for dns_server in CONF . dns_servers . split ( '<STR_LIT:U+002C>' ) <EOL> if dns_server <EOL> ] <EOL> if not dns_servers : <EOL> dns_servers = [ compass_server ] <EOL> domain = CONF . domain <EOL> if not domain : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> search_path = [ <EOL> search_path for search_path in CONF . search_path . split ( '<STR_LIT:U+002C>' ) <EOL> if search_path <EOL> ] <EOL> if not search_path : <EOL> search_path = [ domain ] <EOL> default_gateway = CONF . default_gateway <EOL> if not default_gateway : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> general_config = { <EOL> '<STR_LIT>' : language , <EOL> '<STR_LIT>' : timezone , <EOL> '<STR_LIT>' : ntp_server , <EOL> '<STR_LIT>' : dns_servers , <EOL> '<STR_LIT>' : default_gateway <EOL> } <EOL> if http_proxy : <EOL> general_config [ '<STR_LIT>' ] = http_proxy <EOL> if https_proxy : <EOL> general_config [ '<STR_LIT>' ] = https_proxy <EOL> if no_proxy : <EOL> general_config [ '<STR_LIT>' ] = no_proxy <EOL> if domain : <EOL> general_config [ '<STR_LIT>' ] = domain <EOL> if search_path : <EOL> general_config [ '<STR_LIT>' ] = search_path <EOL> if local_repo_url : <EOL> general_config [ '<STR_LIT>' ] = local_repo_url <EOL> if repo_name : <EOL> general_config [ '<STR_LIT>' ] = repo_name <EOL> if deploy_type : <EOL> general_config [ '<STR_LIT>' ] = deploy_type <EOL> os_config [ "<STR_LIT>" ] = general_config <EOL> server_credential = CONF . server_credential <EOL> if '<STR_LIT:=>' in server_credential : <EOL> server_username , server_password = server_credential . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> elif server_credential : <EOL> server_username = server_password = server_credential <EOL> else : <EOL> server_username = '<STR_LIT:root>' <EOL> server_password = '<STR_LIT:root>' <EOL> os_config [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:username>' : server_username , <EOL> '<STR_LIT:password>' : server_password <EOL> } <EOL> partitions = [ <EOL> partition for partition in CONF . partitions . split ( '<STR_LIT:U+002C>' ) <EOL> if partition <EOL> ] <EOL> partition_config = { } <EOL> for partition in partitions : <EOL> assert ( "<STR_LIT:=>" in partition ) <EOL> partition_name , partition_value = partition . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> partition_name = partition_name . strip ( ) <EOL> partition_value = partition_value . strip ( ) <EOL> assert ( partition_name and partition_value ) <EOL> if partition_value . endswith ( '<STR_LIT:%>' ) : <EOL> partition_type = '<STR_LIT>' <EOL> partition_value = int ( partition_value [ : - <NUM_LIT:1> ] ) <EOL> else : <EOL> partition_type = '<STR_LIT:size>' <EOL> partition_config [ partition_name ] = { <EOL> partition_type : partition_value <EOL> } <EOL> os_config [ '<STR_LIT>' ] = partition_config <EOL> """<STR_LIT>""" <EOL> status , resp = self . client . update_cluster_config ( <EOL> cluster_id , os_config = os_config ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> os_config , cluster_id , status , resp ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> % ( os_config , cluster_id ) ) <EOL> def set_host_networking ( self ) : <EOL> """<STR_LIT>""" <EOL> def get_subnet ( ip_str ) : <EOL> try : <EOL> LOG . info ( "<STR_LIT>" % self . subnet_mapping . keys ( ) ) <EOL> ip = netaddr . IPAddress ( ip_str ) <EOL> for cidr , subnet_id in self . subnet_mapping . items ( ) : <EOL> subnet = netaddr . IPNetwork ( cidr ) <EOL> if ip in subnet : <EOL> return True , subnet_id <EOL> LOG . info ( "<STR_LIT>" % ( ip_str , cidr ) ) <EOL> return False , None <EOL> except Exception : <EOL> LOG . exception ( "<STR_LIT>" % ip_str ) <EOL> return False , None <EOL> for host_network in CONF . host_networks . split ( '<STR_LIT:;>' ) : <EOL> hostname , networks_str = host_network . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> hostname = hostname . strip ( ) <EOL> networks_str = networks_str . strip ( ) <EOL> assert ( hostname in self . host_mapping ) <EOL> host_id = self . host_mapping [ hostname ] <EOL> intf_list = networks_str . split ( '<STR_LIT:U+002C>' ) <EOL> for intf_str in intf_list : <EOL> interface , intf_properties = intf_str . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> intf_properties = intf_properties . strip ( ) . split ( '<STR_LIT:|>' ) <EOL> assert ( intf_properties ) <EOL> ip_str = intf_properties [ <NUM_LIT:0> ] <EOL> status , subnet_id = get_subnet ( ip_str ) <EOL> if not status : <EOL> raise RuntimeError ( "<STR_LIT>" % ip_str ) <EOL> properties = dict ( [ <EOL> ( intf_property , True ) <EOL> for intf_property in intf_properties [ <NUM_LIT:1> : ] <EOL> ] ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> hostname , interface , ip_str , properties ) <EOL> status , response = self . client . add_host_network ( <EOL> host_id , interface , ip = ip_str , subnet_id = subnet_id , <EOL> ** properties <EOL> ) <EOL> LOG . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> hostname , interface , ip_str , properties , <EOL> status , response <EOL> ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . host_ips [ hostname ] . append ( ip_str ) <EOL> def set_cluster_package_config ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> package_config = { "<STR_LIT>" : { } } <EOL> service_credentials = [ <EOL> service_credential <EOL> for service_credential in CONF . service_credentials . split ( '<STR_LIT:U+002C>' ) <EOL> if service_credential <EOL> ] <EOL> service_credential_cfg = { } <EOL> LOG . info ( <EOL> '<STR_LIT>' , service_credentials <EOL> ) <EOL> for service_credential in service_credentials : <EOL> if '<STR_LIT::>' not in service_credential : <EOL> raise Exception ( <EOL> '<STR_LIT>' % service_credential <EOL> ) <EOL> service_name , service_pair = service_credential . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT:=>' not in service_pair : <EOL> raise Exception ( <EOL> '<STR_LIT>' % service_name <EOL> ) <EOL> username , password = service_pair . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> service_credential_cfg [ service_name ] = { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password <EOL> } <EOL> console_credentials = [ <EOL> console_credential <EOL> for console_credential in CONF . console_credentials . split ( '<STR_LIT:U+002C>' ) <EOL> if console_credential <EOL> ] <EOL> LOG . info ( <EOL> '<STR_LIT>' , console_credentials <EOL> ) <EOL> console_credential_cfg = { } <EOL> for console_credential in console_credentials : <EOL> if '<STR_LIT::>' not in console_credential : <EOL> raise Exception ( <EOL> '<STR_LIT>' <EOL> % console_credential <EOL> ) <EOL> console_name , console_pair = console_credential . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT:=>' not in console_pair : <EOL> raise Exception ( <EOL> '<STR_LIT>' % console_name <EOL> ) <EOL> username , password = console_pair . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> console_credential_cfg [ console_name ] = { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password <EOL> } <EOL> package_config [ "<STR_LIT>" ] = { <EOL> "<STR_LIT>" : service_credential_cfg , <EOL> "<STR_LIT>" : console_credential_cfg <EOL> } <EOL> network_mapping = dict ( [ <EOL> network_pair . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> for network_pair in CONF . network_mapping . split ( '<STR_LIT:U+002C>' ) <EOL> if '<STR_LIT:=>' in network_pair <EOL> ] ) <EOL> package_config [ '<STR_LIT>' ] = network_mapping <EOL> assert ( os . path . exists ( CONF . network_cfg ) ) <EOL> network_cfg = yaml . load ( open ( CONF . network_cfg ) ) <EOL> package_config [ "<STR_LIT>" ] = network_cfg <EOL> assert ( os . path . exists ( CONF . neutron_cfg ) ) <EOL> neutron_cfg = yaml . load ( open ( CONF . neutron_cfg ) ) <EOL> package_config [ "<STR_LIT>" ] = neutron_cfg <EOL> """<STR_LIT>""" <EOL> package_config [ '<STR_LIT>' ] = { } <EOL> if CONF . cluster_vip : <EOL> package_config [ "<STR_LIT>" ] [ "<STR_LIT>" ] = CONF . cluster_vip <EOL> package_config [ '<STR_LIT>' ] = ( CONF . enable_secgroup == "<STR_LIT:true>" ) <EOL> status , resp = self . client . update_cluster_config ( <EOL> cluster_id , package_config = package_config ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> package_config , cluster_id , status , resp ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def set_host_roles ( self , cluster_id , host_id , roles ) : <EOL> status , response = self . client . update_cluster_host ( <EOL> cluster_id , host_id , roles = roles ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> cluster_id , host_id , roles , status , response <EOL> ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> for role in roles : <EOL> if role in self . role_mapping : <EOL> self . role_mapping [ role ] = ROLE_ASSIGNED <EOL> def set_all_hosts_roles ( self , cluster_id ) : <EOL> for host_str in CONF . host_roles . split ( '<STR_LIT:;>' ) : <EOL> host_str = host_str . strip ( ) <EOL> hostname , roles_str = host_str . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> assert ( hostname in self . host_mapping ) <EOL> host_id = self . host_mapping [ hostname ] <EOL> roles = [ role . strip ( ) for role in roles_str . split ( '<STR_LIT:U+002C>' ) if role ] <EOL> self . set_host_roles ( cluster_id , host_id , roles ) <EOL> self . host_roles [ hostname ] = roles <EOL> unassigned_hostnames = list ( set ( self . host_mapping . keys ( ) ) <EOL> - set ( self . host_roles . keys ( ) ) ) <EOL> unassigned_roles = [ role for role , status in self . role_mapping . items ( ) <EOL> if is_role_unassigned ( status ) ] <EOL> assert ( len ( unassigned_hostnames ) >= len ( unassigned_roles ) ) <EOL> for hostname , role in map ( <EOL> None , <EOL> unassigned_hostnames , <EOL> unassigned_roles <EOL> ) : <EOL> host_id = self . host_mapping [ hostname ] <EOL> self . set_host_roles ( cluster_id , host_id , [ role ] ) <EOL> self . host_roles [ hostname ] = [ role ] <EOL> unassigned_hostnames = list ( set ( self . host_mapping . keys ( ) ) <EOL> - set ( self . host_roles . keys ( ) ) ) <EOL> if not unassigned_hostnames : <EOL> return <EOL> default_roles = [ <EOL> role for role in CONF . default_roles . split ( '<STR_LIT:U+002C>' ) <EOL> if role <EOL> ] <EOL> assert ( default_roles ) <EOL> cycle_roles = itertools . cycle ( default_roles ) <EOL> for hostname in unassigned_hostnames : <EOL> host_id = self . host_mapping [ hostname ] <EOL> roles = [ cycle_roles . next ( ) ] <EOL> self . set_host_roles ( cluster_id , host_id , roles ) <EOL> self . host_roles [ hostname ] = roles <EOL> def deploy_clusters ( self , cluster_id ) : <EOL> host_ids = self . host_mapping . values ( ) <EOL> status , response = self . client . review_cluster ( <EOL> cluster_id , review = { '<STR_LIT>' : host_ids } <EOL> ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> cluster_id , host_ids , status , response <EOL> ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> status , response = self . client . deploy_cluster ( <EOL> cluster_id , deploy = { '<STR_LIT>' : host_ids } <EOL> ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> cluster_id , host_ids , status , response <EOL> ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def get_installing_progress ( self , cluster_id ) : <EOL> """<STR_LIT>""" <EOL> action_timeout = time . time ( ) + <NUM_LIT> * float ( CONF . action_timeout ) <EOL> deployment_timeout = time . time ( ) + <NUM_LIT> * float ( <EOL> CONF . deployment_timeout ) <EOL> current_time = time . time ( ) <EOL> deployment_failed = True <EOL> while current_time < deployment_timeout : <EOL> status , cluster_state = self . client . get_cluster_state ( cluster_id ) <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> cluster_id , status , cluster_state <EOL> ) <EOL> if not self . is_ok ( status ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if cluster_state [ '<STR_LIT:state>' ] in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if current_time >= action_timeout : <EOL> deployment_failed = True <EOL> break <EOL> else : <EOL> continue <EOL> elif cluster_state [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> deployment_failed = False <EOL> break <EOL> elif cluster_state [ '<STR_LIT:state>' ] == '<STR_LIT>' : <EOL> deployment_failed = True <EOL> break <EOL> if deployment_failed : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def check_dashboard_links ( self , cluster_id ) : <EOL> dashboard_url = CONF . dashboard_url <EOL> if not dashboard_url : <EOL> LOG . info ( '<STR_LIT>' ) <EOL> return <EOL> dashboard_link_pattern = re . compile ( <EOL> CONF . dashboard_link_pattern ) <EOL> r = requests . get ( dashboard_url , verify = False ) <EOL> r . raise_for_status ( ) <EOL> match = dashboard_link_pattern . search ( r . text ) <EOL> if match : <EOL> LOG . info ( <EOL> '<STR_LIT>' , <EOL> cluster_id ) <EOL> else : <EOL> msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % ( dashboard_url , r . text ) <EOL> raise Exception ( msg ) <EOL> def main ( ) : <EOL> client = CompassClient ( ) <EOL> machines = client . get_machines ( ) <EOL> LOG . info ( '<STR_LIT>' , machines ) <EOL> client . add_subnets ( ) <EOL> adapter_id , os_id , flavor_id = client . get_adapter ( ) <EOL> cluster_id = client . add_cluster ( adapter_id , os_id , flavor_id ) <EOL> client . add_cluster_hosts ( cluster_id , machines ) <EOL> client . set_host_networking ( ) <EOL> client . set_cluster_os_config ( cluster_id ) <EOL> if flavor_id : <EOL> client . set_cluster_package_config ( cluster_id ) <EOL> client . set_all_hosts_roles ( cluster_id ) <EOL> client . deploy_clusters ( cluster_id ) <EOL> client . get_installing_progress ( cluster_id ) <EOL> client . check_dashboard_links ( cluster_id ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> CONF ( args = sys . argv [ <NUM_LIT:1> : ] ) <EOL> main ( ) </s>
