Esempio n. 1
0
    def check_type( self, key, value, value_type):
        if value_type == "node_list":
            if not isinstance( value.split(','), list ):
                return [ False, "Value is a %s, format %s" % (value_type, self.type_example(value_type)) ]
            # check if node exists and can be ssh
            error_node = []
            for node in value.split(','):
                if not common.try_ssh(node):
                    error_node.append( node )
            if len(error_node):
                return [ False, "Nodes:%s are not set in /etc/hosts or can't be auto ssh" % error_node ]
            else:
                return [ True, "" ]

        if value_type == "int_list":
            if not isinstance( value.split(','), list ):
                return [ False, "Value is a %s, format %s" % (value_type, "10,10,10,10") ]
            else:
                try:
                    for subdata in value.split(','):
                        int( subdata )
                    return [ True, "" ]
                except:
                    return [ False, "Value is a %s, format %s" % (value_type, "10,10,10,10") ]

        if value_type == "bool":
            if value in ["true", "false"]:
                return [ True, "" ]
            else:
                return [ False, "Value is a %s, format %s" % (value_type, self.type_example(value_type)) ]

        if "static" in value_type:
            static_value = value_type.split(":")[1]
            if value == static_value:
                return [ True, "" ]
            else:
                return [ False, "Value is a static, only can set to be %s" % static_value ]

        if value_type == "int":
            try:
                int(value)
                return [ True, "" ]
            except:
                return [ False, "Value is a %s, format %s" % (value_type, self.type_example(value_type)) ]

        if value_type == "osd_journal_list":
            try:
                error_disk = []
                disk_list = value.split(',')
                print disk_list
                for substr in disk_list:
                    res = common.get_list(substr)[0]
                    if len(res) > 4:
                        raise
                    device_index = 1
                    for device in res:
                        if not common.try_disk(key, device):
                            error_disk.append(device)
                        if device_index == len(res):
                            print "device_%s: %s " % (device_index, device)
                        else:
                            print "device_%s: %s " % (device_index, device),
                        device_index += 1
                if not len(error_disk):
                    return [ True, "" ]
                else:
                    return [ False, "Disks:%s are not exists or it is boot device." % error_disk ]
            except:
                return [ False, "Value is a %s, format %s" % (value_type, self.type_example(value_type)) ]
     
	if value_type == "diskformat":
            try:
                if ',' not in value:
		    return [ True, "" ]
		else:
                    return [ False, "Please use [:] split."]
            except:
                return [ False, "Value is a %s, format %s" % (value_type, self.type_example(value_type)) ]

        if value_type == "network":
            try:
                ip, netmask = value.split('/')
                if int(netmask) < 32 and len(ip.split('.')) == 4:
                    return [ True , "" ]
                else:
                    return [ False, "Value is a %s, format %s" % (value_type, "192.168.5.0/24") ]

            except:
                return [ False, "Value is a %s, format %s" % (value_type, "192.168.5.0/24") ]

        if value_type == "ip":
            try:
                count = 0
                for subip in value.split('.'):
                    count += 1
                    if int(subip) >= 255:
                        return [ False, "Value is a %s, format %s" % (value_type, "192.168.5.1") ]
                if count == 4:
                    return [ True , "" ]
                else:
                    return [ False, "Value is a %s, format %s" % (value_type, "192.168.5.1") ]
            except:
                return [ False, "Value is a %s, format %s" % (value_type, "192.168.5.1") ]

        return [ True, "" ]