def adm(id): b9y = B9y('http://b9y.redis.sg:8050', 'admin', 'XXXXX') sessions = b9y.keys("lab:" + str(id) + ":") s2 = {} for s in sessions: sd = b9y.get(s) s2[s] = sd return dict(labid=str(id), list=sessions, grid=s2)
def do_login(): b9y = B9y('http://b9y.redis.sg:8050', 'admin', 'XXXXXXXXX') pin = request.query['pin'] # cp = request.query['g-recaptcha-response'] lab_data = b9y.get("lab:" + str(pin)) #if cp == None or cp == "": # return template('form', message="Are you a robot?") if lab_data == None: return template('form', message="Invalid PIN. Please try again!") try: lab = json.loads(lab_data) info = lab["info"] session = request.get_cookie("session") except: return template('msg', msg="Something wrent horribly wrong.") if session == None: session = str(uuid.uuid4()).replace("-", "") response.set_cookie("session", str(session), expires=1666908083) session_data_key = "lab:" + str(pin) + ":" + session sessioninfo = b9y.get(session_data_key) # get a resource if session info is empty if sessioninfo == None or sessioninfo == "None": sessioninfo = b9y.pop("stuff") b9y.set(session_data_key, sessioninfo) # if still empty we ran out of resources if sessioninfo == None or sessioninfo == "None": return template( 'msg', msg= "This lab has no more available resources. Sorry! <a href='/logout'> (logout)</a>" ) return template('session', labid=pin, labsession=session, labinfo=info, sessioninfo=sessioninfo)
class b9y_prompt(Cmd): try: args = getopts(sys.argv) except: sys.exit(1) if '-h' in args: b9y_host = args['-h'] else: b9y_host = default_host if '-u' in args: b9y_user = args['-u'] else: b9y_user = default_user if '-p' in args: b9y_password = args['-p'] else: b9y_password = default_password try: b9y = B9y(b9y_host, b9y_user, b9y_password) except: print("""ERROR: unable to connect or credentials not valid. Try using parameters to specifiy hostname and credentials, e.g. b9y-cli -h http://localhost:8888 b9y-cli -h http://b9y.myhost.com:8080 -u user1 - p secret """) sys.exit() b9y_instance, b9y_release = b9y.info() print("Bambleweeny CLI Version " + b9y_cli_release + "\nConnected to " + b9y_instance + " as " + b9y_user) prompt = "b9y v" + str(b9y_release) + "> " intro = "Welcome! Type ? to list commands" def do_token(self, inp): print(self.b9y.get_token()) def do_save(self, inp): r = self.b9y.save() print(r) if r == None: print("error - are you admin?") else: print("ok") def do_exit(self, inp): print("Bye!") return True def do_EOF(self, inp): print("Bye!") return True def help_exit(self): print('Exit the application.') def do_create_user(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) try: r = self.b9y.create_user(items[0], items[1]) print r print("OK. New user id is " + str(r)) except: print("error") def do_users(self, inp): try: r = self.b9y.list_users() for k in r["users"]: print("USER: "******" ID: " + str(k['id']) + " QUOTA: " + str(k['quota'])) except: print("Error. Are you admin?") def do_set(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) r = self.b9y.set(items[0], remove_quotes(items[1])) if r: print("OK") def do_route(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) r = self.b9y.create_route(items[0], remove_quotes(items[1])) if r: print(r) def do_push(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) r = self.b9y.push(items[0], remove_quotes(items[1])) if r: print("OK") def do_incr(self, inp): items = shlex.split(inp, posix=False) if len(items) != 1: print("Error: need exactly one argument.") return (None) r = self.b9y.incr(items[0]) if r != None: print(str(r)) else: print("Error") def do_get(self, inp): items = shlex.split(inp, posix=False) if len(items) != 1: print("Error: need exactly one argument.") return (None) r = self.b9y.get(items[0]) print(str(r)) def do_uget(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) r = self.b9y.uget(items[0], items[1]) print(str(r)) def do_keys(self, inp): items = shlex.split(inp, posix=False) if len(items) == 0: r = self.b9y.keys() else: try: r = self.b9y.keys(items[0]) except: return (None) for k in r: print(k) def do_pop(self, inp): items = shlex.split(inp, posix=False) if len(items) != 1: print("Error: need exactly one argument.") return (None) r = self.b9y.pop(items[0]) print(str(r)) def help_set(self): print("Set a Key. Example: set foo bar") def help_create_user(self): print( "** for admin use** Create a User. Example: create_user user1 secret" ) def help_users(self): print("** for admin use** Lists all users") def help_save(self): print("** trigger a 'save' on Redis to dump data to the filesystem") def help_token(self): print("Gives you a bearer token for including in HTTP requests") def help_route(self): print("Make key publicly readable. Example: route foo text/html") def help_keys(self): print( "List all Keys. You may specifiy an oprtional search string, 'grep style'.\nExample: keys\nExample: keys sys" ) def help_get(self): print("Get a Key. Example: get foo") def help_uget(self): print("** for admin use** Get a Key from a userid. Example: get foo 1") def help_push(self): print("Adds a value to a list / queue. Example: push foo bar") def help_pop(self): print("Retrieves an item from a list. Example: pop foo") def help_incr(self): print("Increase Counter. Example: incr ticket_number") def emptyline(self): print("") def default(self, inp): if inp == 'q': return self.do_exit(inp) print "No idea what you want here. Type 'help' for available commands."
class b9y_prompt(Cmd): try: args = getopts(argv) except: exit(1) if '-h' in args: b9y_host = args['-h'] else: b9y_host = default_host if '-u' in args: b9y_user = args['-u'] else: b9y_user = default_user if '-p' in args: b9y_password = args['-p'] else: b9y_password = default_password try: b9y = B9y(b9y_host, b9y_user, b9y_password) except: print("ERROR: unable to connect or credentials not valid") exit() b9y_instance, b9y_release = b9y.info() print("Bambleweeny CLI Version " + b9y_cli_release + "\nConnected to " + b9y_instance + " as " + b9y_user) prompt = "b9y v" + str(b9y_release) + "> " intro = "Welcome! Type ? to list commands" def do_exit(self, inp): print("Bye!") return True def help_exit(self): print('Exit the application.') def do_set(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) r = self.b9y.set(items[0], items[1]) if r: print("OK") def do_push(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) r = self.b9y.push(items[0], items[1]) if r: print("OK") def do_incr(self, inp): items = shlex.split(inp, posix=False) if len(items) != 1: print("Error: need exactly one argument.") return (None) r = self.b9y.incr(items[0]) if r != None: print(str(r)) else: print("Error") def do_get(self, inp): items = shlex.split(inp, posix=False) if len(items) != 1: print("Error: need exactly one argument.") return (None) r = self.b9y.get(items[0]) print(str(r)) def do_uget(self, inp): items = shlex.split(inp, posix=False) if len(items) != 2: print("Error: need exactly two arguments.") return (None) r = self.b9y.uget(items[0], items[1]) print(str(r)) def do_keys(self, inp): items = shlex.split(inp, posix=False) if len(items) == 0: r = self.b9y.keys() else: try: r = self.b9y.keys(items[0]) except: return (None) for k in r: print(k) def do_pop(self, inp): items = shlex.split(inp, posix=False) if len(items) != 1: print("Error: need exactly one argument.") return (None) r = self.b9y.pop(items[0]) print(str(r)) def help_set(self): print("Set a Key. Example: set foo bar") def help_keys(self): print( "List all Keys. You may specifiy an oprtional search string, 'grep style'.\nExample: keys\nExample: keys sys" ) def help_get(self): print("Get a Key. Example: get foo") def help_uget(self): print("** for admin use** Get a Key from a userid. Example: get foo 1") def help_push(self): print("Adds a value to a list / queue. Example: push foo bar") def help_pop(self): print("Retrieves an item from a list. Example: pop foo") def help_incr(self): print("Increase Counter. Example: incr ticket_number") def emptyline(self): print("") def default(self, inp): if inp == 'q': return self.do_exit(inp) print "No idea what you want here. Type 'help' for available commands."
from b9y import B9y b9y = B9y() # Get info print b9y.info() # Set and get key b9y.set('foo', 'bar') print b9y.get('foo') # Try the counter print b9y.incr('counter') print b9y.incr('counter') print b9y.incr('counter') # Push and pop b9y.push('super', 'trouper') print b9y.pop('super')