def useImage(host, vmid, image, forceGzip=False): assert getState(host, vmid) == generic.State.PREPARED, "VM not prepared" assert fileutil.existsFile(host, image), "Image does not exist" dst = _imagePath(vmid) fileutil.delete(host, dst, recursive=True) fileutil.mkdir(host, dst) fileutil.unpackdir(host, image, dst, "-z" if forceGzip else "")
def startCaptureToFile(host, name, iface, filter=""): assert name, "Name not given" assert ifaceutil.interfaceExists(host, iface), "Interface does not exist" assert _checkSyntax(host, iface, filter), "Syntax error: tcpdump -i %s %s" % (iface, filter) rdir = _remoteDir(name) fileutil.mkdir(host, rdir) ifaceutil.ifup(host, iface) _tcpdump(host, "-i %(iface)s -n -C 10 -w %(rdir)s/capture -U -W 5 -s0 %(filter)s >/dev/null 2>&1 </dev/null & echo $! > %(rdir)s.file.pid" % {"iface": util.escape(iface), "rdir": rdir, "filter": util.escape(filter) })
def migrate(src_host, src_vmid, dst_host, dst_vmid, template, ifaces): assert getState(dst_host, dst_vmid) == generic.State.CREATED, "Destination VM already exists" state = getState(src_host, src_vmid) if state == generic.State.CREATED: #nothing to do return #create a tmp directory on both hosts src_tmp = "/tmp/%s" % uuid.uuid1() dst_tmp = "/tmp/%s" % uuid.uuid1() fileutil.mkdir(src_host, src_tmp) fileutil.mkdir(dst_host, dst_tmp) #create destination vm create(dst_host, dst_vmid, template) try: #transfer vm disk image copyImage(src_host, src_vmid, "%s/disk.tar" % src_tmp) fileutil.fileTransfer(src_host, "%s/disk.tar" % src_tmp, dst_host, "%s/disk.tar" % dst_tmp, compressed=True) if state == generic.State.STARTED: #prepare rdiff before snapshot to save time src_host.execute("rdiff signature %(tmp)s/disk.tar %(tmp)s/rdiff.sigs" % {"tmp": src_tmp}) #create a memory snapshot on old host res = _vzctl(src_host, src_vmid, "chkpnt", ["--dumpfile", "%s/openvz.dump" % src_tmp]) assert fileutil.existsFile(src_host, "%s/openvz.dump" % src_tmp), "Failed to create snapshot: %s" % res try: fileutil.fileTransfer(src_host, "%s/openvz.dump" % src_tmp, dst_host, "%s/openvz.dump" % dst_tmp, compressed=True) #create and transfer a disk image rdiff copyImage(src_host, src_vmid, "%s/disk2.tar" % src_tmp) src_host.execute("rdiff -- delta %(tmp)s/rdiff.sigs %(disk)s - | gzip > %(tmp)s/disk.rdiff.gz" % {"tmp": src_tmp, "disk": "%s/disk2.tar" % src_tmp}) fileutil.fileTransfer(src_host, "%s/disk.rdiff.gz" % src_tmp, dst_host, "%s/disk.rdiff.gz" % dst_tmp, direct=True) #patch disk image with the rdiff dst_host.execute("gunzip < %(tmp)s/disk.rdiff.gz | rdiff -- patch %(tmp)s/disk.tar - %(tmp)s/disk-patched.tar" % {"tmp": dst_tmp}) fileutil.move(dst_host,"%s/disk-patched.tar" % dst_tmp, "%s/disk.tar" % dst_tmp) except: _vzctl(src_host, src_vmid, "restore", ["--dumpfile", "%s/openvz.dump" % src_tmp]) raise #use disk image on new host useImage(dst_host, dst_vmid, "%s/disk.tar" % dst_tmp) for iface in ifaces: addInterface(dst_host, dst_vmid, iface) if state == generic.State.STARTED: try: #restore snapshot _vzctl(dst_host, dst_vmid, "restore", ["--dumpfile", "%s/openvz.dump" % dst_tmp]) assert getState(dst_host, dst_vmid) == generic.State.STARTED except: _vzctl(src_host, src_vmid, "restore", ["--dumpfile", "%s/openvz.dump" % src_tmp]) raise except: destroy(dst_host, dst_vmid) fileutil.delete(src_host, src_tmp, recursive=True) fileutil.delete(dst_host, dst_tmp, recursive=True) raise #destroy vm on old host destroy(src_host, src_vmid) #remove tmp directories fileutil.delete(src_host, src_tmp, recursive=True) fileutil.delete(dst_host, dst_tmp, recursive=True)
def stage(): fileutil.rmdir(stageDir) fileutil.mkdir(stageDir) for x in xcopies: dirName = x[0] exclude = x[1] srcDir = os.path.join(env.home, dirName) if os.path.exists(srcDir): fileutil.cpdir(srcDir, os.path.join(stageDir, dirName), exclude)
def init(): log_dir = r'F:\Log' log_file = log_dir + r'\keyboard_hook.log' fileutil.mkdir(log_dir) logging.basicConfig(level=logging.DEBUG, format='%(asctime)s\t%(levelname)s %(filename)s %(funcName)s [line:%(lineno)d]\t%(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=log_file, # 配置日志文件路径 filemode='w') logging.info(" start ".center(80, '_'))
def _createConfigFile(endpoint, mode, connections): path = _tmpPath(endpoint) fileutil.mkdir(util.localhost, path) tincConfFd = open(path+"/tinc.conf", "w") tincConfFd.write ( "Mode=%s\n" % mode ) tincConfFd.write ( "Name=%s\n" % _tincName(endpoint) ) tincConfFd.write ( "AddressFamily=ipv4\n" ) for (src, dst) in connections: if src == endpoint: tincConfFd.write ( "ConnectTo=%s\n" % _tincName(dst) ) tincConfFd.close()
def startCaptureViaNet(host, name, port, iface, filter=""): assert name, "Name not given" assert port, "Port not given" assert ifaceutil.interfaceExists(host, iface), "Interface does not exist" assert process.portFree(host, port), "Port already in use" assert _checkSyntax(host, iface, filter), "Syntax error: tcpdump -i %s %s" % (iface, filter) rdir = _remoteDir(name) fileutil.mkdir(host, rdir) ifaceutil.ifup(host, iface) host.execute("tcpserver -qHRl 0 0 %(port)d tcpdump -i %(iface)s -nUw - '%(filter)s' >/dev/null 2>&1 </dev/null & echo $! > %(rdir)s.net.pid" % {"iface": util.escape(iface), "rdir": rdir, "filter": util.escape(filter), "port": port }) assert not process.portFree(host, port)
def useImage(host, vmid, image, move=False): assert fileutil.existsFile(host, image), "Image file does not exist" assert getState(host, vmid) == generic.State.PREPARED, "VM must be stopped to change image" _qm(host, vmid, "set", "--ide0 undef") imagePath = _imagePath(vmid) fileutil.mkdir(host, _imagePathDir(vmid)) if move: fileutil.move(host, image, imagePath) else: fileutil.copy(host, image, imagePath) fileutil.chown(host, imagePath, "root:root") _qm(host, vmid, "set", "--ide0=local:%s/disk.qcow2" % vmid) assert fileutil.existsFile(host, imagePath)
def jar(): fileutil.mkdir(OUTPATH) fileutil.mkdir(PACKAGE_PATH) # 拷贝TESDK bin 到 OUTPATH ignore_these = ['BuildConfig.class', 'R$attr.class', 'R$drawable.class', 'R$string.class', 'R.class'] def ignore_most(folder, files): return ignore_these shutil.copytree(TE_SDK_BIN_PATH, OUTPATH + "bin", False, ignore_most) # # 考虑使用java原生的命令打.jar包 # jar 命令打包的文件,加压其中包含META-INF # 对照eclipse打包的jar文件,看是否也包含 os.chdir(OUTPATH) os.system('jar cf TEMobile.jar -C ' + TE_SDK_PAGH + ' res/values -C ' + OUTPATH + 'bin .')
def migrate(src_host, src_vmid, dst_host, dst_vmid, ifaces): assert getState(dst_host, dst_vmid) == generic.State.CREATED, "Destination VM already exists" state = getState(src_host, src_vmid) if state == generic.State.CREATED: #nothing to do return #create a tmp directory on both hosts src_tmp = "/tmp/%s" % uuid.uuid1() dst_tmp = "/tmp/%s" % uuid.uuid1() fileutil.mkdir(src_host, src_tmp) fileutil.mkdir(dst_host, dst_tmp) #create destination vm create(dst_host, dst_vmid) #transfer vm disk image fileutil.copy(src_host, _imagePath(src_vmid), "%s/disk.qcow2" % src_tmp) fileutil.fileTransfer(src_host, "%s/disk.qcow2" % src_tmp, dst_host, "%s/disk.qcow2" % dst_tmp, direct=True) if state == generic.State.STARTED: #prepare rdiff before snapshot to save time src_host.execute("rdiff signature %(tmp)s/disk.qcow2 %(tmp)s/rdiff.sigs" % {"tmp": src_tmp}) #create a memory snapshot on old host _monitor(src_host, src_vmid, "stop") _monitor(src_host, src_vmid, "savevm migrate", timeout=900) #stop vm stop(src_host, src_vmid) #create and transfer a disk image rdiff src_host.execute("rdiff -- delta %(tmp)s/rdiff.sigs %(disk)s - | gzip > %(tmp)s/disk.rdiff.gz" % {"tmp": src_tmp, "disk": _imagePath(src_vmid)}) fileutil.fileTransfer(src_host, "%s/disk.rdiff.gz" % src_tmp, dst_host, "%s/disk.rdiff.gz" % dst_tmp) #patch disk image with the rdiff dst_host.execute("gunzip < %(tmp)s/disk.rdiff.gz | rdiff -- patch %(tmp)s/disk.qcow2 - %(tmp)s/disk-patched.qcow2" % {"tmp": dst_tmp}) fileutil.move(dst_host,"%s/disk-patched.qcow2" % dst_tmp, "%s/disk.qcow2" % dst_tmp) #use disk image on new host useImage(dst_host, dst_vmid, "%s/disk.qcow2" % dst_tmp) for iface in ifaces: addInterface(dst_host, dst_vmid, iface) if state == generic.State.STARTED: start(dst_host, dst_vmid) #restore snapshot _monitor(dst_host, dst_vmid, "stop") _monitor(dst_host, dst_vmid, "loadvm migrate", timeout=900) _monitor(dst_host, dst_vmid, "cont") _monitor(dst_host, dst_vmid, "delvm migrate", timeout=900) #destroy vm on old host destroy(src_host, src_vmid) #remove tmp directories fileutil.delete(src_host, src_tmp, recursive=True) fileutil.delete(dst_host, dst_tmp, recursive=True)
def _createHostFile(endpoint): path = _tmpPath(endpoint) fileutil.mkdir(util.localhost, path+"/hosts") util.localhost.execute("openssl genrsa -out %s/rsa_key.priv" % path) hostFd = open("%s/hosts/%s" % (path, _tincName(endpoint)), "w") hostFd.write("Address=%s\n" % endpoint.getHost()) hostFd.write("Port=%s\n" % endpoint.getPort()) hostFd.write("Cipher=none\n") hostFd.write("Digest=none\n") for sn in endpoint.getSubnets(): hostFd.write("Subnet=%s\n" % sn) util.localhost.execute("openssl rsa -pubout -in %s/rsa_key.priv -out %s/hosts/%s.pub" % (path, path, _tincName(endpoint))) hostPubFd = open("%s/hosts/%s.pub" % (path, _tincName(endpoint)), "r") shutil.copyfileobj(hostPubFd, hostFd) hostFd.close() hostPubFd.close() fileutil.delete(util.localhost, "%s/hosts/%s.pub" % (path, _tincName(endpoint)))
def compile(srcDir, depends, packages, jarFile, func=None): print "Compile [" + os.path.basename(jarFile) + "]" # init jarTemp dir temp = os.path.join(env.build, "tempJar") fileutil.rmdir(temp, [], 0) fileutil.mkdir(temp) # compile using jikes.exe print " Javac [" + srcDir + "]" cmd = env.jikes # cmd += " +E +Pno-shadow" cmd += " -d " + temp cmd += " -classpath " + env.jreRt + env.cpSep + srcDir for depend in depends: cmd += env.cpSep + depend for package in packages: cmd += " " + os.path.join(srcDir, package.replace(".", os.path.sep), "*.java") status = os.system(cmd) if status: raise env.BuildError("FATAL: makejar " + jarFile) # if we have a function call it if (func != None): func(temp) func(srcDir) # jar up using jar.exe cmd = env.jar + " cf " + jarFile + " -C " + temp + " ." status = os.system(cmd) if status: raise env.BuildError("FATAL: makejar " + jarFile) # cleanup fileutil.rmdir(temp, [], 0) # success print " Jar [" + jarFile + "]"
if "unknown" in res: return generic.State.CREATED assert False, "Unable to determine kvm state" def useImage(host, vmid, image, move=False): assert fileutil.existsFile(host, image), "Image file does not exist" try: host.execute("qemu-img check -f qcow2 %s; echo $?" % util.escape(image)) except exceptions.CommandError, exc: #Error 1: Leaked clusters, "This means waste of disk space, but no harm to data." if not exc.errorCode in [1]: raise assert getState(host, vmid) == generic.State.PREPARED, "VM must be stopped to change image" _qm(host, vmid, "set", ["--ide0", "undef"]) imagePath = _imagePath(vmid) fileutil.mkdir(host, _imagePathDir(vmid)) if move: fileutil.move(host, image, imagePath) else: fileutil.copy(host, image, imagePath) fileutil.chown(host, imagePath, "root:root") _qm(host, vmid, "set", ["--ide0", "local:%d/disk.qcow2" % vmid]) assert fileutil.existsFile(host, imagePath) def _vncPidfile(vmid): return "%s/vnc-%d.pid" % (config.REMOTE_DIR, vmid) def vncRunning(host, vmid, port): return process.processRunning(host, _vncPidfile(vmid), "tcpserver") def startVnc(host, vmid, port, password):
def create(host, vmid, template): assert getState(host, vmid) == generic.State.CREATED, "VM already exists" fileutil.mkdir(host, "%s/repy" % config.REMOTE_DIR) fileutil.copy(host, _templatePath(template), _imagePath(vmid)) assert getState(host, vmid) == generic.State.PREPARED, "Failed to create VM"
# 加载现有配置文件 conf = configparser.ConfigParser() conf.read("conf.ini", encoding="utf-8") # 读取配置信息 db_host = conf.get('db_config', 'db_host') db_user = conf.get('db_config', 'db_user') db_password = conf.get('db_config', 'db_password') ftp_host = conf.get('ftp_config', 'ftp_host') ftp_port = conf.getint('ftp_config', 'ftp_port') ftp_user = conf.get('ftp_config', 'ftp_user') ftp_password = conf.get('ftp_config', 'ftp_password') ftp_dir = conf.get('ftp_config', 'ftp_dir') download_dir = conf.get('local_dir', 'download_dir') interval = conf.getint('local_dir', 'interval') # 创建下载目录 fileutil.mkdir() # 设置日志文件 fileutil.log_set() i = 0 # 定时同步FTP while True: # 建立一个ftp连接 # ftp = ftpdownload.FtpDownload(ftp_host, ftp_port) # 下载整个目录下的文件 # ftp.download_file_tree(download_dir, ftp_dir, ftp_user, ftp_password) # 关闭ftp连接 # ftp.close() # csv文件导入数据库 db = ImportOracle(db_user, db_password, db_host) db.import_csv(download_dir)