Esempio n. 1
0
 def check_platform_avaiable(self):
     """Validates the platform specified is ready for use."""
     res = subprocess.run("{} --version".format(self.binary),
                          shell=True,
                          stderr=subprocess.PIPE,
                          stdout=subprocess.PIPE)
     success_or_exit(res.returncode,
                     req_exc.service_down(self.installation))
Esempio n. 2
0
    def run(self):
        """Run tests."""
        try:
            cmd = "{0} run -v {1}:/{2}:z {3} \
/bin/bash -c 'cd {2}; {4}'".format(self.binary, self.vars['project'],
                                   self.vars['project_name'],
                                   self.vars['image'], self.vars['execute'])
        except KeyError as e:
            LOG.error(usage.missing_arg(e.args[0]))
            sys.exit(2)
        LOG.info("Running: {}".format(cmd))
        res = subprocess.run(cmd, shell=True)
        success_or_exit(res.returncode)
        return res
Esempio n. 3
0
def main(args):
    """Runner main entry."""
    if not args.scenario and not args.vars:
        LOG.error(missing_scenario_arg())
        sys.exit(2)
    if args.scenario and not args.platform:
        args.platform = utils.guess_platform(args.scenario)
    if not args.scenario and not args.platform:
        success_or_exit(
            1, "Couldn't figure out which platform to use. \
Please specify --platform")
    Platform = getattr(
        importlib.import_module("infraform.platforms.{}".format(
            args.platform)), args.platform.capitalize())
    platform = Platform(args=args)
    platform.prepare()
    platform.run()
Esempio n. 4
0
def guess_platform(scenario):
    """Try to figure out which platform the user should use or fail."""
    scenario_path, scenario_file = Platform.verify_scenario_exists(
        SCENARIOS_PATH, scenario)
    if scenario_file.endswith(".tf"):
        return "terraform"
    if scenario_file.endswith(".py"):
        return "python"
    if scenario_file.endswith(".sh"):
        return "shell"
    if os.path.dirname(scenario_path).split('/')[-1] == "podman":
        return "podman"
    if "docker-compose" in os.path.dirname(scenario_path).split('/'):
        return "docker_compose"
    success_or_exit(
        1,
        "Couldn't figure out which platform to use. Please specify --platform")
Esempio n. 5
0
 def verify_scenario_exists(scenarios_dir, scenario):
     """Verifies scenario exists."""
     similar = []
     for p, d, files in os.walk(scenarios_dir):
         for f in files:
             until_dot_pattern = re.compile(r"^[^.]*")
             file_without_suffix = re.search(until_dot_pattern, f).group(0)
             file_name = f
             if f.endswith('.j2'):
                 file_name = f[:-3]
             if file_without_suffix == scenario:
                 scenario_file_path = p + '/' + f
                 scenario_file = file_name
                 return scenario_file_path, scenario_file
             elif SequenceMatcher(None, file_without_suffix,
                                  scenario).ratio() >= 0.25:
                 similar.append(file_without_suffix)
     if similar:
         LOG.info("Maybe you meant:\n\n{}".format(
             crayons.yellow("\n".join(similar))))
     success_or_exit(1, usage_exc.missing_scenario(scenario))
Esempio n. 6
0
 def run(self):
     """Execution docker-compose."""
     try:
         cmds = self.vars['execute']
     except KeyError:
         cmds = self.RUN
     if "hosts" in self.args:
         results = process.execute_cmd(commands=cmds,
                                       hosts=self.args['hosts'],
                                       cwd=self.execution_dir)
     else:
         results = process.execute_cmd(commands=cmds,
                                       cwd=self.execution_dir)
     [success_or_exit(res.exited) for res in results]
     return results
Esempio n. 7
0
 def rm(self):
     LOG.info("Removing")
     cmd = self.vars['remove']
     res = subprocess.run(cmd, shell=True, cwd=self.execution_dir)
     success_or_exit(res.returncode)
     return res
Esempio n. 8
0
 def verify_project_exists(self):
     if not os.path.isdir(self.vars['project']):
         success_or_exit(
             2, "Couldn't find project: {}".format(self.vars['project']))
Esempio n. 9
0
 def run(self):
     """Execution docker-compose."""
     cmd = self.vars['execute']
     res = subprocess.run(cmd, shell=True, cwd=self.execution_dir)
     success_or_exit(res.returncode)
     return res