Example #1
0
def deploy_ucsc(sg, hosts, org):  
    """
    Deploy UCSC resources
    """
    try:
        handle = UCSCUtil.ucsc_login(sg)
    except KubamError as e:
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    for h in hosts:
        if "service_profile_template" in h:
            err = 0
            msg = ""
            err, msg = UCSCServer.make_profile_from_template(handle, org, h)
            if err != 0:
                UCSCUtil.ucsc_logout(handle)
                return jsonify({"error": msg}), Const.HTTP_BAD_REQUEST
            # associate the server if it is called out. 
            if "server" in h:
                err, msg = UCSCServer.associate_server(handle,org,h)
        else:
            # TODO: Create this part. 
            print "This part is not implemented yet"

    UCSCUtil.ucsc_logout(handle)
    return jsonify({"status": hosts}), Const.HTTP_CREATED
Example #2
0
 def list_ucsc(handle, wanted):
     """
     Get all the drives of the servers listed and print them out. 
     """
     try:
         all_servers = UCSCServer.list_servers(handle)
         ucs_servers = UCSCUtil.servers_to_objects(all_servers, wanted)
     except KubamError as e:
         UCSCUtil.ucsc_logout(handle)
         return {"error": str(e)}, Const.HTTP_BAD_REQUEST
     disks = {} 
     from ucscsdk.mometa.storage.StorageLocalDisk import StorageLocalDisk
     for i in ucs_servers:
         try:
             server_disks = UCSCServer.list_disks(handle, i)
             disks[i['dn']] = []
             for d in server_disks:
                 # d.__dict__ flattens the object to a dictionary. 
                 kv = d.__dict__
                 kv = dict((key, value) for key, value in kv.iteritems() if not key.startswith('_') )
                 disks[i['dn']].append( kv)
         except KubamError as e:
             UCSUtil.ucs_logout(handle)
             return {"error": str(e)}, Const.HTTP_BAD_REQUEST
     
     out = UCSCUtil.dn_hash_to_out(disks)
     UCSCUtil.ucsc_logout(handle)
     return out, Const.HTTP_OK
Example #3
0
 def test_sp_org(self):
     #
     err, msg = UCSCServer.check_org(
         "org-root/org-O-IAAS/org-O-IAAS-PHY/org-O-IAAS-PHY-S1/ls-kubam",
         "org-root")
     assert (err == 1)
     err, msg = UCSCServer.check_org(
         "org-root/ls-kubam",
         "org-root/org-O-IAAS/org-O-IAAS-PHY/org-O-IAAS-PHY-S1/ls-kubam")
     assert (err == 0)
Example #4
0
    def test_sp_crud(self):
        err, msg = UCSCServer.create_server(self.handle,
                                            "org-root/ls-TestTemplate",
                                            "test_kubam_sp", "org-root")
        print err, msg
        assert (err == 0)

        err, msg = UCSCServer.delete_server(self.handle, "test_kubam_sp",
                                            "org-root")
        #print err, msg
        assert (err == 0)
Example #5
0
def ucsc_servers_to_objects(handle, servers):
    """
    Get the servers from the API and turn them into the UCS objects
    So we can do operations on them. 
    """
    ucs_servers = UCSCServer.list_servers(handle)
    return UCSCUtil.servers_to_objects(ucs_servers, servers)
Example #6
0
 def delete_ucsc(handle, wanted):
     """
     sets the JBOD disks to unconfigured good.
     """
     try:
         all_servers = UCSCServer.list_servers(handle)
         ucs_servers = UCSCUtil.servers_to_objects(all_servers, wanted)
     except KubamError as e:
         UCSCUtil.ucsc_logout(handle)
         return {"error": str(e)}, Const.HTTP_BAD_REQUEST
     from ucscsdk.mometa.storage.StorageLocalDisk import StorageLocalDisk
     for i in ucs_servers:
         try:
             UCSCServer.reset_disks(handle, i)
         except KubamError as e: 
             UCSCUtil.ucsc_logout(handle)
             return {"error": str(e)}, Cont.HTTP_BAD_REQUEST
     return {"status" : "ok"}, 201
Example #7
0
def power_server_ucsc(sg, servers, action):
    """
    perform power operations (hardreset, off, on..) for UCS Central
    """
    try:
        handle = UCSCUtil.ucsc_login(sg)
    except Exception as e:
        return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED
    try:
        ucsc_servers = ucsc_servers_to_objects(handle, servers)
    except KubamError as e:
        UCSCUtil.ucsc_logout(handle)
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    for i in ucsc_servers:
        try:
            UCSCServer.power_server(handle, i, action)
        except KubamError as e:
            UCSUtil.ucs_logout(handle)
            return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    UCSUtil.ucs_logout(handle)
    powerstat = UCSCUtil.objects_to_servers(ucsc_servers, ["oper_power"])
    return jsonify({"status": powerstat}), Const.HTTP_CREATED
Example #8
0
def powerstat_ucsc(sg, wanted_servers):
    """
    Get the power status of the UCS Central servers.
    """
    try:
        handle = UCSCUtil.ucsc_login(sg)
    except KubamError as e:
        return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED
    try: 
        powerstat = UCSCServer.list_servers(handle)
        if not wanted_servers == "all":
            powerstat = UCSCUtil.servers_to_objects(powerstat, wanted_servers)
        powerstat = UCSCUtil.objects_to_servers(powerstat, ["oper_power"])
    except KubamError as e:
        UCSCUtil.ucsc_logout(handle)
    
    UCSCUtil.ucsc_logout(handle)     
    return powerstat
Example #9
0
def get_servers(server_group):
    """
    List all the servers in the server group
    or in this case the domain. 
    1. Make call to UCS to grab the servers. 
    2. Make call to database to see which ones are selected.
    3. Call servers_to_api which merges the two adding 'selected: true' to the servers that are selected.
    """
    db = YamlDB()
    try:
        sg = db.get_server_group(Const.KUBAM_CFG, server_group)
    except KubamError as e:
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    if sg['type'] == "ucsm":
        try:
            handle = UCSUtil.ucs_login(sg)
        except KubamError as e:
            return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED
        ucs_servers = UCSServer.list_servers(handle)
        UCSUtil.ucs_logout(handle)
    elif sg['type'] == 'ucsc':
        try:
            handle = UCSCUtil.ucsc_login(sg)
        except KubamError as e:
            return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED
        ucs_servers = UCSCServer.list_servers(handle)
        UCSCUtil.ucsc_logout(handle)

    # Gets a hash of severs of form:
    # {blades: ["1/1", "1/2",..], rack: ["6", "7"]}
    err, msg, db_servers = db.get_ucs_servers(Const.KUBAM_CFG, server_group)
    if err != 0:
        return jsonify({"error": msg}), Const.HTTP_BAD_REQUEST
    if db_servers is None:
        return jsonify({"servers": ucs_servers})

    ucs_servers = UCSUtil.servers_to_api(ucs_servers, db_servers)
    if err != 0:
        return jsonify({"error": msg}), Const.HTTP_BAD_REQUEST
    return jsonify({"servers": ucs_servers}), Const.HTTP_OK
Example #10
0
def get_server_status_ucsc(sg, wanted):
    try:
        handle = UCSCUtil.ucsc_login(sg)
    except KubamError as e:
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    try:
        all_servers = UCSCServer.list_servers(handle)
        if not wanted == "all":
            all_servers = UCSCUtil.servers_to_objects(all_servers, wanted)
        # put in dn name format
        status = {}
        for i in all_servers:
            status[i['dn']] = i
        out = UCSCUtil.dn_hash_to_out(status)
    except KubamError as e:
        UCSCUtil.ucsc_logout(handle)
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    UCSCUtil.ucsc_logout(handle)
    return jsonify({"servers": out}), Const.HTTP_OK
Example #11
0
def ucsc_fsm(sg, wanted):
    """
    Get the FSM of the servers in UCS Central
    """
    try:
        handle = UCSCUtil.ucsc_login(sg)
    except KubamError as e:
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    try:
        all_servers = UCSCServer.list_servers(handle)
        if not wanted == "all":
            all_servers = UCSCUtil.servers_to_objects(all_servers, wanted)
        # put in dn name format
        status = {}
        for i in all_servers:
            status[i['dn']] = UCSCMonitor.get_fsm(handle, i)
        out = UCSCUtil.dn_hash_to_out(status)
    except KubamError as e:
        UCSCUtil.ucsc_logout(handle)
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    UCSCUtil.ucsc_logout(handle)
    return jsonify({"servers": out}), Const.HTTP_OK
Example #12
0
def create_vmedia(server_group):
    """
    Create the Vmedia policy for a server group
    """
    db = YamlDB()
    # get server group. 
    try:
        sg = db.get_server_group(Const.KUBAM_CFG, server_group)
    except KubamError as e:
        return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

    org = "org-root"
    if "org" in sg:
        org = sg["org"]
    # go through all hosts associated with this server group
    err, msg, hosts = db.list_hosts(Const.KUBAM_CFG)
    if err == 1:
        return jsonify({'error': msg}), Const.HTTP_BAD_REQUEST
    hosts = [x for x in hosts if 'server_group' in x and x['server_group'] == server_group]
    if len(hosts) < 1:
        return jsonify({'error': 'no hosts associated with server group'}), Const.HTTP_OK
    # get the os image they use
    oses = list(set([ x["os"] for x in hosts]))
    # create those vmedia policies
    err = 0
    msg = ""
    err, msg, kubam_ip = db.get_kubam_ip(Const.KUBAM_CFG)
    if kubam_ip is None:
        return jsonify({'error': 'Please define the  KUBAM IP first.'} ), Const.HTTP_OK
    handle = ""
    if sg['type'] == 'ucsm':
        try:
            handle = UCSUtil.ucs_login(sg)
        except KubamError as e:
            return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

        err, msg = UCSServer.make_vmedias(handle, org, kubam_ip, oses)
        UCSUtil.ucs_logout(handle)

    elif sg['type'] == 'ucsc':
        try:
            handle = UCSCUtil.ucsc_login(sg)
        except KubamError as e:
            return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

        err, msg = UCSCServer.make_vmedias(handle, org, kubam_ip, oses)
        UCSCUtil.ucsc_logout(handle)
    elif sg['type'] == 'imc':
         
        try:
            handle = IMCUtil.imc_login(sg)
        except KubamError as e:
            return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST

        try:
            IMCServer.mount_media(handle, kubam_ip, hosts[0]['name'], oses[0])
        except KubamError as e:
            IMCUtil.imc_logout(handle)
            return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST
        IMCUtil.imc_logout(handle)
            
    if err != 0:
        return jsonify({'error': msg}), Const.HTTP_BAD_REQUEST
   
    return jsonify({"status": oses}), Const.HTTP_CREATED
Example #13
0
 def test_list_servers(self):
     servers = UCSCServer.list_servers(self.handle)
     print servers
Example #14
0
 def test_templatelist(self):
     templates = UCSCServer.list_templates(self.handle)
     #print templates
     assert (templates != "")