Exemplo n.º 1
0
def remove_dns_server(ns, setting, address):
    '''
    Remove dns server from given setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    '''
    # Check the IP address
    address, version = util.address_check(address)

    protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value(
        "IPv%d" % version)
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.classname == "LMI_DNSSettingData"
                and settingData.ProtocolIFType == protocolIFType):
            for i in range(len(settingData.DNSServerAddresses)):
                if util.compare_address(settingData.DNSServerAddresses[i],
                                        address):
                    del settingData.DNSServerAddresses[i]
                    settingData.push()
                    return 0
            else:
                raise LmiInvalidOptions(
                    "No DNS with address %s found for setting %s" %
                    (address, setting.Caption))
    else:
        raise LmiInvalidOptions(
            "Can't remove DNS address to setting %s, invalid setting type" %
            setting.Caption)
    LOG().info("DNS server %s removed from setting %s", address,
               setting.Caption)
    return 0
Exemplo n.º 2
0
 def verify_options(self, opts):
     uid = opts['uid']
     gid = opts['gid']
     if uid is not None and not uid.isdigit():
         raise LmiInvalidOptions("User ID must be a number")
     if gid is not None and not gid.isdigit():
         raise LmiInvalidOptions("Group ID must be a number")
Exemplo n.º 3
0
def _gateway_check(gateway, version):
    if gateway is None:
        return None
    try:
        gw, gateway_version = util.address_check(gateway)
    except util.IPCheckFailed:
        raise LmiInvalidOptions("Invalid gateway: %s" % gateway)
    if gateway_version != version:
        raise LmiInvalidOptions("Invalid gateway, should be IPv%d: %s" %
                                (version, gateway))
    return gw
Exemplo n.º 4
0
 def verify_options(self, opts):
     _password = opts['--password']
     _plainpassword = opts['--plainpassword']
     _uid = opts['--uid']
     _gid = opts['--gid']
     if _password is not None and _plainpassword is not None:
         raise LmiInvalidOptions("Must set only one of password options")
     if _uid is not None and not _uid.isdigit():
         raise LmiInvalidOptions("User ID must be a number")
     if _gid is not None and not _gid.isdigit():
         raise LmiInvalidOptions("Group ID must be a number")
Exemplo n.º 5
0
def replace_dns_server(ns, setting, address):
    '''
    Remove all dns servers and add given dns server to the given setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    '''
    # Check the IP address
    address, version = util.address_check(address)

    protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value(
        "IPv%d" % version)
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.classname == "LMI_DNSSettingData"
                and settingData.ProtocolIFType == protocolIFType):
            settingData.DNSServerAddresses = [address]
            settingData.push()
            return 0
    else:
        raise LmiInvalidOptions(
            "Can't remove DNS address to setting %s, invalid setting type" %
            setting.Caption)
    LOG().info("Existing DNS servers replaced with %s in setting %s", address,
               setting.Caption)
    return 0
Exemplo n.º 6
0
def add_dns_server(ns, setting, address):
    '''
    Add a dns server to the given setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    '''
    # Check the IP address
    address, version = util.address_check(address)

    protocolIFType = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.value(
        "IPv%d" % version)
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.classname == "LMI_DNSSettingData"
                and settingData.ProtocolIFType == protocolIFType):
            settingData.DNSServerAddresses.append(address)
            settingData.push()
            break
    else:
        raise LmiInvalidOptions(
            "Can't assign DNS address to setting %s, invalid setting type" %
            setting.Caption)
    LOG().info("DNS server %s/%d added to setting %s", address,
               setting.Caption)
    return 0
Exemplo n.º 7
0
def remove_ip_address(ns, setting, address):
    '''
    Remove the IP address from given static setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    '''
    address, version = util.address_check(address)

    protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict(
    )["IPv%s" % version]
    found = False
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.ProtocolIFType is not None
                and int(settingData.ProtocolIFType) == protocol
                and hasattr(settingData, "IPAddresses")):

            i = 0
            while i < len(settingData.IPAddresses):
                if util.compare_address(settingData.IPAddresses[i], address):
                    del settingData.IPAddresses[i]
                    if version == 4:
                        del settingData.SubnetMasks[i]
                    else:
                        del settingData.IPv6SubnetPrefixLengths[i]
                    del settingData.GatewayAddresses[i]
                    found = True
                i += 1
            settingData.push()
    if not found:
        raise LmiInvalidOptions(
            "Can't remove IP address from setting: invalid setting type or address doesn't exist."
        )
    return 0
Exemplo n.º 8
0
def switch_power_state(ns, state):
    '''
    Switch system power state.

    :param state: Requested power state.
    :type state: POWER_STATE_* constant
    '''
    # Check if the power state is available first
    capabilities = ns.LMI_PowerManagementCapabilities.first_instance()
    if not state in capabilities.PowerStatesSupported:
        raise LmiInvalidOptions("Power state is not supported.")
    service = ns.LMI_PowerManagementService.first_instance()
    service.RequestPowerStateChange(PowerState=state)
    return 0
Exemplo n.º 9
0
def remove_ip_address(ns, setting, address):
    '''
    Remove the IP address from given static setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    '''
    address, version = util.address_check(address)

    protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict(
    )["IPv%s" % version]
    found = False
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.ProtocolIFType is not None
                and int(settingData.ProtocolIFType) == protocol
                and hasattr(settingData, "IPAddresses")):

            i = 0
            # lmishell doesn't handle in-place editing of array parameters properly,
            # we need to create new arrays and then copy them back
            addresses = []
            masks = []
            gateways = []
            while i < len(settingData.IPAddresses):
                if not util.compare_address(settingData.IPAddresses[i],
                                            address):
                    addresses.append(settingData.IPAddresses[i])
                    if version == 4:
                        masks.append(settingData.SubnetMasks[i])
                    else:
                        masks.append(settingData.IPv6SubnetPrefixLengths[i])
                    gateways.append(settingData.GatewayAddresses[i])
                else:
                    found = True
                i += 1
            settingData.IPAddresses = addresses
            if version == 4:
                settingData.SubnetMasks = masks
            else:
                settingData.IPv6SubnetPrefixLengths = masks
            settingData.GatewayAddresses = gateways
            settingData.push()
    if not found:
        raise LmiInvalidOptions(
            "Can't remove IP address from setting: invalid setting type or address doesn't exist."
        )
    LOG().info("IP address %s removed from setting %s", address,
               setting.Caption)
    return 0
Exemplo n.º 10
0
def add_ip_address(ns, setting, address, prefix, gateway=None):
    '''
    Add an IP address to the given static setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    :param int prefix: network prefix.
    :param gateway: default gateway or None
    :type gateway: str or None
    '''
    address, version = util.address_check(address)
    prefix = util.prefix_check(prefix, version)
    gateway = _gateway_check(gateway, version)

    protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict(
    )["IPv%s" % version]
    found = False
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.ProtocolIFType is not None
                and int(settingData.ProtocolIFType) == protocol
                and hasattr(settingData, "IPAddresses")):

            # lmishell doesn't handle in-place editing of array parameters properly,
            # we need to create new arrays and then copy them back
            settingData.IPAddresses = settingData.IPAddresses + [address]
            if version == 4:
                settingData.SubnetMasks = settingData.SubnetMasks + [
                    util.netmask_from_prefix(prefix)
                ]
            else:
                settingData.IPv6SubnetPrefixLengths = settingData.IPv6SubnetPrefixLengths + [
                    str(prefix)
                ]
            if gateway:
                settingData.GatewayAddresses = settingData.GatewayAddresses + [
                    gateway
                ]
            else:
                settingData.GatewayAddresses = settingData.GatewayAddresses + [
                    ""
                ]
            found = True
            settingData.push()
    if not found:
        raise LmiInvalidOptions(
            "Can't add IP address to setting: invalid setting type.")
    LOG().info("IP address %s/%d added to setting %s", address, prefix,
               setting.Caption)
    return 0
Exemplo n.º 11
0
def add_static_route(ns, setting, address, prefix, metric=None, next_hop=None):
    '''
    Add a static route to the given setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    :param int prefix: network prefix.
    :param metric: metric for the route or None
    :type gateway: int or None
    :param next_hop: IPv4 or IPv6 address for the next hop of the route or None
    :type next_hop: str or None
    '''
    # Check the IP address
    address, version = util.address_check(address)
    prefix = util.prefix_check(prefix, version)

    if version == 4:
        result = setting.LMI_AddStaticIPRoute(
            AddressType=setting.LMI_AddStaticIPRoute.AddressTypeValues.IPv4,
            DestinationAddress=address,
            DestinationMask=util.netmask_from_prefix(prefix))
    elif version == 6:
        result = setting.LMI_AddStaticIPRoute(
            AddressType=setting.LMI_AddStaticIPRoute.AddressTypeValues.IPv6,
            DestinationAddress=address,
            PrefixLength=prefix)
    else:
        raise LmiInvalidOptions("Invalid IP address: %s" % address)
    if result.rval != 0:
        raise LmiFailed("Unable to add static route: %s" %
                        (result.errorstr or "unknown error"))
    route = result.rparams["Route"].to_instance()
    if metric is not None or next_hop is not None:
        if metric is not None:
            route.RouteMetric = metric
        if next_hop is not None:
            route.NextHop = next_hop
        route.push()
    LOG().info("Static route to %s/%d added to setting %s", address, prefix,
               setting.Caption)
    return 0
Exemplo n.º 12
0
def replace_ip_address(ns, setting, address, prefix, gateway=None):
    '''
    Remove all IP addresses from given static setting and add new IP address.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    :param int prefix: network prefix.
    :param gateway: default gateway or None
    :type gateway: str or None
    '''
    address, version = util.address_check(address)
    prefix = util.prefix_check(prefix, version)
    gateway = _gateway_check(gateway, version)

    protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict(
    )["IPv%s" % version]
    found = False
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.ProtocolIFType is not None
                and int(settingData.ProtocolIFType) == protocol
                and hasattr(settingData, "IPAddresses")):

            settingData.IPAddresses = [address]
            if version == 4:
                settingData.SubnetMasks = [util.netmask_from_prefix(prefix)]
            else:
                settingData.IPv6SubnetPrefixLengths = [prefix]
            if gateway:
                settingData.GatewayAddresses = [gateway]
            else:
                settingData.GatewayAddresses = [""]
            found = True
            settingData.push()
    if not found:
        raise LmiInvalidOptions(
            "Can't add IP address to setting: invalid setting type.")
    LOG().info(
        "Existing addresses replaced with IP address %s/%d in setting %s",
        address, prefix, setting.Caption)
    return 0
Exemplo n.º 13
0
def remove_static_route(ns, setting, address):
    '''
    Remove static route to the given setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    '''
    # Check the IP address
    address, version = util.address_check(address)

    found = False
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent",
            ResultClass="LMI_IPRouteSettingData"):
        if util.compare_address(settingData.DestinationAddress, address):
            found = True
            settingData.delete()
    if not found:
        raise LmiInvalidOptions("No such route: %s" % address)

    return 0
Exemplo n.º 14
0
def add_ip_address(ns, setting, address, prefix, gateway=None):
    '''
    Add an IP address to the given static setting.

    :param LMI_IPAssignmentSettingData setting: network setting.
    :param str address: IPv4 or IPv6 address.
    :param int prefix: network prefix.
    :param gateway: default gateway or None
    :type gateway: str or None
    '''
    address, version = util.address_check(address)
    prefix = util.prefix_check(prefix, version)
    gateway = _gateway_check(gateway, version)

    protocol = ns.LMI_IPAssignmentSettingData.ProtocolIFTypeValues.values_dict(
    )["IPv%s" % version]
    found = False
    for settingData in setting.associators(
            AssocClass="LMI_OrderedIPAssignmentComponent"):
        if (settingData.ProtocolIFType is not None
                and int(settingData.ProtocolIFType) == protocol
                and hasattr(settingData, "IPAddresses")):

            settingData.IPAddresses.append(address)
            if version == 4:
                settingData.SubnetMasks.append(
                    util.netmask_from_prefix(prefix))
            else:
                settingData.IPv6SubnetPrefixLengths.append(str(prefix))
            if gateway:
                settingData.GatewayAddresses.append(gateway)
            else:
                settingData.GatewayAddresses.append("")
            found = True
            settingData.push()
    if not found:
        raise LmiInvalidOptions(
            "Can't add IP address to setting: invalid setting type.")
    return 0
Exemplo n.º 15
0
 def verify_options(self, opts):
     _gid = opts['--gid']
     if _gid is not None and not _gid.isdigit():
         raise LmiInvalidOptions("Group ID must be a number")