def __call__(self, environ, start_response): request = Request(environ) if request.path.endswith('shell') and request.method == 'GET': try: backend = self.app.routes_mapper(request)['match']['backend'] machine = self.app.routes_mapper(request)['match']['machine'] host = request.params.get('host', None) ssh_user = request.params.get('ssh_user', None) command = request.params.get('command', None) request.registry = self.app.registry if not ssh_user or ssh_user == 'undefined': log.debug("Will select root as the ssh-user as we don't know who we are") ssh_user = '******' try: keypairs = environ['beaker.session']['keypairs'] except: keypairs = request.registry.settings.get('keypairs', {}) preferred_keypairs = get_preferred_keypairs(keypairs, backend, machine) log.debug("preferred keypairs = %s" % preferred_keypairs) if preferred_keypairs: keypair = keypairs[preferred_keypairs[0]] private_key = keypair['private'] s_user = get_ssh_user_from_keypair(keypair, backend, machine) log.debug("get user from keypair returned: %s" % s_user) if s_user: ssh_user = s_user log.debug("Will select %s as the ssh-user" % ssh_user) else: private_key = None conn = connect(request, backend) if conn: return self.stream_command(conn, machine, host, ssh_user, private_key, command, start_response) else: raise except: # leave error handling up to the app return self.app(environ, start_response) else: return self.app(environ, start_response)
def __call__(self, environ, start_response): request = Request(environ) if request.path.endswith('shell') and request.method == 'GET': try: backend = self.app.routes_mapper(request)['match']['backend'] machine = self.app.routes_mapper(request)['match']['machine'] host = request.params.get('host', None) ssh_user = request.params.get('ssh_user', None) command = request.params.get('command', None) request.registry = self.app.registry if not ssh_user or ssh_user == 'undefined': log.debug("Will select root as the ssh-user as we don't know who we are") ssh_user = '******' with get_user(request, readonly=True) as user: keypairs = user['keypairs'] preferred_keypairs = get_preferred_keypairs(keypairs, backend, machine) log.debug("preferred keypairs = %s" % preferred_keypairs) if preferred_keypairs: keypair = keypairs[preferred_keypairs[0]] private_key = keypair['private'] s_user = get_ssh_user_from_keypair(keypair, backend, machine) log.debug("get user from keypair returned: %s" % s_user) if s_user: ssh_user = s_user log.debug("Will select %s as the ssh-user" % ssh_user) else: private_key = None log.error("Missing private key") raise Exception("Missing private key") conn = connect(request, backend) if conn: return self.stream_command(conn, machine, host, ssh_user, private_key, command, start_response) else: raise except: # leave error handling up to the app return self.app(environ, start_response) else: return self.app(environ, start_response)