def get_host_ip(user_id, project_name, service_name): prj_id = Db.exec_one("select id from projects " "where name='%s' and userID='%s')", (project_name, user_id)) return Db.exec_one("select IP from services " "where name='%s' and projectID='%s'", (service_name, prj_id))
def delete_all_services(user_name, project_name): prj_id = Db.exec_one("select id from projects " "where name='%s' and userID = (select id from user where name = '%s')", (project_name, user_name)) if prj_id is None: raise Exception("Project does not exist for %s, %s" % (user_name, project_name)) Db.exec_cmd("delete from services where projectID = '%s'", prj_id)
def get_list(user_id, project_name): prj_id = Db.exec_one("select id from projects " "where name = '%s' and userID=(select id from user where name='%s')", (project_name, user_id)) if prj_id is None: raise Exception("Project does not exist for %s, %s" % (user_id, project_name)) return Db.exec_list("select name from services where projectID='%s'", prj_id)
def delete_user_and_projects(user_name): """ 删除用户及该用户的所有项目和所属服务 """ # todo: 删除所有项目和所属服务是否有上层负责,或以事务方式执行? # todo: 合并下面的多条sql cmds? user_id = Db.exec_one("select id from user where name='%s'", user_name) Db.exec_cmd("delete from services where projectID in " "(select id from projects where userID='%s')", user_id) Db.exec_cmd("delete from projects where userID='%s'", user_id) Db.exec_cmd("delete from user where name='%s'", user_id)
def exists(user_name, project_name): prj = Db.exec_one("select 1 from projects " "where name='%s' and userID = (select id from user where name = '%s')", (project_name, user_name)) return prj is not None
def create(user_name, service_name, machine_ip, project_name): prj_id = Db.exec_one("select id from projects " "where name='%s' and userID in (select id from user where name = '%s')", (project_name, user_name)) Db.exec_cmd("insert into services(name, projectID, IP) values('%s', %s, '%s')", (service_name, prj_id, machine_ip))
def get_machine(index): # todo: what is this? # todo: 用于随机调度? return Db.exec_one("select ip from machine limit %s,1" % index)
def get_info(user_id, project_name): # todo: name已经知道了,url是git repo的url吗? return Db.exec_one("select name, url from projects where userID = '%s' and name = '%s'", (user_id, project_name))