コード例 #1
0
ファイル: osaFilelib.py プロジェクト: briankang/osa
def SaveConfigFile(ip,cname,port):
	'''
	@ip 远程IP地址
	@cname 配置文件名称
	'''
	
	cfile = CFILENAME[cname]
	fsize = None
	
	try:
		fsize = file_recv_main(host='0.0.0.0',port=int(port),filename = cfile)
	except Exception as e:
		save_log('ERROR',str(e))
		return False

	if fsize:
		os.system('/usr/bin/dos2unix -k '+ cfile)
		return 'SYSTEM_RUN_COMMAND!{"saveconfigfile":"saveok"}'
	
	return None
コード例 #2
0
def batchFileRecv(rev):
    '''
	@文件接收函数
	'''

    #文件保存目标位置
    citem = osaBatchLib.getConfigItem(rev)
    dfile = citem['targetpath']
    #文件名
    dfilename = os.path.basename(dfile)
    #文件目录
    fdir = os.path.dirname(dfile)
    #目标目录不存在则创建
    if not os.path.exists(fdir):
        try:
            os.makedirs(fdir)
        except Exception as e:
            save_log('ERROR', 'mkdir is error:' + str(e))
            return "{'status':'ERROR','result':'x0027'}"

    #端口号
    fileport = osaBatchLib.getConfigPort(rev)

    #临时文件
    tempfile = osaBatchLib.tempFilePath(rev)
    #开始接收文件
    try:
        fsize = file_recv_main(host='0.0.0.0',
                               port=int(fileport),
                               filename=tempfile)
    except Exception as e:
        save_log('ERROR', 'file recv error:' + str(e))
        return "{'status':'ERROR','result':'x0026'}"

    if fsize:
        #对文件进行处理
        advance = citem['advance'].split('|')
        backfile = dfile + '.bak.' + str(
            time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime()))
        if advance[0] == 'cut':
            if os.path.exists(dfile):
                try:
                    os.rename(dfile, backfile)
                    os.rename(tempfile, dfile)
                except Exception as e:
                    save_log('ERROR', str(e))
                    return "{'status':'ERROR','result':'x0022'}"

            else:
                try:
                    os.rename(tempfile, dfile)
                except:
                    shutil.copy(tempfile, dfile)
        elif advance[0] == 'copy':
            if os.path.exists(dfile):
                try:
                    os.rename(dfile, backfile)
                    os.rename(tempfile, dfile)
                    os.remove(backfile)
                except Exception as e:
                    save_log('ERROR', str(e))
                    return "{'status':'ERROR','result':'x0023'}"

            else:
                try:
                    os.rename(tempfile, dfile)
                except:
                    shutil.copy(tempfile, dfile)
        else:
            return "{'status':'ERROR','result':'x0021'}"
        if advance[1] == 'document_integrity':
            dict = osaBatchLib.revToDict(rev)
            if dict['md5'] != osaBatchLib.md5sum(dfile):
                return "{'status':'ERROR','result':'x0024'}"
        #执行脚本或者指令
        scmd = citem['distribution_script']
        sr = osaBatchLib.scriptOrCmd(scmd)
        if sr:
            try:
                result = osaBatchLib.runCmdOrScript(scmd)
                return "{'status':'OK','result':'" + result + "'}"
            except Exception as e:
                save_log('ERROR', str(e))
                result = 'Cmd or Script is Faild!'
                return "{'status':'ERROR','result':'" + result + "'}"
        return "{'status':'OK','result':'OK'}"
    return "{'status':'ERROR','result':'x0025'}"
コード例 #3
0
def batchFileRecv(rev):
	'''
	@文件接收函数
	'''
	
	#文件保存目标位置
	citem = osaBatchLib.getConfigItem(rev)
	dfile = citem['targetpath']
	#文件名
	dfilename = os.path.basename(dfile)
	#文件目录
	fdir = os.path.dirname(dfile)
	#目标目录不存在则创建
	if not os.path.exists(fdir):
		try:
			os.makedirs(fdir)
		except Exception as e:
			save_log('ERROR','mkdir is error:'+str(e))
			return "{'status':'ERROR','result':'x0027'}"
			
			
		
	#端口号
	fileport = osaBatchLib.getConfigPort(rev)
	
	#临时文件
	tempfile = osaBatchLib.tempFilePath(rev)
	#开始接收文件
	try:
		fsize = file_recv_main(host='0.0.0.0',port=int(fileport),filename = tempfile)
	except Exception as e:
		save_log('ERROR','file recv error:'+str(e))
		return "{'status':'ERROR','result':'x0026'}"	
		
	if fsize:
		#对文件进行处理
		advance = citem['advance'].split('|')
		backfile = dfile+'.bak.'+str(time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime()))
		if advance[0] == 'cut':
			if os.path.exists(dfile):
				try:
					os.rename(dfile,backfile)
					os.rename(tempfile,dfile)
				except Exception as e:			
					save_log('ERROR',str(e))
					return "{'status':'ERROR','result':'x0022'}"
					
			else:
				try:
					os.rename(tempfile,dfile)
				except:
					shutil.copy(tempfile,dfile)
		elif advance[0] == 'copy':
			if os.path.exists(dfile):
				try:
					os.rename(dfile,backfile)
					os.rename(tempfile,dfile)
					os.remove(backfile)
				except Exception as e:
					save_log('ERROR',str(e))
					return "{'status':'ERROR','result':'x0023'}"
					
			else:
				try:
					os.rename(tempfile,dfile)
				except:
					shutil.copy(tempfile,dfile)
		else:
				return "{'status':'ERROR','result':'x0021'}"
		if advance[1] == 'document_integrity':
			dict = osaBatchLib.revToDict(rev)
			if dict['md5'] != osaBatchLib.md5sum(dfile):
				return "{'status':'ERROR','result':'x0024'}"
		#执行脚本或者指令
		scmd = citem['distribution_script']
		sr = osaBatchLib.scriptOrCmd(scmd)
		if sr:
			try:
				result = osaBatchLib.runCmdOrScript(scmd)
				return "{'status':'OK','result':'"+result+"'}"
			except Exception as e:
				save_log('ERROR',str(e))
				result = 'Cmd or Script is Faild!'
				return "{'status':'ERROR','result':'"+result+"'}"	
		return "{'status':'OK','result':'OK'}"
	return "{'status':'ERROR','result':'x0025'}"