Beispiel #1
0
    def provision(self, arguments):
        """
        Provisions the Mech machine.

        Usage: mech provision [options] [<instance>]

        Options:
            -h, --help                       Print this help
        """
        instance_name = arguments['<instance>']
        instance_name = self.activate(instance_name)

        vmrun = VMrun(self.vmx, self.user, self.password)

        if not vmrun.installedTools():
            puts_err(colored.red("Tools not installed"))
            return

        provisioned = 0
        for i, provision in enumerate(self.get('provision', [])):

            if provision.get('type') == 'file':
                source = provision.get('source')
                destination = provision.get('destination')
                if utils.provision_file(vmrun, source, destination) is None:
                    puts_err(colored.red("Not Provisioned"))
                    return
                provisioned += 1

            elif provision.get('type') == 'shell':
                inline = provision.get('inline')
                path = provision.get('path')
                args = provision.get('args')
                if not isinstance(args, list):
                    args = [args]
                if utils.provision_shell(vmrun, inline, path, args) is None:
                    puts_err(colored.red("Not Provisioned"))
                    return
                provisioned += 1

            else:
                puts_err(colored.red("Not Provisioned ({}".format(i)))
                return
        else:
            puts_err(
                colored.green("Provisioned {} entries".format(provisioned)))
            return

        puts_err(colored.red("Not Provisioned ({}".format(i)))
Beispiel #2
0
    def config_ssh(self):
        vmrun = VMrun(self.vmx, user=self.user, password=self.password)
        lookup = self.get("enable_ip_lookup", False)
        ip = vmrun.getGuestIPAddress(
            wait=False, lookup=lookup) if vmrun.installedTools() else None
        if not ip:
            puts_err(
                colored.red(
                    textwrap.fill(
                        "This Mech machine is reporting that it is not yet ready for SSH. "
                        "Make sure your machine is created and running and try again. "
                        "Additionally, check the output of `mech status` to verify "
                        "that the machine is in the state that you expect.")))
            sys.exit(1)

        insecure_private_key = os.path.abspath(
            os.path.join(HOME, "insecure_private_key"))
        if not os.path.exists(insecure_private_key):
            with open(insecure_private_key, 'w') as f:
                f.write(INSECURE_PRIVATE_KEY)
            os.chmod(insecure_private_key, 0o400)
        config = {
            "Host": DEFAULT_HOST,
            "User": self.user,
            "Port": "22",
            "UserKnownHostsFile": "/dev/null",
            "StrictHostKeyChecking": "no",
            "PasswordAuthentication": "no",
            "IdentityFile": insecure_private_key,
            "IdentitiesOnly": "yes",
            "LogLevel": "FATAL",
        }
        for k, v in self.config.items():
            k = re.sub(r'[ _]+', r' ', k)
            k = re.sub(r'(?<=[^_])([A-Z])', r' \1', k).lower()
            k = re.sub(r'^( *)(.*?)( *)$', r'\2', k)
            callback = lambda pat: pat.group(1).upper()
            k = re.sub(r' (\w)', callback, k)
            if k[0].islower():
                k = k[0].upper() + k[1:]
            config[k] = v
        config.update({
            "HostName": ip,
        })
        return config
Beispiel #3
0
    def down(self, arguments):
        """
        Stops the Mech machine.

        Usage: mech down [options] [<instance>]

        Options:
                --force                      Force a hard stop
            -h, --help                       Print this help
        """
        force = arguments['--force']

        instance_name = arguments['<instance>']
        instance_name = self.activate(instance_name)

        vmrun = VMrun(self.vmx, user=self.user, password=self.password)
        if not force and vmrun.installedTools():
            stopped = vmrun.stop()
        else:
            stopped = vmrun.stop(mode='hard')
        if stopped is None:
            puts_err(colored.red("Not stopped", vmrun))
        else:
            puts_err(colored.green("Stopped", vmrun))