def cold_redeploy(input_dir, config_dict): print(" cold redeploy in progress...") #first make a backup of existing dictionary surveillance_monitor.backup_file(input_dir, "/cloudbook.json", "/previous_cloudbook.json") #wait for creation of agentxx grant. first deletion print("waiting creation of agent_XX_grant files...") print(timestamp(), "sleeping...", surveillance_interval) print() time.sleep( float(surveillance_interval)) # this wait is supposed to be enough. surveillance_monitor.create_file_agents_grant(input_dir) p = Path(input_dir + '/COLD_REDEPLOY') p.touch(exist_ok=True) global dus dus = loader.load_dictionary(input_dir + "/du_list.json") global agents_with_grant agents_with_grant = loader.load_dictionary(input_dir + "/agents_grant.json") agents_in_local_circle = json.dumps(agents_with_grant) deploy_local(agents_in_local_circle, ".", config_dict) #touch restart for run again p = Path(input_dir + '/REQUEST_RESTART') p.touch(exist_ok=True) return
def get_idle_agents(input_dir, config_dict): nba = [] cloudbook = loader.load_dictionary(input_dir + "/cloudbook.json") #print (cloudbook) aal = [] # available agents list agents_with_grant = loader.load_dictionary(input_dir + "/agents_grant.json") available_agents = sorted(agents_with_grant.items()) for i in range(0, len(available_agents)): aal.append(available_agents[i][0]) # check all available agents # for each agent explore all dus assigned for a in aal: #check if it is possible to assign more DUs o agent_0 if (a == "agent_0"): if (config_dict["AGENT0_ONLY_DU0"] == True): continue found = False for du in cloudbook: if du == "du_default": continue #print (cloudbook[du]) # check if this agent (a) has this du (du) for adu in cloudbook[du]: if adu == a: found = True continue if not found: nba.append(a) #print ("NBA:", nba) return nba # non busy agents
def check_agents_changes(input_dir, na, ma, sa): agents_with_grant = loader.load_dictionary(input_dir + "/agents_grant.json") previous_agents_with_grant = loader.load_dictionary( input_dir + "/previous_agents_grant.json") return compare_dictionaries(agents_with_grant, previous_agents_with_grant, na, ma, sa)
def getAgentIP(my_agent_id, agent_id, configuration=None): #Check file "local_IP_info" and get agent_id config_dict = loader.load_dictionary("./config_agent" + my_agent_id + ".json") path = config_dict["DISTRIBUTED_FS"] with open(path + '/local_IP_info.json', 'r') as file: data = json.load(file) #agents_ip[agent_id]={} #agents_ip[agent_id]=data[agent_id] return data[agent_id]
def create_file_agents_grant(input_dir): print("reading files agent_XXX_grant...") agents_with_grant = {} agents_files = [] agents_files = os.listdir(input_dir + "/agents_grant") print(agents_files) # enter in each file for file in agents_files: if file.startswith("agent") and file.endswith(".json"): agent = loader.load_dictionary(input_dir + "/agents_grant/" + file) print("agent= ", agent) for a in agent: agents_with_grant[a] = agent[a] #os.remove (input_dir+"/agents_grant/"+file) agent = loader.load_dictionary(input_dir + "/agents_grant/" + file) print("agent= ", agent) for a in agent: agents_with_grant[a] = agent[a] # No need to retry to delete it. Being used (agent writing it) means the agent is alive, which is the file purpose try: os.remove(input_dir + "/agents_grant/" + file) except: pass print("all agents have been read. The final output is:") print(agents_with_grant) # PENDIENTE SALVAR EL FICHERO, aunque ya esta programado print("saving file agents_grant...") json_str = json.dumps(agents_with_grant) created = False while created == False: try: fo = open(input_dir + "/agents_grant.json", 'w') fo.write(json_str) fo.close() created = True print("file agents_grant.json created succesfully") except: print(" failure creating agents_grant.json -> re-trying...") return
def create_LOCAL_agent(grant, fs): ##generate default dict to be edited????? configure_agent.generate_default_config() config_dict = loader.load_dictionary("./config_agent.json") config_dict["CIRCLE_ID"] = "LOCAL" loader.write_dictionary(config_dict, "./config_agent.json") #????????????????? (my_agent_ID, my_circle_ID) = configure_agent.createAgentID() configure_agent.setFSPath(fs) configure_agent.setGrantLevel(grant, my_agent_ID) os.rename("./config_agent.json", "./config_agent" + my_agent_ID + ".json")
def createAgentID(): #load config file config_dict=loader.load_dictionary("./config_agent.json") #Random agent_id if it doesn't exist my_agent_ID= ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20)) if config_dict["AGENT_ID"]=="0": config_dict["AGENT_ID"]=my_agent_ID else: my_agent_ID=config_dict["AGENT_ID"] my_circle_ID=config_dict["CIRCLE_ID"] loader.write_dictionary(config_dict, "./config_agent.json") return (my_agent_ID, my_circle_ID)
def editGrantLevel(level, my_agent_ID): #load config file config_dict=loader.load_dictionary("./config_agent"+my_agent_ID+".json") if level in ("LOW", "MEDIUM", "HIGH"): config_dict["GRANT_LEVEL"]=level loader.write_dictionary(config_dict, "./config_agent"+my_agent_ID+".json") fr = open("./FS/agent_grant.json", 'r') directory = json.load(fr) directory[my_agent_ID]=level fo = open("./FS/agent_grant.json", 'w') directory= json.dumps(directory) fo.write(directory) fo.close() return
def edit_agent(agent_id, project_name, new_grant='', new_fs=''): if new_grant=='' and new_fs=='': return config_agent_file_path = cloudbook_path + os.sep + project_name + os.sep + "agents" + os.sep + "config_"+agent_id+".json" config_dict = loader.load_dictionary(config_agent_file_path) if new_grant!='' and new_grant!=None: config_dict["GRANT_LEVEL"] = new_grant if new_fs!='' and new_fs!=None: config_dict["DISTRIBUTED_FS"] = new_fs loader.write_dictionary(config_dict, config_agent_file_path)
def announceAgent(my_circle_ID, my_agent_ID, port, configuration=None): #while(True): # Getting local IP print("Announce Agent: ", my_agent_ID) internal_ip = get_local_ip() config_dict = loader.load_dictionary("./config_agent" + my_agent_ID + ".json") path = config_dict["DISTRIBUTED_FS"] print(path + "/local_IP_info.json") #Checking if file is empty, if so, write the IP directly. if not os.path.exists(path + "/local_IP_info.json"): fo = open(path + "/local_IP_info.json", 'w') fo.close() if (os.stat(path + "/local_IP_info.json").st_size == 0): fo = open(path + "/local_IP_info.json", 'w') data = {} data[my_agent_ID] = {} data[my_agent_ID] = {} data[my_agent_ID]["IP"] = internal_ip + ":" + str(port) print(data) json_data = json.dumps(data) fo.write(json_data) fo.close() # File not empty, so we open it to check if the agent has been already written on it. else: fr = open(path + "/local_IP_info.json", 'r') directory = json.load(fr) if my_agent_ID in directory: directory[my_agent_ID]["IP"] = internal_ip + ":" + str(port) fo = open(path + "/local_IP_info.json", 'w') directory = json.dumps(directory) fo.write(directory) fo.close() #continue # if agent not already written, we append it. fr = open(path + "/local_IP_info.json", 'r') directory = json.load(fr) directory[my_agent_ID] = {} directory[my_agent_ID]["IP"] = internal_ip + ":" + str(port) fo = open(path + "/local_IP_info.json", 'w') directory = json.dumps(directory) fo.write(directory) fo.close()
def setGrantLevel(level, my_agent_ID): #load config file config_dict=loader.load_dictionary("./config_agent"+my_agent_ID+".json") if level in ("LOW", "MEDIUM", "HIGH"): config_dict["GRANT_LEVEL"]=level #Config has been set, now, lets write it in agents_grant.json #Checking if file is empty, if so, write the IP directly. while not os.path.exists("./FS/agent_grant.json"): fo = open("./FS/agent_grant.json", 'w') fo.close() if os.stat("./FS/agent_grant.json").st_size==0: fo = open("./FS/agent_grant.json", 'w') data={} data[my_agent_ID]={} data[my_agent_ID]={} data[my_agent_ID]=level json_data=json.dumps(data) fo.write(json_data) fo.close() # File not empty, so we open it to check if the agent has been already written on it. else: fr = open("./FS/agent_grant.json", 'r') directory = json.load(fr) if my_agent_ID in directory: directory[my_agent_ID]=level fo = open("./FS/agent_grant.json", 'w') directory= json.dumps(directory) fo.write(directory) fo.close() return # if agent not already written, we append it. fr = open("./FS/agent_grant.json", 'r') directory = json.load(fr) directory[my_agent_ID]={} directory[my_agent_ID]=level fo = open("./FS/agent_grant.json", 'w') directory= json.dumps(directory) fo.write(directory) fo.close() return
import requests # this import requires pip install requests import loader import sys if __name__ == "__main__": cloudbook_dict_agents = loader.load_dictionary( './du_files/cloudbook_agents.json') host_du0 = cloudbook_dict_agents.get("agent_0").keys()[0] """ try : #text=raw_input() except: print "--" """ #with open(sys.stdin, 'r') as file: #for line in sys.stdin: while True: print "hola" text = raw_input() #text=line#file.readline() print text url = 'http://' + host_du0 + "/invoke?invoked_function=du_0.cloudbook_print('" + text + "')" #url='http://'+host_du0+'/invoke?invoked_function=du_0.cloudbook_print("hola")' print url r = requests.get(url) #print "request lanzada", url #print r.text
def editFSPath(path, my_agent_ID): config_dict=loader.load_dictionary("./config_agent"+my_agent_ID+".json") config_dict["DISTRIBUTED_FS"]=path loader.write_dictionary(config_dict, "./config_agent"+my_agent_ID+".json")
def editCircleID(newCircleID, my_agent_ID): #load config file config_dict=loader.load_dictionary("./config_agent"+my_agent_ID+".json") config_dict["CIRCLE_ID"]=newCircleID loader.write_dictionary(config_dict, "./config_agent"+my_agent_ID+".json")
path = os.environ['HOMEDRIVE'] + os.environ[ 'HOMEPATH'] + os.sep + "cloudbook" + os.sep + project_folder if not os.path.exists(path): os.makedirs(path) else: path = os.environ['HOME'] + os.sep + "cloudbook" + os.sep + project_folder if not os.path.exists(path): os.makedirs(path) # the input and output path is the same #------------------------------------- input_dir = path + os.sep + "distributed" output_dir = path + os.sep + "distributed" config_dir = path + os.sep + "distributed" config_dict = loader.load_dictionary(config_dir + os.sep + "config.json") num_desired_agents = config_dict["NUM_DESIRED_AGENTS"] #set surveillance interval non-faststart and non surveillance mode if (surveillance_interval == 0): surveillance_interval = 2 * int(config_dict["AGENT_GRANT_INTERVAL"]) print(" the sleep interval is set to 2xagent =", surveillance_interval) #clean touch files (CRITICAL, WARNING, HOR_REDEPLOY, etc) # -------------------------------------------------------- surveillance_monitor.clean_touch_files(input_dir) # wait till agents create their agent_xx_grant # -------------------------------------------- if (not fast_start): surveillance_monitor.create_file_agents_grant(input_dir)
def hot_redeploy(input_dir, new_agents_dict, modified_agents_dict, stopped_agents_dict, idle_agents, config_dict): print() print(" ENTER in hot_redeploy() ...") # first make a backup of current dictionary # ----------------------------------------- surveillance_monitor.backup_file(input_dir, "/cloudbook.json", "/previous_cloudbook.json") global dus dus = loader.load_dictionary(input_dir + "/du_list.json") du_default_present = False if 'du_default' in dus: du_default_present = True #extract DU_default from du list #dus.pop('du_default') global agents_with_grant agents_with_grant = loader.load_dictionary(input_dir + "/agents_grant.json") # available agents are all alive agents # ------------------------------------- available_agents = sorted(agents_with_grant.items()) # load current cloudbook old_cloudbook = loader.load_dictionary(input_dir + "/cloudbook.json") new_cloudbook = {} #sorted_new_agents_with_grant = sorted(new_agents_dict.items(), key=operator.itemgetter(1)["GRANT"]) sorted_new_agents_with_grant = sorted(new_agents_dict.items(), key=lambda x: x[1]["GRANT"]) sorted_new_agents_with_grant = sorted_new_agents_with_grant[::-1] aal = [] #available agents list. contaoins all alive agents for i in range(0, len(available_agents)): aal.append(available_agents[i][0]) # only add the name to the list nal = [] # new agents list recently joined for i in range(0, len(sorted_new_agents_with_grant)): nal.append(sorted_new_agents_with_grant[i][0]) #nba=[] # non busy agents #nba= idle_agents #nba=surveillance_monitor.get_non_busy_agents(input_dir) #,aal) ia_index = 0 # idle agent index ia_num = len(idle_agents) #idle agents number #print ("na_num",na_num) print( "-----------------------SUMMARY OF AGENTS------------------------------------" ) print( "An idle agent is a new or existing one but only in charge of du_default" ) print( "An idle agent is called idle because is capable of assuming additional DUs" ) print(" -->available agents (all alive):", available_agents) print(" -->new agents: ", sorted_new_agents_with_grant) print(" -->idle agents (new or not): ", idle_agents) # new or not new print( "--------------------------------------------------------------------------" ) orphan_dict = {} for du in old_cloudbook: if du == "du_default": # du_default never is orphan continue la = [] #print ("lista de ", du, "= ", old_cloudbook[du]) for a in old_cloudbook[du]: #print ("buscando ", a , " en availables") if a in aal: #available_agents: la.append(a) # si la esta vacia, debemos asociar la du a uno de los nuevos agentes # los agentes estan ordenados de mayor a menor power print("ia_index=", ia_index, " ia_num=", ia_num) if la == []: orphan_dict[du] = dus[du] print("orphan dictionary:", orphan_dict) #now check if any orphen DU is critical # an non-critical orphan DU may be hot redeployed when no CRITICAL alarm has been raised. however, # a critical orphan du can not be hot redeployed even without CRITICAL alarm critical_dus = loader.load_dictionary(input_dir + "/critical_dus.json") for critical_du in critical_dus["critical_dus"]: for orphan_du in orphan_dict: if orphan_du == critical_du: return False dus_with_cost = {} for du in orphan_dict: dus_with_cost[du] = dus[du]['cost'] + dus[du][ 'size'] #the complexity and size of the du code is used as cost #print("\ndus_with_cost"); print(dus_with_cost) print("\nSorting orphan DUs...") #this sort operation transform a dictionary into a list, because a dictionary has not order sorted_dus_with_cost = sorted(dus_with_cost.items(), key=operator.itemgetter(1)) sorted_dus_with_cost = sorted_dus_with_cost[:: -1] #reverse the list to have the higher costs first #print("\nsorted_dus_with_cost"); print(sorted_dus_with_cost) for du in old_cloudbook: if du not in orphan_dict and du != "du_default": new_cloudbook[du] = old_cloudbook[du] else: la = [] if ia_index < ia_num: la.append(idle_agents[ia_index]) ia_index += 1 ia_index = ia_index % (ia_num) #print ("du:",du , " will be at agent ",ia_index, "-->",idle_agents[ia_index]) print("du:", du, " will be at ", idle_agents[ia_index]) # if la remains empty is because there are not available idle agents if (la == []): print("the DU , ", du, " remains orphan because all available agents are busy") new_cloudbook[du] = la #assign du_default to all agents #------------------------------- if du_default_present: new_cloudbook["du_default"] = aal #if "AGENT0_ONLY_DU0":true then agent_0 must not take DU_default if (config_dict["AGENT0_ONLY_DU0"] == True): if du_default_present: new_cloudbook["du_default"].remove("agent_0") print("--------- OLD CLOUDBOOK ---------------") print(old_cloudbook) print("--------- NEW CLOUDBOOK ---------------") print(new_cloudbook) print("---------------------------------------") #write output file in json format #--------------------------------- json_str = json.dumps(new_cloudbook) fo = open(output_dir + "/cloudbook.json", 'w') fo.write(json_str) fo.close() p = Path(input_dir + '/HOT_REDEPLOY') p.touch(exist_ok=True) return True
def get_stats(input_dir, delete_stats_files): cumulated_stats = {} print("----") for i in matrix: print(i) print("reading agents stats...") agents_with_grant = {} agents_files = [] agents_files = os.listdir(input_dir + "/stats") print("agent stat files:", agents_files) print("--------------------------------") # For each of the statistics files for file in agents_files: if file.startswith("stats_agent_") and file.endswith(".json"): print("loading file: " + file + " ...") agent_stats_dict = loader.load_dictionary(input_dir + "/stats/" + file) print(" agent_stats_dict --> ", agent_stats_dict) # For each of the invoked functions for invoked_fun in agent_stats_dict: # Looking for: "thread_counter" if invoked_fun == "thread_counter": continue # Looking for: "critical_section_control" elif invoked_fun == "critical_section_control": continue # Looking for: "nonblocking_inv_x_ORIGNAME" with x a number and ORIGNAME the original function name but without the path (fun() instead of nbody.fun()) elif invoked_fun.startswith("nonblocking_inv_"): incomplete_invoked_orig_name = "_".join( invoked_fun.split("_") [3:]) # Take the name from the 3rd underscore onwards for func_name in matrix[0][ 1:]: # For each of the possible names (first row of matrix without "MATRIX") if func_name.split( "." )[-1] == incomplete_invoked_orig_name: # If there is a match with the last part after a dot invoked_orig_name = func_name # Assign that name break # Stop iterating if incomplete_invoked_orig_name == invoked_orig_name: # If they are equal, there was no partial match (unknown orig name) continue # Skip this data # Looking for: "FUNCNAME" else: invoked_orig_name = function_inverse_map[invoked_fun] print(" processing invoked_fun:", invoked_fun, " --> invoked_orig_name:", invoked_orig_name) # For each of the invoker functions for invoker_fun in agent_stats_dict[invoked_fun]: # Looking for: "thread_counter" if invoker_fun == "thread_counter": continue # Looking for: "critical_section_control" elif invoker_fun == "critical_section_control": continue # Looking for: "nonblocking_inv_FUNCNAME" elif invoker_fun.startswith("nonblocking_inv_"): invoker_orig_name = function_inverse_map["_".join( invoker_fun.split("_")[2:])] # Looking for: "FUNCNAME" else: invoker_orig_name = function_inverse_map[invoker_fun] print(" processing invoker_fun:", invoker_fun, " --> invoker_orig_name:", invoker_orig_name) # Get the old value from the matrix previous_value = matrix_get(invoker_orig_name, invoked_orig_name) adding_value = agent_stats_dict[invoked_fun][invoker_fun] final_value = previous_value + adding_value print(" ", previous_value, "(previous_value) +", adding_value, "(adding_value) = ", final_value, "(final_value)") # Assign the new value to the matrix matrix_set(invoker_orig_name, invoked_orig_name, final_value) # Remove the consumed file if delete_stats_files: try: os.remove(input_dir + "/stats/" + file) except: pass print("All agent statistics files have been read") return
if (project_folder == ""): print("option -project_folder missing") sys.exit(0) #load dictionary config.json to extract AGENT_STATS_INTERVAL # ---------------------------------------------------------- if (platform.system() == "Windows"): path = os.environ['HOMEDRIVE'] + os.environ[ 'HOMEPATH'] + os.sep + "cloudbook" + os.sep + project_folder else: path = os.environ['HOME'] + os.sep + "cloudbook" + os.sep + project_folder input_dir = path + os.sep + "distributed" config_dir = path + os.sep + "distributed" config_dict = loader.load_dictionary(config_dir + os.sep + "config.json") agent_stats_interval = config_dict["AGENT_STATS_INTERVAL"] print(" value of AGENT_STATS_INTERVAL at config.json is:", str(agent_stats_interval), " seconds") # default value of stats interval is 3/2 * agent_stats_interval if (stats_interval == 0): stats_interval = (3 * agent_stats_interval) / 2 # check final value of stats_interval if (stats_interval < agent_stats_interval): print( "very low interval value (lower than AGENT_STATS_INTERVAL parameter at config.json" ) sys.exit(0)
##MIRO SI LA OPCION ES CREAR AGENTE if any(i in args for i in ["create"]): create_agent(grant=2, project_name=my_project_folder, fs="", agent_0=agent_file) print("Agent Created!") os._exit(1) # Create multiprocessing Values and Arrays value_var_grant = Value("i", 0) # Value (integer with initial value 0) sharable by processes array_var_ip = Array('c', range(15)) # Array (characters) sharable by processes. IP length is at most 4 3-digit numbers and 3 dots string2array("", array_var_ip) value_var_port = Value("i", 0) # Value (integer with initial value 0) sharable by processes # Load agent config file project_path = cloudbook_path + os.sep + my_project_folder agent_config_dict = loader.load_dictionary(project_path + os.sep + "agents" + os.sep + "config_"+agent_file+".json") my_agent_ID = agent_config_dict["AGENT_ID"] fs_path = agent_config_dict["DISTRIBUTED_FS"] num2value(grant2num(agent_config_dict["GRANT_LEVEL"]), value_var_grant) # If agent is the agent_0, clear the RUNNING file (a previous execution did not end correctly with return) if my_agent_ID=="agent_0": cloudbook_is_running(force_remove=True) # Change working directory os.chdir(fs_path + os.sep + "working_dir") # Load circle config file configjson_dict = loader.load_dictionary(fs_path + os.sep + "config.json")
def is_critical(du): critical_dus_file_path = fs_path + os.sep + "critical_dus.json" critical_dus_dict = loader.load_dictionary(critical_dus_file_path) critical_dus = critical_dus_dict["critical_dus"] return du in critical_dus
import json import os import sys import urllib.request # this import requires pip install requests import logging import random, string from pynat import get_ip_info import loader my_agent_ID = "None" my_circle_ID = "None" agents_ip = {} config_dict = {} config_dict = loader.load_dictionary("config_agent.json") #Creates an agent ID and connects to the directory server to indicate its public IP, agent_id and circle_id def createAgentID(): global my_agent_ID global my_circle_ID #Random agent_id if it doesn't exist my_agent_ID = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(20)) if config_dict["AGENT_ID"] == "0": config_dict["AGENT_ID"] = my_agent_ID else: my_agent_ID = config_dict["AGENT_ID"] my_circle_ID = config_dict["CIRCLE_ID"] fo = open("config_agent.json", 'w') json_data = json.dumps(config_dict)
def read_cloudbook_file(): global cloudbook_dict_agents global du_list cloudbook_dict_agents = loader.load_dictionary(cloudbookjson_file_path) #print("cloudbook.json has been read.\n cloudbook_dict_agents = ", cloudbook_dict_agents) du_list = loader.load_cloudbook_agent_dus(my_agent_ID, cloudbook_dict_agents)
def read_agents_grant_file(): global agents_grant agents_grant = loader.load_dictionary(agents_grant_file_path)
def setFSPath(path): #load config file config_dict=loader.load_dictionary("./config_agent.json") config_dict["DISTRIBUTED_FS"]=path loader.write_dictionary(config_dict, "./config_agent.json")
def flaskThreaded(port): port = int(port) print("Voy a lanzar en", port) application.run(debug=False, host="0.0.0.0", port=port, threaded=True) print( "00000000000000000000000000000000000000000000000000000000000000000000000000" ) if __name__ == "__main__": print("Al lio") #load config file agent_id = sys.argv[1] config_dict = loader.load_dictionary("./config_agent" + agent_id + ".json") #global my_agent_ID #global my_circle_ID my_agent_ID = config_dict["AGENT_ID"] my_circle_ID = config_dict["CIRCLE_ID"] complete_path = config_dict["DISTRIBUTED_FS"] my_grant = config_dict["GRANT_LEVEL"] configure_agent.setGrantLevel(my_grant, my_agent_ID) print("my_agent_ID=" + my_agent_ID) print("loading deployable units for agent " + my_agent_ID + "...") #cloudbook_dict_agents = loader.load_cloudbook_agents() #It will only contain info about agent_id : du_assigned (not IP)
cloudbook_path = os.environ['HOMEDRIVE'] + os.environ[ 'HOMEPATH'] + os.sep + "cloudbook" else: cloudbook_path = os.environ['HOME'] + os.sep + "cloudbook" # The path to the necessary folders project_path = cloudbook_path + os.sep + project_folder agents_grant_path = project_path + os.sep + "distributed" + os.sep + "agents_grant.json" running_path = project_path + os.sep + "distributed" + os.sep + "RUNNING" cloudbookjson_path = project_path + os.sep + "distributed" + os.sep + "cloudbook.json" input_dir = project_path + os.sep + "distributed" # Load agent_0 ip and port agent0_ip_port = "" try: agents_grant_dict = loader.load_dictionary(agents_grant_path) agent0_ip_port = agents_grant_dict['agent_0']['IP'] + ":" + str( agents_grant_dict['agent_0']['PORT']) print("Detected agent_0 is available at:", agent0_ip_port) except: print( "\nERROR: IP or port for agent_0 is unknown, and execution could not start. See agents_grant.json\n" ) os._exit(1) #delete alarms surveillance_monitor.clean_touch_files(input_dir) # Create RUNNING file Path(running_path).touch(exist_ok=True)
def run_LOCAL_agent(agent_id): #load config file config_dict = loader.load_dictionary("./config_agent" + agent_id + ".json") global my_agent_ID global my_circle_ID my_agent_ID = config_dict["AGENT_ID"] my_circle_ID = config_dict["CIRCLE_ID"] complete_path = config_dict["DISTRIBUTED_FS"] print("my_agent_ID=" + my_agent_ID) print("loading deployable units for agent " + my_agent_ID + "...") #cloudbook_dict_agents = loader.load_cloudbook_agents() #It will only contain info about agent_id : du_assigned (not IP) #must be the output file from DEPLOYER #HERE WE MUST WAIT UNTIL THIS FILE EXISTS OR UPDATES: HOW TO DO THIS? while (os.stat(complete_path + '/cloudbook_agents.json').st_size == 0): continue #Check file format :D global cloudbook_dict_agents cloudbook_dict_agents = loader.load_cloudbook(complete_path + '/cloudbook_agents.json') #Loads the DUs that belong to this agent. global du_list du_list = loader.load_cloudbook_agent_dus(my_agent_ID, cloudbook_dict_agents) print("MI DU LIST", du_list) #du_list=["du_0"] # fake j = du_list[0].rfind('_') + 1 # num_du is the initial DU and will be used as offset for listen port num_du = du_list[0][j:] host = local_publisher.get_local_ip() print("this host is ", host) #Local port to be opened local_port = 3000 + int(num_du) print(host, local_port) #get all dus global all_dus for i in cloudbook_dict_agents: all_dus.append(i) for du in du_list: exec("from FS/du_files import " + du) exec(du + ".invoker=outgoing_invoke") log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) threading.Thread(target=local_publisher.announceAgent, args=(my_circle_ID, my_agent_ID, local_port)).start() #Process(target=flaskThreaded, args=(local_port,)).start() threading.Thread(target=flaskThreaded, args=[local_port]).start() #flaskThreaded(local_port) print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")