def Run(self, args): command_list = args.command.split(' ') if args.command else ['bash -l'] project = properties.VALUES.core.project.Get() connection_info = util.PrepareEnvironment(args) command = ssh.SSHCommand( remote=ssh.Remote(host=connection_info.host, user=connection_info.user), port=six.text_type(connection_info.port), identity_file=connection_info.key, remote_command=(['DEVSHELL_PROJECT_ID=' + project] if project else []) + command_list, extra_flags=args.ssh_flag, tty=not args.command, options={'StrictHostKeyChecking': 'no'}, ) if args.dry_run: log.Print(' '.join(command.Build(connection_info.ssh_env))) else: self.done = threading.Event() thread = threading.Thread(target=self.Reauthorize, args=()) thread.daemon = True thread.start() command.Run(connection_info.ssh_env) self.done.set()
def testKeyRunning(self): # Given a running environment with a key self.expectGetEnvironment(response=self.makeEnvironment( has_key=True, running=True, )) # Expect that we neither create a key or start the environment util.PrepareEnvironment(self.makeArgs())
def testNoKeyRunning(self): # Given an environment without a key that is running self.expectGetEnvironment(response=self.makeEnvironment( has_key=False, running=True, )) # Expect that we create a key, but don't start the environment self.expectCreatePublicKey() util.PrepareEnvironment(self.makeArgs())
def testReturnsConnectionInfo(self): self.expectGetEnvironment(response=self.makeEnvironment( has_key=True, running=True, user='******', host='my-host', port=123, )) connection_info = util.PrepareEnvironment(self.makeArgs()) self.assertEqual(connection_info.user, 'my-user') self.assertEqual(connection_info.host, 'my-host') self.assertEqual(connection_info.port, 123)
def testKeyNotRunning(self): # Given an environment that has a key but isn't running self.expectGetEnvironment(response=self.makeEnvironment( has_key=True, running=False, )) # Expect that we start the environment, but don't create a key self.expectStartEnvironment(response=self.messages.Operation( name='some-op')) self.expectGetOperation(name='some-op') self.expectGetEnvironment() util.PrepareEnvironment(self.makeArgs())
def testNoKeyNotRunning(self): # Given an environment without a key that isn't running self.expectGetEnvironment(response=self.makeEnvironment( has_key=False, running=False, )) # Expect that we both create a key and start the environment self.expectCreatePublicKey() self.expectStartEnvironment(response=self.messages.Operation( name='some-op')) self.expectGetOperation(name='some-op') self.expectGetEnvironment() util.PrepareEnvironment(self.makeArgs())
def Run(self, args): if platforms.OperatingSystem.IsWindows(): raise util.UnsupportedPlatform( 'get-mount-command is not currently supported on Windows') else: connection_info = util.PrepareEnvironment(args) log.Print('sshfs {user}@{host}: {mount_dir} -p {port} ' '-oIdentityFile={key_file} -oStrictHostKeyChecking=no'.format( user=connection_info.user, host=connection_info.host, mount_dir=args.mount_dir, port=connection_info.port, key_file=connection_info.key, ))
def testKeyNotRunningNeedsAuthorizedSession(self): # Given an environment that has a key but isn't running self.expectGetEnvironment(response=self.makeEnvironment( has_key=True, running=False, )) # Expect that we start the environment, but don't create a key self.expectStartEnvironment( response=self.messages.Operation(name='some-op'), access_token='access_token') self.expectGetOperation(name='some-op') self.expectGetEnvironment() args = self.makeArgs(authorize_session=True) util.PrepareEnvironment(args)
def Run(self, args): command_list = args.command.split(' ') if args.command else None connection_info = util.PrepareEnvironment(args) command = ssh.SSHCommand( remote=ssh.Remote(host=connection_info.host, user=connection_info.user), port=str(connection_info.port), identity_file=connection_info.key, remote_command=containers.GetRemoteCommand(None, command_list), extra_flags=args.ssh_flag, tty=containers.GetTty(None, args.command), options={'StrictHostKeyChecking': 'no'}, ) if args.dry_run: log.Print(' '.join(command.Build(connection_info.ssh_env))) else: command.Run(connection_info.ssh_env)
def Run(self, args): connection_info = util.PrepareEnvironment(args) remote = ssh.Remote(host=connection_info.host, user=connection_info.user) command = ssh.SCPCommand( sources=[ToFileReference(src, remote) for src in args.sources], destination=ToFileReference(args.destination, remote), recursive=args.recurse, compress=False, port=str(connection_info.port), identity_file=connection_info.key, extra_flags=args.scp_flag, options={'StrictHostKeyChecking': 'no'}, ) if args.dry_run: log.Print(' '.join(command.Build(connection_info.ssh_env))) else: command.Run(connection_info.ssh_env)
def testSlowStart(self): # Given an environment without a key that is running self.expectGetEnvironment( response=self.makeEnvironment( has_key=True, running=False, )) # Expect that we will start it and poll the operation until it is done self.expectStartEnvironment( response=self.messages.Operation(name='my-op', done=False)) self.expectGetOperation( 'my-op', response=self.makeOperation(name='my-op', done=False)) self.expectGetOperation( 'my-op', response=self.makeOperation(name='my-op', done=False)) self.expectGetOperation( 'my-op', response=self.makeOperation(name='my-op', done=True)) self.expectGetEnvironment() util.PrepareEnvironment(self.makeArgs())
def Run(self, args): command_list = args.command.split(' ') if args.command else ['bash -l'] project = properties.VALUES.core.project.Get() connection_info = util.PrepareEnvironment(args) command = ssh.SSHCommand( remote=ssh.Remote(host=connection_info.host, user=connection_info.user), port=str(connection_info.port), identity_file=connection_info.key, remote_command=(['DEVSHELL_PROJECT_ID=' + project] if project else []) + command_list, extra_flags=args.ssh_flag, tty=not args.command, options={'StrictHostKeyChecking': 'no'}, ) if args.dry_run: log.Print(' '.join(command.Build(connection_info.ssh_env))) else: command.Run(connection_info.ssh_env)
def Run(self, args): if not args.authorize_session: log.Print( 'Automatic authentication with GCP CLI tools in Cloud Shell is ' 'disabled. To enable, please rerun command with ' '`--authorize-session` flag.') command_list = args.command.split(' ') if args.command else ['bash -l'] project = properties.VALUES.core.project.Get() connection_info = util.PrepareEnvironment(args) if args.authorize_session: util.AuthorizeEnvironment() command = ssh.SSHCommand( remote=ssh.Remote(host=connection_info.host, user=connection_info.user), port=six.text_type(connection_info.port), identity_file=connection_info.key, remote_command=(['DEVSHELL_PROJECT_ID=' + project] if project else []) + command_list, extra_flags=args.ssh_flag, tty=not args.command, options={'StrictHostKeyChecking': 'no'}, ) if args.dry_run: elems = command.Build(connection_info.ssh_env) log.Print(' '.join([six.moves.shlex_quote(elem) for elem in elems])) elif args.authorize_session: self.done = threading.Event() thread = threading.Thread(target=self.Reauthorize, args=()) thread.daemon = True thread.start() command.Run(connection_info.ssh_env) self.done.set() else: command.Run(connection_info.ssh_env)