<s> import argparse <EOL> import subprocess <EOL> import shlex <EOL> import re <EOL> import os <EOL> import sys <EOL> import json <EOL> import hashlib <EOL> from collections import defaultdict , OrderedDict <EOL> import time <EOL> from StringIO import StringIO <EOL> import webbrowser <EOL> from distutils . version import LooseVersion <EOL> import logging <EOL> import paramiko <EOL> logging . basicConfig ( ) <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> log . setLevel ( logging . DEBUG ) <EOL> from fabric import api as fab <EOL> from fabric . contrib . files import append as fab_append <EOL> from cstar_perf . tool import fab_deploy <EOL> from fabric . tasks import execute as fab_execute <EOL> import tasks <EOL> CONTAINER_DEFAULT_MEMORY = '<STR_LIT>' <EOL> fab . env . user = '<STR_LIT>' <EOL> docker_image_name = '<STR_LIT>' <EOL> dockerfile = """<STR_LIT>""" <EOL> def check_docker_version ( expected_version = '<STR_LIT>' ) : <EOL> version_cmd = shlex . split ( "<STR_LIT>" ) <EOL> try : <EOL> p = subprocess . Popen ( version_cmd , stdout = subprocess . PIPE ) <EOL> version_string = p . communicate ( ) [ <NUM_LIT:0> ] <EOL> except OSError : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> m = re . match ( '<STR_LIT>' , version_string ) <EOL> if m : <EOL> version = m . groups ( ) [ <NUM_LIT:0> ] <EOL> if LooseVersion ( version ) < expected_version : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' . format ( <EOL> version , expected_version ) ) <EOL> def get_dockerfile ( ) : <EOL> ssh_pub_file = get_ssh_key_pair ( ) [ <NUM_LIT:1> ] <EOL> with open ( ssh_pub_file ) as f : <EOL> return dockerfile . format ( ssh_pub_key = f . read ( ) . strip ( ) ) <EOL> def build_docker_image ( tag = docker_image_name , force = False ) : <EOL> if force : <EOL> rmi_cmd = shlex . split ( "<STR_LIT>" . format ( tag ) ) <EOL> log . info ( '<STR_LIT>' ) <EOL> p = subprocess . call ( rmi_cmd ) <EOL> build_cmd = shlex . split ( "<STR_LIT>" . format ( tag , '<STR_LIT>' if force else '<STR_LIT>' ) ) <EOL> p = subprocess . Popen ( build_cmd , stdin = subprocess . PIPE ) <EOL> p . communicate ( get_dockerfile ( ) ) <EOL> if p . returncode == <NUM_LIT:0> : <EOL> dockerfile_hash = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> docker_image_hash = hashlib . sha256 ( get_dockerfile ( ) ) . hexdigest ( ) <EOL> with open ( dockerfile_hash , '<STR_LIT:w>' ) as f : <EOL> f . write ( docker_image_hash ) <EOL> def check_if_build_necessary ( exit_if_not_ready = True ) : <EOL> """<STR_LIT>""" <EOL> current_dockerfile_hash = hashlib . sha256 ( get_dockerfile ( ) ) . hexdigest ( ) <EOL> try : <EOL> with open ( os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" , "<STR_LIT>" ) ) as f : <EOL> previous_dockerfile_hash = f . read ( ) . strip ( ) <EOL> needs_rebuild = not current_dockerfile_hash == previous_dockerfile_hash <EOL> except IOError : <EOL> needs_rebuild = True <EOL> if needs_rebuild and exit_if_not_ready : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> exit ( <NUM_LIT:1> ) <EOL> def get_container_data ( container ) : <EOL> inspect_cmd = shlex . split ( "<STR_LIT>" . format ( container ) ) <EOL> p = subprocess . Popen ( inspect_cmd , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> try : <EOL> return json . loads ( p . communicate ( ) [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise AssertionError ( '<STR_LIT>' . format ( container ) ) <EOL> def get_ssh_key_pair ( ) : <EOL> """<STR_LIT>""" <EOL> key_path = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> pub_key_path = key_path + '<STR_LIT>' <EOL> if not ( os . path . exists ( key_path ) and os . path . exists ( pub_key_path ) ) : <EOL> try : <EOL> os . mkdir ( os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , "<STR_LIT>" ) ) <EOL> except IOError : <EOL> pass <EOL> key = paramiko . rsakey . RSAKey . generate ( <NUM_LIT> ) <EOL> with open ( key_path , '<STR_LIT:w>' ) as f : <EOL> key . write_private_key ( f ) <EOL> with open ( pub_key_path , '<STR_LIT:w>' ) as f : <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( key . get_base64 ( ) ) <EOL> f . write ( "<STR_LIT>" . format ( time . ctime ( ) ) ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> os . chmod ( key_path , <NUM_LIT:0> <NUM_LIT> ) <EOL> os . chmod ( pub_key_path , <NUM_LIT:0> <NUM_LIT> ) <EOL> return ( key_path , pub_key_path ) <EOL> def get_clusters ( cluster_regex = '<STR_LIT:all>' , all_metadata = False ) : <EOL> """<STR_LIT>""" <EOL> cluster_regex = cluster_regex + ( "<STR_LIT>" if cluster_regex . endswith ( "<STR_LIT:$>" ) else "<STR_LIT:$>" ) <EOL> clusters = defaultdict ( list ) <EOL> cluster_nodes = defaultdict ( list ) <EOL> p = subprocess . Popen ( shlex . split ( "<STR_LIT>" ) , stdout = subprocess . PIPE ) <EOL> containers = p . communicate ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> class NoContainersException ( Exception ) : <EOL> pass <EOL> try : <EOL> if containers == '<STR_LIT>' : <EOL> raise NoContainersException <EOL> containers = containers . split ( '<STR_LIT:\n>' ) <EOL> for container in containers : <EOL> data = get_container_data ( container ) <EOL> try : <EOL> labels = data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if labels and labels [ '<STR_LIT>' ] == '<STR_LIT:true>' : <EOL> container_name = data [ '<STR_LIT:Name>' ] = data [ '<STR_LIT:Name>' ] . lstrip ( '<STR_LIT:/>' ) <EOL> node_num = labels [ '<STR_LIT>' ] = int ( labels [ '<STR_LIT>' ] ) <EOL> if cluster_regex . lower ( ) == '<STR_LIT>' or re . match ( cluster_regex , labels [ '<STR_LIT>' ] ) : <EOL> clusters [ labels [ '<STR_LIT>' ] ] . append ( data ) <EOL> cluster_nodes [ labels [ '<STR_LIT>' ] ] . append ( container_name ) <EOL> except KeyError : <EOL> pass <EOL> except NoContainersException : <EOL> pass <EOL> for cluster_name , cluster_data in clusters . items ( ) : <EOL> cluster_data . sort ( key = lambda x : x [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> cluster_types = set ( [ x [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] for x in cluster_data ] ) <EOL> assert len ( cluster_types ) == <NUM_LIT:1> , "<STR_LIT>" . format ( cluster_name , cluster_types ) <EOL> for cluster_name , nodes in cluster_nodes . items ( ) : <EOL> nodes . sort ( ) <EOL> if all_metadata : <EOL> return clusters <EOL> else : <EOL> return cluster_nodes <EOL> def get_ips ( cluster_name ) : <EOL> clusters = get_clusters ( cluster_name , all_metadata = True ) <EOL> cluster = clusters [ cluster_name ] <EOL> return tuple ( ( c [ '<STR_LIT:Name>' ] , c [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) for c in cluster ) <EOL> def check_cluster_exists ( cluster_regex ) : <EOL> existing_nodes = get_clusters ( cluster_regex ) <EOL> return bool ( len ( existing_nodes ) ) <EOL> def launch ( num_nodes , cluster_name = '<STR_LIT>' , destroy_existing = False , <EOL> install_tool = True , frontend = False , mount_host_src = False , verbose = False , <EOL> client_double_duty = False ) : <EOL> """<STR_LIT>""" <EOL> assert num_nodes > <NUM_LIT:0> , "<STR_LIT>" . format ( num_nodes ) <EOL> if frontend : <EOL> assert num_nodes == <NUM_LIT:1> and client_double_duty , "<STR_LIT>" <EOL> cluster_type = '<STR_LIT>' if frontend else '<STR_LIT>' <EOL> try : <EOL> get_container_data ( docker_image_name ) <EOL> except AssertionError : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( docker_image_name ) ) <EOL> exit ( <NUM_LIT:1> ) <EOL> check_if_build_necessary ( ) <EOL> existing_nodes = get_clusters ( cluster_name ) <EOL> if len ( existing_nodes ) : <EOL> if destroy_existing : <EOL> destroy ( cluster_name ) <EOL> else : <EOL> log . error ( '<STR_LIT>' . format ( cluster_name ) ) <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> first_cassandra_node = <NUM_LIT:1> <EOL> if client_double_duty : <EOL> first_cassandra_node = <NUM_LIT:0> <EOL> log . info ( '<STR_LIT>' . format ( num_nodes ) ) <EOL> else : <EOL> num_nodes += <NUM_LIT:1> <EOL> log . info ( '<STR_LIT>' . format ( num_nodes ) ) <EOL> node_data = OrderedDict ( ) <EOL> for i in range ( num_nodes ) : <EOL> node_name = "<STR_LIT>" % ( cluster_name , i ) <EOL> ssh_path = os . path . split ( get_ssh_key_pair ( ) [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] <EOL> run_cmd = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> cluster_name = cluster_name , node_num = i , node_name = node_name , cluster_type = cluster_type , <EOL> CONTAINER_DEFAULT_MEMORY = CONTAINER_DEFAULT_MEMORY , ssh_path = ssh_path , <EOL> port_settings = "<STR_LIT>" if frontend else "<STR_LIT>" ) ) <EOL> if mount_host_src : <EOL> candidates = [ <EOL> os . path . abspath ( os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , os . pardir , os . pardir , os . pardir ) ) , <EOL> os . getcwd ( ) <EOL> ] <EOL> for d in candidates : <EOL> if os . path . exists ( os . path . join ( d , '<STR_LIT>' ) ) and os . path . exists ( os . path . join ( d , '<STR_LIT>' ) ) and os . path . exists ( os . path . join ( d , '<STR_LIT>' ) ) : <EOL> cstar_dir = d <EOL> break <EOL> else : <EOL> log . error ( "<STR_LIT>" ) <EOL> exit ( <NUM_LIT:1> ) <EOL> run_cmd = run_cmd + "<STR_LIT>" . format ( cstar_dir = cstar_dir ) <EOL> run_cmd = run_cmd + '<STR_LIT:U+0020>' + docker_image_name <EOL> log . debug ( run_cmd ) <EOL> p = subprocess . Popen ( shlex . split ( run_cmd ) , <EOL> stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> container_id = p . communicate ( ) [ <NUM_LIT:0> ] . strip ( ) <EOL> node_data [ node_name ] = get_container_data ( container_id ) <EOL> hosts = OrderedDict ( ) <EOL> for name , data in node_data . items ( ) : <EOL> hosts [ name ] = data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> with fab . settings ( hosts = [ n for n in hosts . values ( ) ] ) : <EOL> fab_execute ( fab_deploy . setup_hosts_file , hosts ) <EOL> if frontend : <EOL> log . info ( "<STR_LIT>" ) <EOL> __install_cstar_perf_frontend ( cluster_name , hosts , mount_host_src = mount_host_src ) <EOL> elif install_tool : <EOL> log . info ( "<STR_LIT>" ) <EOL> __install_cstar_perf_tool ( cluster_name , hosts , mount_host_src = mount_host_src , <EOL> first_cassandra_node = first_cassandra_node ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" . format ( num_nodes ) ) <EOL> print ( "<STR_LIT>" ) <EOL> info ( cluster_name ) <EOL> return node_data <EOL> def __install_cstar_perf_frontend ( cluster_name , hosts , mount_host_src = False ) : <EOL> assert len ( hosts ) == <NUM_LIT:1> , "<STR_LIT>" <EOL> host , ip = hosts . popitem ( ) <EOL> with fab . settings ( hosts = ip ) : <EOL> fab_execute ( fab_deploy . setup_fab_dir ) <EOL> __install_cstar_perf_tool ( cluster_name , { host : ip } , mount_host_src = mount_host_src , first_cassandra_node = <NUM_LIT:0> ) <EOL> def setup_cassandra ( ) : <EOL> __update_node_ip_addresses ( cluster_name , static_ips = { host : '<STR_LIT:127.0.0.1>' } ) <EOL> fab . run ( "<STR_LIT>" ) <EOL> with fab . settings ( hosts = ip ) : <EOL> fab_execute ( setup_cassandra ) <EOL> def setup_boot_items ( ) : <EOL> boot_items = "<STR_LIT:\n>" . join ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> fab_append ( "<STR_LIT>" , boot_items ) <EOL> with fab . settings ( hosts = ip , user = "<STR_LIT:root>" ) : <EOL> fab_execute ( setup_boot_items ) <EOL> fab_execute ( fab_deploy . install_cstar_perf_frontend ) <EOL> with fab . settings ( hosts = ip ) : <EOL> fab_execute ( fab_deploy . generate_frontend_credentials ) <EOL> subprocess . call ( shlex . split ( "<STR_LIT>" . format ( host ) ) ) <EOL> frontend_name , frontend_ip = get_ips ( cluster_name ) [ <NUM_LIT:0> ] <EOL> with fab . settings ( hosts = frontend_ip ) : <EOL> fab_execute ( fab_deploy . create_default_frontend_users ) <EOL> log . info ( "<STR_LIT>" ) <EOL> webbrowser . open ( "<STR_LIT>" ) <EOL> log . info ( "<STR_LIT>" ) <EOL> log . info ( "<STR_LIT>" ) <EOL> def __install_cstar_perf_tool ( cluster_name , hosts , mount_host_src = False , first_cassandra_node = None ) : <EOL> first_node = hosts . values ( ) [ <NUM_LIT:0> ] <EOL> other_nodes = hosts . values ( ) [ <NUM_LIT:1> : ] <EOL> if first_cassandra_node is None : <EOL> if len ( hosts ) > <NUM_LIT:1> : <EOL> first_cassandra_node = <NUM_LIT:1> <EOL> else : <EOL> first_cassandra_node = <NUM_LIT:0> <EOL> cluster_config = { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : { <EOL> host : { <EOL> "<STR_LIT>" : host , <EOL> "<STR_LIT>" : ip , <EOL> "<STR_LIT>" : ip , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : '<STR_LIT>' <EOL> } for host , ip in hosts . items ( ) [ first_cassandra_node : ] <EOL> } , <EOL> "<STR_LIT:name>" : cluster_name , <EOL> "<STR_LIT>" : first_node , <EOL> "<STR_LIT:user>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : True <EOL> } <EOL> with fab . settings ( hosts = first_node ) : <EOL> fab_execute ( fab_deploy . copy_cluster_config , cluster_config ) <EOL> with fab . settings ( hosts = first_node ) : <EOL> fab_execute ( fab_deploy . setup_fab_dir ) <EOL> fab_execute ( fab_deploy . install_cstar_perf_tool ) <EOL> fab_execute ( fab_deploy . install_cstar_perf_frontend ) <EOL> if len ( other_nodes ) > <NUM_LIT:0> : <EOL> with fab . settings ( hosts = other_nodes ) : <EOL> fab_execute ( fab_deploy . copy_fab_dir , first_node ) <EOL> def info ( cluster_name ) : <EOL> clusters = get_clusters ( cluster_name , all_metadata = True ) <EOL> containers = clusters [ cluster_name ] <EOL> node_names = [ n [ '<STR_LIT:Name>' ] for n in containers ] <EOL> if len ( containers ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" . format ( cluster_name ) ) <EOL> else : <EOL> print ( "<STR_LIT>" . format ( cluster_name , len ( node_names ) ) ) <EOL> for n , node_name in enumerate ( node_names ) : <EOL> data = containers [ n ] <EOL> if data [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> print ( "<STR_LIT>" . format ( node_name , data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> print ( "<STR_LIT>" . format ( node_name ) ) <EOL> def destroy ( cluster_regex ) : <EOL> """<STR_LIT>""" <EOL> clusters = get_clusters ( cluster_regex ) <EOL> for cluster , containers in clusters . items ( ) : <EOL> if len ( containers ) > <NUM_LIT:0> : <EOL> log . info ( '<STR_LIT>' . format ( cluster_regex ) ) <EOL> for container in containers : <EOL> destroy_cmd = shlex . split ( "<STR_LIT>" . format ( container ) ) <EOL> subprocess . call ( destroy_cmd , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> def associate ( frontend_name , cluster_names , with_dse = False ) : <EOL> try : <EOL> frontend = get_clusters ( frontend_name , all_metadata = True ) [ frontend_name ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise ValueError ( "<STR_LIT>" . format ( frontend_name ) ) <EOL> clusters = [ ] <EOL> for c in cluster_names : <EOL> try : <EOL> cluster = get_clusters ( c , all_metadata = True ) [ c ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise ValueError ( "<STR_LIT>" . format ( c ) ) <EOL> clusters . append ( cluster ) <EOL> frontend_ip = frontend [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> with fab . settings ( hosts = frontend_ip ) : <EOL> frontend_credentials = fab_execute ( fab_deploy . get_frontend_credentials ) . values ( ) [ <NUM_LIT:0> ] <EOL> for cluster in clusters : <EOL> cluster = cluster <EOL> cluster_name = cluster [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> nodes = get_clusters ( c ) [ cluster_name ] [ <NUM_LIT:1> : ] <EOL> cluster_ip = cluster [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> with fab . settings ( hosts = cluster_ip ) : <EOL> fab_execute ( fab_deploy . generate_client_credentials , cluster_name , <EOL> frontend_credentials [ '<STR_LIT>' ] , <EOL> frontend_credentials [ '<STR_LIT>' ] ) <EOL> cluster_credentials = fab_execute ( fab_deploy . get_client_credentials ) . values ( ) [ <NUM_LIT:0> ] <EOL> jvms = fab_execute ( fab_deploy . get_client_jvms ) . values ( ) [ <NUM_LIT:0> ] <EOL> with fab . settings ( hosts = frontend_ip ) : <EOL> fab_execute ( fab_deploy . add_cluster_to_frontend , cluster_name , nodes , <EOL> cluster_credentials [ '<STR_LIT>' ] ) <EOL> for jvm in jvms : <EOL> fab_execute ( fab_deploy . add_jvm_to_cluster , cluster_name , jvm ) <EOL> if with_dse : <EOL> fab_execute ( fab_deploy . add_product_to_cluster , cluster_name , '<STR_LIT>' ) <EOL> with fab . settings ( hosts = cluster_ip , user = "<STR_LIT:root>" ) : <EOL> fab_execute ( tasks . setup_client_daemon , frontend [ '<STR_LIT:Name>' ] ) <EOL> fab_execute ( tasks . add_or_update_host_ips , ( ( frontend [ '<STR_LIT:Name>' ] , frontend_ip ) , ) ) <EOL> def enable_dse ( cluster_name , dse_url , dse_username , dse_password , dse_source_build_artifactory_url , <EOL> dse_source_build_artifactory_username , dse_source_build_artifactory_password , <EOL> dse_source_build_oauth_token ) : <EOL> try : <EOL> cluster = get_clusters ( cluster_name , all_metadata = True ) [ cluster_name ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise ValueError ( "<STR_LIT>" . format ( cluster_name ) ) <EOL> cluster_ip = cluster [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> with fab . settings ( hosts = cluster_ip ) : <EOL> fab_execute ( fab_deploy . enable_dse , dse_url , dse_username , dse_password , dse_source_build_artifactory_url , <EOL> dse_source_build_artifactory_username , dse_source_build_artifactory_password , <EOL> dse_source_build_oauth_token ) <EOL> with fab . settings ( hosts = cluster_ip , user = "<STR_LIT:root>" ) : <EOL> fab_execute ( tasks . restart_all_services ) <EOL> def __update_node_ip_addresses ( cluster_name , static_ips = None ) : <EOL> """<STR_LIT>""" <EOL> clusters = get_clusters ( cluster_name , all_metadata = True ) <EOL> cluster = clusters [ cluster_name ] <EOL> current_ips = dict ( [ ( c [ '<STR_LIT:Name>' ] , c [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) for c in cluster ] ) <EOL> if static_ips : <EOL> updated_ips = static_ips <EOL> else : <EOL> updated_ips = current_ips <EOL> node0 = cluster [ <NUM_LIT:0> ] [ '<STR_LIT:Name>' ] <EOL> with fab . settings ( hosts = current_ips [ node0 ] ) : <EOL> def get_cluster_config ( ) : <EOL> cfg = StringIO ( ) <EOL> fab . get ( "<STR_LIT>" , cfg ) <EOL> cfg . seek ( <NUM_LIT:0> ) <EOL> return json . load ( cfg ) <EOL> cluster_config = fab_execute ( get_cluster_config ) . values ( ) [ <NUM_LIT:0> ] <EOL> for host , cfg in cluster_config [ '<STR_LIT>' ] . items ( ) : <EOL> cluster_config [ '<STR_LIT>' ] [ host ] [ '<STR_LIT>' ] = cluster_config [ '<STR_LIT>' ] [ host ] [ '<STR_LIT>' ] = updated_ips [ host ] <EOL> cluster_config [ '<STR_LIT>' ] = updated_ips [ node0 ] <EOL> with fab . settings ( hosts = cluster [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) : <EOL> def put_cluster_config ( ) : <EOL> cfg = StringIO ( ) <EOL> json . dump ( cluster_config , cfg , indent = <NUM_LIT:2> ) <EOL> fab . put ( cfg , "<STR_LIT>" ) <EOL> fab_execute ( put_cluster_config ) <EOL> hosts = [ ] <EOL> clusters = get_clusters ( '<STR_LIT:all>' , all_metadata = True ) <EOL> for cluster_name in clusters . keys ( ) : <EOL> hosts . extend ( get_ips ( cluster_name ) ) <EOL> with fab . settings ( hosts = [ ip for host , ip in hosts ] , user = "<STR_LIT:root>" ) : <EOL> fab_execute ( tasks . add_or_update_host_ips , hosts ) <EOL> fab_execute ( tasks . restart_all_services ) <EOL> def start ( cluster_name ) : <EOL> """<STR_LIT>""" <EOL> clusters = get_clusters ( cluster_name ) <EOL> cluster = clusters [ cluster_name ] <EOL> for container in cluster : <EOL> start_cmd = shlex . split ( "<STR_LIT>" . format ( container ) ) <EOL> subprocess . call ( start_cmd , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> __update_node_ip_addresses ( cluster_name ) <EOL> def stop ( cluster_name ) : <EOL> """<STR_LIT>""" <EOL> clusters = get_clusters ( cluster_name ) <EOL> cluster = clusters [ cluster_name ] <EOL> for container in cluster : <EOL> stop_cmd = shlex . split ( "<STR_LIT>" . format ( container ) ) <EOL> subprocess . call ( stop_cmd , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> def list_clusters ( ) : <EOL> """<STR_LIT>""" <EOL> clusters = get_clusters ( '<STR_LIT:all>' , all_metadata = True ) <EOL> for cluster , containers in clusters . items ( ) : <EOL> print ( "<STR_LIT>" . format ( <EOL> name = cluster , num_nodes = len ( containers ) , <EOL> cluster_type = containers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> plural = "<STR_LIT:s>" if len ( containers ) > <NUM_LIT:1> else "<STR_LIT>" ) ) <EOL> def ssh ( cluster_name , node , user = '<STR_LIT>' , ssh_key_path = os . path . join ( os . path . expanduser ( "<STR_LIT>" ) , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> clusters = get_clusters ( cluster_name , all_metadata = True ) <EOL> containers = clusters [ cluster_name ] <EOL> node_names = [ c [ '<STR_LIT:Name>' ] for c in containers ] <EOL> if len ( containers ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" . format ( cluster_name ) ) <EOL> elif containers [ node ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] is False : <EOL> log . error ( "<STR_LIT>" . format ( cluster_name ) ) <EOL> else : <EOL> command = '<STR_LIT>' % ( os . devnull , <EOL> user , <EOL> get_ssh_key_pair ( ) [ <NUM_LIT:0> ] , <EOL> containers [ node ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> proc = subprocess . Popen ( command , shell = True ) <EOL> proc . wait ( ) <EOL> def execute_cmd ( cmd , args ) : <EOL> if cmd == '<STR_LIT>' : <EOL> if not check_cluster_exists ( args . name ) or args . destroy_existing : <EOL> try : <EOL> launch ( args . num_nodes , cluster_name = args . name , <EOL> destroy_existing = args . destroy_existing , <EOL> install_tool = not args . no_install , <EOL> mount_host_src = args . mount , verbose = True , <EOL> client_double_duty = args . client_double_duty ) <EOL> except : <EOL> destroy ( args . name ) <EOL> raise <EOL> else : <EOL> log . error ( '<STR_LIT>' . format ( args . name ) ) <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> elif cmd == '<STR_LIT>' : <EOL> if not check_cluster_exists ( args . name ) or args . destroy_existing : <EOL> try : <EOL> launch ( <NUM_LIT:1> , cluster_name = args . name , <EOL> destroy_existing = args . destroy_existing , <EOL> frontend = True , client_double_duty = True , <EOL> mount_host_src = args . mount , verbose = True ) <EOL> except : <EOL> destroy ( args . name ) <EOL> raise <EOL> else : <EOL> log . error ( '<STR_LIT>' . format ( args . name ) ) <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> elif cmd == '<STR_LIT>' : <EOL> associate ( args . frontend , args . clusters , with_dse = args . with_dse ) <EOL> elif cmd == '<STR_LIT:start>' : <EOL> start ( cluster_name = args . name ) <EOL> elif cmd == '<STR_LIT>' : <EOL> stop ( cluster_name = args . name ) <EOL> elif cmd == '<STR_LIT>' : <EOL> stop ( cluster_name = args . name ) <EOL> start ( cluster_name = args . name ) <EOL> elif cmd == '<STR_LIT>' : <EOL> destroy ( args . cluster_regex ) <EOL> elif cmd == '<STR_LIT:list>' : <EOL> list_clusters ( ) <EOL> elif cmd == '<STR_LIT:info>' : <EOL> info ( args . cluster_name ) <EOL> elif cmd == '<STR_LIT>' : <EOL> ssh ( args . cluster_name , args . node , user = args . login_name ) <EOL> elif cmd == '<STR_LIT>' : <EOL> build_docker_image ( force = args . force ) <EOL> elif cmd == '<STR_LIT>' : <EOL> enable_dse ( args . frontend , args . dse_repo_url , args . dse_repo_username , args . dse_repo_password , <EOL> args . dse_source_build_artifactory_url , args . dse_source_build_artifactory_username , <EOL> args . dse_source_build_artifactory_password , args . dse_source_build_oauth_token ) <EOL> else : <EOL> raise AssertionError ( '<STR_LIT>' . format ( cmd = cmd ) ) <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter ) <EOL> parser_subparsers = parser . add_subparsers ( dest = '<STR_LIT>' ) <EOL> launch = parser_subparsers . add_parser ( '<STR_LIT>' , description = "<STR_LIT>" ) <EOL> launch . add_argument ( '<STR_LIT:name>' , help = '<STR_LIT>' ) <EOL> launch . add_argument ( '<STR_LIT>' , type = int , help = '<STR_LIT>' ) <EOL> launch . add_argument ( '<STR_LIT:-c>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , help = '<STR_LIT>' ) <EOL> launch . add_argument ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> launch . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> launch . add_argument ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' , action = "<STR_LIT:store_true>" ) <EOL> frontend = parser_subparsers . add_parser ( '<STR_LIT>' , description = "<STR_LIT>" ) <EOL> frontend . add_argument ( '<STR_LIT:name>' , help = '<STR_LIT>' ) <EOL> frontend . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> frontend . add_argument ( <EOL> '<STR_LIT>' , help = '<STR_LIT>' , action = "<STR_LIT:store_true>" ) <EOL> associate = parser_subparsers . add_parser ( '<STR_LIT>' , description = "<STR_LIT>" ) <EOL> associate . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> associate . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:+>' ) <EOL> associate . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) <EOL> destroy = parser_subparsers . add_parser ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> destroy . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> list_clusters = parser_subparsers . add_parser ( '<STR_LIT:list>' , description = '<STR_LIT>' ) <EOL> info = parser_subparsers . add_parser ( '<STR_LIT:info>' , description = '<STR_LIT>' ) <EOL> info . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> ssh = parser_subparsers . add_parser ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> ssh . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> ssh . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , type = int , nargs = '<STR_LIT:?>' , default = <NUM_LIT:0> ) <EOL> ssh . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> build = parser_subparsers . add_parser ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> build . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> start = parser_subparsers . add_parser ( '<STR_LIT:start>' , description = '<STR_LIT>' ) <EOL> start . add_argument ( '<STR_LIT:name>' , help = '<STR_LIT>' ) <EOL> stop = parser_subparsers . add_parser ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> stop . add_argument ( '<STR_LIT:name>' , help = '<STR_LIT>' ) <EOL> restart = parser_subparsers . add_parser ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> restart . add_argument ( '<STR_LIT:name>' , help = '<STR_LIT>' ) <EOL> enable_dse = parser_subparsers . add_parser ( '<STR_LIT>' , description = "<STR_LIT>" ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = None , help = '<STR_LIT>' ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = None , help = '<STR_LIT>' ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = None , help = '<STR_LIT>' ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = None , help = '<STR_LIT>' ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = None , help = '<STR_LIT>' ) <EOL> enable_dse . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = None , help = '<STR_LIT>' ) <EOL> try : <EOL> args = parser . parse_args ( ) <EOL> finally : <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> parser . print_help ( ) <EOL> check_docker_version ( ) <EOL> execute_cmd ( args . command , args ) <EOL> fab . env . key_filename = get_ssh_key_pair ( ) [ <NUM_LIT:0> ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
