<s> from __future__ import absolute_import <EOL> from functools import partial <EOL> from functools import reduce <EOL> import json <EOL> import os <EOL> import socket <EOL> import ssl <EOL> import time <EOL> from warnings import warn <EOL> import paramiko <EOL> from six . moves import http_client <EOL> from six . moves . urllib import request <EOL> from six . moves import xmlrpc_client <EOL> from devops . error import AuthenticationError <EOL> from devops . error import DevopsError <EOL> from devops . error import TimeoutError <EOL> from devops . helpers . ssh_client import SSHClient <EOL> from devops import logger <EOL> from devops . settings import KEYSTONE_CREDS <EOL> from devops . settings import SSH_CREDENTIALS <EOL> from devops . settings import SSH_SLAVE_CREDENTIALS <EOL> def get_free_port ( ) : <EOL> for port in range ( <NUM_LIT> , <NUM_LIT> ) : <EOL> if not tcp_ping ( '<STR_LIT:localhost>' , port ) : <EOL> return port <EOL> raise DevopsError ( '<STR_LIT>' ) <EOL> def icmp_ping ( host , timeout = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return os . system ( <EOL> "<STR_LIT>" % { <EOL> '<STR_LIT:host>' : host , '<STR_LIT>' : timeout } ) == <NUM_LIT:0> <EOL> def tcp_ping_ ( host , port , timeout = None ) : <EOL> s = socket . socket ( ) <EOL> if timeout : <EOL> s . settimeout ( timeout ) <EOL> s . connect ( ( str ( host ) , int ( port ) ) ) <EOL> s . close ( ) <EOL> def _tcp_ping ( * args , ** kwargs ) : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> warn ( '<STR_LIT>' , DeprecationWarning ) <EOL> return tcp_ping_ ( * args , ** kwargs ) <EOL> def tcp_ping ( host , port , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> tcp_ping_ ( host , port , timeout ) <EOL> except socket . error : <EOL> return False <EOL> return True <EOL> def wait ( predicate , interval = <NUM_LIT:5> , timeout = <NUM_LIT> , timeout_msg = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> start_time = time . time ( ) <EOL> if not timeout : <EOL> return predicate ( ) <EOL> while not predicate ( ) : <EOL> if start_time + timeout < time . time ( ) : <EOL> raise TimeoutError ( timeout_msg ) <EOL> seconds_to_sleep = max ( <EOL> <NUM_LIT:0> , <EOL> min ( interval , start_time + timeout - time . time ( ) ) ) <EOL> time . sleep ( seconds_to_sleep ) <EOL> return timeout + start_time - time . time ( ) <EOL> def wait_pass ( raising_predicate , expected = Exception , interval = <NUM_LIT:5> , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> start_time = time . time ( ) <EOL> while True : <EOL> try : <EOL> return raising_predicate ( ) <EOL> except expected : <EOL> if timeout and start_time + timeout < time . time ( ) : <EOL> raise <EOL> time . sleep ( interval ) <EOL> def _wait ( * args , ** kwargs ) : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> warn ( '<STR_LIT>' , DeprecationWarning ) <EOL> return wait_pass ( * args , ** kwargs ) <EOL> def wait_tcp ( host , port , timeout ) : <EOL> is_port_active = partial ( tcp_ping , host = host , port = port ) <EOL> wait ( is_port_active , timeout = timeout ) <EOL> def wait_ssh_cmd ( host , port , check_cmd , username , password , timeout ) : <EOL> ssh_client = SSHClient ( host = host , port = port , <EOL> username = SSH_CREDENTIALS [ '<STR_LIT>' ] , <EOL> password = SSH_CREDENTIALS [ '<STR_LIT:password>' ] ) <EOL> wait ( lambda : not ssh_client . execute ( check_cmd ) [ '<STR_LIT>' ] , <EOL> timeout = timeout ) <EOL> def http ( host = '<STR_LIT:localhost>' , port = <NUM_LIT> , method = '<STR_LIT:GET>' , url = '<STR_LIT:/>' , waited_code = <NUM_LIT:200> ) : <EOL> try : <EOL> conn = http_client . HTTPConnection ( str ( host ) , int ( port ) ) <EOL> conn . request ( method , url ) <EOL> res = conn . getresponse ( ) <EOL> return res . status == waited_code <EOL> except Exception : <EOL> return False <EOL> def get_private_keys ( env ) : <EOL> _ssh_keys = [ ] <EOL> admin_remote = get_admin_remote ( env ) <EOL> for key_string in [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> if admin_remote . isfile ( key_string ) : <EOL> with admin_remote . open ( key_string ) as f : <EOL> _ssh_keys . append ( paramiko . RSAKey . from_private_key ( f ) ) <EOL> return _ssh_keys <EOL> def get_admin_remote ( env , login = SSH_CREDENTIALS [ '<STR_LIT>' ] , <EOL> password = SSH_CREDENTIALS [ '<STR_LIT:password>' ] ) : <EOL> admin_ip = get_admin_ip ( env ) <EOL> wait ( lambda : tcp_ping ( admin_ip , <NUM_LIT> ) , <EOL> timeout = <NUM_LIT> , <EOL> timeout_msg = ( "<STR_LIT>" <EOL> . format ( ip = admin_ip ) ) ) <EOL> return env . get_node ( <EOL> name = '<STR_LIT>' ) . remote ( network_name = SSH_CREDENTIALS [ '<STR_LIT>' ] , <EOL> login = login , <EOL> password = password ) <EOL> def get_node_remote ( env , node_name , login = SSH_SLAVE_CREDENTIALS [ '<STR_LIT>' ] , <EOL> password = SSH_SLAVE_CREDENTIALS [ '<STR_LIT:password>' ] ) : <EOL> ip = get_slave_ip ( env , env . get_node ( <EOL> name = node_name ) . interfaces [ <NUM_LIT:0> ] . mac_address ) <EOL> wait ( lambda : tcp_ping ( ip , <NUM_LIT> ) , timeout = <NUM_LIT> , <EOL> timeout_msg = "<STR_LIT>" . format ( ip = ip ) ) <EOL> return SSHClient ( ip , <EOL> username = login , <EOL> password = password , <EOL> private_keys = get_private_keys ( env ) ) <EOL> def get_admin_ip ( env ) : <EOL> return env . get_node ( name = '<STR_LIT>' ) . get_ip_address_by_network_name ( '<STR_LIT>' ) <EOL> def get_ip_from_json ( js , mac ) : <EOL> def poor_mac ( mac_addr ) : <EOL> return [ m . lower ( ) for m in mac_addr if m . lower ( ) in '<STR_LIT>' ] <EOL> for node in js : <EOL> for interface in node [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if poor_mac ( interface [ '<STR_LIT>' ] ) == poor_mac ( mac ) : <EOL> logger . debug ( "<STR_LIT>" . format ( <EOL> mac , node [ '<STR_LIT>' ] ) ) <EOL> return node [ '<STR_LIT>' ] <EOL> raise DevopsError ( <EOL> '<STR_LIT>' . format ( mac ) ) <EOL> def get_slave_ip ( env , node_mac_address ) : <EOL> admin_ip = get_admin_ip ( env ) <EOL> js = get_nodes ( admin_ip ) <EOL> return get_ip_from_json ( js , node_mac_address ) <EOL> class KeyPolicy ( paramiko . WarningPolicy ) : <EOL> def __init__ ( self ) : <EOL> warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , DeprecationWarning ) <EOL> logger . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> super ( KeyPolicy , self ) . __init__ ( ) <EOL> def missing_host_key ( self , client , hostname , key ) : <EOL> return <EOL> def ssh ( * args , ** kwargs ) : <EOL> warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , DeprecationWarning ) <EOL> return SSHClient ( * args , ** kwargs ) <EOL> def xmlrpctoken ( uri , login , password ) : <EOL> server = xmlrpc_client . Server ( uri ) <EOL> try : <EOL> return server . login ( login , password ) <EOL> except Exception : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> def xmlrpcmethod ( uri , method ) : <EOL> server = xmlrpc_client . Server ( uri ) <EOL> try : <EOL> return getattr ( server , method ) <EOL> except Exception : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> def generate_mac ( ) : <EOL> return "<STR_LIT>" . format ( <EOL> * bytearray ( os . urandom ( <NUM_LIT:5> ) ) ) <EOL> def get_file_size ( path ) : <EOL> """<STR_LIT>""" <EOL> return os . stat ( path ) . st_size <EOL> def _get_file_size ( * args , ** kwargs ) : <EOL> logger . warning ( <EOL> '<STR_LIT>' ) <EOL> warn ( <EOL> '<STR_LIT>' , <EOL> DeprecationWarning ) <EOL> return get_file_size ( * args , ** kwargs ) <EOL> def deepgetattr ( obj , attr , default = None , splitter = '<STR_LIT:.>' , do_raise = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return reduce ( getattr , attr . split ( splitter ) , obj ) <EOL> except AttributeError : <EOL> if do_raise : <EOL> raise <EOL> return default <EOL> def underscored ( * args ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:_>' . join ( filter ( bool , list ( args ) ) ) <EOL> def _underscored ( * args ) : <EOL> logger . warning ( <EOL> '<STR_LIT>' ) <EOL> warn ( <EOL> '<STR_LIT>' , <EOL> DeprecationWarning ) <EOL> return underscored ( * args ) <EOL> def get_nodes ( admin_ip ) : <EOL> auth_data = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' . format ( <EOL> KEYSTONE_CREDS [ '<STR_LIT>' ] , <EOL> KEYSTONE_CREDS [ '<STR_LIT:username>' ] , <EOL> KEYSTONE_CREDS [ '<STR_LIT:password>' ] ) <EOL> url = "<STR_LIT>" . format ( admin_ip ) <EOL> endpoint = '<STR_LIT>' <EOL> keystone_url = "<STR_LIT>" . format ( admin_ip ) <EOL> tokens_request = request . Request ( keystone_url ) <EOL> tokens_request . add_header ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> tokens_request . add_data ( auth_data ) <EOL> tokens_response = request . urlopen ( tokens_request ) <EOL> tokens_dct = json . load ( tokens_response ) <EOL> token = tokens_dct [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] <EOL> nodes_request = request . Request ( url + endpoint ) <EOL> nodes_request . add_header ( '<STR_LIT>' , token ) <EOL> if hasattr ( ssl , '<STR_LIT>' ) : <EOL> nodes_response = request . urlopen ( <EOL> nodes_request , context = ssl . _create_unverified_context ( ) ) <EOL> else : <EOL> nodes_response = request . urlopen ( nodes_request ) <EOL> nodes = json . load ( nodes_response ) <EOL> return nodes </s>
