示例#1
0
def execute_command(command: str, sudo=False, host='localhost', user=None, password=None, port=None):
    """
    Execute shell command in local or remote server
    :param sudo: True/False, execute as sudo
    :param host: host in which execute command
    :param user: ssh username
    :param password: ssh password
    :param port: ssh port
    :return: stdout and stderr in tuple, (None, None) if error
    """

    regexp = '^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$'
    is_localhost = bool(re.search(regexp, host))

    # Create fabric connection
    if is_localhost:
        conn = fabric.Connection(host)
        modified_command = 'sudo ' + command if sudo else command
        logger.info('Executing command " {} " in {} as sudo={}'.format(modified_command, host, sudo))
        output = conn.local(modified_command)
    else:
        conn = fabric.Connection(host, user=user, port=port)
        if password and len(password) > 0:
            conn.connect_kwargs.look_for_keys = False
            conn.connect_kwargs.password = password

        logger.info('Executing command " {} " in {} as sudo={}'.format(command, host, sudo))
        output = conn.sudo(command) if sudo else conn.run(command)

    return output.stdout, output.stderr
示例#2
0
        def execute_command_wrapper(command: str,
                                    sudo=False,
                                    host='localhost',
                                    user=None,
                                    password=None,
                                    port=None):
            regexp = '^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$'
            is_localhost = bool(re.search(regexp, host))

            # Create fabric connection
            try:
                if is_localhost:
                    conn = fabric.Connection(host)
                    modified_command = 'sudo ' + command if sudo else command
                    output = conn.local(modified_command)
                else:
                    conn = fabric.Connection(host, user=user, port=port)
                    if password and len(password) > 0:
                        conn.connect_kwargs.look_for_keys = False
                        conn.connect_kwargs.password = password
                        conn.connect_kwargs.allow_agent = False
                    output = conn.sudo(command) if sudo else conn.run(command)

                return output.stdout, output.stderr
            except Exception:
                logger.error(GenericErrorMessages.EXECUTED_COMMAND_ERROR,
                             exc_info=True)
                return None, None
示例#3
0
    def connect(self) -> None:
        if not self.can_connect():
            if not self.config:
                raise CouldNotConnectError("config missing!")
            raise CouldNotConnectError("config invalid!")

        current = dict(
            host=self.config.ip,
            user=self.config.user,
            port=self.config.port,
            connect_kwargs=dict(key_filename=str(self.keyfile)),
            connect_timeout=2,
            gateway=fabric.Connection(os.path.expandvars(self.config.gateway))
            if "gateway" in self.config else None,
        )

        if getattr(self, "_fabric_config", {}) != current:
            self._fabric_config = current
            self._connection = fabric.Connection(**self._fabric_config)
            self.connection.open()
        else:
            if not self.connection.is_connected:
                self.connection.open()

        assert self.connection, "backend must have a working underlying connection"
        self._connected = self.connection.is_connected

        if not self.connected:
            raise CouldNotConnectError(current)
示例#4
0
文件: db.py 项目: jjmontesl/awesit
 def ssh_context(self):
     if self.ssh_host:
         return fabric.Connection(host=self.ssh_host,
                                  port=self.ssh_port,
                                  user=self.get_ssh_user())
     else:
         # FIXME: Normalize local / SSH connections for different adaptors, this is doing an unneeded/invalid SSH connection
         return fabric.Connection(host="localhost")
示例#5
0
 def g5k_connection(cls, site, username):
     if 'grid5000' in socket.getfqdn(
     ):  # already inside G5K, no need for a gateway
         connection = fabric.Connection(site, user=username)
     else:
         gateway = fabric.Connection('access.grid5000.fr', user=username)
         connection = fabric.Connection(site,
                                        user=username,
                                        gateway=gateway)
     return connection
示例#6
0
    def file_put(self, local_path, remote_path):
        final_path = os.path.join(self.path, remote_path)
        logger.debug("Writing file via SSH from %s to %s@%s:%s", local_path, self.get_user(), self.host, final_path)

        # Create dir
        dirname = os.path.dirname(final_path)
        with fabric.Connection(host=self.host, port=self.port, user=self.get_user()) as c:
            if self.sudo:
                c.sudo('mkdir -p "%s"' % dirname)
            else:
                c.run('mkdir -p "%s"' % dirname)

        with fabric.Connection(host=self.host, port=self.port, user=self.get_user()) as c:
            result = c.put(local_path, final_path)
示例#7
0
def execute_command(command, sudo, host, user, port):
    regexp = '^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$'
    is_localhost = bool(re.search(regexp, host))

    # Create fabric connection
    output = (None, None)
    if is_localhost:
        conn = fabric.Connection(host)
        modified_command = 'sudo ' + command if sudo else command
        output = conn.local(modified_command)
    else:
        conn = fabric.Connection(host, user=user, port=port)
        output = conn.sudo(command) if sudo else conn.run(command)

    return (output.stdout, output.stderr)
示例#8
0
文件: remove.py 项目: shlemph/miranda
def delete_duplicates(*,
                      source: Union[str, Path],
                      target: Union[str, Path],
                      server: Optional[Union[str, Path]],
                      user: str = None,
                      password: str = None,
                      pattern: str = None,
                      delete_target_duplicates: bool = False) -> None:
    """

    Parameters
    ----------
    source : Union[str, Path]
    target : Union[str, Path]
    server : Optional[Union[str, Path]]
    user: str
    password : str
    pattern: str
    delete_target_duplicates : bool

    Returns
    -------
    None
    """

    user = user or input("Username:"******"Password:"******"*.nc"

    connection = fabric.Connection(host=server,
                                   user=user,
                                   connect_kwargs=dict(password=password))

    nc_files_source = Path(source).rglob(glob_pattern)
    nc_files_source = {f.stem for f in nc_files_source}
    nc_files_target = Path(target).rglob(glob_pattern)

    nc_file_duplicates = []
    for f in nc_files_target:
        if f.name in nc_files_source:
            logging.info("Duplicate found: {}".format(f.name))
            nc_file_duplicates.append(f)

    nc_file_duplicates.sort()
    logging.info("Found {} files totalling {}".format(
        len(nc_file_duplicates), report_file_size(nc_file_duplicates)))

    freed_space = 0
    deleted_files = 0
    if delete_target_duplicates:
        with connection as context:
            for dup in nc_file_duplicates:
                freed_space += Path(dup).stat().st_size
                logging.info("Deleting {}".format(dup.name))
                context.remove(dup)
                deleted_files += 1

    logging.info("Removed {} files totalling {}".format(
        deleted_files, report_file_size(freed_space)))
    return
示例#9
0
 def _dl_file(self, filename):
     fabric.Connection(
         self.dutusername + "@" + self.dutip,
         connect_kwargs={
             "password": self.dutpassword
         },
     ).get(filename)
示例#10
0
    def reboot_board(self, bypass_sleep=False):
        """ Reboot board over SSH, otherwise raise exception
        """
        log.info("Rebooting board over SSH")
        # Try to reboot board with SSH if possible
        retries = 3
        for t in range(retries):
            try:
                result = fabric.Connection(
                    self.dutusername + "@" + self.dutip,
                    connect_kwargs={
                        "password": self.dutpassword
                    },
                ).run("/sbin/reboot", hide=False)
                if result.ok:
                    print("Rebooting board with SSH")
                    if not bypass_sleep:
                        time.sleep(30)
                    break
                else:
                    # Use PDU
                    raise Exception("PDU reset not implemented yet")

            except Exception as ex:
                log.warning("Exception raised: " + str(ex))
                time.sleep(3)
                if t >= (retries - 1):
                    raise Exception("Exception occurred during SSH Reboot",
                                    str(ex))
示例#11
0
文件: remote.py 项目: despiegk/js-ng
def execute(cmd, command_ctx, connection_ctx):
    """
    kwargs: fabric.Connection(host, user=None, port=None, config=None, gateway=None, forward_agent=None, connect_timeout=None, connect_kwargs=None, inline_ssh_env=None)
    """
    with fabric.Connection(**connection_ctx) as c:
        res = c.run(cmd, **command_ctx)
        return res.return_code, res.stdout, res.stderr
示例#12
0
 def _connect(self):
     logger.debug("Opening ssh connection ... ")
     logger.debug("Trying '%s' key ... " % self.private_key )
     private_key_path = expanduser(self.private_key)
     if (
             (not exists( private_key_path)) and
             ('PRIVATE KEY' not in self.private_key)
     ):
         output = "'%s'key does not exist" % private_key_path
         raise ComException(output)
     logger.debug(
         "Connecting to '%s' as user '%s' port  '%s' ..."
         % ( self.frontend, self.username, self.port )
     )
     if ':' in self.frontend:
         self.frontend, self.port = self.frontend.split( ':' )
     try:
         self._conn = fabric.Connection(
             host=self.frontend,
             user=self.username,
             connect_kwargs={
                 "key_filename": private_key_path,
             },
             connect_timeout=300
         )
     except Exception as err:
         logger.warning(
             "Error connecting '%s': %s" %
             (self.frontend, str(err))
         )
示例#13
0
文件: network.py 项目: salotz/refugue
    def construct_connection(
        self,
        ssh_conn,
    ):
        """Construct an object inheriting from invoke.Context for remote
        execution.

        Currently this is achieved through the fabric.Connection class.

        Parameters
        ----------

        conn_d : dict

        Returns
        -------

        conn : Context subclass

        """

        conn = fab.Connection(
            host=ssh_conn.host,
            user=ssh_conn.user,
        )
        return conn
示例#14
0
    def can_connect(self) -> bool:
        if self.configured:
            # Will throw if the key is not available
            self.keyfile = self.config.key
            self._local_adb_version = self._get_adb_version()

            if "gateway" in self.config:
                try:
                    self._gateway = fabric.Connection(
                        os.path.expandvars(self.config.gateway),
                        config=self._fabric_config)
                    if self.gateway.run("pwd").exited != 0:
                        get_root_logger().critical("Gateway connection failed")
                        return False

                    self._remote_adb_version = self._get_adb_version(
                        self.gateway)

                    if self._local_adb_version != self._remote_adb_version:
                        get_root_logger().critical(
                            ("The ADB version of the gateway server {} does "
                             "not match the client version {}!").format(
                                 self._remote_adb_version,
                                 self._local_adb_version))
                        return False
                except Exception as e:
                    get_root_logger().critical("Couldn't connect to gateway",
                                               e.args)
                    return False

            return True
        else:
            return False
示例#15
0
def deploy(destination, repo, checkout, env_args, env_file):
    """
    \b
    Deploy the application by:
      - pulling the latest source and checking out a branch/commit
      - updating the remote .env file
      - building the Docker Compose services
      - applying the database migrations
      - collecting the static files together
      - starting the Docker containers
    """
    c = fabric.Connection(host=destination)

    app_dir = f"/home/{c.user}/app"
    c.run(f"mkdir -p {app_dir}")

    with c.cd(app_dir):
        pull_latest_source(c, repo, checkout)
        create_or_update_dot_env(c, env_args, env_file)
        build_services(c)
        apply_migrations(c)
        collect_static(c)
        start_containers(c)

    info("Deployment complete")
def _CreateFabricContext(ssh_config):
  """Create a fabric context."""
  # fabric has problems to understand complex ssh config.
  # Point to a user config
  fab_config = fabric.Config(system_ssh_path='~/.ssh/config')
  connect_kwargs = {
      'config': fab_config
  }
  if ssh_config.ssh_key or ssh_config.password:
    connect_kwargs['connect_kwargs'] = {}
  if ssh_config.ssh_key:
    logger.debug(
        'Use ssh key %s ssh %s@%s.', ssh_config.ssh_key,
        ssh_config.user, ssh_config.hostname)
    connect_kwargs['connect_kwargs']['key_filename'] = ssh_config.ssh_key
  if ssh_config.password:
    logger.debug('Use password to ssh %s@%s.',
                 ssh_config.user, ssh_config.hostname)
    connect_kwargs['connect_kwargs']['password'] = ssh_config.password
  connect_kwargs['user'] = ssh_config.user
  try:
    wrapped_context = fabric.Connection(
        host=ssh_config.hostname, **connect_kwargs)
    wrapped_context.open()
    return wrapped_context
  except (paramiko.AuthenticationException, paramiko.SSHException) as e:
    logger.debug(e)
    raise AuthenticationError(e)
def migration(servers_login):
    print("launthing thread...")
    #print str(servers_login)
    for i in servers_login.keys():
        if str(i) == 'Password':
            passw = servers_login[i]
        elif str(i) == 'Login_IP':
            host = servers_login[i]
        elif str(i) == 'User_Name':
            user = servers_login[i]
        elif str(i) == 'Key_Path':
            key_filename = servers_login[i]
        elif str(i) == 'Login_Port':
            port = servers_login[i]
        else:
            print("load source servers info, failed")
    print("Import the source servers information, Finished")
    print("start the multi thread migration service...\n")
    conn = fabric.Connection(host=host,
                             user=user,
                             port=port,
                             connect_kwargs={
                                 "key_filename": key_filename,
                                 "password": passw
                             })
    conn.run('mkdir -p /tmp/temp_CE')

    conn.put(local_script_path + '/aws_model', '/tmp/temp_CE/aws_model')
    conn.put(local_script_path + '/aws-instances.csv',
             '/tmp/temp_CE/aws-instances.csv')
    conn.put(local_script_path + '/vlookup.sh', '/tmp/temp_CE/vlookup.sh')
    conn.put(local_script_path + '/get-source-instance-type-2.py',
             '/tmp/temp_CE/get-source-instance-type.py')
    conn.put(local_script_path + '/get-CPU-MEM.sh',
             '/tmp/temp_CE/get-CPU-MEM.sh')
    conn.put(local_script_path + '/deletion_duplication.py',
             '/tmp/temp_CE/deletion_duplication.py')
    conn.put(local_script_path + '/CloudEndure.py',
             '/tmp/temp_CE/CloudEndure.py')
    conn.put(local_script_path + '/Cloudendure_Account_Info.csv',
             '/tmp/temp_CE/Cloudendure_Account_Info.csv')
    conn.put(local_script_path + '/CE_Account.py',
             '/tmp/temp_CE/CE_Account.py')
    #conn.get('/tmp/temp_CE/get-login-info.py', local_script_path+'/get_login_info.py')

    conn.run(
        "sudo python /tmp/temp_CE/get-source-instance-type.py $(. /tmp/temp_CE/get-CPU-MEM.sh)"
    )
    conn.run("sudo python /tmp/temp_CE/deletion_duplication.py")
    conn.run(
        "sudo sh /tmp/temp_CE/vlookup.sh /tmp/temp_CE/aws_model /tmp/temp_CE/b /tmp/temp_CE/c"
    )
    conn.run(
        "cat /tmp/temp_CE/c|sed -n 1p |cut -d \"'\" -f 8-8 > /tmp/temp_CE/d")
    conn.run(
        "sudo python /tmp/temp_CE/CloudEndure.py $(sudo python /tmp/temp_CE/CE_Account.py)"
    )
    #conn.run("sudo python /tmp/temp_CE/CloudEndure.py -u CE_User_Name -p CE_Password  -n ${HOSTNAME} -j CE_Project_Name")

    conn.close()
示例#18
0
文件: core.py 项目: JleMyP/wol
 def _remote_exec_command(creds: SshCredentials,
                          command: str,
                          sudo: bool = False) -> RemoteExecResult:
     try:
         with fabric.Connection(creds.host,
                                creds.login,
                                creds.port,
                                connect_kwargs={'password':
                                                creds.password}) as c:
             if sudo:
                 res = c.sudo(command,
                              warn=True,
                              hide=True,
                              password=creds.password)
             else:
                 res = c.run(command, warn=True, hide=True)
     except NoValidConnectionsError:
         raise RemoteExecError(ERROR_NOT_CONNECTED, "can't connect to host")
     except SSHException as e:
         raise RemoteExecError(ERROR_SSH, "ssh exception", vars(e))
     if res.exited:
         raise RemoteExecError(ERROR_EXEC, "can't exec command", {
             'out': res.stdout,
             'err': res.stderr
         })
     return RemoteExecResult(stdout=res.stdout,
                             stderr=res.stderr,
                             exit_code=res.exited)
示例#19
0
 def nodes(self):
     try:
         return self.__nodes
     except AttributeError:
         if self.deploy:
             user = '******'
         else:
             user = self.user
         connections = [
             fabric.Connection(host,
                               user=user,
                               gateway=self.frontend.nodes[0])
             for host in self.hostnames
         ]
         self.__nodes = Nodes(connections,
                              name='allnodes',
                              working_dir='/tmp')
         self.orchestra = Nodes(connections[1:],
                                name='orchestra',
                                working_dir='/tmp')
         self.director = Nodes([connections[0]],
                               name='director',
                               working_dir='/tmp')
         self.__open_nodes_connection()
         return self.__nodes
示例#20
0
def main():
    addr = get_remote_address()

    # should be executed as background process ?
    in_background = '-bg' in sys.argv
    if in_background:
        del sys.argv[sys.argv.index('-bg')]

    # execute on server
    # establish connection to the server
    connection = fabric.Connection(addr,
                                   inline_ssh_env=True)  # connect to server
    connection.client.load_system_host_keys()

    srun_options = load_srun_options(connection)
    env = get_environment_variables(srun_options)
    if 'CUDA_VISIBLE_DEVICES' in env:
        srun_options['CUDA_VISIBLE_DEVICES'] = env['CUDA_VISIBLE_DEVICES']
    path = '/tmp/ladolphs/{}'.format(uuid.uuid4())  # new tmp folder on server

    # upload the files to the server
    upload_files_to_server(addr, path)

    # construct the command
    cmd = get_commands(path, srun_options, in_background)

    construct_venv(srun_options['VIRTUALENV'], connection)

    # execute the command
    connection.run(cmd, env=env)

    if in_background:
        print('[i] Started background tmux session {} on {}'.format(
            path.split('/')[-1],
            addr.split('@')[-1]))
示例#21
0
def start_master(
    machine_ssh: str = typer.Argument(
        ..., help="machine ssh string: user@host:port"),
    machine_base_dir: str = typer.Argument(..., help="deployed base name"),
    submit_port: int = typer.Argument(..., help="submit port"),
    party_id: str = typer.Argument(..., help="party id"),
    cluster_manager_address: str = typer.Argument(
        ..., help="cluster manager address"),
    coordinator_address: str = typer.Argument(..., help="coordinator address"),
):
    with fabric.Connection(machine_ssh) as c:
        with c.cd(machine_base_dir):
            if c.run(
                    f"FEDVISION_PYTHON_EXECUTABLE={os.path.join(machine_base_dir, 'venv/bin/python')} "
                    f"{__BASE_NAME__}/sbin/master.sh start "
                    f"{submit_port} {party_id} {cluster_manager_address} {coordinator_address}",
                    warn=True,
            ).failed:
                typer.echo(f"failed: can't start master at port {submit_port}")
                return False
            else:
                typer.echo(
                    f"{machine_ssh}:{machine_base_dir} started master: port={submit_port}"
                )
                return True
示例#22
0
def start_cluster_worker(
        machine_ssh: str = typer.Argument(
            ..., help="machine ssh string: user@host:port"),
        machine_base_dir: str = typer.Argument(..., help="deployed base name"),
        name: str = typer.Argument(..., help="worker name"),
        local_ip: str = typer.Argument(..., help="local ip"),
        port_start: int = typer.Argument(..., help="port start"),
        port_end: int = typer.Argument(..., help="port start"),
        max_tasks: int = typer.Argument(...,
                                        help="num of maximum parallel tasks"),
        cluster_manager_address=typer.Argument(...,
                                               help="cluster manager address"),
        data_base_dir: str = typer.Option(None, "--data-dir", help="data dir"),
):
    if data_base_dir is None or isinstance(data_base_dir,
                                           typer.params.OptionInfo):
        data_base_dir = os.path.join(machine_base_dir, __BASE_NAME__, "data")
    with fabric.Connection(machine_ssh) as c:
        with c.cd(machine_base_dir):
            if c.run(
                    f"FEDVISION_PYTHON_EXECUTABLE={os.path.join(machine_base_dir, 'venv/bin/python')} "
                    f"{__BASE_NAME__}/sbin/cluster_worker.sh start "
                    f"{name} {local_ip} {port_start} {port_end} {max_tasks} {cluster_manager_address} {data_base_dir}",
                    warn=True,
            ).failed:
                typer.echo(f"failed: can't start cluster worker named {name}")
                return False
            else:
                typer.echo(
                    f"{machine_ssh}:{machine_base_dir} started cluster worker: name={name}"
                )
                return True
示例#23
0
def import_remote_trips(hostname: str, trip_dirs: List[str], out_dir: str,
                        progress_bar):
    logging.info("import_remote_trips()")
    with fabric.Connection(hostname) as conn:
        progress_bar.progress(0)
        for i, remote_trip_dir in enumerate(trip_dirs):
            logging.info(f"Importing {remote_trip_dir}")
            result = conn.run(f"ls {remote_trip_dir}", hide=True)
            trip_files = result.stdout.strip().split("\n")

            local_trip_dir = os.path.join(out_dir,
                                          os.path.basename(remote_trip_dir))
            os.makedirs(local_trip_dir, exist_ok=True)

            for trip_file in trip_files:
                logging.info(f"Copy {remote_trip_dir}/{trip_file}")
                conn.get(
                    f"{remote_trip_dir}/{trip_file}",
                    os.path.join(local_trip_dir, trip_file),
                )

            # TODO: Improve me!
            picam_h264_path = os.path.join(local_trip_dir, "picam.h264")
            if os.path.isfile(picam_h264_path):
                mp4_path = f"{picam_h264_path[:-4]}mp4"
                ffmpeg_path = r"C:\Users\vobject\Tools\ffmpeg-4.2.1-win64-static\bin\ffmpeg.exe"
                # ffmpeg_path = r"C:\Users\user\Downloads\Python\ffmpeg-20200417-889ad93-win64-static\bin\ffmpeg.exe"
                cmd = f"{ffmpeg_path} -framerate 10 -i {picam_h264_path} -c copy {mp4_path} -y"
                logging.info(cmd)
                subprocess.run(cmd)

            progress_bar.progress(1. / len(trip_dirs) * (i + 1))
示例#24
0
文件: ssh_plugin.py 项目: nasa/CTF
    def init_connection(self,
                        host,
                        user=None,
                        port=None,
                        gateway=None,
                        ssh_config_path=None,
                        args=None):
        """
        init_connection provides implementation of SSH plugin's init_connection method:
                self.targets[name].init_connection(host, user, port, gateway, ssh_config_path, args)
        """
        try:
            if self.connection and self.connection.is_connected:
                log.debug("Closing connection to {}".format(
                    self.connection.host))
                self.connection.close()

            if ssh_config_path:
                ssh_config_path = expand_path(ssh_config_path)
            config = fabric.Config(runtime_ssh_path=ssh_config_path)
            self.connection = fabric.Connection(host,
                                                user=user,
                                                port=port,
                                                gateway=gateway,
                                                config=config,
                                                connect_kwargs=args)
            log.debug("Opening connection to {}...".format(
                self.connection.host))
            self.connection.open()
            return True
        except Exception as exception:
            log.error("Remote connection failed: {}".format(exception))
            raise CtfTestError("Error in init_connection") from exception
示例#25
0
文件: ssh.py 项目: wrabcak/core
def connect(host, users=None):
    log.debug("Connecting to '{}'".format(host))
    if "@" in host:
        parts = host.split("@")
        assert len(parts) == 2
        host = parts[1]
        if not users:
            users = [parts[0]]
    if not users:
        users = [
            "Administrator", "ubuntu", "ec2-user", "centos", "vagrant", "root"
        ]
        # Similar to ssh, try own username first,
        # some systems will lock us out if we have too many failed attempts.
        if whoami() not in users:
            users = [whoami()] + users
    for user in users:
        try:
            log.debug("Attempting ssh: {}@{}".format(user, host))
            c = fabric.Connection(host=host, user=user)
            c.ssh_user = user
            c.ssh_host = host
            c.run("whoami", hide=True)
            return c
        except AuthenticationException:
            continue
    sys.exit("Could not ssh into '{}'".format(host))
示例#26
0
文件: srun.py 项目: yk/srun
def main():
    if os.getcwd() == os.path.expanduser('~'):
        print('NO')
        exit(1)
    addr = sys.argv[1]
    del sys.argv[1]
    path = '/tmp/{}'.format(uuid.uuid4())
    env = []
    while '=' in sys.argv[1]:
        env.append(sys.argv[1].split('='))
        del sys.argv[1]
    env = dict(env)

    print(
        sh.rsync('-a', '-v', '-z', '--exclude', '__pycache__', '--exclude',
                 '*.swp', '--exclude', '.git', '{}/'.format(os.getcwd()),
                 '{}:{}'.format(addr, path)))

    cmd = 'cd {} && {}'.format(path, ' '.join(sys.argv[1:]))

    connection = fabric.Connection(addr, inline_ssh_env=True)
    connection.client.load_system_host_keys()
    connection.run(cmd, env=env)

    print(
        sh.rsync('-a', '-v', '-z', '{}:{}/logs/'.format(addr, path),
                 os.getcwd() + '/logs'))
示例#27
0
def ssh_cmd_to_vm(cmd_to_run, vm_name):
    ''' Runs a command on a named VM. Returns success_boolean, output_text '''
    if not vm_name in VM_INFO:
        die("Attempt to run on {}, which is not a valid VM".format(vm_name))
    is_vm_running(vm_name)
    this_control_address = (rt_config["vm_info"][vm_name]).get("control_addr")
    if not this_control_address:
        die("There was no address for {}".format(vm_name))
    fabconn = fabric.Connection(host=this_control_address,
                                user="******",
                                connect_kwargs={"password": ROOT_PASS})
    try:
        fabconn.open()
    except Exception as this_e:
        die("Could not open an SSH connection to {} on {}: '{}'.".format(
            vm_name, this_control_address, this_e))
    # Is this the right VM?
    ret_hostname = fabconn.run("hostname", hide=True)
    if ret_hostname.failed:
        die("Could not run hostname on {}".format(vm_name))
    ret_text = ret_hostname.stdout
    if ret_text.rstrip() != vm_name:
        die("The host at {} is not {}: '{}'".format(this_control_address,
                                                    vm_name, ret_text))
    # Run the command
    ret_main_cmd = fabconn.run(cmd_to_run, hide=True, warn=True)
    if ret_main_cmd.failed:
        return False, "Error: {}".format(ret_main_cmd.stderr.strip())
    else:
        return True, ret_main_cmd.stdout.strip()
    fabconn.close()
示例#28
0
def connect():
    return fabric.Connection(host=os.environ['HOST'],
                             user=os.environ['USER'],
                             connect_kwargs=dict(
                                 password=os.getenv('PASSWORD'),
                                 key_filename=os.getenv('SSH_KEY')),
                             port=os.getenv('PORT', 22))
示例#29
0
def deploy_cluster_resources(cluster: structures.cluster.Cluster) -> None:

    handle, tmpfile = tempfile.mkstemp(prefix="cluster_bootstrap")

    # Write the bootstrap cluster yaml
    with open(handle, 'w') as f:
        yaml = cluster.to_yaml()
        f.write(yaml)

    # Deploy the bootstrap yaml to all cluster nodes
    ssh_key = Path(cluster.auth.ssh_private_key).expanduser().absolute()
    hosts = [cluster.provider.head_ip] + cluster.provider.worker_ips

    for host in tuple(hosts):

        connection = fabric.Connection(
            host=host,
            user=cluster.auth.ssh_user,
            connect_kwargs={'key_filename': str(ssh_key)})

        result: transfer.Result = connection.put(
            local=tmpfile, remote="cluster_bootstrap.yaml")

        result: transfer.Result = connection.put(local=str(ssh_key),
                                                 remote="cluster_ssh_key.pem")
示例#30
0
    def _deploy_keystore(self, password: str):
        with fabric.Connection(f'pi@{root_node}',
                               connect_kwargs={"password": password}) as conn:
            src = "./setup/keystore"
            dest = "/home/pi/iot-trust-task-alloc/resource_rich/root"

            patchwork.transfers.rsync(conn, src, dest, rsync_opts="-r")