def _get_manager_kp(self):
     """
     Retrieves manager kp to the local machine.
     :return: path to the local manager kp.
     """
     remote_manager_kp_path = self.bootstrap_inputs['ssh_key_filename']
     with fab_env(**self.centos_client_env):
         local_manager_kp = fab.get(remote_manager_kp_path, self.workdir)[0]
     os.chmod(local_manager_kp, stat.S_IRUSR | stat.S_IWUSR)
     return local_manager_kp
 def _run_cmd_on_custom_machine(self, cmd, fab_conf, sudo=False, retries=3):
     """
     Runs a command on a remote machine using fabric (would work only on
     machines with fabric).
     :param cmd: the command to run.
     :param fab_conf: fabric environment settings.
     :param sudo: whether run the cmd in sudo mode.
     :param retries: number of times to retry the cmd.
     :return: None
     """
     with fab_env(**fab_conf):
         self._execute_command(cmd, sudo=sudo, retries=retries)
    def _execute_command(
            self,
            cmd,
            sudo=False,
            pty=True,
            log_cmd=True,
            retries=0,
            warn_only=False,
            **_):
        """
        Executed the given command on the file server vm.

        :param cmd: the command to execute.
        :param sudo: whether to use sudo or not.
        :param pty: passed as an arg to fabric run/sudo.
        :param log_cmd: Specifies whether to log the command executing.
        :param retries: number of command retries.
        :return:
        """
        if log_cmd:
            self.logger.info('Executing command: {0}'.format(cmd))
        else:
            self.logger.info('Executing command: ***')

        with fab_env(**self.fab_env_conf):
            while True:
                if sudo:
                    out = fab.sudo(cmd, pty=pty, warn_only=warn_only)
                else:
                    out = fab.run(cmd, pty=pty, warn_only=warn_only)

                self.logger.info("""Command execution result:
        Status code: {0}
        STDOUT:
        {1}
        STDERR:
        {2}""".format(out.return_code, out, out.stderr))
                if out.succeeded or (warn_only and retries == 0):
                    return out
                else:
                    if retries > 0:
                        time.sleep(30)
                        retries -= 1
                    else:
                        raise Exception('Command: {0} exited with code: '
                                        '{1}. Tried {2} times.'
                                        .format(cmd, out.return_code,
                                                retries + 1))
Example #4
0
    def _execute_command(self,
                         cmd,
                         sudo=False,
                         pty=True,
                         log_cmd=True,
                         retries=0,
                         warn_only=False):
        """
        Executed the given command on the file server vm.

        :param cmd: the command to execute.
        :param sudo: whether to use sudo or not.
        :param pty: passed as an arg to fabric run/sudo.
        :param log_cmd: Specifies whether to log the command executing.
        :param retries: number of command retries.
        :return:
        """
        if log_cmd:
            self.logger.info('Executing command: {0}'.format(cmd))
        else:
            self.logger.info('Executing command: ***')
        with fab_env(**self.fab_env_conf):
            while True:
                if sudo:
                    out = fab.sudo(cmd, pty=pty, warn_only=warn_only)
                else:
                    out = fab.run(cmd, pty=pty, warn_only=warn_only)

                self.logger.info("""Command execution result:
        Status code: {0}
        STDOUT:
        {1}
        STDERR:
        {2}""".format(out.return_code, out, out.stderr))
                if out.succeeded or (warn_only and retries == 0):
                    return out
                else:
                    if retries > 0:
                        time.sleep(30)
                        retries -= 1
                    else:
                        raise Exception('Command: {0} exited with code: '
                                        '{1}. Tried {2} times.'.format(
                                            cmd, out.return_code, retries + 1))
 def _get_remote_blueprint(self, env=None):
     env = env or self.centos_client_env
     manager_blueprint = StringIO()
     with fab_env(**env):
         fab.get(self.manager_blueprint_path, manager_blueprint)
     return yaml.load(manager_blueprint.getvalue())
Example #6
0
 def _get_remote_blueprint(self, env=None):
     env = env or self.centos_client_env
     manager_blueprint = StringIO()
     with fab_env(**env):
         fab.get(self.manager_blueprint_path, manager_blueprint)
     return yaml.load(manager_blueprint.getvalue())