def verify(conf): if not conf.workspace and conf.workspace == "": raise ValueError(Format.alert("You should set the workspace value in a configured yaml file e.g. vars.yaml" "or set env var WORKSPACE before using testrunner)")) if not conf.terraform.stack_name: raise ValueError(Format.alert("Either a terraform stack name or an user name must be specified")) if os.path.normpath(conf.workspace) == os.path.normpath((os.getenv("HOME"))): raise ValueError(Format.alert("workspace should not be your home directory")) return conf
def _verify_bootstrap_dependency(self): if not os.path.exists(os.path.join(self.conf.workspace, "test-cluster")): raise Exception( Format.alert( "test-cluster not found. Please run bootstrap and try again" ))
def get_var_dict(yaml_path): config_yaml_file_path = BaseConfig.get_yaml_path(yaml_path) if not os.path.isfile(config_yaml_file_path): print(Format.alert("You have incorrect -v path for xml file: {}".format(config_yaml_file_path))) raise FileNotFoundError with open(config_yaml_file_path, 'r') as stream: _conf = yaml.safe_load(stream) return _conf
def runshellcommand_withoutput(self, cmd, ignore_errors=True): p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, err = p.communicate() rc = p.returncode if not ignore_errors: if rc != 0: print(err) raise RuntimeError(Format.alert("Cannot run command {}{}\033[0m".format(cmd))) return output.decode()
def runshellcommand(self, cmd, cwd=None, env={}, ignore_errors=False): """Running shell command in {workspace} if cwd == None Eg) cwd is "skuba", cmd will run shell in {workspace}/skuba/ cwd is None, cmd will run in {workspace} cwd is abs path, cmd will run in cwd Keyword arguments: cmd -- command to run cwd -- dir to run the cmd env -- environment variables ignore_errors -- don't raise exception if command fails """ if not cwd: cwd = self.conf.workspace if not os.path.isabs(cwd): cwd = os.path.join(self.conf.workspace, cwd) if not os.path.exists(cwd): raise FileNotFoundError( Format.alert("Directory {} does not exists".format(cwd))) if logging.DEBUG >= logger.level: logger.debug("Executing command\n" " cwd: {} \n" " env: {}\n" " cmd: {}".format(cwd, str(env) if env else "{}", cmd)) else: logger.info("Executing command {}".format(cmd)) stdout, stderr = [], [] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=env, cwd=cwd) stdoutStreamer = Thread(target=self.read_fd, args=(p, p.stdout, logger.debug, stdout)) stderrStreamer = Thread(target=self.read_fd, args=(p, p.stderr, logger.error, stderr)) stdoutStreamer.start() stderrStreamer.start() stdoutStreamer.join() stderrStreamer.join() # this is redundant, at this point threads were joined and they waited for the subprocess # to exit, however it should not hurt to explicitly wait for it again (no-op). p.wait() stdout, stderr = "".join(stdout), "".join(stderr) if p.returncode != 0: if not ignore_errors: raise RuntimeError("Error executing command {}".format(cmd)) else: return stderr return stdout
def runshellcommand(self, cmd, cwd=None, env=None): """Running shell command in {workspace} if cwd == None Eg) cwd is "skuba", cmd will run shell in {workspace}/skuba/ cwd is None, cmd will run in {workspace} cwd is abs path, cmd will run in cwd Keyword arguments: cmd -- command to run cwd -- dir to run the cmd env -- environment variables """ if not cwd: cwd = self.conf.workspace if not os.path.isabs(cwd): cwd = os.path.join(self.conf.workspace, cwd) if not os.path.exists(cwd): raise Exception(Format.alert("Directory {} does not exists".format(cwd))) print(Format.alert("$ {} > {}".format(cwd, cmd))) subprocess.check_call(cmd, cwd=cwd, shell=True, env=env)
def runshellcommand(self, cmd, cwd=None, env={}, ignore_errors=False, stdin=None): """Running shell command Keyword arguments: cmd -- command to run cwd -- dir to run the cmd env -- environment variables ignore_errors -- don't raise exception if command fails stdin -- standard input for the command in bytes """ cmd_env = { "SSH_AUTH_SOCK": self.conf.utils.ssh_sock, "PATH": os.environ['PATH'], **env } if cwd and not os.path.exists(cwd): raise FileNotFoundError(Format.alert("Directory {} does not exists".format(cwd))) if logging.DEBUG >= logger.level: logger.debug("Executing command\n" " cwd: {} \n" " env: {}\n" " cmd: {}".format(cwd, str(cmd_env) if cmd_env else "{}", cmd)) else: logger.info("Executing command {}".format(cmd)) stdout, stderr = [], [] p = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, stdin=subprocess.PIPE if stdin else None, shell=True, env=cmd_env ) if stdin: p.stdin.write(stdin) p.stdin.close() stdoutStreamer = Thread(target = self.read_fd, args = (p, p.stdout, logger.debug, stdout)) stderrStreamer = Thread(target = self.read_fd, args = (p, p.stderr, logger.error, stderr)) stdoutStreamer.start() stderrStreamer.start() stdoutStreamer.join() stderrStreamer.join() # this is redundant, at this point threads were joined and they waited for the subprocess # to exit, however it should not hurt to explicitly wait for it again (no-op). p.wait() stdout, stderr = "".join(stdout), "".join(stderr) if p.returncode != 0: if not ignore_errors: raise RuntimeError("Error executing command {}".format(cmd)) else: return stderr return stdout
def info(self): """Node info""" print("Env vars: {}".format(sorted(os.environ))) self.runshellcommand('ip a') self.runshellcommand('ip r') self.runshellcommand('cat /etc/resolv.conf') try: r = requests.get('http://169.254.169.254/2009-04-04/meta-data/public-ipv4', timeout=2) r.raise_for_status() except (requests.HTTPError, requests.Timeout) as err: print(err) print(Format.alert('Meta Data service unavailable could not get external IP addr{}')) else: print('External IP addr: {}'.format(r.text))
def runshellcommand(self, cmd, cwd=None, env={}, ignore_errors=False): """Running shell command in {workspace} if cwd == None Eg) cwd is "skuba", cmd will run shell in {workspace}/skuba/ cwd is None, cmd will run in {workspace} cwd is abs path, cmd will run in cwd Keyword arguments: cmd -- command to run cwd -- dir to run the cmd env -- environment variables ignore_errors -- don't raise exception if command fails """ if not cwd: cwd = self.conf.workspace if not os.path.isabs(cwd): cwd = os.path.join(self.conf.workspace, cwd) if not os.path.exists(cwd): raise FileNotFoundError( Format.alert("Directory {} does not exists".format(cwd))) if logging.DEBUG >= logger.level: logger.debug("Executing command\n" " cwd: {} \n" " env: {}\n" " cmd: {}".format(cwd, str(env) if env else "{}", cmd)) else: logger.info("Executing command {}".format(cmd)) p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=env, cwd=cwd) logger.debug(p.stdout.decode()) logger.error(p.stderr.decode()) if p.returncode != 0: if not ignore_errors: raise RuntimeError("Error executing command {}".format(cmd)) else: return p.stderr.decode() return p.stdout.decode()
def node_reset(self, role="worker", nr=0): self._verify_bootstrap_dependency() ip_addrs = self.platform.get_nodes_ipaddrs(role) if nr < 0: raise ValueError("Node number cannot be negative") if nr >= len(ip_addrs): raise Exception( Format.alert("Node {role}-{nr} not deployed in " "infrastructure".format(role=role, nr=nr))) cmd = "node reset --user {username} --sudo --target {ip}".format( ip=ip_addrs[nr], username=self.conf.nodeuser) try: self._run_skuba(cmd) except Exception as ex: raise Exception("Error executing cmd {}".format(cmd)) from ex
def node_join(self, role="worker", nr=0): self._verify_bootstrap_dependency() ip_addrs = self.platform.get_nodes_ipaddrs(role) node_names = self.platform.get_nodes_names(role) if nr < 0: raise ValueError("Node number cannot be negative") if nr >= len(ip_addrs): raise Exception( Format.alert("Node {role}-{nr} is not deployed in " "infrastructure".format(role=role, nr=nr))) cmd = (f'node join --role {role} --user {self.conf.nodeuser} ' f' --sudo --target {ip_addrs[nr]} {node_names[nr]}') try: self._run_skuba(cmd) except Exception as ex: raise Exception("Error executing cmd {}") from ex
def _verify_skuba_bin_dependency(self): if not os.path.isfile(self.binpath): raise FileNotFoundError( Format.alert("skuba not found at {}".format(self.binpath)))