Пример #1
0
    def power_server(handle, server, action):
        """
        Takes in a service profile and applies the appropriate power
        action to the service profile. The service profile should be
        the full organization of the server. e.g:
        "org-root/ls-miner04"
        """
        st = ""
        if action == "off":
            st = "admin-down"
        elif action == "on":
            st = "admin-up"
        elif action == "hardreset":
            st = "cycle-immediate"
        elif action == "softreset":
            st = "cycle-wait"
        else:
            raise KubamError("Power method {0} is not a valid power action.".format(action))


        if action in ["on", "off"] and server["service_profile"] == "":
            raise KubamError("Can not power {0}, no service profile associated with {1}".format(action, server["dn"]))

        from ucsmsdk.mometa.ls.LsPower import LsPower
        mo = LsPower(parent_mo_or_dn=server["service_profile"],
                     state=st)
        handle.add_mo(mo, True)
        try:
            handle.commit()
        except UcsException as err:
            raise KubamError("{0}".format(err))
Пример #2
0
 def servers_to_objects(objects, servers):
     """
     take in all of the UCS objects and filter out the ones the API 
     passed into us and return a list with just the servers we wanted. 
     """
     r_s = []
     if "blades" in servers:
         for s in servers["blades"]:
             found = False
             b_parts = s.split("/")
             for real in objects:
                 if not "chassis_id" in real:
                     continue
                 if (real['domain_id'] == b_parts[0]
                         and real['chassis_id'] == b_parts[1]
                         and real['slot'] == b_parts[2]):
                     found = True
                     r_s.append(real)
             if not found:
                 raise KubamError("server {0} does not exist:".format(s))
     if "rack_servers" in servers:
         for s in servers["rack_servers"]:
             found = False
             r_parts = s.split("/")
             for real in objects:
                 if not "rack_id" in real:
                     continue
                 if (real['domain_id'] == r_parts[0]
                         and real['rack_id'] == r_parts[1]):
                     found = True
                     r_s.append(real)
             if not found:
                 raise KubamError("server {0} does not exist!".format(s))
     return r_s
Пример #3
0
 def get_server_group(self, file_name, group_name):
     err, msg, groups = self.list_server_group(file_name)
     if err == 1:
         raise KubamError(msg)
     for g in groups: 
         if g['name'] == group_name:
             return g
     raise KubamError("Server group: {0} not found.".format(group_name))
Пример #4
0
 def delete_template(self, file_name, req, sg, templates):
     config, sp_temp = self.check_template(file_name, req, template)
     for g in config['server_groups']:
         if g['name'] == sg:
             del g['sp_template']
             err, msg = self.write_config(config, file_name)
             if err:
                 raise KubamError(msg)
             return "Template {0} deleted within the {1} server group".format(sp_temp, sg)
     raise KubamError("Server group ID {0} not found.".format(sg))
Пример #5
0
 def check_template(self, file_name, req, templates):
     if not isinstance(req, dict) or "sp_template" not in req:
         raise KubamError("No service profile name was passed into the request.")
     sp_temp = req['sp_template']
     if not any(t['name'] == sp_temp for t in templates):
         raise KubamError("Selected UCS template does not exist.")
     err, msg, config = self.open_config(file_name)
     if err:
         raise KubamError(msg)
     return config, sp_temp
Пример #6
0
 def check_ucsc_login(request):
     if not isinstance(request, dict):
         raise KubamError("improper request sent")
     if 'credentials' not in request:
         raise KubamError("no credentials found in request")
     for v in ['user', 'password', 'ip']:
         if v not in request['credentials']:
             raise KubamError("credentials should include {0}".format(v))
     user = request['credentials']['user']
     pw = request['credentials']['password']
     ip = request['credentials']['ip']
     if ip == "":
         raise KubamError("Please enter a valid UCSM IP address.")
     ucsc_session = UCSCSession()
     h, err = ucsc_session.login(user, pw, ip)
     if not h:
         raise KubamError(err)
     UCSCSession.logout(h)
Пример #7
0
    def ucsc_login(server_group):
        """
        login to a UCS and return a login handle
        """
        if not isinstance(server_group, dict):
            raise KubamError("Login format is not correct")
        if "credentials" in server_group:
            credentials = server_group["credentials"]
            if "user" in credentials and "password" in credentials and "ip" in credentials:
                ucs_session = UCSCSession()
                db = YamlDB()
                err, msg, password = db.decrypt_password(
                    credentials['password'])
                if err == 1:
                    raise KubamError(msg)

                h, msg = ucs_session.login(credentials['user'], password,
                                           credentials['ip'])
                if msg:
                    raise KubamError(msg)
                if h:
                    return h
                else:
                    raise KubamError("Not logged in into UCS")
            else:
                raise KubamError(
                    "The file kubam.yaml does not include the user, password, and IP properties to login."
                )
        else:
            raise KubamError(
                "UCS Credentials have not been entered.  Please login to UCS to continue."
            )
Пример #8
0
    def list_templates(handle):

        filter_str = "(type, 'initial-template', type='eq') or (type, 'updating-template', type='eq')"
        try:
            query = handle.query_classid("lsServer", filter_str=filter_str)
            templates = list()

            for q in query:
                templates.append({"name": q.name})
            return templates

        except UcscException as e:
            raise KubamError(e)
Пример #9
0
    def power_server(handle, server, action):
        """
        Takes in a server object and applies the appropriate power
        action to the server
        """
        st = ""
        if action == "off":
            st = "admin-down"
        elif action == "on":
            st = "admin-up"
        elif action == "hardreset":
            st = "cycle-immediate"
        elif action == "softreset":
            st = "cycle-wait"
        else:
            raise KubamError(
                "Power method {0} is not a valid power action.".format(action))

        if action in ["on", "off"] and server["service_profile"] == "":
            raise KubamError(
                "Can not power {0}, no service profile associated with {1}".
                format(action, server["dn"]))
        from ucscsdk.mometa.ls.LsServerOperation import LsServerOperation
        # UCS central requires a few different ways of doing remote calls to the server.
        # an example looks as follows:
        #  mo = LsServerOperation(parent_mo_or_dn="org-root/org-SLCLAB3/req-SLC-KVM-02/inst-1009",
        #                state="admin-up")
        # notice the 'ls-' is changed to 'req-' and the domain ID is appended to the end.
        # also instaed of handle.add_mo set_mo seems to be the way to make this work.
        mo_name = "{0}/inst-{1}".format(server["service_profile"],
                                        server['domain_id'])
        mo_name = mo_name.replace("/ls-", "/req-")
        mo = LsServerOperation(parent_mo_or_dn=mo_name, state=st)
        handle.add_mo(mo, True)
        try:
            handle.commit()
        except UcscException as err:
            raise KubamError("{0}\n{1}".format(mo, err))
Пример #10
0
    def servers_to_objects(objects, servers):
        """
        takes in a bunch of objects: real UCS objects
        and servers: stuff we get from the API like {"blades": [1/1, 2/1], "rack_servers": ["1", "2",...]}
        and filters them
        """
        # if we don't get an array or hash, then we just return all
        # of them.
        if servers == "all":
            return objects

        r_s = []
        if "blades" in servers:
            for s in servers["blades"]:
                found = False
                b_parts = s.split("/")
                for real in objects:
                    if not "chassis_id" in real:
                        continue
                    if (real['chassis_id'] == b_parts[0]
                            and real['slot'] == b_parts[1]):
                        found = True
                        r_s.append(real)
                if not found:
                    raise KubamError("server {0} does not exist.".format(s))
        if "rack_servers" in servers:
            for s in servers["rack_servers"]:
                found = False
                for real in objects:
                    if not "rack_id" in real:
                        continue
                    if real['rack_id'] == s:
                        found = True
                        r_s.append(real)
                if not found:
                    raise KubamError("server {0} does not exist.".format(s))
        return r_s
Пример #11
0
    def list_templates(handle):
        # from ucsmsdk.mometa.ls.LsServer import LsServer

        # Get all the updating and initial templates
        filter_str = "(type, 'initial-template', type='eq') or (type, 'updating-template', type='eq')"
        try:
            query = handle.query_classid("lsServer", filter_str=filter_str)
            templates = list()

            for q in query:
                templates.append({"name": q.dn})
            return templates

        except UcsException as e:
            raise KubamError(e)
Пример #12
0
 def mount_media(handle, kubam_server, host_name, os):
     """
     Mount vMedia on IMC server. 
     """
     media = os + "-boot.iso"
     if os in ["esxi6.0", "esxi6.5"]:
         media = host_name + ".iso"
     try:
         vmedia.vmedia_mount_create(
             handle,
             volume_name="c",
             map="www",
             mount_options="",
             remote_share="http://{0}/kubam".format(kubam_server),
             remote_file=media,
             username="",
             password="")
     #https://github.com/CiscoUcs/imcsdk/blob/master/imcsdk/imcexception.py
     except ImcOperationError as e:
         raise KubamError(e.message)
Пример #13
0
 def logout(handle):
     try:
         handle.logout()
     except UcsException as e:
         raise KubamError(str(e))