def getConnectionManageRefresh(self, guacHostname, username, password, url_path, method): logging.debug("getConnectionManageStatus(): instantiated") self.writeStatus = ConnectionManage.CONNECTION_MANAGE_REFRESHING try: self.lock.acquire() self.usersConnsStatus.clear() guacConn = Guacamole(guacHostname, username=username, password=password, url_path=url_path, method=method) #username, connName/VMName, userStatus (admin/etc.), connStatus (connected/not) users = guacConn.get_users() connIDsNames = {} activeConns = {} allConnections = guacConn.get_connections() if 'childConnections' in allConnections: for conn in guacConn.get_connections()['childConnections']: connIDsNames[conn['identifier']] = conn['name'] guac_activeConns = guacConn.get_active_connections() for conn in guac_activeConns: activeConns[( guac_activeConns[conn]["username"], guac_activeConns[conn]["connectionIdentifier"])] = True for user in users: #user status first perm = guacConn.get_permissions(user) user_perm = "not_found" if "READ" in perm['userPermissions'][user]: user_perm = "Non-Admin" if "ADMINISTER" in perm['userPermissions'][user]: user_perm = "Admin" #next, get the list of connections and the names of those connections and their status associated with those connections for connID in perm['connectionPermissions']: active = "not_connected" #if the connection is in an active state (exists in our activeConns dict), then state it as such if (user, connID) in activeConns: active = "connected" self.usersConnsStatus[(user, connIDsNames[connID])] = { "user_status": user_perm, "connStatus": active } except Exception as e: logging.error( "Error in getConnectionManageStatus(). Did not remove connection or relation!" ) exc_type, exc_value, exc_traceback = sys.exc_info() trace_back = traceback.extract_tb(exc_traceback) #traceback.print_exception(exc_type, exc_value, exc_traceback) return None finally: self.lock.release() self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE
def generate_conn(self, username=None, password=None): if not username: username = '******' if not password: password = '******' guacConn = Guacamole(self.addr, username=username, password=password, url_path=self.path, method='http') return guacConn
def runClearAllConnections(self, guacHostname, username, password, url_path, method): self.writeStatus = ConnectionManage.CONNECTION_MANAGE_REMOVING #sample guacConn = Guacamole(192.168.99.102',username='******',password='******',url_path='/guacamole',method='http') logging.debug("runClearAllConnections(): guacHostname: " + str(guacHostname) + " username/pass: "******" url_path: " + url_path + " method: " + str(method)) guacConn = Guacamole(guacHostname, username=username, password=password, url_path=url_path, method=method) if guacConn == None: logging.error("Error with guac connection... skipping: " + str(guacHostname) + " " + str(username)) self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE return -1 # Get list of all users usernames = guacConn.get_users() for username in usernames: logging.debug("Removing Username: "******"runClearAllConnections(): Error in runClearAllConnections(): when trying to remove user." ) exc_type, exc_value, exc_traceback = sys.exc_info() #traceback.print_exception(exc_type, exc_value, exc_traceback) # Remove All Connections connections = guacConn.get_connections() logging.debug("Retrieved Connections: " + str(connections)) if "childConnections" in connections: for connection in connections["childConnections"]: logging.debug("Removing Connection: " + str(connection)) try: guacConn.delete_connection(connection["identifier"]) except Exception: logging.error( "runClearAllConnections(): Error in runClearAllConnections(): when trying to remove connection." ) exc_type, exc_value, exc_traceback = sys.exc_info() #traceback.print_exception(exc_type, exc_value, exc_traceback) self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE
def quacamole_account(email): print("Setup Guacamole account") c = Guacamole(os.environ.get("GUACAMOLE_HOST", "localhost") + ":" + os.environ.get("GUACAMOLE_PORT", "80"), os.environ.get("GUACAMOLE_USER", "guadmin"), os.environ.get("GUACAMOLE_PASS", "guadmin"), method='http', url_path='/guacamole', default_datasource=None, verify=True) # (delete)/create user in Guacamole registry with actual password... connection = os.environ.get("GUACAMOLE_CONNECTION", 1) try: c.delete_user(email) except: pass c.add_user( payload={ "username": email, # "password":password, "attributes": { "disabled": "", "expired": "", "access-window-start": "", "access-window-end": "", "valid-from": "", "valid-until": "", "timezone": "" } }) c.grant_permission(email, payload=[{ "op": "add", "path": "/connectionPermissions/{}".format(connection), "value": "READ" }])
def runCreateConnections(self, configname, guacHostname, musername, mpassword, url_path, method, maxConnections, maxConnectionsPerUser, width, height, bitdepth, creds_file, itype, name): logging.debug("runCreateConnections(): instantiated") #call guac backend API to make connections as specified in config file and then set the complete status rolledoutjson = self.eco.getExperimentVMRolledOut(configname) validconnsnames = self.eco.getValidVMsFromTypeName( configname, itype, name, rolledoutjson) userpool = UserPool() usersConns = userpool.generateUsersConns(configname, creds_file=creds_file) try: self.writeStatus = ConnectionManage.CONNECTION_MANAGE_CREATING logging.debug("runCreateConnection(): guacHostname: " + str(guacHostname) + " username/pass: "******" url_path: " + url_path + " method: " + str(method) + " creds_file: " + creds_file) guacConn = Guacamole(guacHostname, username=musername, password=mpassword, url_path=url_path, method=method) if guacConn == None: logging.error( "runCreateConnection(): Error with guac connection... skipping: " + str(guacHostname) + " " + str(musername)) self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE return -1 user_dict = guacConn.get_users() try: for (username, password) in usersConns: for conn in usersConns[(username, password)]: cloneVMName = conn[0] vmServerIP = conn[1] vrdpPort = conn[2] #only if this is a specific connection to create; based on itype and name if cloneVMName in validconnsnames: #if user doesn't exist, create it if username not in user_dict: logging.debug("Creating User: "******"already_exists": logging.debug( "User already exists; skipping...") #add to the list of known users user_dict[username] = "" except Exception: logging.error( "runCreateConnections(): Error in runCreateConnections(): when trying to add user." ) exc_type, exc_value, exc_traceback = sys.exc_info( ) traceback.print_exception( exc_type, exc_value, exc_traceback) #add the connection association result = self.createConnAssociation( guacConn, cloneVMName, username, vmServerIP, vrdpPort, maxConnections, maxConnectionsPerUser, width, height, bitdepth) if result == "already_exists": logging.debug( "Connection already exists; skipping...") except Exception: logging.error( "runCreateConnections(): Error in runCreateConnections(): when trying to add connection." ) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) logging.debug("runCreateConnections(): Complete...") self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE except Exception: logging.error( "runCreateConnections(): Error in runCreateConnections(): An error occured " ) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE return finally: self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE
def runRemoveConnections(self, configname, guacHostname, username, password, url_path, method, creds_file, itype, name): self.writeStatus = ConnectionManage.CONNECTION_MANAGE_REMOVING logging.debug("runRemoveConnections(): instantiated") #call guac backend API to make connections as specified in config file and then set the complete status rolledoutjson = self.eco.getExperimentVMRolledOut(configname) validconnsnames = self.eco.getValidVMsFromTypeName( configname, itype, name, rolledoutjson) userpool = UserPool() try: usersConns = userpool.generateUsersConns(configname, creds_file=creds_file) self.writeStatus = ConnectionManage.CONNECTION_MANAGE_CREATING logging.debug("runRemoveConnections(): guacHostname: " + str(guacHostname) + " username/pass: "******" url_path: " + url_path + " method: " + str(method) + " creds_file: " + creds_file) guacConn = Guacamole(guacHostname, username=username, password=password, url_path=url_path, method=method) if guacConn == None: logging.error( "runRemoveConnections(): Error with guac connection... skipping: " + str(guacHostname) + " " + str(username)) self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE return -1 for (username, password) in usersConns: logging.debug("Removing Connection for Username: "******"Does not Exist": logging.debug( "Connection doesn't exists; skipping...") #check if any other connections exist for user, if not, remove the user too try: result = guacConn.get_permissions(username) if len(result["connectionPermissions"]) == 0: logging.debug("Removing User: "******"Does not Exist": logging.debug( "User doesn't exist; skipping...") except Exception: logging.error( "runRemoveConnections(): Error in runRemoveConnections(): when trying to remove user." ) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) except Exception: logging.error( "runRemoveConnections(): Error in runRemoveConnections(): when trying to remove connection." ) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) logging.debug("runRemoveConnections(): Complete...") self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE except Exception: logging.error( "runRemoveConnections(): Error in runRemoveConnections(): An error occured " ) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE return finally: self.writeStatus = ConnectionManage.CONNECTION_MANAGE_COMPLETE
def createGuacEntries(self, inputFilename, guacHostname, guacUsername, guacPass, guacURLPath, guacConnMethod, inputFileBasename): logging.info("createGuacEntry(): instantiated") inputFileBasename = os.path.splitext( os.path.basename(inputFilename))[0] #######Guac connection########## #guacConn = Guacamole('192.168.99.102',username='******',password='******',url_path='/guacamole',method='http') guacConn = Guacamole(guacHostname, username=guacUsername, password=guacPass, url_path=guacURLPath, method=guacConnMethod) if guacConn == None: logging.error("Error with guac connection") exit() logging.debug("Connection to guac successful: " + str(guacConn)) #######Read experiment-related info from file########## tree = ET.parse(inputFilename) root = tree.getroot() pathToVirtualBox = root.find('vbox-setup').find( 'path-to-vboxmanage').text netConfig = root.find('testbed-setup').find('network-config') vmset = root.find('testbed-setup').find('vm-set') # ---get ip address information vmServerIP = netConfig.find('vm-server-ip').text # ---here we look at each vmset numClones = int(vmset.find('num-clones').text) cloneSnapshots = vmset.find('clone-snapshots').text linkedClones = vmset.find('linked-clones').text baseGroupname = vmset.find('base-groupname').text baseOutname = vmset.find('base-outname').text vrdpBaseport = vmset.find('vrdp-baseport').text #first create all users (one per clone) for i in range(1, numClones + 1): #create username #username = "******"+str(i) username = baseGroupname + str(i) username = ''.join(e for e in username if e.isalnum()) logging.info("Creating Username: "******"user"+str(i) username = baseGroupname + str(i) username = ''.join(e for e in username if e.isalnum()) myBaseOutname = baseOutname newvmName = vmname + myBaseOutname + str(i) # vrdp setup if vrdpEnabled and vrdpEnabled == 'true': #guacConn, connName, username, password, ip, port): self.createConnAssociation(guacConn, newvmName, username, vmServerIP, vrdpBaseport) vrdpBaseport = str(int(vrdpBaseport) + 1) logging.info(""" ************************************************************************************** Guacamole User and Connection script complete ************************************************************************************** """)