Search in sources :

Example 6 with AddressModel

use of org.platformlayer.core.model.AddressModel in project platformlayer by platformlayer.

the class InstanceScript method addIpNeighborProxy.

private void addIpNeighborProxy(ScriptBuilder sb) throws OpsException {
    if (addresses != null) {
        for (Provider<AddressModel> addressProvider : addresses) {
            AddressModel addressModel = addressProvider.get();
            InetAddress address = addressModel.getInetAddress();
            if (InetAddressUtils.isIpv6(address)) {
                String hostPrimaryInterface = getHostPrimaryInterface();
                if (Strings.isNullOrEmpty(hostPrimaryInterface)) {
                    throw new OpsException("primaryInterface not specified");
                }
                Command command = Command.build("ip neigh add proxy {0} dev {1}", address, hostPrimaryInterface);
                sb.add(command);
            }
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) AddressModel(org.platformlayer.core.model.AddressModel) InetAddress(java.net.InetAddress)

Example 7 with AddressModel

use of org.platformlayer.core.model.AddressModel in project platformlayer by platformlayer.

the class KvmInstance method addChildren.

@Override
protected void addChildren() throws OpsException {
    CloudInstanceMapper instance;
    {
        instance = injected(CloudInstanceMapper.class);
        instance.instance = OpsContext.get().getInstance(DirectInstance.class);
        addChild(instance);
    }
    instance.addChild(ManagedDirectory.build(getInstanceDir(), "700"));
    final NetworkAddressPoolAssignment address4;
    {
        address4 = instance.addChild(NetworkAddressPoolAssignment.class);
        address4.holder = owner;
        address4.poolProvider = DirectCloudUtils.getPrivateAddressPool4();
    }
    final NetworkAddressPoolAssignment address6;
    {
        address6 = instance.addChild(NetworkAddressPoolAssignment.class);
        address6.holder = owner;
        address6.poolProvider = directCloudHelpers.getAddressPool6();
    }
    {
        NetworkTunDevice tun = injected(NetworkTunDevice.class);
        tun.interfaceName = getEthernetDeviceName();
        tun.bridgeName = new Provider<String>() {

            @Override
            public String get() {
                DirectHost host = OpsContext.get().getInstance(DirectHost.class);
                return host.bridge;
            }
        };
        instance.addChild(tun);
    }
    final SocketAddressPoolAssignment assignMonitorPort;
    {
        assignMonitorPort = injected(SocketAddressPoolAssignment.class);
        assignMonitorPort.holder = owner;
        assignMonitorPort.poolProvider = DirectCloudUtils.getKvmMonitorPortPool();
        instance.addChild(assignMonitorPort);
    }
    final SocketAddressPoolAssignment assignVncPort;
    {
        assignVncPort = injected(SocketAddressPoolAssignment.class);
        assignVncPort.holder = owner;
        assignVncPort.poolProvider = DirectCloudUtils.getVncPortPool();
        instance.addChild(assignVncPort);
    }
    {
        ConfigIso iso = injected(ConfigIso.class);
        iso.isoFile = getConfigIsoPath();
        iso.buildDir = new File(getInstanceDir(), "config_iso_src");
        iso.model = new TemplateDataSource() {

            @Override
            public void buildTemplateModel(Map<String, Object> model) throws OpsException {
                InterfaceModel eth0 = InterfaceModel.build("eth0");
                AddressModel ipv4 = address4.get();
                eth0.addAddress(ipv4);
                AddressModel ipv6 = address6.get();
                eth0.addAddress(ipv6);
                List<InterfaceModel> interfaces = Lists.newArrayList();
                interfaces.add(eth0);
                model.put("interfaces", interfaces);
                List<String> authorizedKeys = Lists.newArrayList();
                authorizedKeys.add(OpenSshUtils.serialize(sshPublicKey));
                model.put("authorizedKeys", authorizedKeys);
            }
        };
        instance.addChild(iso);
    }
    {
        DownloadImage download = injected(DownloadImage.class);
        download.imageFile = getImagePath();
        download.recipeKey = recipeId;
        download.imageFormats = Arrays.asList(ImageFormat.DiskRaw, ImageFormat.DiskQcow2);
        instance.addChild(download);
    }
    {
        ManagedKvmInstance kvmInstance = addChild(ManagedKvmInstance.class);
        kvmInstance.id = id;
        kvmInstance.memoryMb = Math.max(256, minimumMemoryMB);
        kvmInstance.vcpus = 1;
        kvmInstance.base = getInstanceDir();
        kvmInstance.monitor = assignMonitorPort;
        kvmInstance.vnc = assignVncPort;
        kvmInstance.nics = buildVnics();
        kvmInstance.drives = buildDrives();
        kvmInstance.addresses.add(address4);
        kvmInstance.addresses.add(address6);
    }
    {
        final DirectInstance model = OpsContext.get().getInstance(DirectInstance.class);
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                tagChanges.addTags.add(Tag.INSTANCE_KEY.build(model.getKey()));
                AddressModel ipv4 = address4.get();
                AddressModel ipv6 = address6.get();
                tagChanges.addTags.add(Tag.NETWORK_ADDRESS.build(ipv4));
                tagChanges.addTags.add(Tag.NETWORK_ADDRESS.build(ipv6));
                return tagChanges;
            }
        };
        addChild(Tagger.build(model, tagChanges));
    }
}
Also used : NetworkAddressPoolAssignment(org.platformlayer.ops.pool.NetworkAddressPoolAssignment) SocketAddressPoolAssignment(org.platformlayer.ops.pool.SocketAddressPoolAssignment) TemplateDataSource(org.platformlayer.ops.templates.TemplateDataSource) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) TagChanges(org.platformlayer.core.model.TagChanges) OpsProvider(org.platformlayer.ops.OpsProvider) Provider(com.google.inject.Provider) CloudInstanceMapper(org.platformlayer.service.cloud.direct.ops.CloudInstanceMapper) DirectHost(org.platformlayer.service.cloud.direct.model.DirectHost) InterfaceModel(org.platformlayer.ops.networks.InterfaceModel) OpsProvider(org.platformlayer.ops.OpsProvider) AddressModel(org.platformlayer.core.model.AddressModel) File(java.io.File) Map(java.util.Map) DownloadImage(org.platformlayer.service.cloud.direct.ops.DownloadImage)

Example 8 with AddressModel

use of org.platformlayer.core.model.AddressModel in project platformlayer by platformlayer.

the class DirectPublicPorts method addChildren.

@Override
protected void addChildren() throws OpsException {
    final CloudInstanceMapper cloudHost;
    {
        cloudHost = injected(CloudInstanceMapper.class);
        cloudHost.createInstance = false;
        cloudHost.instance = backendItem;
        addChild(cloudHost);
    }
    final SocketAddressPoolAssignment assignPublicAddress;
    {
        assignPublicAddress = cloudHost.addChild(SocketAddressPoolAssignment.class);
        assignPublicAddress.holder = backendItem.getKey();
        if (Objects.equal(transport, Transport.Ipv6)) {
            assignPublicAddress.poolProvider = new OpsProvider<ResourcePool<InetSocketAddress>>() {

                @Override
                public ResourcePool<InetSocketAddress> get() throws OpsException {
                    final ResourcePool<AddressModel> pool = directCloudHelpers.getAddressPool6().get();
                    return new AssignPortToAddressPool(pool, publicPort);
                }
            };
        } else {
            List<Integer> publicPortCluster = this.publicPortCluster;
            if (publicPortCluster == null) {
                publicPortCluster = Lists.newArrayList();
                publicPortCluster.add(publicPort);
            }
            if (!publicPortCluster.contains(publicPort)) {
                throw new OpsException("Port set specified, but public port not in the set");
            }
            assignPublicAddress.poolProvider = directCloudHelpers.getPublicAddressPool4(publicPort, publicPortCluster);
        }
    }
    if (Objects.equal(transport, Transport.Ipv6)) {
        // TODO: Do we need separate frontend / backend ports really?
        if (this.publicPort != this.backendPort) {
            throw new UnsupportedOperationException();
        }
    } else {
        for (Protocol protocol : Protocol.TcpAndUdp()) {
            IptablesForwardPort forward = injected(IptablesForwardPort.class);
            forward.publicAddress = assignPublicAddress;
            forward.ruleKey = protocol.name() + "-" + uuid;
            forward.protocol = protocol;
            forward.privateAddress = new OpsProvider<String>() {

                @Override
                public String get() throws OpsException {
                    // Refresh item to pick up new tags
                    backendItem = platformLayerClient.getItem(backendItem.getKey(), DirectInstance.class);
                    PlatformLayerCloudMachine instanceMachine = (PlatformLayerCloudMachine) instanceHelpers.getMachine(backendItem);
                    DirectInstance instance = (DirectInstance) instanceMachine.getInstance();
                    List<InetAddress> addresses = Tag.NETWORK_ADDRESS.find(instance);
                    InetAddress address = InetAddressChooser.preferIpv4().choose(addresses);
                    if (address == null) {
                        throw new IllegalStateException();
                    }
                    if (InetAddressUtils.isIpv6(address)) {
                        // We can't NAT IPV4 -> IPV6 (I think)
                        throw new IllegalStateException();
                    }
                    return address.getHostAddress();
                }
            };
            forward.privatePort = backendPort;
            cloudHost.addChild(forward);
        }
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                InetSocketAddress socketAddress = assignPublicAddress.get();
                if (socketAddress == null) {
                    return null;
                }
                if (socketAddress.getPort() != publicPort) {
                    throw new IllegalStateException();
                }
                EndpointInfo endpoint = new EndpointInfo(socketAddress);
                tagChanges.addTags.add(endpoint.toTag());
                return tagChanges;
            }
        };
        for (ItemBase tagItem : tagItems) {
            Tagger tagger = addChild(Tagger.class);
            tagger.platformLayerKey = tagItem.getKey();
            tagger.tagChangesProvider = tagChanges;
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tagger(org.platformlayer.ops.tagger.Tagger) SocketAddressPoolAssignment(org.platformlayer.ops.pool.SocketAddressPoolAssignment) ItemBase(org.platformlayer.core.model.ItemBase) InetSocketAddress(java.net.InetSocketAddress) PlatformLayerCloudMachine(org.platformlayer.ops.machines.PlatformLayerCloudMachine) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) TagChanges(org.platformlayer.core.model.TagChanges) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) AddressModel(org.platformlayer.core.model.AddressModel) List(java.util.List) Protocol(org.platformlayer.ops.firewall.Protocol) IptablesForwardPort(org.platformlayer.ops.firewall.scripts.IptablesForwardPort) InetAddress(java.net.InetAddress)

Example 9 with AddressModel

use of org.platformlayer.core.model.AddressModel in project platformlayer by platformlayer.

the class LxcBootstrap method buildModel.

Map<String, Object> buildModel() {
    Map<String, Object> model = Maps.newHashMap();
    model.put("hostname", getHostname());
    model.put("name", lxcId);
    InterfaceModel eth0 = InterfaceModel.build("eth0");
    AddressModel ipv4 = address4.get();
    eth0.addAddress(ipv4);
    AddressModel ipv6 = address6.get();
    eth0.addAddress(ipv6);
    List<InterfaceModel> interfaces = Lists.newArrayList();
    interfaces.add(eth0);
    model.put("interfaces", interfaces);
    model.put("externalBridge", OpsContext.get().getInstance(DirectHost.class).bridge);
    model.put("ipv4", ipv4);
    model.put("ipv6", ipv6);
    return model;
}
Also used : InterfaceModel(org.platformlayer.ops.networks.InterfaceModel) AddressModel(org.platformlayer.core.model.AddressModel)

Example 10 with AddressModel

use of org.platformlayer.core.model.AddressModel in project platformlayer by platformlayer.

the class AssignPortToAddressPool method reverse.

@Override
protected AddressModel reverse(InetSocketAddress item) {
    if (item == null) {
        return null;
    }
    AddressModel addressModel = new AddressModel();
    addressModel.address = item.getAddress().getHostAddress();
    AddressModels.populateDefaults(addressModel);
    return addressModel;
}
Also used : AddressModel(org.platformlayer.core.model.AddressModel)

Aggregations

AddressModel (org.platformlayer.core.model.AddressModel)11 OpsProvider (org.platformlayer.ops.OpsProvider)6 DirectHost (org.platformlayer.service.cloud.direct.model.DirectHost)4 File (java.io.File)3 InetAddress (java.net.InetAddress)3 TagChanges (org.platformlayer.core.model.TagChanges)3 OpsException (org.platformlayer.ops.OpsException)3 NetworkPoint (org.platformlayer.ops.networks.NetworkPoint)3 NetworkPoolBuilder (org.platformlayer.ops.pool.NetworkPoolBuilder)3 DirectInstance (org.platformlayer.service.cloud.direct.model.DirectInstance)3 InetSocketAddress (java.net.InetSocketAddress)2 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)2 Command (org.platformlayer.ops.Command)2 InterfaceModel (org.platformlayer.ops.networks.InterfaceModel)2 IpRange (org.platformlayer.ops.networks.IpRange)2 NetworkAddressPoolAssignment (org.platformlayer.ops.pool.NetworkAddressPoolAssignment)2 PoolBuilder (org.platformlayer.ops.pool.PoolBuilder)2 SocketAddressPoolAssignment (org.platformlayer.ops.pool.SocketAddressPoolAssignment)2 CloudInstanceMapper (org.platformlayer.service.cloud.direct.ops.CloudInstanceMapper)2 DownloadImage (org.platformlayer.service.cloud.direct.ops.DownloadImage)2