def insert_resources(resources): """ insert the provided resources (dict) into the mod_appkernel database. :type resources dict :param resources: a dict of resources that should be inserted into the mod_appkernel.resource database. :return: void """ if resources is not None and len(resources) > 0: parameters = tuple([(resource['name'], int(resource['id']), False) for (resource) in resources]) resources_parameters = tuple([(resource['name'], int(resource['id'])) for (resource) in resources]) connection, cursor = akrr.getAKDB() with connection: cursor.executemany(''' INSERT INTO `mod_appkernel`.`resource` (resource, xdmod_resource_id, visible) VALUES(%s, %s, %s) ''', parameters) cursor.executemany(''' INSERT INTO `mod_akrr`.`resources` (name, xdmod_resource_id) VALUES(%s, %s) ''', resources_parameters)
def validate_resource_name(resource_name): if resource_name.strip()=="": logging.error("Bad name for resource, try a different name") return False #check config file presence file_path = os.path.abspath(os.path.join(resources_dir, resource_name)) if os.path.exists(file_path): logging.error("Resource configuration directory (%s) for resource with name %s already present on file system, try a different name"%(file_path,resource_name,)) return False #check the entry in mod_appkernel dbAK,curAK=akrr.getAKDB(True) curAK.execute('''SELECT * FROM resource WHERE nickname=%s''', (resource_name,)) resource_in_AKDB = curAK.fetchall() if len(resource_in_AKDB)!=0: logging.error("Resource with name %s already present in mod_appkernel DB, try a different name"%(resource_name,)) return False #check the entry in mod_akrr db,cur=akrr.getDB(True) cur.execute('''SELECT * FROM resources WHERE name=%s''', (resource_name,)) resource_in_DB = cur.fetchall() if len(resource_in_DB)!=0: logging.error("Resource with name %s already present in mod_akrr DB, try a different name"%(resource_name,)) return False return True
def generate_resource_config(resource_id, resource_name, queuing_system): logging.info("Initiating %s at AKRR"%(resource_name,)) slurm_template_contents = retrieve_queue_template(os.path.join(akrr.curdir, 'templates', 'template.{0}.inp.py'), 'slurm') pbs_template_contents = retrieve_queue_template(os.path.join(akrr.curdir, 'templates', 'template.{0}.inp.py'), 'pbs') queues = {'slurm': slurm_template_contents, 'pbs': pbs_template_contents} if not args.test: os.mkdir(os.path.join(resources_dir, resource_name),0700) file_path = os.path.abspath(os.path.join(resources_dir, resource_name, 'resource.inp.py')) global resource_cfg_filename resource_cfg_filename=file_path create_resource_template(file_path, queues[queuing_system], queues[queuing_system]) if not args.test: #add entry to mod_appkernel.resource dbAK,curAK=akrr.getAKDB(True) curAK.execute('''SELECT * FROM resource WHERE nickname=%s''', (resource_name,)) resource_in_AKDB = curAK.fetchall() if len(resource_in_AKDB)==0: curAK.execute('''INSERT INTO resource (resource,nickname,description,enabled,visible,xdmod_resource_id) VALUES(%s,%s,%s,0,0,%s);''', (resource_name,resource_name,resource_name,resource_id)) dbAK.commit() curAK.execute('''SELECT * FROM resource WHERE nickname=%s''', (resource_name,)) resource_in_AKDB = curAK.fetchall() resource_id_in_AKDB=resource_in_AKDB[0]['resource_id'] #add entry to mod_akrr.resource db,cur=akrr.getDB(True) cur.execute('''SELECT * FROM resources WHERE name=%s''', (resource_name,)) resource_in_DB = cur.fetchall() if len(resource_in_DB)==0: cur.execute('''INSERT INTO resources (id,xdmod_resource_id,name,enabled) VALUES(%s,%s,%s,%s);''', (resource_id_in_AKDB,resource_id,resource_name,0)) db.commit() logging.info("Resource configuration is in "+file_path)
def insert_resource(resource): """ Insert the provided resource (dict) into the mod_appkernel database. :type resource dict :param resource: :return: void """ if resource is not None: name, id = resource connection, cursor = akrr.getAKDB() with connection: cursor.execute(''' INSERT INTO `mod_appkernel`.`resource` (resource, xdmod_resource_id, visible) VALUES(%s, %s, %s) ''', (name, id, False)) cursor.execute(''' INSERT INTO `mod_akrr`.`resources` (name, xdmod_resource_id) VALUES(%s, %s) ''', (name, id))
rsh.close(force=True) del rsh sys.stdout=sys.__stdout__ sys.stderr=sys.__stderr__ except Exception,e: msg2=str_io.getvalue() msg2+="\n"+traceback.format_exc() sys.stdout=sys.__stdout__ sys.stderr=sys.__stderr__ logerr("Can not connect to """+resource['name']+"\n"+ "Probably invalid credential, see full error report below",msg2) exit() #enabling resource for execution try: dbAK,curAK=akrr.getAKDB(True) curAK.execute('''SELECT * FROM resource WHERE nickname=%s''', (resource_name,)) resource_in_AKDB = curAK.fetchall() if len(resource_in_AKDB)==0: log("There is no record of %s in mod_appkernel.resource will add one."%(resource_name,),highlight="warning") curAK.execute('''INSERT INTO resource (resource,nickname,description,enabled,visible) VALUES(%s,%s,%s,0,0);''', (resource_name,resource_name,resource['info'])) dbAK.commit() curAK.execute('''SELECT * FROM resource WHERE nickname=%s''', (resource_name,)) resource_in_AKDB = curAK.fetchall() resource_in_AKDB=resource_in_AKDB[0] #enable and make visible curAK.execute('''UPDATE resource