def readCredentials(file): with open("credentials.txt", "r") as f: for count, line in enumerate(f): # print("Line {}: {}".format(count, line)) newLine = line.split(",") # print(newLine) host = newLine[0] username = newLine[1] password = newLine[2].rstrip() sftp.upload(host, username, password, file)
def check_up(server_num, sftp_conns, localPath, remotePath, fileName, cmdPath, n): if (os.path.isdir(localPath)): os.chdir(os.path.split(localPath)[0]) cmd = 'find ' + localPath + ' -type f | wc -l' for line in os.popen(cmd): file_num = int(line) if (file_num > 15): # input_result = raw_input( '上传文件数量为:%d,建议压缩后再上传(输入y继续上传,输入t打包下载,输入n退出):' %file_num ) input_result = 't' if (input_result == 'y'): sftp.up_files(sftp_conns, localPath, remotePath) elif (input_result == 't'): tar_name = common.getTarName(fileName) if (n != 0): print('开始上传 %s' % tar_name) else: cmd = 'tar -czf %s %s' % (tar_name, fileName) os.system(cmd) print('打包完成,开始上传 %s' % tar_name) sftp.upload(server_num, localPath[0:-len(fileName)] + tar_name, remotePath + tar_name) # input_result2 = raw_input( '上传完成,是否解压(y/n):' ) input_result2 = 'y' if (input_result2 == 'y'): cmd = 'tar -xvf %s' % tar_name print(cmd) result = ssh.cmd(server_num, 'cd ' + cmdPath + ' && ' + cmd) cmd = 'rm %s' % tar_name print(cmd) result = ssh.cmd(server_num, 'cd ' + cmdPath + ' && ' + cmd) else: return else: return else: sftp.up_files(sftp_conns, localPath, remotePath) else: sftp.upload(server_num, localPath, remotePath + fileName)
def check_up(server_num,sftp_conns,localPath,remotePath,fileName,cmdPath,n): if( os.path.isdir( localPath ) ): os.chdir(os.path.split(localPath)[0]) cmd = 'find ' + localPath + ' -type f | wc -l' for line in os.popen(cmd): file_num = int(line) if( file_num > 15): # input_result = raw_input( '上传文件数量为:%d,建议压缩后再上传(输入y继续上传,输入t打包下载,输入n退出):' %file_num ) input_result = 't' if( input_result == 'y'): sftp.up_files(sftp_conns,localPath,remotePath ) elif( input_result == 't'): tar_name = common.getTarName(fileName) if ( n != 0 ): print('开始上传 %s' %tar_name) else: cmd = 'tar -czf %s %s' %(tar_name , fileName) os.system( cmd ) print('打包完成,开始上传 %s' %tar_name) sftp.upload(server_num,localPath[0:-len(fileName)]+tar_name,remotePath + tar_name) # input_result2 = raw_input( '上传完成,是否解压(y/n):' ) input_result2 = 'y' if( input_result2 == 'y'): cmd = 'tar -xvf %s' %tar_name print( cmd ) result = ssh.cmd(server_num,'cd '+cmdPath+' && '+ cmd) cmd= 'rm %s' %tar_name print( cmd ) result = ssh.cmd(server_num,'cd '+cmdPath+' && '+ cmd) else: return else: return else: sftp.up_files(sftp_conns,localPath,remotePath ) else: sftp.upload(server_num,localPath,remotePath + fileName)
def cmd_sync(p_cmd): global server_list sync_info = p_cmd.split('>') data.client_file = {} add_file = {} temp_master = sync_info[0].split(' ') temp_master = list_del_empty(temp_master) master_server = int(temp_master[1]) if len(sync_info) > 1: client_server = sync_info[1].split(' ') client_server = list_del_empty(client_server) else: client_server = list() client_server.extend(server_list) client_server.remove(master_server) master_file = ssh.show_remote_file(master_server, data.paths[master_server]) master_remote_path = data.paths[master_server] if not master_file: print(' 没有需要同步的文件') return scan_documents = [] for server_num in client_server: server_num = int(server_num) scan_documents.append( threading.Thread(target=threads_func.scan_document, args=('scan_document', server_num))) threads_func.threads_handle(scan_documents) is_all_sync_file = False for server_num in client_server: server_num = int(server_num) server_info = data.servers[server_num] print('\33[34m%d:\33[31m%s(%s)\33[0m' % (server_num, server_info['name'], common.hideipFun(server_info['host']))) add_file[server_num] = list() is_sync_file = False for file_name in master_file: if (file_name in data.client_file[server_num]): if (master_file[file_name] > data.client_file[server_num][file_name]): x = time.localtime(master_file[file_name]) mtime = time.strftime('%Y-%m-%d %H:%M:%S', x) print(" 更新: %s 修改时间:%s" % (file_name, mtime)) add_file[server_num].append(file_name) is_all_sync_file = True is_sync_file = True else: x = time.localtime(master_file[file_name]) mtime = time.strftime('%Y-%m-%d %H:%M:%S', x) print(" 添加: %s 修改时间:%s" % (file_name, mtime)) add_file[server_num].append(file_name) is_all_sync_file = True is_sync_file = True if (not is_sync_file): print(' 没有需要同步的文件') if (not is_all_sync_file): return certain = raw_input('确定要同步吗?(y/n):') if (certain != 'y'): return else: files_list = list() for server_num in add_file: client_remote_path = data.paths[server_num] server_info = data.servers[master_server] files_list.extend(add_file[server_num]) files_list = list(set(files_list)) print('\33[34m%d:\33[31m%s@%s(%s)\33[0m 下载中' % (master_server, server_info['user'], server_info['name'], common.hideipFun(server_info['host']))) for file_name in files_list: os.system('mkdir -p "' + source_path + data.servers[master_server]['name'] + '-SYNC/' + file_name[0:file_name.rindex('/')] + '/"') print(' ' + file_name[file_name.index('/') + 1:]) sftp.down( master_server, master_remote_path + '/' + file_name[file_name.index('/') + 1:], source_path + data.servers[master_server]['name'] + '-SYNC/' + file_name) for server_num in add_file: server_info = data.servers[server_num] print('\33[34m%d:\33[31m%s@%s(%s)\33[0m 上传中' % (server_num, server_info['user'], server_info['name'], common.hideipFun(server_info['host']))) for file_name in add_file[server_num]: if (file_name.count('/') > 1): try: cmd = 'mkdir -p "' + client_remote_path + file_name[ file_name.index('/'):file_name.rindex('/')] + '/"' ssh.cmd(server_num, cmd) except (Exception) as e: pass print(' ' + file_name[file_name.index('/') + 1:]) sftp.upload( server_num, source_path + data.servers[master_server]['name'] + '-SYNC/' + file_name, client_remote_path + '/' + file_name[file_name.index('/') + 1:])
logging.error("An error occurred while parsing arguments") exit(1) name = os.path.splitext(os.path.basename(ns.project))[0] logging.basicConfig( level=logging.INFO, filename="/var/log/tilebuilder.log", filemode="a", format=f"[%(levelname)s]:[{name}]:[%(asctime)s] => %(message)s") logging.info(f"Processing {ns.project}") try: filepath = buildmbtiles(ns.project, ns.minzoom, ns.maxzoom, ns.extend, f"/opt/tiles/{name}.mbtiles") except: exit(1) logging.info(f"MBTiles file saved to {filepath}") logging.info("Starting upload of mbtiles file") try: sftp.upload(filepath) except Exception as e: logging.error(str(e)) os.remove(filepath) exit(1) os.remove(filepath) logging.info("Deleting mbtiles file")
def cmd_sync(p_cmd): global server_list sync_info = p_cmd.split( '>' ) data.client_file={} add_file ={} temp_master = sync_info[0].split(' ') temp_master = list_del_empty( temp_master ) master_server = int( temp_master[1] ) if len(sync_info) > 1 : client_server = sync_info[1].split(' ') client_server = list_del_empty( client_server ) else: client_server =list() client_server.extend( server_list ) client_server.remove( master_server ) master_file = ssh.show_remote_file( master_server, data.paths[ master_server ]) master_remote_path = data.paths[master_server] if not master_file: print(' 没有需要同步的文件') return scan_documents = [] for server_num in client_server: server_num = int(server_num) scan_documents.append( threading.Thread(target=threads_func.scan_document,args=('scan_document',server_num)) ) threads_func.threads_handle(scan_documents) is_all_sync_file = False for server_num in client_server: server_num = int(server_num) server_info = data.servers[ server_num ] print( '\33[34m%d:\33[31m%s(%s)\33[0m' %( server_num,server_info['name'], common.hideipFun(server_info['host']) )) add_file[ server_num ] =list() is_sync_file= False for file_name in master_file: if( file_name in data.client_file[server_num] ): if( master_file[ file_name ] > data.client_file[ server_num ][ file_name ]): x = time.localtime( master_file[ file_name ] ) mtime = time.strftime('%Y-%m-%d %H:%M:%S',x) print( " 更新: %s 修改时间:%s" %( file_name, mtime) ) add_file[ server_num ].append(file_name) is_all_sync_file =True is_sync_file = True else: x = time.localtime( master_file[ file_name ] ) mtime = time.strftime('%Y-%m-%d %H:%M:%S',x) print( " 添加: %s 修改时间:%s"%( file_name, mtime) ) add_file[ server_num ].append(file_name) is_all_sync_file =True is_sync_file = True if( not is_sync_file ): print(' 没有需要同步的文件') if( not is_all_sync_file ): return certain = raw_input('确定要同步吗?(y/n):') if( certain !='y'): return else: files_list =list() for server_num in add_file: client_remote_path = data.paths[server_num] server_info = data.servers[ master_server ] files_list.extend(add_file[ server_num ]) files_list = list( set(files_list) ) print( '\33[34m%d:\33[31m%s@%s(%s)\33[0m 下载中' %( master_server,server_info['user'], server_info['name'], common.hideipFun(server_info['host']) )) for file_name in files_list: os.system('mkdir -p "'+ source_path+data.servers[master_server]['name']+'-SYNC/' +file_name[0:file_name.rindex('/')] + '/"') print(' ' + file_name[file_name.index('/')+1:]) sftp.down( master_server, master_remote_path +'/'+file_name[file_name.index('/')+1:], source_path+data.servers[master_server]['name']+'-SYNC/'+file_name ) for server_num in add_file: server_info = data.servers[ server_num ] print( '\33[34m%d:\33[31m%s@%s(%s)\33[0m 上传中' %( server_num,server_info['user'], server_info['name'], common.hideipFun(server_info['host']) )) for file_name in add_file[server_num]: if(file_name.count('/') > 1): try: cmd = 'mkdir -p "' + client_remote_path + file_name[ file_name.index('/'):file_name.rindex('/')] + '/"' ssh.cmd(server_num, cmd) except (Exception) as e: pass print(' ' + file_name[file_name.index('/')+1:]) sftp.upload( server_num, source_path+data.servers[master_server]['name']+'-SYNC/'+file_name, client_remote_path +'/'+file_name[file_name.index('/')+1:] )