Search in sources :

Example 6 with DirectInstance

use of org.platformlayer.service.cloud.direct.model.DirectInstance 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 7 with DirectInstance

use of org.platformlayer.service.cloud.direct.model.DirectInstance in project platformlayer by platformlayer.

the class LxcInstanceController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final DirectInstance model = OpsContext.get().getInstance(DirectInstance.class);
    CloudInstanceMapper instance;
    {
        instance = injected(CloudInstanceMapper.class);
        instance.instance = OpsContext.get().getInstance(DirectInstance.class);
        addChild(instance);
    }
    instance.addChild(ManagedDirectory.build(getInstanceDir(), "700"));
    // TODO: If we're not going to assign an IPV4 redirect, we might not need this
    final Provider<AddressModel> address4;
    {
        NetworkAddressPoolAssignment provider = instance.addChild(NetworkAddressPoolAssignment.class);
        provider.holder = model.getKey();
        provider.poolProvider = DirectCloudUtils.getPrivateAddressPool4();
        address4 = provider;
    }
    final Provider<AddressModel> address6;
    {
        NetworkAddressPoolAssignment provider = instance.addChild(NetworkAddressPoolAssignment.class);
        provider.holder = model.getKey();
        provider.poolProvider = directCloudHelpers.getAddressPool6();
        address6 = provider;
    }
    // {
    // NetworkTunDevice tun = injected(NetworkTunDevice.class);
    // tun.interfaceName = getEthernetDeviceName();
    // tun.bridgeName = Providers.getProperty(assignNetworkAddress, "bridge");
    // instance.addChild(tun);
    // }
    {
        DownloadImage download = injected(DownloadImage.class);
        download.imageFile = new File(getInstanceDir(), "rootfs");
        download.recipeKey = model.recipeId;
        download.imageFormats = Collections.singletonList(ImageFormat.Tar);
        instance.addChild(download);
    }
    {
        LxcBootstrap bootstrap = injected(LxcBootstrap.class);
        bootstrap.address4 = address4;
        bootstrap.address6 = address6;
        bootstrap.lxcId = id;
        bootstrap.instanceDir = instanceDir;
        try {
            bootstrap.sshPublicKey = OpenSshUtils.readSshPublicKey(model.sshPublicKey);
        } catch (IOException e) {
            throw new OpsException("Error deserializing SSH key", e);
        }
        bootstrap.hostname = model.hostname;
        instance.addChild(bootstrap);
    }
    InstanceScript script;
    {
        script = instance.addChild(InstanceScript.class);
        script.filePath = new File(DirectHostController.LXC_INSTANCE_DIR, id);
        String key = "lxc-" + id;
        script.key = key;
        script.addresses.add(address4);
        script.addresses.add(address6);
        // script.hostPrimaryInterface = hostModel.publicInterface;
        Command command = Command.build("lxc-start");
        command.addLiteral("--name").addQuoted(id);
        script.launchInstanceCommand = command;
    }
    {
        // ManagedSupervisordInstance service = instance.addChild(ManagedSupervisordInstance.class);
        StandardService service = instance.addChild(StandardService.class);
        script.configure(model, service);
    }
    {
        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();
                if (ipv4 != null) {
                    tagChanges.addTags.add(Tag.NETWORK_ADDRESS.build(ipv4));
                }
                if (ipv6 != null) {
                    tagChanges.addTags.add(Tag.NETWORK_ADDRESS.build(ipv6));
                }
                return tagChanges;
            }
        };
        instance.addChild(Tagger.build(model, tagChanges));
    }
}
Also used : NetworkAddressPoolAssignment(org.platformlayer.ops.pool.NetworkAddressPoolAssignment) OpsException(org.platformlayer.ops.OpsException) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) InstanceScript(org.platformlayer.service.cloud.direct.ops.InstanceScript) StandardService(org.platformlayer.ops.supervisor.StandardService) IOException(java.io.IOException) TagChanges(org.platformlayer.core.model.TagChanges) CloudInstanceMapper(org.platformlayer.service.cloud.direct.ops.CloudInstanceMapper) OpsProvider(org.platformlayer.ops.OpsProvider) Command(org.platformlayer.ops.Command) AddressModel(org.platformlayer.core.model.AddressModel) File(java.io.File) DownloadImage(org.platformlayer.service.cloud.direct.ops.DownloadImage)

Aggregations

DirectInstance (org.platformlayer.service.cloud.direct.model.DirectInstance)7 AddressModel (org.platformlayer.core.model.AddressModel)3 TagChanges (org.platformlayer.core.model.TagChanges)3 OpsException (org.platformlayer.ops.OpsException)3 OpsProvider (org.platformlayer.ops.OpsProvider)3 Function (com.google.common.base.Function)2 File (java.io.File)2 HostPolicy (org.platformlayer.core.model.HostPolicy)2 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)2 NetworkAddressPoolAssignment (org.platformlayer.ops.pool.NetworkAddressPoolAssignment)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 Provider (com.google.inject.Provider)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 Map (java.util.Map)1 EndpointInfo (org.platformlayer.core.model.EndpointInfo)1