def post(self, action_type: str): """ request.data : { 'params': {...} } :param action_type: :return: """ connector = get_connector() data = request.get_json() action_type = action_type.lower() if 'params' not in data: return {"message": "NOT OK"} params = data['params'] if action_type == "horizontal_scaling": self.horizontal_scaling(connector, params) return {"message": "OK"} commands = {} if action_type == "vertical_scaling": commands['vertical_scaling'] = self.vertical_scaling(params) if action_type == "network": commands["network"] = self.network(params) if action_type == "stress": commands["stress"] = self.stress(connector, params) if action_type == "command": commands["command"] = self.command(params) if action_type == "links": commands["links"] = self.links(params) Communicator(get_connector()).agents__perform_action( commands, **params) return {"message": "OK"}
def post(self, service): commands, res = {'links': {'network': 'all'}}, [] for instance_type in service.split("|"): res.append( Communicator(get_connector()).agents__perform_action( commands, instance_type=instance_type)) return {"message": "OK"}
def connector_callback(request, connector_name): connector = connectors.get_connector(connector_name) uid, name, access_token, expire_time = connector.login_callback(request) request.session['%s_access_token' % connector_name] = access_token if request.user.is_anonymous(): try: connection = Connection.objects.get(connector_id=uid, connector=connector_name) if connection.user.connector: credential = connection.user.username user = authenticate(credential=credential, password=credential) else: user = authenticate(connection_id=connection.id) except Connection.DoesNotExist: seed = os.urandom(40) credential = hashlib.sha1(seed).hexdigest() user = User.objects.create_user(username=credential, email=credential, password=credential) user.connector = connector_name user.save() user = authenticate(credential=credential, password=credential) Connection.objects.create(user=user, connector=connector_name, connector_id=uid, connector_username=name) auth_login(request, user) return redirect(index) try: Connection.objects.get(user=request.user, connector=connector_name) return redirect(connector_detail, connector_name=connector_name, uid=uid) except Connection.DoesNotExist: Connection.objects.create(user=request.user, connector=connector_name, connector_id=uid, connector_username=name) return redirect(settings)
def print_sqoop_cmds(task): with get_connector(task.source) as conn: to_import = get_tables_to_import(conn, task) for table in sorted(to_import, key=lambda tbl: tbl.name): columns = conn.get_columns(table) cmd = SqoopCmd(task, table, columns) print cmd.as_string()
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 print_schema(task): with get_connector(task.source) as conn: to_import = get_tables_to_import(conn, task) for table in to_import: print '\n=== {0}.{1} ==='.format(task.source['db'], table[0]) columns = conn.get_columns(table) for c in columns: print '{0}: {1}'.format(c.name, c.type)
def get(self): """ Returns the current status of the fogify deployment""" infrastructure_status = Status.query.filter_by( name="infrastructure").first() if infrastructure_status and infrastructure_status.value == 'error': raise exceptions.APIException( 'The deployment is failed. Please check the logs of the Fogify Controller.' ) connector = get_connector() return connector.return_deployment()
def get(self, service): if service.lower() != "controller-properties": return {"message": "error"} try: return {"credits": get_connector().get_manager_info()} except Exception as ex: logging.error( "The system does not return the credits of the manager.", exc_info=True) return {"credits": ""}
def get(self): """ Returns the stored monitoring data """ try: query = "" from_timestamp = request.args.get('from_timestamp') to_timestamp = request.args.get('to_timestamp') service = request.args.get('service') query += "from_timestamp=" + from_timestamp + "&" if from_timestamp else "" query += "to_timestamp=" + to_timestamp + "&" if to_timestamp else "" query += "service=" + service if service else "" return Communicator(get_connector()).agents__get_metrics(query) except Exception as e: return {"Error": "{0}".format(e)}
def get(self): """ Returns the stored monitoring data """ try: query = "" from_timestamp = request.args.get('from_timestamp') to_timestamp = request.args.get('to_timestamp') service = request.args.get('service') packet_type = request.args.get('packet_type') query += "from_timestamp=" + from_timestamp + "&" if from_timestamp else "" query += "to_timestamp=" + to_timestamp + "&" if to_timestamp else "" query += "service=" + service if service else "" query += "packet_type=" + packet_type if packet_type else "" return Communicator(get_connector()).agents__get_packets(query) except Exception as e: logging.error("The system could not return the sniffer's data.", exc_info=True) return {"Error": "{0}".format(e)}
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, name): path = os.getcwd() + app.config['UPLOAD_FOLDER'] if 'file' in request.files: file = request.files['file'] if not os.path.exists(path): os.mkdir(path) file.save(os.path.join(path, "rttdata.txt")) res = NetworkController.generate_network_distribution(path, name) lines = res.split("\n") res = {} for line in lines: line_arr = line.split("=") if len(line_arr) == 2: res[line_arr[0].strip()] = line_arr[1].strip() Communicator(get_connector()).agents__disseminate_net_distribution( name, open(os.path.join(path, name + ".dist"), 'rb')) return {"generated-distribution": res}
def delete(self): """ Removes the stored monitoring data """ return Communicator(get_connector()).agents__delete_packets()
def __init__(self, args, app): """ It instantiates the agent and starts the API server :param args: :param app: """ db_path = os.getcwd() + '/agent_database.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_path Agent.db = SQLAlchemy(app) if os.path.exists(db_path): os.remove(db_path) os.mknod(db_path) app.config['UPLOAD_FOLDER'] = "/current_agent/" os.environ['UPLOAD_FOLDER'] = "/current_agent/" if not os.path.exists(os.getcwd() + app.config['UPLOAD_FOLDER']): os.mkdir(os.getcwd() + app.config['UPLOAD_FOLDER']) connector = get_connector() app.config['CONNECTOR'] = connector node_labels = {} if 'LABELS' in os.environ: node_labels = { i.split(":")[0]: i.split(":")[1] for i in os.environ['LABELS'].split(",") if len(i.split(":")) == 2 } node_labels.update(HostInfo.get_all_properties()) connector.inject_labels( node_labels, HOST_IP=os.environ['HOST_IP'] if 'HOST_IP' in os.environ else None) from utils.monitoring import MetricCollector from agent.views import MonitoringAPI, ActionsAPI, TopologyAPI, DistributionAPI, SnifferAPI # Add the api routes app.add_url_rule('/topology/', view_func=TopologyAPI.as_view('Topology')) app.add_url_rule('/monitorings/', view_func=MonitoringAPI.as_view('Monitoring')) app.add_url_rule('/actions/', view_func=ActionsAPI.as_view('Action')) app.add_url_rule('/packets/', view_func=SnifferAPI.as_view('Packet')) app.add_url_rule( '/generate-network-distribution/<string:name>/', view_func=DistributionAPI.as_view('NetworkDistribution')) # The thread that runs the monitoring agent metricController = MetricCollector() metricControllerTask = AsyncTask(metricController, 'start_monitoring', [args.agent_ip, connector, 5]) metricControllerTask.start() # The thread that inspect containers and apply network QoS networkController = NetworkController(connector) networkControllerTask = AsyncTask(networkController, 'listen', []) networkControllerTask.start() self.app = app self.args = args
def connector_login(request, connector_name): connector = connectors.get_connector(connector_name) return redirect(connector.login())