Exemple #1
0
def get_virtual_machine(vm, **_):
    """
    Load the configuration for a given virtual machine
    
    Variables: 
    vm       => Name of the virtual machine to get the info
    
    Arguments:
    None
    
    Data Block:
    None
    
    Result example:
    { 
     enabled: true,                  # Is VM enabled
     name: "Extract",                # Name of the VM
     num_workers: 1,                 # Number of workers
     os_type: "windows",             # Type of OS
     os_variant: "win7",             # Variant of OS
     ram: 1024,                      # Amount of RAM
     revert_every: 600,              # Auto revert seconds interval
     vcpus: 1,                       # Number of CPUs
     virtual_disk_url: "img.qcow2"   # Name of the virtual disk to download
    }                
    """
    return make_api_response(STORAGE.get_virtualmachine(vm))
Exemple #2
0
def list_services_workers(**_):
    """
    List number of workers for each services in the system.
    
    Variables:
    None
    
    Arguments:
    None
    
    Data Block:
    None
    
    Result example:
    {"MY SERVICE": 1, ... } # Dictionary of services and number of workers
    """
    services = {s["name"]: 0 for s in STORAGE.list_services() if s['enabled']}
    profiles = STORAGE.get_profiles_dict(
        list(
            set([
                p["_yz_rk"] for p in STORAGE.stream_search(
                    "profile", "_yz_rk:*", fl="_yz_rk")
            ])))
    used_profiles = {
        n['mac_address']: n['profile']
        for n in STORAGE.get_nodes(STORAGE.list_node_keys())
        if n['profile'] != ""
    }

    for _mac, used_p in used_profiles.iteritems():
        if used_p in profiles:
            for srv, cfg in profiles[used_p]["services"].iteritems():
                if srv in services:
                    services[srv] += cfg["workers"]

            for srv, cfg in profiles[used_p]["virtual_machines"].iteritems():
                if srv in services:
                    vm = STORAGE.get_virtualmachine(srv)
                    if not vm:
                        continue
                    services[srv] += vm['num_workers'] * cfg['num_instances']

    return make_api_response(services, err=[profiles, used_profiles, services])
Exemple #3
0
def add_virtual_machine(vm, **_):
    """
    Add the vm configuration to the system
    
    Variables: 
    vm       => Name of the vm
    
    Arguments:
    None
    
    Data Block:
    { 
     enabled: true,                  # Is VM enabled
     name: "Extract",                # Name of the VM
     num_workers: 1,                 # Number of service workers
     os_type: "windows",             # Type of OS
     os_variant: "win7",             # Variant of OS
     ram: 1024,                      # Amount of RAM
     revert_every: 600,              # Auto revert seconds interval
     vcpus: 1,                       # Number of CPUs
     virtual_disk_url: "img.qcow2"   # Name of the virtual disk to download
    }
    
    Result example:
    { "success" : True }
    """
    data = request.json

    if not STORAGE.get_virtualmachine(vm):
        STORAGE.save_virtualmachine(vm, data)

        return make_api_response({"success": True})
    else:
        return make_api_response({"success": False},
                                 "You cannot add a vm that already exists...",
                                 400)
Exemple #4
0
def get_system_configuration_overview(**_):
    """
    Display a system configuration overview.
    
    Variables:
    None
    
    Arguments:
    None
    
    Data Block:
    None
    
    Result example:
    {
     "errors": {          # Errors in the current config 
       "profiles": [],      # Profiles in error
       "services": [] },    # Services in error
     "services": {        # Services overview
       "SRV_NAME": {        # Single service overview 
         "enabled": True,     # is enabled?
         "profiles" : [],     # profiles referencing it
         "queue": 0,          # items in queue
         "workers": 1 },      # number of workers
       ...,} 
    }
    """
    errors = {"services": [], "profiles": []}
    services = {
        s["name"]: {
            "workers": 0,
            "enabled": s["enabled"],
            "profiles": [],
            "queue": 0
        }
        for s in STORAGE.list_services()
    }
    profiles = STORAGE.get_profiles_dict(
        list(
            set([
                p["_yz_rk"] for p in STORAGE.stream_search(
                    "profile", "_yz_rk:*", fl="_yz_rk")
            ])))
    used_profiles = {
        n['mac_address']: n['profile']
        for n in STORAGE.get_nodes(STORAGE.list_node_keys())
        if n['profile'] != ""
    }

    for mac, used_p in used_profiles.iteritems():
        if profiles.has_key(used_p):
            for srv, cfg in profiles[used_p]["services"].iteritems():
                if not services.has_key(srv):
                    errors["services"].append({
                        "service": srv,
                        "profile": used_p
                    })
                    continue
                services[srv]["workers"] += cfg["workers"]
                if used_p not in services[srv]["profiles"]:
                    services[srv]["profiles"].append(used_p)

            for srv, cfg in profiles[used_p]["virtual_machines"].iteritems():
                if not services.has_key(srv):
                    errors["services"].append({
                        "service": srv,
                        "profile": used_p
                    })
                    continue

                vm = STORAGE.get_virtualmachine(srv)
                if not vm:
                    errors["services"].append({
                        "service": srv,
                        "profile": used_p
                    })
                    continue

                services[srv][
                    "workers"] += vm['num_workers'] * cfg['num_instances']
                if used_p not in services[srv]["profiles"]:
                    services[srv]["profiles"].append(used_p)
        else:
            errors["profiles"].append({"profile": used_p, "mac": mac})

    for srv in services:
        services[srv]["queue"] = get_service_queue_length(srv)

    return make_api_response({"services": services, "errors": errors})