def wait_for_vm_to_become_ssh_available(env_settings, executor, logger=None, retries=10, retry_interval=30):
    """
    Asserts that a machine received the ssh key for the key manager, and it is
    no ready to be connected via ssh.
    :param env_settings: The fabric setting for the remote machine.
    :param executor: An executer function, which executes code on the remote
    machine.
    :param logger: custom logger. defaults to None.
    :param retries: number of time to check for availability. default to 5.
    :param retry_interval: length of the intervals between each try. defaults
    to 20.
    :return: None
    """
    local_env_setting = copy.deepcopy(env_settings)
    local_env_setting.update({"abort_exception": FabException})
    if logger:
        logger.info("Waiting for ssh key to register on the vm...")
    while retries >= 0:
        try:
            with fab_env(**local_env_setting):
                executor("echo")
                if logger:
                    logger.info("Machine is ready to be logged in...")
                return
        except FabException as e:
            if retries == 0:
                raise e
            else:
                if logger:
                    logger.info(
                        "Machine is not yet ready, waiting for {0} " "secs and trying again".format(retry_interval)
                    )
                retries -= 1
                time.sleep(retry_interval)
                continue
Beispiel #2
0
 def write_file_remotely(self,
                         local_file_path,
                         remote_file_path,
                         env=None,
                         sudo=False,
                         **kwargs):
     env = env or self.centos_client_env
     with fab_env(**env):
         fab.put(local_file_path, remote_file_path, use_sudo=sudo)
Beispiel #3
0
 def add_dns_nameservers_to_manager_blueprint(self, local_modify_script):
     remote_modify_script = os.path.join(self.client_cfy_work_dir,
                                         'modify.py')
     self.logger.info('Uploading {0} to {1} on manager...'.format(
         local_modify_script, remote_modify_script))
     with fab_env(**self.centos_client_env):
         fab.put(local_modify_script, remote_modify_script, use_sudo=True)
         self.logger.info(
             'Adding DNS name servers to remote manager blueprint...')
         fab.run('sudo python {0} {1}'.format(remote_modify_script,
                                              self.manager_blueprint_path))
 def add_dns_nameservers_to_manager_blueprint(self, local_modify_script):
     remote_modify_script = os.path.join(self.client_cfy_work_dir,
                                         'modify.py')
     self.logger.info(
         'Uploading {0} to {1} on manager...'.format(local_modify_script,
                                                     remote_modify_script))
     with fab_env(**self.centos_client_env):
         fab.put(local_modify_script, remote_modify_script, use_sudo=True)
         self.logger.info(
             'Adding DNS name servers to remote manager blueprint...')
         fab.run('sudo python {0} {1}'.format(
             remote_modify_script, self.manager_blueprint_path))
 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_on_linux(
            self,
            cmd,
            fabric_env=None,
            within_cfy_env=False,
            sudo=False,
            log_cmd=True,
            retries=0,
            warn_only=False):

        if not fabric_env:
            fabric_env = self.centos_client_env
        if within_cfy_env:
            cmd = 'cfy {0}'.format(cmd)

        if log_cmd:
            self.logger.info('Executing command: {0}'.format(cmd))
        else:
            self.logger.info('Executing command: ***')

        while True:
            with shell_env(
                    CLOUDIFY_USERNAME=os.environ.get(
                            cli_constants.CLOUDIFY_USERNAME_ENV),
                    CLOUDIFY_PASSWORD=os.environ.get(
                            cli_constants.CLOUDIFY_PASSWORD_ENV)):
                with fab_env(**fabric_env):
                    if sudo:
                        out = fab.sudo(cmd, warn_only=warn_only)
                    else:
                        out = fab.run(cmd, 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 _execute_command_on_linux(
            self,
            cmd,
            fabric_env=None,
            within_cfy_env=False,
            sudo=False,
            log_cmd=True,
            retries=0,
            warn_only=False):

        if not fabric_env:
            fabric_env = self.centos_client_env
        if within_cfy_env:
            cmd = 'source {0}/env/bin/activate && cfy {1}' \
                  .format(self.client_cfy_work_dir, cmd)

        if log_cmd:
            self.logger.info('Executing command: {0}'.format(cmd))
        else:
            self.logger.info('Executing command: ***')

        while True:
            with fab_env(**fabric_env):
                if sudo:
                    out = fab.sudo(cmd, warn_only=warn_only)
                else:
                    out = fab.run(cmd, 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))
Beispiel #8
0
def wait_for_vm_to_become_ssh_available(env_settings,
                                        executor,
                                        logger=None,
                                        retries=10,
                                        retry_interval=30,
                                        timeout=20):
    """
    Asserts that a machine received the ssh key for the key manager, and
    it is no ready to be connected via ssh.
    :param env_settings: The fabric setting for the remote machine.
    :param executor: An executer function, which executes code on the
    remote machine.
    :param logger: custom logger. defaults to None.
    :param retries: number of time to check for availability. default to
    10.
    :param retry_interval: length of the intervals between each try.
    defaults to 30.
    :param timeout: timeout for each check try. default to 60.
    :return: None
    """
    local_env_setting = copy.deepcopy(env_settings)
    local_env_setting.update({'abort_exception': FabException})
    local_env_setting.update({'timeout': timeout})
    if logger:
        logger.info('Waiting for ssh key to register on the vm...')
    while retries >= 0:
        try:
            with fab_env(**local_env_setting):
                executor('echo Success')
                if logger:
                    logger.info('Machine is ready to be logged in...')
                return
        except FabException as e:
            if retries == 0:
                raise e
            else:
                if logger:
                    logger.info(
                        'Machine is not yet ready, waiting for {0}'
                        ' secs and trying again'.format(retry_interval))
                retries -= 1
                time.sleep(retry_interval)
                continue
    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)
                        )
Beispiel #10
0
    def _execute_command_on_linux(self,
                                  cmd,
                                  fabric_env=None,
                                  within_cfy_env=False,
                                  sudo=False,
                                  log_cmd=True,
                                  retries=0,
                                  warn_only=False):

        if not fabric_env:
            fabric_env = self.centos_client_env
        if within_cfy_env:
            cmd = 'cfy {0}'.format(cmd)

        if log_cmd:
            self.logger.info('Executing command: {0}'.format(cmd))
        else:
            self.logger.info('Executing command: ***')

        while True:
            with fab_env(**fabric_env):
                if sudo:
                    out = fab.sudo(cmd, warn_only=warn_only)
                else:
                    out = fab.run(cmd, 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 write_file_remotely(self, local_file_path, remote_file_path, env=None,
                         sudo=False, **kwargs):
     env = env or self.centos_client_env
     with fab_env(**env):
         fab.put(local_file_path, remote_file_path, use_sudo=sudo)