def change_plan_status(plan_name, new_state, plan_type = ""): """ Turn plan on and off. """ plans_collection = tingDB_service.get_config_collection("plans") if plan_type == "": # We get the type ourselves plan_cursor = plans_collection.find({"plan_name": plan_name}) plan_type = plan_cursor[0]["type"] doc_col = plan_composer.get_collection_name_for_plan_type(plan_type) if new_state == "online": ret_data = start_plan(plan_name, doc_col) elif new_state == "offline": ret_data = stop_plan(plan_name, doc_col) else: return False if ret_data != "": plans_collection.update({"plan_name": plan_name}, {"$set": {"status": new_state}}) return True
def test_hosts_integrity(): hosts_collection = tingDB_service.get_config_collection("hosts") for hostname in __hosts: host = hosts_collection.find({"hostname": hostname}) if host.count() == 0: return False return True
def __get_server_for_plan(plan): """ Get server descriptor from plan descriptor. """ plan_name = plan["_id"] plan_type = plan["type"] col_name = plan_composer.get_collection_name_for_plan_type(plan_type) servers_collection = tingDB_service.get_config_collection(col_name) server_cursor = servers_collection.find({"_id": plan_name}) return server_cursor[0]
def get_plans_for_user(username): """ Returns all the plans for a given user. """ plans_collection = tingDB_service.get_config_collection("plans") plans_cursor = plans_collection.find({"owner": username}) plans = [] for plan in plans_cursor: plans.append(plan) return plans
def __save_plan(plan_name, username, max_disk_size, plan_type): """ Save the generated plan to database. """ try: plan_doc = { "_id": plan_name, "owner": username, "max_disk_size": max_disk_size, "type": plan_type, "status": "offline" } plans_collection = tingDB_service.get_config_collection("plans") plans_collection.insert(plan_doc) except Exception, e: return False
def load_hosts(): """ Import hard coded hosts. """ hosts_collection = tingDB_service.get_config_collection("hosts") for h in __hosts: record = { "hostname": "", "ports_left": globals.DEFAULT_USER_PORT_MAX - globals.DEFAULT_USER_PORT_MIN, "ports_map": None } # We insert all the ports descriptors into the document ports_map = [] i = globals.DEFAULT_USER_PORT_MIN while i < globals.DEFAULT_USER_PORT_MAX: ports_map.append(i) i += 1 # record["hostname"] = h + "cse.ust.hk" record["hostname"] = h record["ports_map"] = ports_map hosts_collection.insert(record)