def update_conf(self, conf_file, factory): """ Update sync configuration @param configuration file @param factory id @return True/False """ try: base = 'https://%s' % self.host faclink = '%s/factory' % base resp = self._get_response(faclink, self.cookie) facts = json.loads(resp) for fact in facts: if fact["ident"] == factory: fct = fact break if not fct: return False # logging.debug(fct) addlink = '%s/agent/add_config' % base f = open(conf_file, 'r') cnf = f.read() data = {'_id': fct["_id"], 'config': cnf} # logging.debug(data) resp = self._post_response(addlink, self.cookie, json.dumps(data)) return True except Exception as e: logging.debug("DBG trace %s" % traceback.format_exc()) logging.error(e) return False
def instance_can_upgrade(self, instance_id): try: value = self._call_get('agent/can_upgrade/%s' % instance_id) return value except HTTPError, ex: logging.error("cannot get can_upgrade") return "Error%s" % ex.code
def on_init(protocol, args): """ server side """ from AVMaster import vm_manager vm, mq = protocol.vm, protocol.mq cmd = "c:\\python27\\python.exe" if args: redis = args else: redis = config.redis arg = ["C:\\AVTest\\AVAgent\\av_agent.py", "-m", vm, "-s", mq.session, "-d", redis] ret = vm_manager.execute(vm, "executeCmd", cmd, arg, 40, True, True) #logging.debug("execution: %s" % ret) python = None for i in range(5): time.sleep(10) processes = vm_manager.execute(vm, "list_processes") if not processes: logging.debug("%s: null list_processes" % vm) continue python = [ p for p in processes if "python" in p['cmd_line'] ] logging.debug("processes python: %s" % python) if python: return True if not python: logging.error("Error executing command av_agent on %s" % vm) return False
def _get_response(self, link, cookies=None): """ Basic HTTP Request/Response with Cookie @param link @param cookie @returns response page """ try: #print "calling link: %s" % link req = urllib2.Request(link) req.add_header('Accept-encoding', 'gzip') if cookies: opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookies)) response = opener.open(req) if response.info().get('Content-Encoding') == 'gzip': buf = StringIO(response.read()) f = gzip.GzipFile(fileobj=buf) data = f.read() else: data = response.read() sleep(1) return data except HTTPError as e: logging.error("ERROR: processing %s: %s, %s" % (link, e, e.read())) raise e
def add_plan_result(proj_id, plan_id, config, run_name, test_case, result, elapsed = 0, comment="avg"): logging.debug("adding plan result: %s, %s, %s, %s, %s" % (config, run_name, test_case, result, comment)) statuses = get_statuses() results = dict([ (s['name'],s['id']) for s in statuses]) plan = search_plan(proj_id, plan_id) entries = plan["entries"] for entry in entries: runs = entry["runs"] for r in runs: if r["name"] != run_name: continue #logging.debug("run: %s" % r) if r["config"] != config: continue for t in get_tests(r["id"]): if test_case in t["title"]: logging.debug("adding result for test: %s" % t["id"]) #results = get_results(t["id"]) #logging.debug("results: %s" % results ) add_result(t["id"], results[result], comment, elapsed) return r["id"] logging.error("cannot find correct test case")
def wait_for(task): s = task.wait_for_state(['success', 'error']) if s == 'error': logging.error("ERROR: problem with task %s: %s" % (func, task.get_error_message())) return False return True
def add_plan_result(proj_id, plan_id, config, run_name, test_case, result, elapsed=0, comment="avg"): logging.debug("adding plan result: %s, %s, %s, %s, %s" % (config, run_name, test_case, result, comment)) statuses = get_statuses() results = dict([(s['name'], s['id']) for s in statuses]) plan = search_plan(proj_id, plan_id) entries = plan["entries"] for entry in entries: runs = entry["runs"] for r in runs: if r["name"] != run_name: continue #logging.debug("run: %s" % r) if r["config"] != config: continue for t in get_tests(r["id"]): if test_case in t["title"]: logging.debug("adding result for test: %s" % t["id"]) #results = get_results(t["id"]) #logging.debug("results: %s" % results ) add_result(t["id"], results[result], comment, elapsed) return r["id"] logging.error("cannot find correct test case")
def on_init(protocol, args): """ server side """ from AVMaster import vm_manager vm, mq = protocol.vm, protocol.mq cmd = "c:\\python27\\python.exe" if args: redis = args else: redis = config.redis arg = [ "C:\\AVTest\\AVAgent\\av_agent.py", "-m", vm, "-s", mq.session, "-d", redis ] ret = vm_manager.execute(vm, "executeCmd", cmd, arg, 40, True, True) #logging.debug("execution: %s" % ret) python = None for i in range(5): time.sleep(10) processes = vm_manager.execute(vm, "list_processes") if not processes: logging.debug("%s: null list_processes" % vm) continue python = [p for p in processes if "python" in p['cmd_line']] logging.debug("processes python: %s" % python) if python: return True if not python: logging.error("Error executing command av_agent on %s" % vm) return False
def _get_response(self, link, cookies=None): """ Basic HTTP Request/Response with Cookie @param link @param cookie @returns response page """ try: #print "calling link: %s" % link req = urllib2.Request(link) req.add_header('Accept-encoding', 'gzip') if cookies: opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookies)) response = opener.open(req) if response.info().get('Content-Encoding') == 'gzip': buf = StringIO( response.read()) f = gzip.GzipFile(fileobj=buf) data = f.read() else: data = response.read() sleep(1) return data except HTTPError as e: logging.error("ERROR: processing %s: %s, %s" % (link, e, e.read())) raise e
def receive_client(self, client, blocking=False, timeout=60): assert (isinstance(client, str)) if client not in self.channels.keys(): logging.debug(" MQ error, receiveClient, client (%s) not found: %s" % (client, self.channels)) ch = self.channels[client] message = ch.read(blocking, timeout) if not message: logging.error("TIMEOUT") return message
def receive_client(self, client, blocking=False, timeout=60): assert (isinstance(client, str)) if client not in self.channels.keys(): logging.debug( " MQ error, receiveClient, client (%s) not found: %s" % (client, self.channels)) ch = self.channels[client] message = ch.read(blocking, timeout) if not message: logging.error("TIMEOUT") return message
def _run_cmd(self, func, *params): try: with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: f = getattr(vm, func) if len(params) is None: return f else: return f(*params) except Exception as e: logging.error("%s, ERROR: Problem running %s. Reason: %s" % (self.name, func, e)) raise
def user_create(self, name, password, privs, group_id): """ Create a user """ logging.debug("user_create: %s, %s, %s" % (name, password, group_id)) try: data = {'name': name, 'pass': password, 'group_ids': [group_id], 'privs': privs, 'enabled': True} user = self._call_post('user/create', data) return user['_id'] # return True except HTTPError as e: if e.code == 409: return True logging.error(e) return False
def receive_server(self, blocking=False, timeout=10): #logging.debug(" MQ receive_server") payload = self.channel_to_server.read(blocking, timeout) if not payload: logging.error("TIMEOUT") return None p = re.compile("\('(\w+)', (.+)\)") m = p.match(payload) assert m, "wrong format: %s" % m cmd, args = m.group(1), m.group(2) #logging.debug(" MQ read: %s args: %s" % (str(cmd), str(args))) #client, message = payload return cmd, args
def _post_response(self, link, cj, data=None): """ Basic POST Request / Response @param link @param data @param cookie @returns response page """ try: opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPHandler()) req = urllib2.Request(link, data) resp = opener.open(req).read() sleep(1) return resp except HTTPError as e: logging.error("ERROR: processing %s: %s, %s" % (link, e, e.read())) raise e
def user_create(self, name, password, privs, group_id): """ Create a user """ logging.debug("user_create: %s, %s, %s" % (name, password, group_id)) try: data = { 'name': name, 'pass': password, 'group_ids': [group_id], 'privs': privs, 'enabled': True } user = self._call_post('user/create', data) return user['_id'] # return True except HTTPError as e: if e.code == 409: return True logging.error(e) return False
def _call_post(self, api_name, data={}, binary=False, argjson=True): link = 'https://%s/%s' % (self.host, api_name) logging.debug("_call: %s" % link) #logging.debug("binary %s, argjson %s" % (binary, argjson)) arg = data if argjson: arg = json.dumps(data) resp = self._post_response(link, self.cookie, arg) if binary: return resp try: result = json.loads(resp) return result except Exception, e: logging.error("ERROR: %s" % e) logging.debug("DBG trace %s" % traceback.format_exc()) logging.debug("call error: %s" % resp) raise e
def get_params(self, conf_file): try: self.config = ConfigParser() self.config.read(conf_file) self.path = self.config.get("vms", self.name) self.snapshot = self.config.get("vm_config", "snapshot") self.user = self.config.get("vm_config", "user") self.passwd = self.config.get("vm_config", "passwd") self.sdkhost = self.config.get("vsphere", "host") self.sdkuser = self.config.get("vsphere", "user") self.sdkdomain = self.config.get("vsphere", "domain") self.sdkpasswd = self.config.get("vsphere", "passwd") except NoSectionError: logging.debug("cwd: %s" % os.getcwd()) logging.error("VM or VM stuff not found on %s" % conf_file) raise
def _run_task(self, func, *params): def wait_for(task): s = task.wait_for_state(['success', 'error']) if s == 'error': logging.error("ERROR: problem with task %s: %s" % (func, task.get_error_message())) return False return True try: with vSphere(self.path, self.sdkhost, self.sdkuser, self.sdkdomain, self.sdkpasswd) as vm: f = getattr(vm, func) if len(params) is None: task = f(sync_run=False) else: task = f(sync_run=False, *params) return wait_for(task) except Exception as e: logging.error("%s, ERROR: Problem running %s. Reason: %s" % (self.name, func, e)) raise
def check(): ret = True called = set() system = [] try: for name, p in Procedure.procedures.items(): for c in p.command_list: if c.name == "CALL": call = c.args called.add(call) if call not in Procedure.procedures.keys(): logging.error( "Error in procedure: %s, call to non existant proc: %s" % (name, call)) ret = False if c.name == "REPORT": calls = c.args for c in calls: call = None if isinstance(c, basestring): call = c elif isinstance(c, dict): call = c.keys()[0] else: logging.error( "Error in procedure: %s, call to non compliant proc: %s" % (name, call)) if call: called.add(call) if call not in Procedure.procedures.keys(): logging.error( "Error in procedure: %s, call to non existant proc: %s" % (name, call)) ret = False procs = set(Procedure.procedures.keys()) for p in called: if p.startswith("SYSTEM_"): logging.warn("system proc called: %s" % p) for p in procs.difference(called): good_start = ["TEST", "SYSTEM", "UPDATE"] if not any([p.startswith(g) for g in good_start]): logging.warn("probably unused PROC: %s" % p) else: system.append(p) system.sort() logging.info("Callable Procedures: %s" % system) return ret except: logging.exception("Check")
def check(): ret = True called = set() system = [] try: for name,p in Procedure.procedures.items(): for c in p.command_list: if c.name == "CALL": call = c.args called.add(call) if call not in Procedure.procedures.keys(): logging.error("Error in procedure: %s, call to non existant proc: %s" % (name, call)) ret = False if c.name == "REPORT": calls = c.args for c in calls: call = None if isinstance(c, basestring): call = c elif isinstance(c, dict): call = c.keys()[0] else: logging.error("Error in procedure: %s, call to non compliant proc: %s" % (name, call)) if call: called.add(call) if call not in Procedure.procedures.keys(): logging.error("Error in procedure: %s, call to non existant proc: %s" % (name, call)) ret = False procs = set(Procedure.procedures.keys()) for p in called: if p.startswith("SYSTEM_"): logging.warn("system proc called: %s" % p) for p in procs.difference(called): good_start=["TEST","SYSTEM","UPDATE"] if not any([p.startswith(g) for g in good_start]) : logging.warn("probably unused PROC: %s" % p) else: system.append(p) system.sort() logging.info("Callable Procedures: %s" % system) return ret except: logging.exception("Check")
def execute(vm_name, cmd, *args): global vm_conf_file # pysphere, vi_server vmachine_cmds = ["startup", "shutdown", "reboot", "get_snapshots", "revert_last_snapshot", "revert_to_snapshot", "create_snapshot", "delete_snapshot", "is_powered_on", "is_powered_off", "get_status", "list_directory", "make_directory", "get_file", "send_file", "list_processes"] # vmware tools vmrun_cmds = ["executeCmd", "runTest", "takeScreenshot", "listProcesses", "mkdirInGuest", "copyFileToGuest", "copyFileFromGuest", "deleteDirectoryInGuest", "listDirectoryInGuest", "refreshSnapshot"] if config.verbose: logging.debug("vm: %s, command: %s" % (vm_name, cmd)) try: vm = VMachine(vm_name) vm.get_params(vm_conf_file) assert vm.config if cmd in vmrun_cmds: vmrun = VMRun(vm_conf_file) f = getattr(vmrun, cmd) if not args: return f(vm) else: return f(vm, *args) elif cmd in vmachine_cmds: f = getattr(vm, cmd) if not args: return f() else: return f(args) else: logging.error("command not found: %s" % cmd) raise Exception("Command not found") except AssertionError as ae: logging.error("Assertion found: %s" % ae) raise except Exception as e: logging.error("Exception found. %s" % e) raise
def write(self, message): """ writes a message to the channel. The channel is created automatically """ if config.verbose: logging.debug(" CH write: channel: %s message: %s" % (str(self.channel), str(message))) while(True): pipe = self.redis.pipeline() l1,ret,l2 = pipe.llen(self.channel).rpush(self.channel, message).llen(self.channel).execute() if not ret: logging.error("not ret: %s" % self.channel) continue if not l2>0: logging.error("not l2>0 %s" % self.channel) continue if l1 and not l2 == l1 +1: logging.error("l1 and not l2 == l1 +1: %s" % self.channel) continue break
def write(self, message): """ writes a message to the channel. The channel is created automatically """ if config.verbose: logging.debug(" CH write: channel: %s message: %s" % (str(self.channel), str(message))) while (True): pipe = self.redis.pipeline() l1, ret, l2 = pipe.llen(self.channel).rpush( self.channel, message).llen(self.channel).execute() if not ret: logging.error("not ret: %s" % self.channel) continue if not l2 > 0: logging.error("not l2>0 %s" % self.channel) continue if l1 and not l2 == l1 + 1: logging.error("l1 and not l2 == l1 +1: %s" % self.channel) continue break
logging.debug("DBG: check_static: %s" % src) dst = "%s.copy.exe" % src if os.path.exists(src): logging.debug("Copying %s to %s" % (src, dst)) try: shutil.copy(src, dst) except Exception, ex: logging.exception("Exception file: %s" % src) time.sleep(15) for src in files: dst = "%s.copy.exe" % src if not os.path.exists(src): failed.append(src) logging.error("Not existent file: %s" % src) else: if os.path.exists(dst) and os.path.exists(src): success.append(src) logging.debug("succesful copy %s to %s" % (src, dst)) else: logging.error("cannot copy") failed.append(src) allowed = { 'rcs': ['AVAgent/assets/check/mac_core', 'AVAgent/assets/check/mac_osax'] } for src in files: if not os.path.exists(src): continue
logging.debug("DBG: check_static: %s" % src) dst = "%s.copy.exe" % src if os.path.exists(src): logging.debug("Copying %s to %s" % (src, dst)) try: shutil.copy(src, dst) except Exception, ex: logging.exception("Exception file: %s" % src) time.sleep(15) for src in files: dst = "%s.copy.exe" % src if not os.path.exists(src): failed.append(src) logging.error("Not existent file: %s" % src) else: if os.path.exists(dst) and os.path.exists(src): success.append(src) logging.debug("succesful copy %s to %s" % (src, dst)) else: logging.error("cannot copy") failed.append(src) allowed = { 'rcs': ['AVAgent/assets/check/mac_core', 'AVAgent/assets/check/mac_osax'] } for src in files: if not os.path.exists(src): continue f = open(src) all = f.read() for key in rcs_words: