Beispiel #1
0
def _GetSharedSshArgs(args, user, instance, ip_address, env, keys):
    """Returns list with ssh commands and shared arguments it."""
    ssh_args = [env.ssh]
    if not args.plain:
        ssh_args.extend(ssh.GetDefaultFlags(keys.key_file))
        # 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:
            for flag_part in flag.split():  # We want grouping here
                dereferenced_flag = (flag_part.replace('%USER%', user).replace(
                    '%INSTANCE%', ip_address))
                ssh_args.append(dereferenced_flag)

    host_key_alias = HostKeyAlias(instance)
    ssh_args.extend(
        ssh.GetHostKeyArgs(host_key_alias, args.plain,
                           args.strict_host_key_checking))

    ssh_args.append(ssh.UserHost(user, ip_address))
    return ssh_args
Beispiel #2
0
    def Run(self, args):
        super(SshGA, self).Run(args)
        user, instance_name = ssh_utils.GetUserAndInstance(
            args.user_host, self._use_accounts_service, self.http)
        instance_ref = instance_flags.SSH_INSTANCE_RESOLVER.ResolveResources(
            [instance_name],
            compute_scope.ScopeEnum.ZONE,
            args.zone,
            self.resources,
            scope_lister=flags.GetDefaultScopeLister(self.compute_client))[0]
        instance = self.GetInstance(instance_ref)
        external_ip_address = ssh_utils.GetExternalIPAddress(instance)

        ssh_args = [self.env.ssh]
        if not args.plain:
            ssh_args.extend(ssh.GetDefaultFlags(self.keys.key_file))
            # 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:
                for flag_part in flag.split():  # We want grouping here
                    dereferenced_flag = (flag_part.replace(
                        '%USER%', user).replace('%INSTANCE%',
                                                external_ip_address))
                    ssh_args.append(dereferenced_flag)

        host_key_alias = ssh_utils.HostKeyAlias(instance)
        ssh_args.extend(
            ssh.GetHostKeyArgs(host_key_alias, args.plain,
                               args.strict_host_key_checking))

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

        if args.ssh_args:
            ssh_args.extend(args.ssh_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:
            if not platforms.OperatingSystem.IsWindows():
                ssh_args.append('--')
            ssh_args.append(args.command)

        # Don't use strict error checking for ssh: if the executed command fails, we
        # don't want to consider it an error. We do, however, want to propagate its
        # return code.
        return_code = self.ActuallyRun(
            args,
            ssh_args,
            user,
            instance,
            instance_ref.project,
            external_ip_address,
            strict_error_checking=False,
            use_account_service=self._use_accounts_service)
        if return_code:
            # Can't raise an exception because we don't want any "ERROR" message
            # printed; the output from `ssh` will be enough.
            sys.exit(return_code)
Beispiel #3
0
    def Run(self, args):
        super(CopyFiles, self).Run(args)
        # TODO(b/33467618): Change this implementation to use ssh.SCPCommand and
        # ssh.FileReference objects.

        file_specs = []

        # Parses the positional arguments.
        for arg in args.sources + [args.destination]:
            if ssh.IsScpLocalPath(arg):
                file_specs.append(LocalFile(arg))
            else:
                user_host, file_path = arg.split(':', 1)
                user_host_parts = user_host.split('@', 1)
                if len(user_host_parts) == 1:
                    user = ssh.GetDefaultSshUsername(warn_on_account_user=True)
                    source_instance = user_host_parts[0]
                else:
                    user, source_instance = user_host_parts
                file_specs.append(RemoteFile(user, source_instance, file_path))

        log.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.')

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

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

        source_instance_ref = instance_flags.SSH_INSTANCE_RESOLVER.ResolveResources(
            [source_instance],
            compute_scope.ScopeEnum.ZONE,
            args.zone,
            self.resources,
            scope_lister=flags.GetDefaultScopeLister(self.compute_client,
                                                     self.project))[0]
        source_instance = self.GetInstance(source_instance_ref)
        external_ip_address = ssh_utils.GetExternalIPAddress(source_instance)

        # Builds the scp command.
        scp_args = [self.env.scp]
        if not args.plain:
            scp_args.extend(ssh.GetDefaultFlags(self.keys.key_file))
            host_key_alias = self.HostKeyAlias(source_instance)
            scp_args.extend(
                ssh.GetHostKeyArgs(host_key_alias, args.plain,
                                   args.strict_host_key_checking))
            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.UserHost(file_spec.user, external_ip_address),
                    file_spec.file_path))

        self.ActuallyRun(args, scp_args, user, source_instance,
                         source_instance_ref.project)