def POST(self): help.check_access() input = web.input(name="NULL", type="NULL", servers=[]) if input.name == "NULL" or input.type == "NULL": help.append_user_alerts( "warning", "Invalid Entry", "The information you have input is invalid") return help.renderPage( "Add Environment", render.environment_form(0, env_types(), free_servers(), input.servers, [input.name, input.desc, input.type])) # Add Environment into the database eid = db().executeID("Environment_Forms", 0, [input.name, input.desc, input.type]) for server in input.servers: db().execute("Environment_Forms", 1, [eid, server]) help.append_user_alerts( "info", "Successfully Added", "An environment definition has been successfully added") raise web.seeother("environments.html")
def GET(self, srv_id): help.check_access() # Get Server Information Server = list(db().executeLiteral( "SELECT * FROM servers WHERE srv_id = ?", [srv_id])[0]) if Server[1] == 0: Server = ["N/A"] + Server else: name = db().executeLiteral( "SELECT name FROM environments WHERE eid = ?", [Server[1]])[0][0] Server = [name] + Server # Get Service information for the server Services = db().execute( "Server_Details", 1, [Server[1]]) data = [] for ser in Services: result = Logger().getAnalytics( srv_id, ser[0] ) data.append( [ser[2], ser[1], result[2], result[3], result[4], str( result[0] ) + '-' + str( result[1] )] ) path = join("./configs/", srv_id) file_reader = open( path, "r") master = cfg.Config( file_reader.read() ) file_reader.close() masterInfo = [['Analytics Server' , master.Analytic_Settings.items()] , ['Management Server', master.Management_Settings.items()] , ['Data Logger', master.Logging.items()]] return help.renderPage("Server Details", render.server_details( Server, data, masterInfo ))
def GET(self, sgid): help.check_access() redirect() paths = db().executeLiteral("SELECT pid, name FROM paths", []) data = db().executeLiteral("SELECT * FROM services WHERE sgid = ?", [sgid])[0] return help.renderPage("Edit Service", render.service_group_form(sgid, data, paths))
def GET(self): help.check_access() log = Logger() # Counts the number of services and the number of servers known to the Management Console. counts = db().executeLiteral( "SELECT (SELECT count(*) FROM services), (SELECT count(*) FROM servers)", [])[0] analytics = log.getAnalytics() tiles = [ analytics[0], analytics[3], analytics[1], counts[0], counts[1], analytics[5] ] data = db().execute("Dashboard", 0, []) developing = [] for row in data: percentage = (row[2] * 100) / row[3] temp = [row[0], help.strVersion(row[1]), percentage, row[4]] developing.append(temp) #ENVIRONMENTS envinfo = db().execute("Dashboard", 1, []) environments = [] for env in envinfo: machines = db().executeLiteral( "SELECT srv_id FROM servers WHERE eid = ?", [env[0]]) envServices = db().execute("Dashboard", 3, [env[0]]) if len(machines) == 0 or len(envServices) == 0: continue processedServices = [] for ser in envServices[:5]: processedServices.append( [ser[0], ser[1], help.strVersion(ser[2]), ser[3]]) reqtCount = 0 for server in machines: server = server[0] reqtCount += log.getAnalytics(server)[0] temp = [ env[1], env[2], reqtCount, len(machines), processedServices, env[0] ] environments.append(temp) print developing print environments return help.renderPage( "Dashboard", render.dashboard(tiles, developing, environments))
def write(self): for d in self.mp: for p in self.pairs: if (d['symbol'] in p): self.summary_headers.append(d['symbol']) self.summary_prices.append(d['price']) self.data[d['symbol']] = {"price":d['price'],"timestamp":time.time(),"pair":d['symbol']} # sheets().write_summary_headers(self.summary_headers,'Binance') # sheets().write_summary_data(self.summary_prices,'Binance') db().insert_new_price([self.data],'binance')
def validateService(name, resource): nCount = db().executeLiteral( "SELECT count(*) FROM services WHERE service_name = ?", [name])[0][0] rCount = db().executeLiteral( "SELECT count(*) FROM services WHERE resource_name = ?", [name])[0][0] if (nCount + rCount) != 0: help.append_user_alerts( "warning", "Invalid Input", "The service name or resource have already been taken, please either select alternatives or free the service before proceeding." ) return False return True
def GET( self ): # Get all paths from database all_paths = db().execute( "Path_Manage", 0, []) path_info = [] types = ['DEV','TEST','STAGING','LIVE'] # Sort the according to the types hierachy for path in all_paths: temp = [path[0], path[2], path[1]] for t in types: temp.append( (db().execute( "Path_Manage", 1, [path[0], t])[0])[0] ) path_info.append( temp ) return help.renderPage( "Environment Paths", render.paths( path_info ) )
def store_prices(self, prices): for d in prices: for p in self.pairs: if (d['symbol'] in p): self.summary_headers.append(d['symbol']) self.summary_prices.append(d['price']) self.data[d['symbol']] = { "price": d['price'], "timestamp": time.time(), "pair": d['symbol'] } db().insert_new_price([self.data], 'binance')
def env_data(): types = db().execute("Server_Forms", 0, []) data = [] inner_data = [] for env_type in types: inner_data = [env_type['name']] envs = list(db().execute("Server_Forms", 1, [env_type['name']])) for env in envs: inner_data.append(env) data.append(inner_data) return data
def GET(self, sgid): help.check_access() usr = (web.ctx.session).username token = (web.ctx.session).access_token msgHeaders = {"Username": usr, "Access-Token": token} # Collect the resouce of the service. resource = db().executeLiteral( "SELECT resource_name FROM services WHERE sgid = ?", [sgid])[0][0] # Collect a list of all servers the service is running on. servers = db().executeLiteral( "SELECT srv.machine_address, srv.m_port_num FROM services_versions sv, service_deployment_lkp sdl, servers srv WHERE sv.sgid = ? AND sv.sid = sdl.sid AND sdl.eid = srv.eid GROUP BY srv.srv_id", [sgid]) # Delete the resource on every server for eachServer in servers: Server(eachServer[0], eachServer[1]).send("DELETE", resource, msgHeaders) # Delete local for service. clearDir(join("./services", str(sgid))) # Decouple Database db().executeLiteral("DELETE FROM services WHERE sgid = ?", [sgid]) db().executeLiteral( "DELETE FROM service_deployment_lkp WHERE sid IN (SELECT sid FROM services_versions WHERE sgid = ?)", [sgid]) db().executeLiteral("DELETE FROM services_versions WHERE sgid = ?", [sgid]) help.append_user_alerts( "info", "Successfully Deleted", "We have successfully deleted the service information.") raise web.seeother("manage_services.html")
def POST(self, pid): help.check_access() redirect() input = web.input(name="NULL") if input.name == "NULL": help.append_user_alerts( "warning", "Invalid Entry", "The Path Name was invalid, please try again") return help.renderPage( "Add Path", render.path_forms(0, [input.name, input.desc])) db().execute("Path_Forms", 2, [input.name, input.desc, pid]) raise web.seeother("path_details_" + str(pid))
def GET(self, sgid): # TODO get format from somewhere env_version = db().executeLiteral( "SELECT DISTINCT env.eid, ser.version, ser.sid FROM services_versions ser, service_deployment_lkp sdl, environments env WHERE ser.sgid = ? AND ser.sid = sdl.sid AND sdl.eid = env.eid AND (env.env_type = 'STAGING' OR env.env_type = 'LIVE') GROUP BY env.eid ORDER BY ser.version", [int(sgid)]) page_info = { 'xAxis': ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] } version_requests = {} for env in env_version: #data = environment().getServiceRequests( env[0], env[2], startDate, endData) data = [randint(0, 100) for x in range(7)] if env[1] in version_requests: version_requests[help.strVersion(env[1])] = [ x + y for x, y in zip(version_requests[env[1]], data) ] else: version_requests[help.strVersion(env[1])] = data page_info["versions"] = version_requests return json.dumps(page_info)
def GET(self, pid): help.check_access() redirect() path_info = db().execute("Path_Forms", 1, [pid])[0] return help.renderPage( "Add Path", render.path_forms(path_info[0], [path_info[2], path_info[3]]))
def write(self): for d in self.mp['Data']: for p in self.pairs: if (d['Label'] in p): self.summary_headers.append(d['Label']) self.summary_prices.append(d['LastPrice']) self.data[d['Label']] = { "price": d['LastPrice'], "timestamp": time.time(), "pair": d['Label'] } # sheets().write_summary_headers(self.summary_headers,'cryptopia') # sheets().write_summary_data(self.summary_prices,'cryptopia') db().insert_new_price([self.data], 'cryptopia')
def GET(self, eid): help.check_access() content = db().execute("Environment_Forms", 2, [eid])[0] return help.renderPage( "Edit Environment", render.environment_form(eid, env_types(), free_servers(eid), selected_servers(eid), content))
def GET(self, eid): # TODO get format from somewhere help.check_access() services = db().execute("Environment_Details", 0, [eid]) page_info = { 'xAxis': ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] } version_requests = {} for ser in services: data = [randint(0, 100) for x in range(7)] if ser[0] in version_requests: version_requests[ser[0]] = [ x + y for x, y in zip(version_requests[ser[0]], data) ] else: version_requests[ser[0]] = data page_info["services"] = version_requests return json.dumps(page_info)
def getPathInfo(sgid): pathName = db().executeLiteral( "SELECT name FROM services, paths WHERE services.pid = services.pid AND sgid = ?", [sgid])[0][0] versions = db().executeLiteral( "SELECT sid, version FROM services_versions WHERE sgid = ? ORDER BY version ASC", [sgid]) pathEnvs = db().executeLiteral( "SELECT services.pid, environments.eid, environments.name FROM services, env_path_lkp, environments WHERE sgid = ? AND services.pid = env_path_lkp.pid AND env_path_lkp.eid = environments.eid ORDER BY position DESC", [sgid]) collection = [pathName] envInfo = [] for env in pathEnvs: running = db().executeLiteral( "SELECT sv.version FROM services_versions sv, service_deployment_lkp sdl WHERE sv.sgid = ? AND sv.sid = sdl.sid AND sdl.eid = ? ORDER BY sv.version DESC", [sgid, env[1]]) print(len(running)) if len(running) == 0: # No version running on environment. top = 0 else: top = running[0][0] print(top) envVersions = [] for ver in versions: # If version is top version just append, no need to check it exists. if ver[1] == top: envVersions.append([ver[1], "info"]) continue count = db().executeLiteral( "SELECT count(*) FROM service_deployment_lkp WHERE sid = ? AND eid = ?", [ver[0], env[1]])[0][0] if count == 1: envVersions.append([ver[1], "success"]) else: envVersions.append([""]) envInfo.append([env[2], envVersions]) collection.append(envInfo) return collection
def GET(self): all_paths = db().executeLiteral("SELECT * FROM paths", []) path_info = [] types = ['DEV', 'TEST', 'STAGING', 'LIVE'] for path in all_paths: temp = [path[0], path[2], path[1]] for t in types: temp.append((db().executeLiteral( "SELECT COUNT(*) FROM environments env, env_path_lkp epl WHERE epl.pid = ? AND env.env_type = ? AND epl.eid = env.eid", [path[0], t])[0])[0]) path_info.append(temp) return help.renderPage("Environment Paths", render.environment_paths(path_info))
def redirect(): count = db().executeLiteral("SELECT count(*) FROM environments", [])[0][0] if (count == 0): help.append_user_alerts( 'info', 'Need Environments first!', 'You cannot create a new path without having Environments to traverse, please create an Environment now.' ) raise web.seeother("environment_form_0")
def redirect(): count = db().executeLiteral("SELECT count(*) FROM paths", [])[0][0] if (count == 0): help.append_user_alerts( 'info', 'Need a Development path first!', 'You cannot create a new service without having a path to follow, please create one now.' ) raise web.seeother("path_form_0")
def GET(self, srv_id): # Tell server to delete its contents address, port = db().executeLiteral( "SELECT machine_address, m_port_num FROM servers WHERE srv_id = ?", [srv_id])[0] resources = [ x[0] for x in db().executeLiteral( "SELECT ser.resource_name FROM services ser, services_versions sv, service_deployment_lkp sdl, servers srv WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = srv.eid AND srv.srv_id = ? GROUP BY ser.sgid", [srv_id]) ] usr = (web.ctx.session).username token = (web.ctx.session).access_token msgHeaders = {"Username": usr, "Access-Token": token} interface = Server(address, port) for res in resources: interface.send("DELETE", res, msgHeaders) # Delete Config try: remove(join("./configs", srv_id)) except: web.debug("Tried to delete Analytics config but it was not found!") # Delete log files logLoc = join("./logs", srv_id) try: logs = listdir(logLoc) for log in logs: remove(join(logLoc, log)) rmdir(logLoc) except: web.debug("Tried to delete log files at " + logLoc + " but they didn't exist.") # Delete from database db().execute("Server_Forms", 4, [srv_id]) help.append_user_alerts( 'default', 'Successfully Deleted', "Some server information has been deleted? (get it! :P we don't even know what server it was!)" ) raise web.seeother("/manage_servers.html")
def POST(self): input = web.input(machine_address="NULL", a_port="NULL", m_port="NULL", ssh_port="NULL") response = "{" if input.machine_address == "NULL": return '{ "a_port":"false", "m_port":"false", "ssh_port":"false" }' else: if input.a_port != "NULL": data = [input.machine_address] + [input.a_port] * 3 count = db().execute("Validation", 0, data)[0][0] if count == 0: response += '"a_port":"true",' else: response += '"a_port":"false",' else: response += '"a_port":"false",' if (input.m_port != "NULL"): data = [input.machine_address] + [input.m_port] * 3 count = db().execute("Validation", 0, data)[0][0] if count == 0: response += '"m_port":"true",' else: response += '"m_port":"false",' else: response += '"m_port":"false",' if (input.ssh_port != "NULL"): data = [input.machine_address] + [input.ssh_port] * 3 count = db().execute("Validation", 0, data)[0][0] if count == 0: response += '"ssh_port":"true"' else: response += '"ssh_port":"false"' else: response += '"ssh_port":"false"' return response + "}"
def get_price(self): veneth_price = market_api().get_latest_trades('VENETH')[0]['price'] veneth_old_price = db().calculate_price_change('VENETH', 'binance') self.veneth = { 'veneth_price': veneth_price, 'veneth_old_price': veneth_old_price, 'veneth_delta': 1 - (float(veneth_price) / float(veneth_old_price)) } btcusdt_price = market_api().get_latest_trades('BTCUSDT')[0]['price'] btcusdt_old_price = db().calculate_price_change('BTCUSDT', 'binance') self.btcusdt = { 'btcusd_price': btcusdt_price, 'btcusd_old_price': btcusdt_old_price, 'btcusdt_delta': 1 - (float(btcusdt_price) / float(btcusdt_old_price)) }
def GET(self): # Extract Server Information and send it to the page. help.check_access() servers = db().executeLiteral( "SELECT srv_id, eid, display_name, machine_address, a_port_num, m_port_num FROM servers",[]) serverInfo = [] for each in servers: each = list( each ) temp = [each[0]] if each[1] == 0: temp.append( "N/A" ) else: temp.append( db().executeLiteral( "SELECT name FROM environments WHERE eid = ?", [each[1]] )[0][0] ) temp += each[2:] serverInfo.append( temp ) return help.renderPage( 'Manage Servers', render.manage_servers( serverInfo ) )
def GET(self): #Return service add form help.check_access() redirect() paths = db().executeLiteral("SELECT pid, name FROM paths", []) return help.renderPage( "Add Service", render.service_version_form([], [], paths, si().getVersion()))
def POST(self): help.check_access() # Collect and verity input information. input = web.input(eid="NULL", sid="NULL") if input.eid == "NULL" or input.sid == "NULL": raise web.badrequest() # Construct the message information plus target information. usr = (web.ctx.session).username token = (web.ctx.session).access_token msgHeaders = {"Username": usr, "Access-Token": token} resource = "/ACTIVATE" + db().executeLiteral( "SELECT ser.resource_name FROM services ser, services_versions srv WHERE srv.sid = ? AND srv.sgid = ser.sgid", [input.sid])[0][0] Env_Ser = db().executeLiteral( "SELECT display_name, machine_address, m_port_num FROM servers WHERE eid = ?", input.eid) # Activate the service on all targets. count = len(Env_Ser) for server in Env_Ser: resp = Server(server[1], server[2]).send("GET", resource, msgHeaders) print "RESPONSE FROM SERVER" print resp if resp == "FAILED" or resp[0] != "200": help.append_user_alerts( "warning", "Whoops", "Error on communicating with " + server[0]) count -= 1 if count == 0: #None of the services were correctly activated, do not change stored status. help.append_user_alerts( "danger", "Unable to activate", "The service has not been correctly activated on any server of the environment." ) else: # Update the database of the change db().executeLiteral( "UPDATE service_deployment_lkp SET status = 'ACTIVE' WHERE eid = ? AND sid = ?", [input.eid, input.sid]) raise web.seeother("/environment_details_" + input.eid)
def GET(self, pid): dev = [ name for row in db().executeLiteral( "SELECT env.name FROM environments env, env_path_lkp epl WHERE env.eid = epl.eid AND env.env_type = 'DEV' AND epl.pid = ?", [pid]) for name in row ] test = [ name for row in db().executeLiteral( "SELECT env.name FROM environments env, env_path_lkp epl WHERE env.eid = epl.eid AND env.env_type = 'TEST' AND epl.pid = ?", [pid]) for name in row ] stage = [ name for row in db().executeLiteral( "SELECT env.name FROM environments env, env_path_lkp epl WHERE env.eid = epl.eid AND env.env_type = 'STAGING' AND epl.pid = ?", [pid]) for name in row ] live = [ name for row in db().executeLiteral( "SELECT env.name FROM environments env, env_path_lkp epl WHERE env.eid = epl.eid AND env.env_type = 'LIVE' AND epl.pid = ?", [pid]) for name in row ] selected = [dev, test, stage, live] ns_dev = [ name for row in db().executeLiteral( "SELECT name FROM environments WHERE env_type = 'DEV'", []) for name in row if not name in dev ] ns_test = [ name for row in db().executeLiteral( "SELECT name FROM environments WHERE env_type = 'TEST'", []) for name in row if not name in test ] ns_stage = [ name for row in db().executeLiteral( "SELECT name FROM environments WHERE env_type = 'STAGING'", []) for name in row if not name in stage ] ns_live = [ name for row in db().executeLiteral( "SELECT name FROM environments WHERE env_type = 'LIVE'", []) for name in row if not name in live ] not_selected = [ns_dev, ns_test, ns_stage, ns_live] path_info = [0, "Quick Production"] return help.renderPage( "Environment Paths", render.environment_details(path_info, not_selected, selected))
def GET(self, sgid): help.check_access() service_group = list(db().executeLiteral( "SELECT * FROM services WHERE sgid = ?", [int(sgid)])[0]) service_group[0] = str(service_group[0]) path_graphic = getPathInfo(sgid) version_info = getVersionInfo(sgid) return help.renderPage( "Service Details", render.service_details(service_group, path_graphic, version_info))
def GET(self, pid): help.check_access() # Count Services currently on path count = db().executeLiteral( "SELECT count(*) FROM services WHERE pid = ?", [pid])[0][0] if (count != 0): help.append_user_alerts( "warning", "Development Path still followed!", "You cannot delete a path when services are still following it, please redirect them before trying again." ) return web.seeother("path_details_" + str(pid)) db().executeLiteral("DELETE FROM paths WHERE pid = ?", [pid]) db().executeLiteral("DELETE FROM env_path_lkp WHERE pid = ?", [pid]) help.append_user_alerts( "info", "Path Deleted", "Information about the path has been deleted.") raise web.seeother("paths.html")
def GET(self, pid): help.check_access() dev = [ name for row in db().execute("Path_Details", 0, ['DEV', pid]) for name in row ] test = [ name for row in db().execute("Path_Details", 0, ['TEST', pid]) for name in row ] stage = [ name for row in db().execute("Path_Details", 0, ['STAGING', pid]) for name in row ] live = [ name for row in db().execute("Path_Details", 0, ['LIVE', pid]) for name in row ] selected = [dev, test, stage, live] ns_dev = [ name for row in db().execute("Path_Details", 1, ['DEV']) for name in row if not name in dev ] ns_test = [ name for row in db().execute("Path_Details", 1, ['TEST']) for name in row if not name in test ] ns_stage = [ name for row in db().execute("Path_Details", 1, ['STAGING']) for name in row if not name in stage ] ns_live = [ name for row in db().execute("Path_Details", 1, ['LIVE']) for name in row if not name in live ] not_selected = [ns_dev, ns_test, ns_stage, ns_live] path_info = db().execute("Path_Details", 2, [pid])[0] return help.renderPage( "Path Details", render.path_details(path_info, not_selected, selected))