def _push(self, instance_list, host_list, pattern): # check all machine whether has agent finish_host_status = {} for host in host_list: finish_host_status[host] = DeployFinishStatus() cmd = 'test -e ' + util.get_remote_deploy_file( self.gcm_data.deploy_info.dst_root_path) ssh.ssh_cmd(host, self.user, self.password, cmd) pack_repo = util.pack_repo_files( self.gcm_data.artifact_map, self.gcm_data.deploy_info.tmp_root_path, self.src_path, instance_list, util.get_repo_name()) dst_root_path = self.gcm_data.deploy_info.dst_root_path remote_repo = util.get_remote_repo(dst_root_path) self._real_push(host_list[0], pack_repo, remote_repo) # wait finish_host_status[host_list[0]].finish = True while True: finish_cnt = 0 for key, value in finish_host_status.items(): if value.finish: finish_cnt += 1 if not value.show: LogInfo('host: %s is finish' % key) value.show = True if finish_cnt == len(finish_host_status): break time.sleep(1) LogInfo('success to finish')
def ssh_upload_repo(ip, user, passwd, src_file, dst_file): try: dirname = os.path.dirname(dst_file) ssh_cmd(ip, user, passwd, 'mkdir -p ' + dirname) proccess = ShowProcess(100) transport = paramiko.Transport((ip, 22)) transport.connect(username=user, password=passwd) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(src_file, dst_file, callback=upload_proccess(proccess)) sftp.close() LogInfo('success to upload %s to %s@%s:%s' % (src_file, user, ip, dst_file)) except BaseException as err: LogError('fail to upload %s to %s@%s:%s' % (src_file, user, ip, dst_file)) raise err
def ssh_cmd(ip, user, passwd, cmd): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(ip, 22, user, passwd, timeout=30) stdin, stdout, stderr = client.exec_command(cmd) ret = False if stdout.channel.recv_exit_status() == 0: LogInfo('success to: ' + cmd) ret = True else: LogError('fail to: ' + cmd) for out in stderr.readlines(): LogError(out) client.close() if not ret: raise BaseException()
def gen_py_from_pb(proto_path): files = os.listdir(proto_path) for file in files: if not file.endswith('.proto'): continue pre_file = file[:len(file) - len('.proto')] pre_file += '_pb2.py' pre_file = '../proto/' + pre_file if os.path.exists(pre_file): pass if platform.system() == "Windows": command = ".\\bin\\protoc.exe --proto_path=./deploy/proto --python_out=./deploy/proto/ " + file else: command = "protoc --proto_path=./deploy/proto --python_out=./deploy/proto " + file try: LogInfo(command) os.system(command) except BaseException: LogError('protoc %s failed' % file) raise
def copy(self, source_host, host_list): LogInfo( '\n\n==================================== copy %s ====================================' % host_list) pass
def clean(self, pattern): LogInfo( '\n\n==================================== clean %s ====================================' % pattern) self.do_cmd('clean', pattern)
def reload(self, pattern): LogInfo( '\n\n==================================== reload %s ====================================' % pattern) self.do_cmd("reload", pattern)
def push(self, pattern): LogInfo( '\n\n==================================== push %s ====================================' % pattern) self.do_cmd("push", pattern)
def check(self, pattern): LogInfo( '\n\n==================================== check %s ====================================' % pattern) self.do_cmd("check", pattern)
def stop(self, pattern): LogInfo( '\n\n==================================== stop %s ====================================' % pattern) self.do_cmd('stop', pattern)
def start(self, pattern): LogInfo( '\n\n==================================== start %s ====================================' % pattern) self.do_cmd('start', pattern)
def push_agent(self, pattern): LogInfo( '\n\n==================================== push_agent %s ====================================' % pattern) self.do_cmd('push_agent', pattern)