def getPid(ppid, name): result = command.exec(["ps", "-f", "-o", "pid,cmd", "--ppid", str(ppid)]) result = result.stdout.decode("utf-8") m = re.search(".*{}.*".format(name), result) return m.group(0).strip().split(" ")[0] if m else ""
def getActiveCmdType(self): result = command.exec([CmdExecuter.cmd_processlist], shell=True) stdout = result.stdout.decode("utf-8") active_cmd_type = None for term in CmdExecuter.process_mapping: if "{} ".format(term) in stdout: active_cmd_type = CmdExecuter.process_mapping[term] break return active_cmd_type
def __init__(self, plugin_config, global_config): self.repository = plugin_config['repository'] if self.repository.find("/") == -1: self.repository = "library/{}".format(self.repository) self.pattern = plugin_config['pattern'] if Repository.repositories is None: Repository.repositories = {} result = command.exec(["/usr/bin/docker", "image", "list"]) lines = result.stdout.decode("utf-8").split("\n") for line in lines: columns = line.split() if len(columns) == 0: continue if columns[0] not in Repository.repositories: Repository.repositories[columns[0]] = [] Repository.repositories[columns[0]].append({ 'tag': columns[1], 'image': columns[2] }) data_r = Repository.repositories[plugin_config['repository']] version = None tag = None tag_r = [] for data in data_r: _version = Version.parseVersionString(data['tag'], self.pattern) if _version is not None and (version is None or version.compare(_version) == 1): version = _version tag = data['tag'] tag_r.append(data['tag']) if version: self.current_version = version.getVersionString() self.current_tag = tag else: raise Exception( 'Can\'t find current version with pattern \'{}\'. Available versions are {}' .format(self.pattern, tag_r)) url = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:{}:pull".format( self.repository) token_result = self._requestData(url) self.token = token_result['token']
def getUpdates(self): result = command.exec(["/usr/bin/zypper", "list-updates"]) lines = result.stdout.decode("utf-8").strip().split("\n") lines = reversed(lines) updates = [] for line in lines: columns = line.split(" | ") if len(columns) < 5: break columns = [ele.strip() for ele in columns] updates.append({ 'repository': columns[1], 'name': columns[2], 'current': columns[3], 'update': columns[4], 'arch': columns[5] }) return updates
def getUpdates(self): #apt list --upgradable result = command.exec([ "/usr/bin/apt", "list", "--upgradable" ]) lines = result.stdout.decode("utf-8").strip().split("\n") updates = [] for line in lines: columns = line.split(" ") #print(columns) #print(len(columns)) if len(columns) != 6: continue name, repo = columns[0].split("/") _repo = repo.split(",") updates.append({'repository': _repo[0], 'name': name, 'current': columns[5][:-1], 'update': columns[1], "arch": columns[2] }) return updates
def getUpdates(self): result = command.exec(["/usr/bin/dnf", "-y", "list updates"]) lines = result.stdout.decode("utf-8").strip().split("\n") lines = reversed(lines) updates = [] for line in lines: columns = re.sub(r"\s+", " ", line).split(" ") if len(columns) != 3: continue columns = [ele.strip() for ele in columns] name, arch = columns[0].rsplit('.', 1) updates.append({ 'repository': columns[2], 'name': name, 'current': None, 'update': columns[1], "arch": arch }) return updates
def process(self, update_time): smartserver_code = None smartserver_pull = None smartserver_changes = None if self.deployment_state is None: smartserver_code = "missing" else: # git add files (intent to add) command.exec([ "git", "add", "-N", "*" ], cwd=self.config.deployment_directory ) result = command.exec([ "git", "diff-index", "--name-status", "origin/master" ], cwd=self.config.deployment_directory ) uncommitted_changes = result.stdout.decode("utf-8").strip().split("\n") deployment_stat = os.stat(self.config.deployment_state_file) deployment_mtime = deployment_stat.st_mtime if len(uncommitted_changes) == 1 and uncommitted_changes[0] == "": can_pull = False if "github" in self.config.git_remote: result = command.exec([ "git", "ls-remote", self.config.git_remote ], cwd=self.config.deployment_directory ) commits = result.stdout.decode("utf-8").strip().split("\n") last_git_hash = commits[0].split("\t")[0] repository_owner = GitHub.getRepositoryOwner(self.config.git_remote) result = GitHub.getStates(repository_owner,last_git_hash) states = Counter(result.values()) if "failure" in states: smartserver_code = "failed" elif "pending" in states or "success" not in states: smartserver_code = "pending" else: can_pull = True smartserver_code = "pulled_tested" else: can_pull = True smartserver_code = "pulled_untested" if can_pull: result = command.exec([ "git", "pull" ], cwd=self.config.deployment_directory ) if result.returncode != 0: raise Exception(result.stdout.decode("utf-8")) smartserver_pull = update_time; else: smartserver_code = "uncommitted" last_deployment = datetime.fromtimestamp(deployment_mtime, tz=timezone.utc) #last_deployment = "2020-01-20 14:02:00.651984+00:00" #print( " ".join([ "git", "-C", self.config.deployment_directory, "rev-list", "-1", "--before", str(last_deployment), "origin/master" ])) result = command.exec([ "git", "rev-list", "-1", "--before", str(last_deployment), "origin/master" ], cwd=self.config.deployment_directory ) ref = result.stdout.decode("utf-8").strip() #print( " ".join([ "git", "-C", self.config.deployment_directory, "diff-index", "--name-status", ref ])) result = command.exec([ "git", "diff-index", "--name-status", ref ], cwd=self.config.deployment_directory ) committed_changes = result.stdout.decode("utf-8").strip().split("\n") lines = uncommitted_changes + committed_changes lines = [ele.split("\t") for ele in lines] filtered_lines = {} for line in lines: if len(line) == 1: continue flag, path = line if flag != "D": file_stat = os.stat("{}/{}".format(self.config.deployment_directory,path)) file_mtime = file_stat.st_mtime if file_mtime > deployment_mtime: if path not in filtered_lines or flag == "A": filtered_lines[path] = {"flag": flag, "path": path} filtered_lines = dict(sorted(filtered_lines.items())) files = glob.glob("{}/**/**/*".format(config.deployment_config_path), recursive = True) config_files = {} for filename in files: file_stat = os.stat(filename) if file_stat.st_mtime > deployment_mtime: path = filename[len(config.deployment_directory):] config_files[path] = {"flag": "M", "path": path} lines = list(config_files.values()) + list(filtered_lines.values()) smartserver_changes = lines return smartserver_code, smartserver_pull, smartserver_changes
def execCommand(cmd, cwd=None, exitstatus_check=True): return command.exec([cmd], shell=True, cwd=cwd, exitstatus_check=exitstatus_check)
def getRebootState(self): result = command.exec(["/usr/bin/zypper", "needs-rebooting"], exitstatus_check=False) return result.returncode != 0
def getRebootState(self): result = command.exec(["/usr/bin/needs-restarting", "-r"], exitstatus_check=False) return result.returncode == 1