Example #1
0
    def execute_package(self, ssh):
        logging.header('Remotely executing package on ' + ssh.hostname)

        ssh.run_command(['python', self._package], sudo=self._sudo)

        if ssh.output:
            for line in ssh.output:
                logging.info(line)

        exit_code = 0

        if ssh.error:
            for line in ssh.error:
                logging.error(line)
                exit_code = 1

        if ssh.returncode is None:
            pass
        elif ssh.returncode == 0:
            logging.debug('return code: 0')
        elif ssh.returncode > 0:
            logging.error('return code: ' + str(ssh.returncode))
            exit_code = 1

        if exit_code:
            logging.error('Remotely executing package failed.')
            sys.exit(exit_code)

        logging.success('Remote package executed.')
Example #2
0
def homebrew_install(user=None, force=False):
    if not os.path.exists('/usr/local/bin/brew') or force:
        logging.header('Install homebrew')
        brew_cmd = '/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"'
        Run(['bash', '-e'], stdin=brew_cmd, user=user).run()
    else:
        logging.info('Homebrew already installed.')
Example #3
0
    def copy_package(self, ssh):
        logging.header('Sending package to ' + ssh.hostname)
        ssh.copy_to(self._package, self._package)

        for line in ssh.output:
            logging.debug(line)

        exit_code = 0

        if ssh.error:
            for line in ssh.error:
                logging.error(line)
                exit_code = 1

        if ssh.returncode is None:
            pass
        elif ssh.returncode == 0:
            logging.debug('return code: 0')
        elif ssh.returncode > 0:
            logging.error('return code: ' + str(ssh.returncode))
            exit_code = 1

        if exit_code:
            logging.error('Sending package failed.')
            sys.exit(exit_code)

        logging.success('Sent.')
Example #4
0
 def run(self):
     logging.header('File ' + self._extension + ' handled by ' +
                    self._handler)
     file_handler_tool = self.__class__.file_handler_tool()
     if file_handler_tool == FiletypeHandlerType.DUTI:
         return Run(['duti', '-s', self._handler, self._extension,
                     'all']).run()
     raise Exception('No tool available for handling file types.')
Example #5
0
def file(src=None,
         dst=None,
         replacements={},
         owner=None,
         group=None,
         diff=True,
         action=FileAction.ADD,
         secure=False):

    global context

    if action == FileAction.ADD:
        logging.header('Setting up file ' + dst)

        if not dst:
            if not src:
                raise Exception('No src nor dst specified for file()')
            dst = os.path.basename(urlparse(src).path)
            logging.debug('No destination file specified; assuming ' + dst)

        if not src:
            filename = os.path.basename(urlparse(dst).path)
            if filename:
                path = os.path.join(context.role.path, 'files', filename)
                if os.path.exists(path) and os.path.isfile(path):
                    src = path
                    logging.debug('No source file specified; assuming ' + src)

        base = os.path.dirname(dst)
        if base != dst:
            mkdir(base)

        if src:
            logging.info('Copying ' + src + ' to ' + dst)

            copy_file(src,
                      dst,
                      replacements=replacements,
                      relative=os.path.join(context.role.path, 'files'),
                      diff=diff)
        else:
            logging.info('Creating empty file ' + dst)

            def touch(path, times=None):
                with open(path, 'a'):
                    os.utime(path, times)

            touch(dst)

        if owner or group:
            chown_util(dst, owner, group, recursive=False)

    elif action == FileAction.REMOVE:
        logging.header('Removing file ' + src)
        os.unlink(src)

    else:
        raise Exception('Invalid action')
Example #6
0
def module(module, action=KernelModuleAction.ADD, secure=False):
    if action == KernelModuleAction.ADD:
        logging.header('Add kernel module' + module)
        add_string_if_not_present_in_file('/etc/modules', module)

    elif action == KernelModuleAction.REMOVE:
        logging.header('Remove kernel module' + module)
        delete_string_from_file('/etc/modules', module)

    else:
        raise Exception('Invalid action')
Example #7
0
    def run(self):
        logging.debug('Provisioner run')

        def user_check():
            logging.info('Running provisioner as user ' + getpass.getuser())
            if getpass.getuser() != 'root':
                logging.warning(
                    'Provisioning not running as root (use --sudo if needed)')

        user_check()

        logging.header('Runbook ' + self._runbook.path)
        logging.header('Roles ' + ', '.join(self._roles))

        def exec_locals(runbook, role):

            to_import = {'prvsnlib.tasks': ['*']}
            to_import.update(self._extra_imports)

            exec_locals = {}
            for module_name, symbol_names in to_import.items():
                module = importlib.import_module(module_name)
                for symbol_name in symbol_names:
                    if symbol_name == '*':
                        exec_locals.update(module.__dict__)
                    else:
                        exec_locals[symbol_name] = getattr(module, symbol_name)
            exec_locals['runbook'] = runbook
            exec_locals['role'] = role
            return exec_locals

        try:
            with StdoutLogger():
                for role in self._roles:
                    role = Role(os.path.join(self._runbook.path, 'roles',
                                             role))

                    global provisioning_context

                    context.runbook = self._runbook
                    context.role = role

                    with open(role.main_file) as f:
                        code = compile(f.read(), role.path, 'exec')
                    exec(code, exec_locals(self._runbook, role))

        except subprocess.CalledProcessError as e:
            logging.error(str(e))
            logging.error('return code: ' + str(e.returncode))
            sys.exit(1)

        logging.success('Provisioned.')
Example #8
0
def command(interpreter,
            commands,
            user=None,
            ignore_errors=False,
            action=CommandAction.RUN):
    if action == CommandAction.RUN:
        logging.header('Run ' + interpreter[0])

        return Run(interpreter,
                   stdin=commands,
                   user=user,
                   ignore_errors=ignore_errors).run()

    else:
        raise Exception('Invalid action')
Example #9
0
def file_contains(path,
                  string,
                  newline=True,
                  owner=None,
                  group=None,
                  diff=True,
                  secure=False):
    logging.header('Content for file ' + path)
    with open(path, 'r') as f:
        content = f.read()
        if not string in content:
            with open(path, 'a+') as f:
                if newline:
                    f.write('\n')
                f.write(string)
    if owner or group:
        chown_util(path, owner, group, recursive=False)
Example #10
0
def untar(src, dest, owner=None, group=None, action=UnarchiveAction.EXTRACT):
    if action == UnarchiveAction.EXTRACT:
        logging.header('Extract tar file ' + src)

        mkdir_p(os.path.dirname(dest))

        global context

        if not os.path.exists(src):
            src = os.path.join(context.role.path, 'files', src)

        with tarfile.open(src) as tar:
            tar.extractall(dest)

        if owner or group:
            chown(dest, owner, group, recursive=True)
    else:
        raise Exception('Invalid action')
Example #11
0
    def manage_public_keys(self, ssh):
        if not self._no_copy_keys:
            logging.header('Copying public keys to ' + self._hostname)
            ssh.copy_public_keys()

            all_lines = []
            for line in ssh.output:
                logging.debug(line)
                all_lines.append(line)

            if 'skipped because they already exist' in ''.join(all_lines):
                logging.success('Public keys already present. Skipping.')
            else:
                if ssh.returncode is None:
                    pass
                elif ssh.returncode == 0:
                    logging.success('Public keys copied.')
                elif ssh.returncode > 0:
                    logging.error('Could not copy public key')
Example #12
0
    def build_package(self):
        logging.header('Packaging runbook "' + self._runbook.path + '"')

        if not self._dest:
            fd, self._dest = tempfile.mkstemp(suffix='.pyz')

        dest_path = os.path.dirname(self._dest)
        mkdir_p(dest_path)

        self.prepare_package()

        logging.debug('Building package at "' + self._dest + '"')

        zipdir(
            self._tmpdir,
            self._dest,
        )

        self.cleanup_package()

        logging.success('Packaged.')
        return self._dest
Example #13
0
def hostname(name, secure=False):
    logging.header('Hostname ' + name)
    Run(['hostnamectl', 'set-hostname', name]).run()
Example #14
0
 def run(self):
     logging.header(self.header)
Example #15
0
def chown(path, owner=None, group=None, recursive=False):
    logging.header('Changing ownership of file ' + path)
    chown_util(path, owner, group, recursive)