Esempio n. 1
0
    def changesystem(self, jsonstr):
        """
        Apply system IPMI configuration

        :param jsonstr - JSON formatted string
        :returns: status, PxJSON
        :raises: None
        Any parameter set to None is not applied to the system.
        """

        status = 1
        obj = PxJSON("Unable to set IPMI configuration")

        status, jsondict = self.validateipmijson(self.loadjson(jsonstr, obj))

        obj.setroute(PxJSON.IPMI_INFO, jsondict[PxJSON.IPMI_INFO])

        if status == 0:
            status, obj = self.changesystem2(obj)

        return status, obj.getjson()
Esempio n. 2
0
    def testip(self, ip):
        """
        test an IP if it exists

        :param ip - ip address
        :returns: status
        :raises: None
        """
        obj = PxJSON("IP address already exists")
        status, obj, _ = self.runcmd(['/usr/bin/ping', '-qc', '3', ip], obj)
        if status == 0:
            return True, obj
        return False, obj
Esempio n. 3
0
    def setconfiguration(self, jsonstr, applyconfiguration):
        """
        Set IPMI configuration in database
        :param jsonstr - JSON formatted string
        :param applyconfiguration - apply configuration to system as well?
        :returns: status, PxJSON
        :raises: None
        """

        status = 1
        obj = PxJSON("Unable to set IPMI configuration")

        status, jsondict = self.validateipmijson(self.loadjson(jsonstr, obj))

        obj.setroute(PxJSON.IPMI_INFO, jsondict[PxJSON.IPMI_INFO])

        if status == 0:
            status, obj = self.setconfiguration2(obj)

        if status == 0 and applyconfiguration:
            status, obj = self.changesystem2(obj)

        return status, obj.getjson()
Esempio n. 4
0
    def applyconfiguration(self):
        """ call getconfiguration() and changesystem2() """

        obj = PxJSON("Unable to apply IPMI configuration")
        tmpobj = PxJSON("Unable to apply IPMI configuration")

        tmpstatus, tmpjson = self.getconfiguration()
        jsondict = json.loads(tmpjson)
        tmpobj.setjson(jsondict)

        if tmpstatus != 0 or not tmpobj.issuccess():
            obj.internal(tmpobj.getinternal())
            return tmpstatus, obj

        configuration = {}
        for key in jsondict[PxJSON.IPMI_INFO]:
            configuration[key] = {PxJSON.VALUE: jsondict[PxJSON.IPMI_INFO][key]}
        obj.setroute(PxJSON.IPMI_INFO, configuration)

        status, obj = self.changesystem2(obj)

        return status, obj.getjson()
Esempio n. 5
0
    def setconfiguration2(self, obj):
        """
        Set IPMI configuration in database

        :param obj - PxJSON object
        :returns: status, PxJSON
        :raises: None
        Any attribute set to None, is set to null in the database.
        """

        status = 1
        tmpobj = PxJSON("Unable to set IPMI configuration")

        tmpstatus, tmpjson = self.getconfiguration()
        tmpobj.setjson(json.loads(tmpjson))

        if tmpstatus != 0 or not tmpobj.issuccess():
            obj.internal(tmpobj.getinternal())
            return status, obj

        old_connectiontype = tmpobj.getroute(PxJSON.IPMI_INFO)[PxJSON.CONNECTIONTYPE]
        old_ipaddress = tmpobj.getroute(PxJSON.IPMI_INFO)[PxJSON.IPV4]
        old_netmask = tmpobj.getroute(PxJSON.IPMI_INFO)[PxJSON.NETMASK]
        old_gateway = tmpobj.getroute(PxJSON.IPMI_INFO)[PxJSON.GATEWAY]
        old_vlan = tmpobj.getroute(PxJSON.IPMI_INFO)[PxJSON.VLAN]

        new_connectiontype = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.CONNECTIONTYPE][PxJSON.VALUE]
        new_ipaddress = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.IPV4][PxJSON.VALUE]
        new_netmask = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.NETMASK][PxJSON.VALUE]
        new_gateway = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.GATEWAY][PxJSON.VALUE]
        new_vlan = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.VLAN][PxJSON.VALUE]

        if old_connectiontype != new_connectiontype:
            self.logger.info("Changing {0}: {1} to {2}".format(PxJSON.CONNECTIONTYPE, old_connectiontype, new_connectiontype))

        if old_ipaddress != new_ipaddress:
            stat, tmpobj = self.testip(new_ipaddress)
            if stat:
                self.logger.error("{0} already exists".format(new_ipaddress))
                return 1, tmpobj
            self.logger.info("Changing {0}: {1} to {2}".format(PxJSON.IPV4, old_ipaddress, new_ipaddress))


        if old_netmask != new_netmask:
            self.logger.info("Changing {0}: {1} to {2}".format(PxJSON.NETMASK, old_netmask, new_netmask))

        if old_gateway != new_gateway:
            self.logger.info("Changing {0}: {1} to {2}".format(PxJSON.GATEWAY, old_gateway, new_gateway))

        if old_vlan != new_vlan:
            self.logger.info("Changing {0}: {1} to {2}".format(PxJSON.VLAN, old_vlan, new_vlan))

        try:
            table_name = 'systemsetups'
            DataModel().ExecuteRawQueryStatement2("UPDATE {0} SET ipmi_connection_type=?, ipmi_address=?, ipmi_netmask=?, ipmi_gateway=?, ipmi_vlan=?".format(table_name), (new_connectiontype, new_ipaddress, new_netmask, new_gateway, new_vlan))

            obj.setsuccess()
            status = 0
        except sqlalchemy.exc.OperationalError as ex:
            self.logger.error(traceback.format_exc())
            obj.internal({"exception": str(ex)})
        except Exception as ex:
            self.logger.error(traceback.format_exc())
            obj.internal({"exception": str(ex)})

        self.logger.info("status={0} json={1}".format(status, obj.getjsonpretty()))

        return status, obj
Esempio n. 6
0
    def getconfiguration(self):
        """
        Fetch IPMI configuration from database

        :param: none
        :returns: status, reply
        :raises: None
        """

        status = 1
        obj = PxJSON("Unable to obtain IPMI configuration")

        try:
            table_name = 'systemsetups'
            res = DataModel().ExecuteRawQueryStatement("SELECT ipmi_connection_type, ipmi_address, ipmi_netmask, ipmi_gateway, ipmi_vlan from {0}".format(table_name))
            reply = {}
            for row in res:
                self.logger.info(row)
                reply[PxJSON.CONNECTIONTYPE] = row['ipmi_connection_type']
                reply[PxJSON.IPV4] = row['ipmi_address']
                reply[PxJSON.NETMASK] = row['ipmi_netmask']
                reply[PxJSON.GATEWAY] = row['ipmi_gateway']
                reply[PxJSON.VLAN] = row['ipmi_vlan'] if row['ipmi_vlan'] != 0 else 'undefined'

            obj.setroute(PxJSON.IPMI_INFO, reply)
            obj.setsuccess()
            status = 0
        except sqlalchemy.exc.OperationalError as ex:
            self.logger.error(traceback.format_exc())
            obj.internal({"exception": str(ex)})
        except Exception as ex:
            self.logger.error(traceback.format_exc())
            obj.internal({"exception": str(ex)})

        self.logger.info("status={0} json={1}".format(status, obj.getjsonpretty()))

        return status, obj.getjson()
Esempio n. 7
0
    def changesystem2(self, obj):
        """
        Apply system IPMI configuration

        :param obj: PxJSON object
        :returns: status, PxJSON
        :raises: None
        Any parameter set to None is not applied to the system.
        """

        status = 1
        obj.setfailure("Unable to set IPMI configuration")

        new_connectiontype = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.CONNECTIONTYPE][PxJSON.VALUE]
        new_ipaddress = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.IPV4][PxJSON.VALUE]
        new_netmask = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.NETMASK][PxJSON.VALUE]
        new_gateway = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.GATEWAY][PxJSON.VALUE]
        new_vlan = obj.getroute(PxJSON.IPMI_INFO)[PxJSON.VLAN][PxJSON.VALUE]

        condition = True

        while condition:
            if not self.ipmisupported:
                # VM guest?
                self.logger.info("IPMI is not supported.  Not applying changes.")
                obj.setsuccess()
                status = 0
                break

            systemstatus, systemjson = self.querysystem()
            systemobj = PxJSON("")
            systemobj.setjson(json.loads(systemjson))
            if systemstatus != 0 or not systemobj.issuccess():
                obj.internal(systemobj.getinternal())
                break

            self.logger.info("systemreply={0}".format(systemobj.getjson()))

            if self.checkattribute(systemobj.getjsondict(), PxJSON.CONNECTIONTYPE, new_connectiontype):
                self.logger.info("Setting={}".format(new_connectiontype))
                args = [self.ipmitool, 'lan', 'set', '1', 'ipsrc', new_connectiontype]
                status, obj, _ = self.runcmd(args, obj)
                if status != 0:
                    break

            if new_ipaddress is not None and PxJSON.CONNECTIONTYPE != PxJSON.CONNECTIONDHCP:
                if self.checkattribute(systemobj.getjsondict(), PxJSON.IPV4, new_ipaddress):
                    args = [self.ipmitool, 'lan', 'set', '1', 'ipaddr', new_ipaddress]
                    status, obj, _ = self.runcmd(args, obj)
                    if status != 0:
                        break

            if new_netmask is not None and PxJSON.CONNECTIONTYPE != PxJSON.CONNECTIONDHCP:
                if self.checkattribute(systemobj.getjsondict(), PxJSON.NETMASK, new_netmask):
                    args = [self.ipmitool, 'lan', 'set', '1', 'netmask', new_netmask]
                    status, obj, _ = self.runcmd(args, obj)
                    if status != 0:
                        break

            if new_gateway is not None and PxJSON.CONNECTIONTYPE != PxJSON.CONNECTIONDHCP:
                if self.checkattribute(systemobj.getjsondict(), PxJSON.GATEWAY, new_gateway):
                    args = [self.ipmitool, 'lan', 'set', '1', 'defgw', 'ipaddr', new_gateway]
                    status, obj, _ = self.runcmd(args, obj)
                    if status != 0:
                        break

            if new_vlan is not None:
                if self.checkattribute(systemobj.getjsondict(), PxJSON.VLAN, new_vlan):
                    self.logger.info("Setting={}".format(new_vlan))
                    if new_vlan == "0":
                        args = [self.ipmitool, 'lan', 'set', '1', 'vlan', 'id', 'off']
                    else:
                        args = [self.ipmitool, 'lan', 'set', '1', 'vlan', 'id', new_vlan]
                    status, obj, _ = self.runcmd(args, obj)
                    if status != 0:
                        break

            obj.setsuccess()
            status = 0

            condition = False


        self.logger.info("status={0} json={1}".format(status, obj.getjsonpretty()))
        return status, obj
Esempio n. 8
0
    def querysystem(self):
        """
        Fetch current IPMI status from system

        :param : None
        :returns: status, reply
        :raises: None
        """

        status = 1
        obj = PxJSON("Unable to obtain IPMI information")

        condition = True

        while condition:
            condition = False
            if not self.ipmisupported:
                # VM guest?
                self.logger.info("IPMI is not supported.  Unable to fetch values from system.")
                reply = {}
                reply[PxJSON.CONNECTIONTYPE] = None
                reply[PxJSON.IPV4] = None
                reply[PxJSON.NETMASK] = None
                reply[PxJSON.GATEWAY] = None
                reply[PxJSON.MAC] = None
                reply[PxJSON.VLAN] = None
                obj.setroute(PxJSON.IPMI_INFO, reply)
                obj.setsuccess()
                status = 0
                break

            args = [self.ipmitool, 'lan', 'print']
            status, obj, output = self.runcmd(args, obj)

            if status == 0:
                status, reply, internalreply = self.parseoutput(output)
                if status == 0:
                    status = 1
                    if reply is None:
                        self.logger.error("reply={0}".format(reply))
                    elif len(reply) != 6: # this is a count of how many json elements we expect
                        self.logger.error("len(reply)={0}, {1}".format(len(reply), reply))
                    else:
                        obj.setroute(PxJSON.IPMI_INFO, reply)
                        obj.setsuccess()
                        status = 0
                elif internalreply:
                    obj.internal(internalreply)

        self.logger.info("status={0} json={1}".format(status, obj.getjsonpretty()))

        return status, obj.getjson()