def vertical_scaling(self, params): vaction = VerticalScalingAction(**params['action']) Annotation.create(Annotation.TYPES.V_SCALE.value, instance_names=params['action']['instance_type'], params="parameters: " + vaction.get_command() + "=>" + vaction.get_value()) return {vaction.get_command(): vaction.get_value()}
def links(self, params): """ {'network': 'internet', 'links': [{'from_node': 'cloud-server', 'to_node': 'mec-svc-1', 'bidirectional': False, 'properties': {'latency': {'delay': '1500ms'}}}], 'instance_type': 'cloud-server', 'instances': 1} """ if 'links' not in params: return {} if 'network' not in params: return {} commands = {} commands['network'] = params['network'] res = [] for link in params['links']: obj = link['parameters'] obj['from_node'] = link['from_node'] obj['to_node'] = link['to_node'] obj['bidirectional'] = link['bidirectional'] res.extend(Network.get_link(obj)) commands['links'] = res Annotation.create(Annotation.TYPES.UPDATE_LINKS.value, instance_names=params['instance_type'], params="parameters: " + "Network" + "=>" + commands['network'] + ", link number=>" + str(len(commands['links']))) return commands
def submition(self, connector, path, networks): """ A utility function that deploys a topology """ nodes = connector.get_nodes() #add network rules for i in nodes: r = requests.post( 'http://' + nodes[i] + ':5500/topology/', files={'file': open(path + "fogified-network.yaml", 'rb')}) for network in networks: try: connector.create_network(network) except Exception: pass #submit the current deployment try: connector.deploy() except Exception as ex: print(ex) Status.update_config('error') return Status.update_config('running') Annotation.create(Annotation.TYPES.DEPLOY.value)
def delete(self): """ Remove a Fogify deployment""" Status.update_config('submit_delete') Annotation.create(Annotation.TYPES.STOP.value) connector = get_connector() t = AsyncTask(self, 'remove', [connector]) t.start() return {"message": "The topology is down."}
def delete(self): """ Remove a Fogify deployment""" Status.update_config('submit_delete') Annotation.create(Annotation.TYPES.STOP.value) path = os.getcwd() + app.config['UPLOAD_FOLDER'] t = AsyncTask(self, 'remove') t.start() return {"message": "The topology is down."}
def stress(self, connector, params): if 'instance_id' in params: name = params['instance_id'] if 'instance_type' in params: name = params['instance_type'] service_cpu = connector.get_running_container_processing( connector.instance_name(name).split(".")[0]) if not service_cpu: service_cpu = 1 commands = StressAction(**params['action']).get_command(service_cpu) Annotation.create(Annotation.TYPES.STRESS.value, instance_names=params['instance_type'], params="parameters: " + commands) return commands
def horizontal_scaling(self, connector, params): instances = int(params['instances']) if params['type'] == 'up': service_count = int( connector.count_services(params['instance_type'])) + instances Annotation.create(Annotation.TYPES.H_SCALE_UP.value, instance_names=params['instance_type'], params="num of instances: " + str(instances)) else: service_count = int( connector.count_services(params['instance_type'])) - instances service_count = 0 if service_count < 0 else service_count Annotation.create(Annotation.TYPES.H_SCALE_DOWN.value, instance_names=params['instance_type'], params="num of instances: " + str(instances)) connector.scale(params['instance_type'], service_count)
def post(self): """ Introduce a new deployment to the fogify""" path = os.getcwd() + app.config['UPLOAD_FOLDER'] Annotation.create(Annotation.TYPES.START.value) if 'file' in request.files: file = request.files['file'] if not os.path.exists(path): os.mkdir(path) file.save(os.path.join(path, "docker-compose.yaml")) f = open(os.path.join(path, "docker-compose.yaml"), "r") infra = yaml.load(f) model = FogifyModel(infra) try: connector = get_connector(model=model) controller_response = connector.generate_files() yaml.dump(controller_response, open(path + "fogified-swarm.yaml", 'w'), default_flow_style=False) networks = model.generate_network_rules() except Exception as ex: logging.error( "An error occurred on monitoring view. The metrics did not retrieved.", exc_info=True) raise exceptions.APIException( "Fogify could not generate the orchestrator files." "Please check your fogify model again.") yaml.dump(networks, open(path + "fogified-network.yaml", 'w'), default_flow_style=False) t = AsyncTask(self, 'submition', [connector, path, model.all_networks]) t.start() return { "message": "OK", "swarm": controller_response, "networks": networks }
def post(self): """ Introduce a new deployment to the fogify""" path = os.getcwd() + app.config['UPLOAD_FOLDER'] Annotation.create(Annotation.TYPES.START.value) if 'file' in request.files: file = request.files['file'] if not os.path.exists(path): os.mkdir(path) file.save(os.path.join(path, "docker-compose.yaml")) f = open(os.path.join(path, "docker-compose.yaml"), "r") infra = yaml.load(f) # application = infra.copy() from models.base import FogifyModel model = FogifyModel(infra) connector = SwarmConnector( model, path=path, frequency=int(os.environ['CPU_FREQ']) if 'CPU_FREQ' in os.environ else 2400, cpu_oversubscription=int( os.environ['CPU_OVERSUBSCRIPTION_PERCENTAGE']) if 'CPU_OVERSUBSCRIPTION_PERCENTAGE' in os.environ else 0, ram_oversubscription=int( os.environ['RAM_OVERSUBSCRIPTION_PERCENTAGE']) if 'RAM_OVERSUBSCRIPTION_PERCENTAGE' in os.environ else 0) swarm = connector.generate_files() networks = NetworkGenerator(model).generate_network_rules() yaml.dump(networks, open(path + "fogified-network.yaml", 'w'), default_flow_style=False) t = AsyncTask(self, 'submition', [connector, path, model.all_networks]) t.start() return {"success": True, "swarm": swarm, "networks": networks}
def network(self, params): new_params = { i: params[i] for i in params if i not in ['instance_type', 'network', 'instance_id', 'instances'] } network = params['network'] if not new_params and 'network' in params: new_params = params['network'] network = new_params['network'] if not new_params: return {} commands = Network(new_params).network_record commands['network'] = network Annotation.create(Annotation.TYPES.NETWORK.value, instance_names=params['instance_type'], params="parameters: " + "Network" + "=>" + commands['network'] + ", uplink=>" + commands['uplink'] + ", downlink=>" + commands['downlink']) return commands
def submition(self, connector, path, networks): """ A utility function that deploys a topology """ file = open(path + "fogified-network.yaml", 'rb') Communicator(connector).agents__forward_network_file(file) for network in networks: try: connector.create_network(network) except Exception: logging.error("The system could not create %s network." % network) # submit the current deployment try: connector.deploy() except Exception as ex: logging.error("An error occured in the deployment.", exc_info=True) Status.update_config('error') return Status.update_config('running') Annotation.create(Annotation.TYPES.DEPLOY.value)
def command(self, params): commands = {"command": CommandAction(**params['action']).get_command()} Annotation.create(Annotation.TYPES.COMMAND.value, instance_names=params['instance_type'], params="parameters: " + commands['command']) return commands
def remove(self, connector): """ A utility function that destroys a topology """ connector.down() Annotation.create(Annotation.TYPES.UNDEPLOY.value)
def remove(self): """ A utility function that destroys a topology """ swarmController = SwarmConnector() swarmController.down() Annotation.create(Annotation.TYPES.UNDEPLOY.value)
def post(self, action_type): """ request.data : { 'params': {...} } :param action_type: :return: """ swarmController = SwarmConnector() data = request.get_json() # TODO check how to define the possible containers or services if 'params' in data: params = data['params'] if action_type == "horizontal_scaling": instances = int(params['instances']) if params['type'] == 'up': service_count = int( swarmController.count_services( params['instance_type'])) + instances Annotation.create(Annotation.TYPES.H_SCALE_UP.value, instance_names=params['instance_type'], params="num of instances: " + str(instances)) else: service_count = int( swarmController.count_services( params['instance_type'])) - instances service_count = 0 if service_count < 0 else service_count Annotation.create(Annotation.TYPES.H_SCALE_DOWN.value, instance_names=params['instance_type'], params="num of instances: " + str(instances)) swarmController.scale(params['instance_type'], service_count) else: selected_instances = self.instance_ids(params) commands = {} if action_type == "vertical_scaling": vaction = VerticalScalingAction(**data['params']['action']) commands['vertical_scaling'] = { vaction.get_command(): vaction.get_value() } Annotation.create(Annotation.TYPES.V_SCALE.value, instance_names=params['instance_type'], params="parameters: " + vaction.get_command() + "=>" + vaction.get_value()) if action_type == "network": action = data['params'] commands = Network(action).network_record commands['network'] = action['network'] Annotation.create(Annotation.TYPES.NETWORK.value, instance_names=params['instance_type'], params="parameters: " + "Network" + "=>" + commands['network'] + ", uplink=>" + commands['uplink'] + ", downlink=>" + commands['downlink']) if action_type == "stress": service_cpu = None if 'instance_type' in params: service_cpu = swarmController.get_running_container_processing( params['instance_type'] if not params['instance_type'].split(".")[-1]. isnumeric() else "".join(params['instance_type']. split(".")[:-1])) elif 'instance_id' in params: service_cpu = swarmController.get_running_container_processing( params['instance_id'] if not params['instance_id']. split(".")[-1].isnumeric() else "". join(params['instance_id'].split(".")[:-1])) else: service_cpu = 1 service_cpu = 1 if not service_cpu else service_cpu commands['stress'] = StressAction( **data['params']['action']).get_command(service_cpu) Annotation.create(Annotation.TYPES.STRESS.value, instance_names=params['instance_type'], params="parameters: " + commands['stress']) if action_type == "command": commands["command"] = CommandAction( **data['params']['action']).get_command() Annotation.create(Annotation.TYPES.COMMAND.value, instance_names=params['instance_type'], params="parameters: " + commands['command']) action_url = 'http://%s:5500/actions/' for i in selected_instances: requests.post(action_url % socket.gethostbyname(i), json={ 'instances': selected_instances[i], 'commands': commands }, headers={'Content-Type': "application/json"}) return {"message": "OK"}