Search in sources :

Example 11 with Builder

use of io.fabric8.maven.docker.config.ImageConfiguration.Builder in project fabric8 by jboss-fuse.

the class ProfileChangeParentsAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    Version version = versionId != null ? profileService.getRequiredVersion(versionId) : fabricService.getRequiredDefaultVersion();
    Profile profile = version.getRequiredProfile(profileId);
    // we can only change parents to existing profiles
    Profile[] parents = FabricCommand.getExistingProfiles(fabricService, version, parentIds);
    ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
    List<String> oldParents = builder.getParents();
    for (Profile parent : parents) {
        builder.addParent(parent.getId());
    }
    // remove old parent profiles
    for (String oldParent : oldParents) {
        if (!parentIds.contains(oldParent)) {
            builder.removeParent(oldParent);
        }
    }
    profileService.updateProfile(builder.getProfile());
    return null;
}
Also used : Version(io.fabric8.api.Version) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile)

Example 12 with Builder

use of io.fabric8.maven.docker.config.ImageConfiguration.Builder in project fabric8 by jboss-fuse.

the class VersionCreateAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    String latestVersion = null;
    ProfileService profileService = fabricService.adapt(ProfileService.class);
    List<String> versions = profileService.getVersions();
    if (versions.size() > 0) {
        latestVersion = versions.get(versions.size() - 1);
    }
    if (versionId == null) {
        IllegalStateAssertion.assertNotNull(latestVersion, "Cannot default the new version name as there are no versions available");
        VersionSequence sequence = new VersionSequence(latestVersion);
        versionId = sequence.next().getName();
    }
    // TODO we maybe want to choose the version which is less than the 'name' if it was specified
    // e.g. if you create a version 1.1 then it should use 1.0 if there is already a 2.0
    String sourceId = null;
    if (parentVersion == null) {
        sourceId = latestVersion;
    } else {
        IllegalStateAssertion.assertTrue(profileService.hasVersion(parentVersion), "Cannot find parent version: " + parentVersion);
        sourceId = parentVersion;
    }
    Version targetVersion;
    if (sourceId != null) {
        Map<String, String> attributes = new HashMap<String, String>(Collections.singletonMap(Version.PARENT, sourceId));
        if (description != null) {
            attributes.put(Version.DESCRIPTION, description);
        }
        targetVersion = profileService.createVersionFrom(sourceId, versionId, attributes);
        System.out.println("Created version: " + versionId + " as copy of: " + sourceId);
    } else {
        VersionBuilder builder = VersionBuilder.Factory.create(versionId);
        if (description != null) {
            builder.addAttribute(Version.DESCRIPTION, description);
        }
        targetVersion = profileService.createVersion(builder.getVersion());
        System.out.println("Create version: " + versionId);
    }
    if (defaultVersion == Boolean.TRUE) {
        fabricService.setDefaultVersionId(targetVersion.getId());
    }
    return null;
}
Also used : ProfileService(io.fabric8.api.ProfileService) Version(io.fabric8.api.Version) HashMap(java.util.HashMap) VersionBuilder(io.fabric8.api.VersionBuilder) VersionSequence(io.fabric8.api.VersionSequence)

Example 13 with Builder

use of io.fabric8.maven.docker.config.ImageConfiguration.Builder in project fabric8 by jboss-fuse.

the class Agent method loadResources.

// 
// State support
// 
public static Callable<Map<String, Resource>> loadResources(DownloadManager manager, Map<String, Map<VersionRange, Map<String, String>>> metadata, Set<String> uris) throws MultiException, InterruptedException, MalformedURLException {
    final Map<String, Resource> resources = new HashMap<>();
    final Downloader downloader = manager.createDownloader();
    final MetadataBuilder builder = new MetadataBuilder(metadata);
    final DownloadCallback callback = new DownloadCallback() {

        @Override
        public void downloaded(StreamProvider provider) throws Exception {
            String uri = provider.getUrl();
            Map<String, String> headers = builder.getMetadata(uri, provider.getFile());
            Resource resource = ResourceBuilder.build(uri, headers);
            synchronized (resources) {
                resources.put(uri, resource);
            }
        }
    };
    for (String uri : uris) {
        downloader.download(uri, callback);
    }
    return new Callable<Map<String, Resource>>() {

        @Override
        public Map<String, Resource> call() throws Exception {
            downloader.await();
            return resources;
        }
    };
}
Also used : StreamProvider(io.fabric8.agent.download.StreamProvider) HashMap(java.util.HashMap) DownloadCallback(io.fabric8.agent.download.DownloadCallback) Resource(org.osgi.resource.Resource) Downloader(io.fabric8.agent.download.Downloader) Callable(java.util.concurrent.Callable)

Example 14 with Builder

use of io.fabric8.maven.docker.config.ImageConfiguration.Builder in project fabric8 by jboss-fuse.

the class FabricServiceImpl method setConfigurationValue.

@Override
public void setConfigurationValue(String versionId, String profileId, String pid, String key, String value) {
    assertValid();
    Version version = profileService.get().getRequiredVersion(versionId);
    Profile profile = version.getRequiredProfile(profileId);
    ProfileBuilder builder = ProfileBuilder.Factory.createFrom(profile);
    Map<String, String> config = builder.getConfiguration(pid);
    config.put(key, value);
    builder.addConfiguration(pid, config);
    profileService.get().updateProfile(builder.getProfile());
}
Also used : Version(io.fabric8.api.Version) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile)

Example 15 with Builder

use of io.fabric8.maven.docker.config.ImageConfiguration.Builder in project fabric8 by jboss-fuse.

the class MQServiceImpl method createOrUpdateMQProfile.

@Override
public Profile createOrUpdateMQProfile(String versionId, String profileId, String brokerName, Map<String, String> configs, boolean replicated) {
    Version version = profileService.getRequiredVersion(versionId);
    String parentProfileName = null;
    if (configs != null && configs.containsKey("parent")) {
        parentProfileName = configs.remove("parent");
    }
    if (Strings.isNullOrBlank(parentProfileName)) {
        parentProfileName = replicated ? MQ_PROFILE_REPLICATED : MQ_PROFILE_BASE;
    }
    Profile parentProfile = version.getRequiredProfile(parentProfileName);
    if (brokerName == null || profileId == null) {
        return parentProfile;
    }
    String pidName = getBrokerPID(brokerName);
    // lets check we have a config value
    ProfileBuilder builder;
    Profile overlay;
    // create a profile if it doesn't exist
    Map<String, String> config = null;
    boolean create = !version.hasProfile(profileId);
    if (create) {
        builder = ProfileBuilder.Factory.create(versionId, profileId);
        if (parentProfile != null) {
            builder.addParent(parentProfile.getId());
        }
        overlay = profileService.getOverlayProfile(parentProfile);
    } else {
        Profile profile = version.getRequiredProfile(profileId);
        builder = ProfileBuilder.Factory.createFrom(profile);
        config = builder.getConfiguration(pidName);
        overlay = profileService.getOverlayProfile(profile);
    }
    Map<String, String> parentProfileConfig = ProfileBuilder.Factory.createFrom(overlay).getConfiguration(MQ_PID_TEMPLATE);
    if (config == null) {
        config = parentProfileConfig;
    }
    if (configs != null && "true".equals(configs.get("ssl"))) {
        // Only generate the keystore file if it does not exist.
        // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
        byte[] keystore = overlay.getFileConfiguration("keystore.jks");
        if (keystore == null) {
            try {
                String host = configs.get("keystore.cn");
                if (host == null) {
                    host = configs.get(GROUP);
                    if (host == null) {
                        host = "localhost";
                    }
                    configs.put("keystore.cn", host);
                }
                String password = configs.get("keystore.password");
                if (password == null) {
                    password = generatePassword(8);
                    configs.put("keystore.password", password);
                }
                File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                keystoreFile.delete();
                LOG.info("Generating ssl keystore...");
                int rc = system("keytool", "-genkey", "-storetype", "JKS", "-storepass", password, "-keystore", keystoreFile.getCanonicalPath(), "-keypass", password, "-alias", host, "-keyalg", "RSA", "-keysize", "4096", "-dname", String.format("cn=%s", host), "-validity", "3650");
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                keystore = Files.readBytes(keystoreFile);
                keystoreFile.delete();
                LOG.info("Keystore generated");
                builder.addFileConfiguration("keystore.jks", keystore);
                configs.put("keystore.file", "profile:keystore.jks");
            } catch (IOException e) {
                LOG.error("Failed to generate keystore.jks: " + e.getMessage(), e);
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
        byte[] truststore = overlay.getFileConfiguration("truststore.jks");
        if (truststore == null && configs.get("keystore.password") != null) {
            try {
                String password = configs.get("truststore.password");
                if (password == null) {
                    password = configs.get("keystore.password");
                    configs.put("truststore.password", password);
                }
                File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                Files.writeToFile(keystoreFile, keystore);
                File certFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                certFile.delete();
                LOG.info("Exporting broker certificate to create truststore.jks");
                int rc = system("keytool", "-exportcert", "-rfc", "-keystore", keystoreFile.getCanonicalPath(), "-storepass", configs.get("keystore.password"), "-alias", configs.get("keystore.cn"), "--file", certFile.getCanonicalPath());
                keystoreFile.delete();
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                LOG.info("Creating truststore.jks");
                File truststoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                truststoreFile.delete();
                rc = system("keytool", "-importcert", "-noprompt", "-keystore", truststoreFile.getCanonicalPath(), "-storepass", password, "--file", certFile.getCanonicalPath());
                certFile.delete();
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                truststore = Files.readBytes(truststoreFile);
                truststoreFile.delete();
                builder.addFileConfiguration("truststore.jks", truststore);
                configs.put("truststore.file", "profile:truststore.jks");
            } catch (IOException e) {
                LOG.error("Failed to generate truststore.jks due: " + e.getMessage(), e);
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
    config.put("broker-name", brokerName);
    if (configs != null) {
        config.putAll(configs);
    }
    // lets check we've a bunch of config values inherited from the template
    String[] propertiesToDefault = { CONFIG_URL, STANDBY_POOL, CONNECTORS };
    for (String key : propertiesToDefault) {
        if (config.get(key) == null) {
            String defaultValue = parentProfileConfig.get(key);
            if (Strings.isNotBlank(defaultValue)) {
                config.put(key, defaultValue);
            }
        }
    }
    // config map is not from "official" profile, so it doesn't have to use felix' Properties class
    builder.addConfiguration(pidName, config);
    Profile profile = builder.getProfile();
    return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
}
Also used : Version(io.fabric8.api.Version) IOException(java.io.IOException) ProfileBuilder(io.fabric8.api.ProfileBuilder) File(java.io.File) Profile(io.fabric8.api.Profile)

Aggregations

Test (org.junit.Test)60 ProfileBuilder (io.fabric8.api.ProfileBuilder)34 Profile (io.fabric8.api.Profile)33 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)27 FabricService (io.fabric8.api.FabricService)24 InputStream (java.io.InputStream)21 Deployment (org.jboss.arquillian.container.test.api.Deployment)21 OSGiManifestBuilder (org.jboss.osgi.metadata.OSGiManifestBuilder)21 Asset (org.jboss.shrinkwrap.api.asset.Asset)21 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)21 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 HashMap (java.util.HashMap)19 Map (java.util.Map)19 Logger (org.slf4j.Logger)19 CommandSupport (io.fabric8.itests.support.CommandSupport)18 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)17 ServiceLocator (io.fabric8.api.gravia.ServiceLocator)16 IOException (java.io.IOException)16 Action (org.apache.felix.gogo.commands.Action)16 AbstractCommand (org.apache.felix.gogo.commands.basic.AbstractCommand)16