コード例 #1
0
ファイル: ssh.py プロジェクト: harshilasu/GraphicMelon
    def Run(self, args):
        super(SSH, self).Run(args)
        parts = args.user_host.split('@')
        if len(parts) == 1:
            user = getpass.getuser()
            instance = parts[0]
        elif len(parts) == 2:
            user, instance = parts
        else:
            raise exceptions.ToolException(
                'Expected argument of the form [USER@]INSTANCE; received [{0}].'
                .format(args.user_host))

        instance_ref = self.CreateZonalReference(instance, args.zone)
        external_ip_address = self.GetInstanceExternalIpAddress(instance_ref)

        ssh_args = [self.ssh_executable]
        if not args.plain:
            ssh_args.extend(self.GetDefaultFlags())
            # Allocates a tty if no command was provided and a container was provided.
            if args.container and not args.command:
                ssh_args.append('-t')

        if args.ssh_flag:
            for flag in args.ssh_flag:
                dereferenced_flag = (flag.replace('%USER%', user).replace(
                    '%INSTANCE%', external_ip_address))
                ssh_args.append(dereferenced_flag)

        ssh_args.append(ssh_utils.UserHost(user, external_ip_address))

        interactive_ssh = False
        if args.container:
            ssh_args.append('--')
            ssh_args.append('container_exec')
            ssh_args.append(args.container)
            # Runs the given command inside the given container if --command was
            # specified, otherwise runs /bin/sh.
            if args.command:
                ssh_args.append(args.command)
            else:
                ssh_args.append('/bin/sh')

        elif args.command:
            ssh_args.append('--')
            ssh_args.append(args.command)

        else:
            interactive_ssh = True

        self.ActuallyRun(args,
                         ssh_args,
                         user,
                         external_ip_address,
                         interactive_ssh=interactive_ssh)
コード例 #2
0
  def Run(self, args):
    super(CopyFiles, self).Run(args)

    file_specs = []

    # Parses the positional arguments.
    for arg in args.sources + [args.destination]:
      # If the argument begins with "./" or "/", then we are dealing
      # with a local file that can potentially contain colons, so we
      # avoid splitting on colons. The case of remote files containing
      # colons is handled below by splitting only on the first colon.
      if arg.startswith('./') or arg.startswith('/'):
        file_specs.append(LocalFile(arg))
        continue

      host_file_parts = arg.split(':', 1)
      if len(host_file_parts) == 1:
        file_specs.append(LocalFile(host_file_parts[0]))
      else:
        user_host, file_path = host_file_parts
        user_host_parts = user_host.split('@', 1)
        if len(user_host_parts) == 1:
          user = getpass.getuser()
          instance = user_host_parts[0]
        else:
          user, instance = user_host_parts

        file_specs.append(RemoteFile(user, instance, file_path))

    logging.debug('Normalized arguments: %s', file_specs)

    # Validates the positional arguments.
    # TODO(user): Look into relaxing these conditions.
    sources = file_specs[:-1]
    destination = file_specs[-1]
    if isinstance(destination, LocalFile):
      for source in sources:
        if isinstance(source, LocalFile):
          raise exceptions.ToolException(
              'All sources must be remote files when the destination '
              'is local.')

    else:  # RemoteFile
      for source in sources:
        if isinstance(source, RemoteFile):
          raise exceptions.ToolException(
              'All sources must be local files when the destination '
              'is remote.')

    instances = set()
    for file_spec in file_specs:
      if isinstance(file_spec, RemoteFile):
        instances.add(file_spec.instance_name)

    if len(instances) > 1:
      raise exceptions.ToolException(
          'Copies must involve exactly one virtual machine instance; '
          'your invocation refers to [{0}] instances: [{1}].'.format(
              len(instances), ', '.join(sorted(instances))))

    instance_ref = self.CreateZonalReference(instances.pop(), args.zone)
    external_ip_address = self.GetInstanceExternalIpAddress(instance_ref)

    # Builds the scp command.
    scp_args = [self.scp_executable]
    if not args.plain:
      scp_args.extend(self.GetDefaultFlags())
      scp_args.append('-r')

    for file_spec in file_specs:
      if isinstance(file_spec, LocalFile):
        scp_args.append(file_spec.file_path)

      else:
        scp_args.append('{0}:{1}'.format(
            ssh_utils.UserHost(file_spec.user, external_ip_address),
            file_spec.file_path))

    self.ActuallyRun(args, scp_args, user, external_ip_address)
コード例 #3
0
    def Run(self, args):
        super(SshGA, self).Run(args)

        parts = args.user_host.split('@')
        if len(parts) == 1:
            if self._use_accounts_service:  # Using Account Service.
                user = gaia_utils.GetDefaultAccountName(self.http)
            else:  # Uploading keys through metadata.
                user = getpass.getuser()
            instance = parts[0]
        elif len(parts) == 2:
            user, instance = parts
        else:
            raise exceptions.ToolException(
                'Expected argument of the form [USER@]INSTANCE; received [{0}].'
                .format(args.user_host))

        instance_ref = self.CreateZonalReference(instance, args.zone)
        external_ip_address = self.GetInstanceExternalIpAddress(instance_ref)

        ssh_args = [self.ssh_executable]
        if not args.plain:
            ssh_args.extend(self.GetDefaultFlags())
            # Allocates a tty if no command was provided and a container was provided.
            if args.container and not args.command:
                ssh_args.append('-t')

        if args.ssh_flag:
            for flag in args.ssh_flag:
                dereferenced_flag = (flag.replace('%USER%', user).replace(
                    '%INSTANCE%', external_ip_address))
                ssh_args.append(dereferenced_flag)

        ssh_args.append(ssh_utils.UserHost(user, external_ip_address))

        interactive_ssh = False
        if args.implementation_args:
            # Backslash-escape shell special characters -- ultra conservative.
            pattern = re.compile(r'(\W)')
            ssh_args.append(' '.join(
                pattern.sub(r'\\\1', arg) for arg in args.implementation_args))
        if args.container:
            ssh_args.append('--')
            ssh_args.append('container_exec')
            ssh_args.append(args.container)
            # Runs the given command inside the given container if --command was
            # specified, otherwise runs /bin/sh.
            if args.command:
                ssh_args.append(args.command)
            else:
                ssh_args.append('/bin/sh')

        elif args.command:
            ssh_args.append('--')
            ssh_args.append(args.command)

        else:
            interactive_ssh = True

        self.ActuallyRun(args,
                         ssh_args,
                         user,
                         external_ip_address,
                         interactive_ssh=interactive_ssh,
                         use_account_service=self._use_accounts_service)