<s> import logging <EOL> import os <EOL> import tarfile <EOL> import time <EOL> import zipfile <EOL> import urllib <EOL> from subprocess import call <EOL> import zopkio . constants as constants <EOL> from zopkio . deployer import Deployer , Process <EOL> from zopkio . remote_host_helper import better_exec_command , DeploymentError , get_sftp_client , get_ssh_client , open_remote_file , log_output , exec_with_env <EOL> import zopkio . runtime as runtime <EOL> logger = logging . getLogger ( __name__ ) <EOL> class SSHDeployer ( Deployer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , service_name , configs = None ) : <EOL> """<STR_LIT>""" <EOL> logging . getLogger ( "<STR_LIT>" ) . setLevel ( logging . ERROR ) <EOL> self . service_name = service_name <EOL> self . default_configs = { } if configs is None else configs <EOL> Deployer . __init__ ( self ) <EOL> def install ( self , unique_id , configs = None ) : <EOL> """<STR_LIT>""" <EOL> if configs is None : <EOL> configs = { } <EOL> tmp = self . default_configs . copy ( ) <EOL> tmp . update ( configs ) <EOL> configs = tmp <EOL> hostname = None <EOL> is_tarfile = False <EOL> is_zipfile = False <EOL> if unique_id in self . processes and '<STR_LIT>' in configs : <EOL> self . uninstall ( unique_id , configs ) <EOL> hostname = configs [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in configs : <EOL> hostname = configs [ '<STR_LIT>' ] <EOL> elif unique_id not in self . processes : <EOL> raise DeploymentError ( "<STR_LIT>" + unique_id ) <EOL> env = configs . get ( "<STR_LIT>" , { } ) <EOL> install_path = configs . get ( '<STR_LIT>' ) or self . default_configs . get ( '<STR_LIT>' ) <EOL> pid_file = configs . get ( '<STR_LIT>' ) or self . default_configs . get ( '<STR_LIT>' ) <EOL> if install_path is None : <EOL> logger . error ( "<STR_LIT>" + unique_id ) <EOL> raise DeploymentError ( "<STR_LIT>" + unique_id ) <EOL> if not configs . get ( '<STR_LIT>' , False ) : <EOL> with get_ssh_client ( hostname , username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as ssh : <EOL> log_output ( better_exec_command ( ssh , "<STR_LIT>" . format ( install_path ) , <EOL> "<STR_LIT>" . format ( install_path ) ) ) <EOL> log_output ( better_exec_command ( ssh , "<STR_LIT>" . format ( install_path ) , <EOL> "<STR_LIT>" . format ( install_path ) ) ) <EOL> executable = configs . get ( '<STR_LIT>' ) or self . default_configs . get ( '<STR_LIT>' ) <EOL> if executable is None : <EOL> logger . error ( "<STR_LIT>" + unique_id ) <EOL> raise DeploymentError ( "<STR_LIT>" + unique_id ) <EOL> copy_from_remote_location = False ; <EOL> if ( "<STR_LIT::>" in executable ) : <EOL> copy_from_remote_location = True <EOL> if ( "<STR_LIT:http>" not in executable ) : <EOL> remote_location_server = executable . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] <EOL> remote_file_path = executable . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> remote_file_name = os . path . basename ( remote_file_path ) <EOL> local_temp_file_name = os . path . join ( configs . get ( "<STR_LIT>" , "<STR_LIT>" ) , remote_file_name ) <EOL> if not os . path . exists ( local_temp_file_name ) : <EOL> with get_sftp_client ( remote_location_server , username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as ftp : <EOL> try : <EOL> ftp . get ( remote_file_path , local_temp_file_name ) <EOL> executable = local_temp_file_name <EOL> except : <EOL> raise DeploymentError ( "<STR_LIT>" + executable ) <EOL> else : <EOL> remote_file_name = executable . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> local_temp_file_name = os . path . join ( configs . get ( "<STR_LIT>" , "<STR_LIT>" ) , remote_file_name ) <EOL> if not os . path . exists ( local_temp_file_name ) : <EOL> try : <EOL> urllib . urlretrieve ( executable , local_temp_file_name ) <EOL> except : <EOL> raise DeploymentError ( "<STR_LIT>" + executable ) <EOL> executable = local_temp_file_name <EOL> try : <EOL> exec_name = os . path . basename ( executable ) <EOL> install_location = os . path . join ( install_path , exec_name ) <EOL> with get_sftp_client ( hostname , username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as ftp : <EOL> ftp . put ( executable , install_location ) <EOL> except : <EOL> raise DeploymentError ( "<STR_LIT>" + install_location ) <EOL> finally : <EOL> is_tarfile = tarfile . is_tarfile ( executable ) <EOL> is_zipfile = zipfile . is_zipfile ( executable ) <EOL> if ( copy_from_remote_location and not configs . get ( '<STR_LIT>' , False ) ) : <EOL> os . remove ( executable ) <EOL> if configs . get ( '<STR_LIT>' , False ) or self . default_configs . get ( '<STR_LIT>' , False ) : <EOL> if is_tarfile : <EOL> log_output ( better_exec_command ( ssh , "<STR_LIT>" . format ( install_location , install_path ) , <EOL> "<STR_LIT>" . format ( exec_name ) ) ) <EOL> elif is_zipfile : <EOL> log_output ( better_exec_command ( ssh , "<STR_LIT>" . format ( install_location , install_path ) , <EOL> "<STR_LIT>" . format ( exec_name ) ) ) <EOL> else : <EOL> logger . error ( executable + "<STR_LIT>" ) <EOL> raise DeploymentError ( executable + "<STR_LIT>" ) <EOL> post_install_cmds = configs . get ( '<STR_LIT>' , False ) or self . default_configs . get ( '<STR_LIT>' , [ ] ) <EOL> for cmd in post_install_cmds : <EOL> relative_cmd = "<STR_LIT>" . format ( install_path , cmd ) <EOL> log_output ( exec_with_env ( ssh , relative_cmd , <EOL> msg = "<STR_LIT>" . format ( relative_cmd ) , env = env ) ) <EOL> self . processes [ unique_id ] = Process ( unique_id , self . service_name , hostname , install_path ) <EOL> self . processes [ unique_id ] . pid_file = pid_file <EOL> def start ( self , unique_id , configs = None ) : <EOL> """<STR_LIT>""" <EOL> if configs is None : <EOL> configs = { } <EOL> tmp = self . default_configs . copy ( ) <EOL> tmp . update ( configs ) <EOL> configs = tmp <EOL> logger . debug ( "<STR_LIT>" + unique_id ) <EOL> if self . get_pid ( unique_id , configs ) is not constants . PROCESS_NOT_RUNNING_PID : <EOL> return None <EOL> if unique_id not in self . processes : <EOL> self . install ( unique_id , configs ) <EOL> hostname = self . processes [ unique_id ] . hostname <EOL> install_path = self . processes [ unique_id ] . install_path <EOL> start_command = configs . get ( '<STR_LIT>' ) or self . processes [ unique_id ] . start_command or self . default_configs . get ( '<STR_LIT>' ) <EOL> pid_file = configs . get ( '<STR_LIT>' ) or self . default_configs . get ( '<STR_LIT>' ) <EOL> if start_command is None : <EOL> logger . error ( "<STR_LIT>" + unique_id ) <EOL> raise DeploymentError ( "<STR_LIT>" + unique_id ) <EOL> args = configs . get ( '<STR_LIT:args>' ) or self . processes [ unique_id ] . args or self . default_configs . get ( '<STR_LIT:args>' ) <EOL> if args is not None : <EOL> full_start_command = "<STR_LIT>" . format ( start_command , '<STR_LIT:U+0020>' . join ( args ) ) <EOL> else : <EOL> full_start_command = start_command <EOL> command = "<STR_LIT>" . format ( install_path , full_start_command ) <EOL> env = configs . get ( "<STR_LIT>" , { } ) <EOL> with get_ssh_client ( hostname , username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as ssh : <EOL> exec_with_env ( ssh , command , msg = "<STR_LIT>" , env = env , sync = configs . get ( '<STR_LIT>' , False ) ) <EOL> self . processes [ unique_id ] . start_command = start_command <EOL> self . processes [ unique_id ] . args = args <EOL> if self . processes [ unique_id ] . pid_file is None : <EOL> self . processes [ unique_id ] . pid_file = pid_file <EOL> if '<STR_LIT>' in configs : <EOL> time . sleep ( configs [ '<STR_LIT>' ] ) <EOL> def stop ( self , unique_id , configs = None ) : <EOL> """<STR_LIT>""" <EOL> if configs is None : <EOL> configs = { } <EOL> tmp = self . default_configs . copy ( ) <EOL> tmp . update ( configs ) <EOL> configs = tmp <EOL> logger . debug ( "<STR_LIT>" + unique_id ) <EOL> if unique_id in self . processes : <EOL> hostname = self . processes [ unique_id ] . hostname <EOL> else : <EOL> logger . error ( "<STR_LIT>" . format ( unique_id ) ) <EOL> raise DeploymentError ( "<STR_LIT>" . format ( unique_id ) ) <EOL> if configs . get ( '<STR_LIT>' , False ) : <EOL> self . terminate ( unique_id , configs ) <EOL> else : <EOL> stop_command = configs . get ( '<STR_LIT>' ) or self . default_configs . get ( '<STR_LIT>' ) <EOL> env = configs . get ( "<STR_LIT>" , { } ) <EOL> if stop_command is not None : <EOL> install_path = self . processes [ unique_id ] . install_path <EOL> with get_ssh_client ( hostname , username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as ssh : <EOL> log_output ( exec_with_env ( ssh , "<STR_LIT>" . format ( install_path , stop_command ) , <EOL> msg = "<STR_LIT>" . format ( unique_id ) , env = env ) ) <EOL> else : <EOL> self . terminate ( unique_id , configs ) <EOL> if '<STR_LIT>' in configs : <EOL> time . sleep ( configs [ '<STR_LIT>' ] ) <EOL> def uninstall ( self , unique_id , configs = None ) : <EOL> """<STR_LIT>""" <EOL> if configs is None : <EOL> configs = { } <EOL> tmp = self . default_configs . copy ( ) <EOL> tmp . update ( configs ) <EOL> configs = tmp <EOL> if unique_id in self . processes : <EOL> hostname = self . processes [ unique_id ] . hostname <EOL> else : <EOL> logger . error ( "<STR_LIT>" . format ( unique_id ) ) <EOL> raise DeploymentError ( "<STR_LIT>" . format ( unique_id ) ) <EOL> install_path = self . processes [ unique_id ] . install_path <EOL> directories_to_remove = self . default_configs . get ( '<STR_LIT>' , [ ] ) <EOL> directories_to_remove . extend ( configs . get ( '<STR_LIT>' , [ ] ) ) <EOL> if install_path not in directories_to_remove : <EOL> directories_to_remove . append ( install_path ) <EOL> with get_ssh_client ( hostname , username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as ssh : <EOL> for directory_to_remove in directories_to_remove : <EOL> log_output ( better_exec_command ( ssh , "<STR_LIT>" . format ( directory_to_remove ) , <EOL> "<STR_LIT>" . format ( directory_to_remove ) ) ) <EOL> def get_pid ( self , unique_id , configs = None ) : <EOL> """<STR_LIT>""" <EOL> RECV_BLOCK_SIZE = <NUM_LIT:16> <EOL> if configs is None : <EOL> configs = { } <EOL> tmp = self . default_configs . copy ( ) <EOL> tmp . update ( configs ) <EOL> configs = tmp <EOL> if unique_id in self . processes : <EOL> hostname = self . processes [ unique_id ] . hostname <EOL> else : <EOL> return constants . PROCESS_NOT_RUNNING_PID <EOL> if self . processes [ unique_id ] . start_command is None : <EOL> return constants . PROCESS_NOT_RUNNING_PID <EOL> if self . processes [ unique_id ] . pid_file is not None : <EOL> with open_remote_file ( hostname , self . processes [ unique_id ] . pid_file , <EOL> username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as pid_file : <EOL> full_output = pid_file . read ( ) <EOL> elif '<STR_LIT>' in configs . keys ( ) : <EOL> with open_remote_file ( hostname , configs [ '<STR_LIT>' ] , <EOL> username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as pid_file : <EOL> full_output = pid_file . read ( ) <EOL> else : <EOL> pid_keyword = self . processes [ unique_id ] . start_command <EOL> if self . processes [ unique_id ] . args is not None : <EOL> pid_keyword = "<STR_LIT>" . format ( pid_keyword , '<STR_LIT:U+0020>' . join ( self . processes [ unique_id ] . args ) ) <EOL> pid_keyword = configs . get ( '<STR_LIT>' , pid_keyword ) <EOL> pid_command = "<STR_LIT>" . format ( pid_keyword ) <EOL> pid_command = configs . get ( '<STR_LIT>' , pid_command ) <EOL> non_failing_command = "<STR_LIT>" . format ( pid_command ) <EOL> env = configs . get ( "<STR_LIT>" , { } ) <EOL> with get_ssh_client ( hostname , username = runtime . get_username ( ) , password = runtime . get_password ( ) ) as ssh : <EOL> chan = exec_with_env ( ssh , non_failing_command , msg = "<STR_LIT>" , env = env ) <EOL> output = chan . recv ( RECV_BLOCK_SIZE ) <EOL> full_output = output <EOL> while len ( output ) > <NUM_LIT:0> : <EOL> output = chan . recv ( RECV_BLOCK_SIZE ) <EOL> full_output += output <EOL> if len ( full_output ) > <NUM_LIT:0> : <EOL> pids = [ int ( pid_str ) for pid_str in full_output . split ( '<STR_LIT:\n>' ) if pid_str . isdigit ( ) ] <EOL> if len ( pids ) > <NUM_LIT:0> : <EOL> return pids <EOL> return constants . PROCESS_NOT_RUNNING_PID <EOL> def get_host ( self , unique_id ) : <EOL> """<STR_LIT>""" <EOL> if unique_id in self . processes : <EOL> return self . processes [ unique_id ] . hostname <EOL> logger . error ( "<STR_LIT>" . format ( unique_id ) ) <EOL> raise NameError ( "<STR_LIT>" . format ( unique_id ) ) <EOL> def get_processes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . processes . values ( ) <EOL> def kill_all_process ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( runtime . get_active_config ( "<STR_LIT>" , False ) ) : <EOL> for process in self . get_processes ( ) : <EOL> self . terminate ( process . unique_id ) </s>
