def start_proc(proc, body): """Start individual process as specified in startClusterReq command. proc - the process object in the message body - the whole message """ f = proc['file'] (user, pwd) = get_cred(body) with produce_ABClusterHost(f['hostName'], user, pwd) as ch: pc = proc['procCtrl'] params = proc['params'] if f.has_key('autoComplete'): if isinstance(f['autoComplete'], list): executable = ch.auto_complete(f['path'], f['autoComplete'], f['name']) else: executable = ch.auto_complete(f['path'], ['bin', 'sbin', 'scripts', '', ch.path_module.join('..','scripts')], f['name']) else: executable = ch.path_module.join(f['path'], f['name']) stdinFile = None if f.has_key('stdinFile'): assert (ch.file_exists(f['stdinFile'])), 'File ' + f['stdinFile'] + " does not exist on host " + ch.host stdinFile = f['stdinFile'] _logger.debug('Attempting to launch '+executable+' on '+ch.host+ ' with pc='+str(pc)) cmdv = util.params_to_cmdv(executable, params) if proc.has_key('isCommand'): return ch.execute_command(cmdv, stdinFile) return ch.exec_cmdv(cmdv, pc, stdinFile)
def handle_createFileReq(req, body): """Handler function for createFileReq commands. Creates a file on the remote host with content from the message. req - top level message object body - shortcut to the body part of the message """ (user, pwd) = get_cred(body) f = body['file'] with produce_ABClusterHost(f['hostName'], user, pwd) as ch: pathname = f['path'] if f.has_key('name'): pathname = ch.path_module.join(f['path'], f['name']) assert not (f.has_key('autoComplete') and f['autoComplete']) assert not (not (f.has_key('overwrite') and f['overwrite']) and ch.file_exists(pathname)), 'File '+pathname+' already exists on host '+ch.host ch.mkdir_p(f['path']) with ch.open(pathname, 'w+') as rf: rf.write(body['contentString']) with ch.open(pathname) as rf: assert rf.read() == body['contentString'] else: ch.mkdir_p(f['path']) _logger.debug('pathname ' + pathname + ' created') return make_rep(req)
def handle_createFileReq(req, body): """Handler function for createFileReq commands. Creates a file on the remote host with content from the message. req - top level message object body - shortcut to the body part of the message There is a problem with this function on Windows: Since pathname = ch.path_module.join(pathname, f['name']) DIRECTORY pathname instead of file PATH/NAME is created leading to Access violation :-/ """ f = body['file'] (key_based, user, pwd, key_file) = get_cred(f['hostName'], body) with produce_ABClusterHost(f['hostName'], key_based, user, pwd, key_file) as ch: pathname = f['path'] if f.has_key('name'): pathname = ch.path_module.join(pathname, f['name']) assert not (f.has_key('autoComplete') and f['autoComplete']) assert not ( not (f.has_key('overwrite') and f['overwrite']) and ch.file_exists(pathname) ), 'File ' + pathname + ' already exists on host ' + ch.host ch.mkdir_p(f['path']) with ch.open(pathname, 'w+') as rf: rf.write(body['contentString']) with ch.open(pathname) as rf: assert rf.read() == body['contentString'] else: ch.mkdir_p(f['path']) _logger.debug('pathname ' + pathname + ' created') return make_rep(req)
def handle_createFileReq(req, body): """Handler function for createFileReq commands. Creates a file on the remote host with content from the message. req - top level message object body - shortcut to the body part of the message """ (user, pwd) = get_cred(body) f = body['file'] with produce_ABClusterHost(f['hostName'], user, pwd) as ch: pathname = f['path'] if f.has_key('name'): pathname = ch.path_module.join(f['path'], f['name']) assert not (f.has_key('autoComplete') and f['autoComplete']) assert not ( not (f.has_key('overwrite') and f['overwrite']) and ch.file_exists(pathname) ), 'File ' + pathname + ' already exists on host ' + ch.host ch.mkdir_p(f['path']) with ch.open(pathname, 'w+') as rf: rf.write(body['contentString']) with ch.open(pathname) as rf: assert rf.read() == body['contentString'] else: ch.mkdir_p(f['path']) _logger.debug('pathname ' + pathname + ' created') return make_rep(req)
def handle_fileExistsReq(req, body): """Handler function for fileExistsReq command. req - top level message object body - shortcut to the body part of the message Plain file exists on host or not. """ path = body['path'] filename = body['fname'] resStr = "" (key_based, user, pwd, key_file) = get_cred(body['hostName'], body) with produce_ABClusterHost(body['hostName'], key_based, user, pwd, key_file) as ch: _logger.warning('Produced ABC (FER).') if (path == "~"): path = ch.homedir sp = ch.path_module.join(path, filename) _logger.warning('pathname ' + sp + ' in checking.') resStr = ch.file_exists(sp) if not (resStr is None): result = 1 else: result = 0 _logger.warning('FER result is ' + str(result) + '.') return make_rep( req, { 'host': { 'name': ch.host }, 'hostRes': { 'fname': filename, 'exists': result } })
def handle_appendFileReq(req, body): """Handler function for appendFileReq commands. Opens two files on the remote host, copies from source and appends to destination. req - top level message object body - shortcut to the body part of the message """ (user, pwd) = get_cred(body) assert (body.has_key('sourceFile') and body.has_key('destinationFile')) sf = body['sourceFile'] df = body['destinationFile'] assert (sf.has_key('path') and sf.has_key('name') and sf.has_key('hostName')) assert (df.has_key('path') and df.has_key('name') and df.has_key('hostName')) with produce_ABClusterHost(sf['hostName'], user, pwd) as ch: sp = ch.path_module.join(sf['path'], sf['name']) dp = ch.path_module.join(df['path'], df['name']) assert (ch.file_exists(dp) ), 'File ' + dp + ' does not exist on host ' + ch.host content = None with ch.open(sp) as sourceFile: content = sourceFile.read() assert (ch.file_exists(sp) ), 'File ' + sp + ' does not exist on host ' + ch.host with ch.open(dp, 'a+') as destinationFile: destinationFile.write(content) return make_rep(req)
def handle_readCfgFileReq(req, body): """Handler function for readCfgFileReq command. req - top level message object body - shortcut to the body part of the message Plain file read on host. Body: hostName path fname phr Ignore passphrase for now. """ path = body['path'] #name = body['fname'] global passphrase global configFile #if passphrase is None: passphrase = body['phr'] #if configFile is None: configFile = body['fname'] (key_based, user, pwd, key_file) = get_cred(body['hostName'], body) with produce_ABClusterHost(body['hostName'], key_based, user, pwd, key_file) as ch: _logger.warning('Inside produce_ABClusterHost, readcfgfilereq.') if (path == "~"): path = ch.homedir pathname = ch.path_module.join(path, configFile) _logger.warning('pathname ' + pathname + ' in opening.') with open(pathname, 'rb') as rencf: inp = rencf.read() res = decrypt(passphrase, inp) _logger.warning('File ' + pathname + ' read.') return make_rep(req, { 'host': {'name': ch.host }, 'hostRes': {'fname': configFile, 'contentString': res}})
def handle_listDirectoryReq(req, body): """Handler function for listDirectoryReq command. req - top level message object body - shortcut to the body part of the message """ # This is what new session starts with so reset globals from previous run. global passphrase global configFile passphrase = None configFile = None _logger.warning('Passphrase & ConfigFileName reset.') f = body['file'] results = [] path = f['path'] ext = f['ext'] (key_based, user, pwd, key_file) = get_cred(f['hostName'], body) with produce_ABClusterHost(f['hostName'], key_based, user, pwd, key_file) as ch: if (path == "~"): path = ch.homedir #os.path.expanduser(path) path = os.path.join(path, '') #Add last /... Well, IF MCC is not running on localhost, this will fail. Will fix later if needs be. results += [each for each in os.listdir(path) if (each.endswith(ext) and os.path.getsize(path+each) > 10)] return make_rep(req, { 'host': {'name': ch.host }, 'hostRes': {'listDirectory': results, 'realpath': path.replace("\\","/")}})
def handle_hostInfoReq(req, body): """Handler function for hostInfoReq commands. Will connect to the specified host through a remote.ClusterHost object to retrieve information. req - top level message object body - shortcut to the body part of the message """ (key_based, user, pwd, key_file) = get_cred(body['hostName'], body) with produce_ABClusterHost(body['hostName'], key_based, user, pwd, key_file) as ch: return make_rep( req, { 'host': { 'name': ch.host }, 'hostRes': { 'ram': ch.ram, 'cores': ch.cores, 'uname': ch.hostInfo.uname, 'installdir': ch.installdir, 'datadir': ch.hostInfo.pm.join(ch.homedir, 'MySQL_Cluster'), 'diskfree': ch.hostInfo.disk_free, 'fqdn': socket.getfqdn(ch.host) } })
def handle_appendFileReq(req, body): """Handler function for appendFileReq commands. Opens two files on the remote host, copies from source and appends to destination. req - top level message object body - shortcut to the body part of the message """ (user, pwd) = get_cred(body) assert (body.has_key('sourceFile') and body.has_key('destinationFile')) sf = body['sourceFile'] df = body['destinationFile'] assert (sf.has_key('path') and sf.has_key('name') and sf.has_key('hostName')) assert (df.has_key('path') and df.has_key('name') and df.has_key('hostName')) with produce_ABClusterHost(sf['hostName'], user, pwd) as ch: sp = ch.path_module.join(sf['path'], sf['name']) dp = ch.path_module.join(df['path'], df['name']) assert (ch.file_exists(dp)), 'File ' + dp + ' does not exist on host ' + ch.host content = None with ch.open(sp) as sourceFile: content = sourceFile.read() assert (ch.file_exists(sp)), 'File ' + sp + ' does not exist on host ' + ch.host with ch.open(dp, 'a+') as destinationFile: destinationFile.write(content) return make_rep(req)
def handle_createCfgFileReq(req, body): """Handler function for createCfgFileReq command. req - top level message object body - shortcut to the body part of the message Plain file create on host. """ global passphrase global configFile path = body['path'] #if passphrase is None: passphrase = body['phr'] configFile = body['fname'] (key_based, user, pwd, key_file) = get_cred(body['hostName'], body) with produce_ABClusterHost(body['hostName'], key_based, user, pwd, key_file) as ch: if (path == "~"): path = ch.homedir _logger.warning('path is ' + path + ' and name is ' + configFile + '.') pathname = ch.path_module.join(path, configFile) if (body.has_key('contentString')): with ch.open(pathname, 'w+') as rf: rf.write(body['contentString']) with ch.open(pathname) as rf: assert rf.read() == body['contentString'] #encrypt(getKey(passphrase), pathname) #""" output_file = pathname + ".encrypted" with open(pathname, 'r') as inputfile: inp = inputfile.read() res = encrypt(passphrase, inp) with open(output_file, 'wb') as outf: outf.write(res) #Remove plain text file. if os.path.isfile(pathname): os.remove(pathname) else: #Show an error. print("Encrypt error: Original file %s not found" % pathname) #Rename new file to old name. old_file = output_file #name.mcc.encrypted. new_file = pathname #name.mcc os.rename(old_file, new_file) #""" _logger.warning('File ' + pathname + ' created.') return make_rep( req, { 'host': { 'name': ch.host }, 'hostRes': { 'fname': configFile, 'created': 1 } })
def handle_getConfigIni(req, body): (user, pwd) = get_cred(body) cf = body['configFile'] assert (cf.has_key('path') and cf.has_key('name') and cf.has_key('hostName')) with produce_ABClusterHost(sf['hostName'], user, pwd) as ch: sp = ch.path_module.join(sf['path'], sf['name']) assert (ch.file_exists(sp)), 'File ' + sp + ' does not exist on host ' + ch.host with ch.open(sp) as ini: return make_rep(req, {'config': config_parser.parse_cluster_config_ini_(ini)})
def handle_hostDockerReq(req, body): """Handler function for hostDockerReq command. Will connect to the specified host through a remote.ClusterHost object to retrieve information. req - top level message object body - shortcut to the body part of the message """ (key_based, user, pwd, key_file) = get_cred(body['hostName'], body) with produce_ABClusterHost(body['hostName'], key_based, user, pwd, key_file) as ch: return make_rep(req, { 'host': {'name': ch.host }, 'hostRes': {'DockerInfo':ch.hostInfo.docker_info}})
def handle_getNdbConfig(req, body): (user, pwd) = get_cred(body) with produce_ABClusterHost(body['hostName'], user, pwd) as ch: ndb_config = ch.path_module.join(body['installpath'], 'ndb_config') return make_rep( req, { 'ndb_config': json.dumps( util.xml_to_python( ch.exec_cmdv([ndb_config, '--configinfo', '--xml']))) })
def handle_hostInfoReq(req, body): """Handler function for hostInfoReq commands. Will connect to the specified host through a remote.ClusterHost object to retrieve information. req - top level message object body - shortcut to the body part of the message """ (user, pwd) = get_cred(body) with produce_ABClusterHost(body['hostName'], user, pwd) as ch: return make_rep(req, { 'host': {'name': ch.host }, 'hostRes': {'ram':ch.ram, 'cores': ch.cores, 'uname': ch.hostInfo.uname, 'installdir': ch.installdir, 'datadir': ch.hostInfo.pm.join(ch.homedir, 'MySQL_Cluster') }})
def handle_getLogTailReq(req, body): """Handler function for getLogTailReq commands. Opens a file on the remote host and adds content to reply req - top level message object body - shortcut to the body part of the message """ (user, pwd) = get_cred(body) sf = body['logFile'] assert (sf.has_key('path') and sf.has_key('name') and sf.has_key('hostName')) with produce_ABClusterHost(sf['hostName'], user, pwd) as ch: sp = ch.path_module.join(sf['path'], sf['name']) assert (ch.file_exists(sp)), 'File ' + sp + ' does not exist on host ' + ch.host with ch.open(sp) as logFile: return make_rep(req, {'tail': logFile.read()})
def handle_checkFileReq(req, body): """Handler function for checkFileReq commands. Check if a file exists on a remote host. req - top level message object body - shortcut to the body part of the message """ f = body['file'] (key_based, user, pwd, key_file) = get_cred(f['hostName'], body) with produce_ABClusterHost(f['hostName'], key_based, user, pwd, key_file) as ch: sp = ch.path_module.join(f['path'], f['name']) _logger.warning('pathname ' + sp + ' in checking') assert (ch.file_exists(sp)), 'File ' + sp + ' does not exist on host ' + ch.host _logger.debug('pathname ' + sp + ' checked') return make_rep(req)
def mock_ABClusterHost(hle): return produce_ABClusterHost(hostname=hle['hostInfoRep']['host']['name'], user=util.get_val(hle, 'username'), pwd=util.get_val(hle, 'password'))
def handle_getNdbConfig(req, body): (user, pwd) = get_cred(body) with produce_ABClusterHost(body['hostName'], user, pwd) as ch: ndb_config = ch.path_module.join(body['installpath'], 'ndb_config') return make_rep(req, { 'ndb_config': json.dumps(util.xml_to_python(ch.exec_cmdv([ndb_config, '--configinfo', '--xml']))) })