Beispiel #1
0
def configure_agents(controller_address, auth_token, microservices,
                     fog_per_microservice):

    for microserviceKey in microservices:
        microservice = microservices[microserviceKey]
        new_fog_config = microservice.get("agent-config", {})
        fog = fog_per_microservice[microserviceKey]
        if not new_fog_config:
            return
        # fog.update(new_fog_config)
        print_info("====> Updating Agent config")
        agent_service.update_agent(controller_address, fog["uuid"],
                                   new_fog_config, auth_token)
Beispiel #2
0
def load_config(config_file):
    # Check that everything we need exists
    if not os.path.exists(config_file):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(config_file) + "`")

    # Read our config file
    with open(config_file, 'r') as ymlfile:
        theConfig = yaml.load(ymlfile, Loader=yaml.FullLoader)

    print_info("Loaded config from '" + os.path.abspath(config_file) + "`")
    print(theConfig)

    return theConfig
Beispiel #3
0
def update_catalog(controller_address, auth_token, catalog_item):
    # Delete all microservices that uses this specific catalog item
    print_info(
        "====> Deleting all microservices currently using this catalog item")
    microservice_service.delete_all_by_catalog_id(controller_address,
                                                  auth_token,
                                                  catalog_item["id"])
    # Update catalog item
    data = create_catalog_curl_data(catalog_item)
    if data == {}:
        # If data has failed to be created, exit here, and echo which service failed
        return "{} failed to create curl data".format(catalog_item)
    post_address = "{}/catalog/microservices/{}".format(
        controller_address, catalog_item["id"])
    json_response = create_rest_call(data,
                                     post_address,
                                     auth_token,
                                     method="PATCH")
Beispiel #4
0
def setup(controller_address, auth_token, microservices):
    # For each microservice check if catalog item exists
    print_info("====> Reading the catalog")
    updated = False
    existing_catalog_items = get_catalog(controller_address, auth_token)
    catalog_ids = {}
    for microserviceKey in microservices:
        microservice = microservices[microserviceKey]
        catalog_item = {
            "images": microservice["images"],
            "name": microservice["microservice"]["name"] + "_catalog"
        }
        catalog_id = ""
        existing_catalog_item = next((x for x in existing_catalog_items
                                      if x["name"] == catalog_item["name"]),
                                     None)
        # If it does not exists yet, create
        if existing_catalog_item == None:
            print_info("====> Adding to the catalog")
            updated = True
            catalog_id = add_to_catalog(controller_address, auth_token,
                                        catalog_item)["id"]
        # Otherwise, patch to update images
        else:
            catalog_item["id"] = existing_catalog_item["id"]
            # Check if images have changed
            if is_same(catalog_item, existing_catalog_item) == False:
                print_info("====> Updating a catalog item (Delete / Recreate)")
                updated = True
                # update_catalog(controller_address, auth_token, catalog_item)
                delete_by_id(controller_address, existing_catalog_item["id"],
                             auth_token)
                catalog_id = add_to_catalog(controller_address, auth_token,
                                            catalog_item)["id"]
            else:
                catalog_id = catalog_item["id"]
        catalog_ids[microserviceKey] = catalog_id
    if updated == False:
        print_info("====> Catalog is up-to-date")
    else:
        print_info("====> Catalog updated")
    return catalog_ids
Beispiel #5
0
def update_routing(controller_address, microservices_per_name, auth_token, routes):
    print_info("====> Update routing")
    updated = False
    for route in routes:
        # Check if route exists
        msvc = microservices_per_name.get(route["from"], None)
        dest_msvc = microservices_per_name.get(route["to"], None)
        if msvc == None:
            print_error("No source microservice for route: {} - Microservices: {}".format(route, microservices_per_name))
            continue
        if dest_msvc == None:
            print_error("No destination microservice for route: {} - Microservices: {}".format(route, microservices_per_name))
            continue
        route_exists = msvc != None and next((x for x in msvc.get("routes", []) if x == dest_msvc["uuid"]), False)
        # Create missing route
        if route_exists == False:
            print_info("====> Create new route")
            updated = True
            create_route(controller_address, auth_token,
                {
                    "from": microservices_per_name[route["from"]]["uuid"],
                    "to": microservices_per_name[route["to"]]["uuid"]
                })
    # Delete unecessary routes
    for microservice_name in microservices_per_name:
        microservice = microservices_per_name[microservice_name]
        microservice_routes = microservice.get("routes", [])
        for route in microservice_routes:
            route_needed = next((x for x in routes if x["from"] == microservice["name"] and microservices_per_name.get(x["to"], {"uuid": None})["uuid"] == route), False)
            if route_needed == False:
                print_info("====> Delete outdated route")
                updated = True
                delete_route(controller_address, auth_token,
                {
                    "from": microservice["uuid"],
                    "to": route
                })
    if updated == False:
        print_info("====> Routing is up-to-date.")
    else:
        print_info("====> Routing updated.")
Beispiel #6
0
def update_ports(controller_address, microservice, auth_token):
    print_info("====> Getting current port mapping")
    updated = False
    ports = microservice.get("ports", [])
    existing_port_mappings = get_microservice_port_mapping(controller_address, microservice["uuid"], auth_token)
    # Remove false port mapping
    for existing_mapping in existing_port_mappings:
        valid = next((x for x in ports if x["internal"] == existing_mapping["internal"] and x["external"] == existing_mapping["external"]), False)
        if valid == False:
            print_info("====> Remove outdated port mapping")
            updated = True
            delete_microservice_port_mapping(controller_address, microservice["uuid"], existing_mapping, auth_token)
    # Create missing port mapping
    for new_mapping in ports:
        exists = next((x for x in existing_port_mappings if x["internal"] == new_mapping["internal"] and x["external"] == new_mapping["external"]), False)
        if exists == False:
            print_info("====> Create new port mapping")
            updated = True
            create_microservice_port_mapping(controller_address, microservice["uuid"], new_mapping, auth_token)
    if updated == False:
        print_info("====> Port mapping is up-to-date")
    else:
        print_info("====> Port mapping updated")
Beispiel #7
0
def clean(theConfig):
    # Get values from config
    user = theConfig["user"]
    controller = theConfig["controller"]
    controller_address = controller["address"] + "/api/v3"
    microservices = theConfig["microservices"]
    routes = theConfig["routes"]

    print_info("====> Beginning cleanup process")
    print_info("====> Authenticating")
    auth_token = iofog_auth(controller_address, user["email"],
                            user["password"])

    try:
        print_info("====> Retrieving flow id")
        flow_id = flow_service.get_id(controller_address, theConfig["flow"],
                                      auth_token)

        print_info("====> Deleting flow")
        print_info(
            "====> Deleting the flow will remove the microservices and routes")
        flow_id = flow_service.delete_flow(controller_address, flow_id,
                                           auth_token)
    except Exception as err:
        print_error(f'Could not delete the flow. Error: {err}')

    try:
        catalog_items = []
        print_info("====> Listing catalog")
        catalog_list = catalog_service.get_catalog(controller_address,
                                                   auth_token)
        for microservice in microservices.values():
            microservice_name = microservice["microservice"][
                "name"] + "_catalog"
            catalog_id = next(x for x in catalog_list
                              if x["name"] == microservice_name)["id"]
            catalog_items.append(catalog_id)

        print_info("====> Deleting from catalog")
        catalog_service.delete_items(controller_address, catalog_items,
                                     auth_token)

        print_success("====> You are done !")
    except Exception as err:
        print_error(f'Could not remove from catalog. Error: {err}')
        print_error(sys.exc_info()[0])
Beispiel #8
0
def setup(theConfig):
    # Get values from config
    user = theConfig["user"]
    controller = theConfig["controller"]
    controller_address = controller["address"] + "/api/v3"
    microservices = theConfig["microservices"]
    routes = theConfig["routes"]

    print_info("====> Authenticating")
    auth_token = iofog_auth(controller_address, user["email"],
                            user["password"])

    # Create our standard flow_id for everything else
    print_info("====> Creating or stoping existing flow")
    flow_id = flow_service.create_flow_if_not_exist(controller_address,
                                                    auth_token,
                                                    theConfig["flow"])
    # Stoping the flow
    flow_service.stop_flow(controller_address, flow_id, theConfig["flow"],
                           auth_token)

    # Updating agent config
    print_info("====> Getting Agent uuids")
    fog_per_microservice = agent_service.get_agent_per_microservice(
        controller_address, auth_token, microservices)

    print_info("====> Configuring Agents")
    configure_agents(controller_address, auth_token, microservices,
                     fog_per_microservice)

    # Updating catalog
    print_info("====> Registering microservices images to the catalog")
    catalog_id_per_microservice = catalog_service.setup(
        controller_address, auth_token, microservices)

    # Setup the microservices in controller and attached to our agent
    print_info("====> Creating microservices and routes")
    microservice_service.setup(controller_address, flow_id,
                               fog_per_microservice,
                               catalog_id_per_microservice, auth_token,
                               microservices, routes)

    # Start up the rest of the service
    print_info("====> Starting flow")
    flow_service.restart_flow(controller_address, flow_id, theConfig["flow"],
                              auth_token)

    print_success("====> You are done !")
Beispiel #9
0
if __name__ == "__main__":

    # Initialize colorama
    init(autoreset=True)

    parser = argparse.ArgumentParser(
        description='Setup microservices according to a YAML configuration file'
    )
    parser.add_argument(
        '--clean',
        dest='clean',
        action='store_true',
        default=False,
        help='Clean previously set up microservices from the controller')
    parser.add_argument(
        '--config',
        dest='config_file',
        action='store',
        default='config.yml',
        help='Specify the config file (default to config.yaml)')

    args = parser.parse_args()

    print_info("===> Loading our configuration")
    config = load_config(args.config_file)
    if args.clean:
        clean(config)
    else:
        setup(config)