Search in sources :

Example 61 with Check

use of io.fabric8.karaf.checks.Check in project docker-maven-plugin by fabric8io.

the class AuthConfigTest method mapConstructor.

@Test
public void mapConstructor() {
    AuthConfig config = new AuthConfig("roland", "secret", "roland@jolokia.org", null);
    check(config);
}
Also used : AuthConfig(io.fabric8.maven.docker.access.AuthConfig) Test(org.junit.Test)

Example 62 with Check

use of io.fabric8.karaf.checks.Check in project docker-maven-plugin by fabric8io.

the class AuthConfigFactory method createStandardAuthConfig.

/**
 * Create an authentication config object which can be used for communication with a Docker registry
 *
 * The authentication information is looked up at various places (in this order):
 *
 * <ul>
 *    <li>From system properties</li>
 *    <li>From the provided map which can contain key-value pairs</li>
 *    <li>From the openshift settings in ~/.config/kube</li>
 *    <li>From the Maven settings stored typically in ~/.m2/settings.xml</li>
 * </ul>
 *
 * The following properties (prefix with 'docker.') and config key are evaluated:
 *
 * <ul>
 *     <li>username: User to authenticate</li>
 *     <li>password: Password to authenticate. Can be encrypted</li>
 *     <li>email: Optional EMail address which is send to the registry, too</li>
 * </ul>
 *
 * @param isPush if true this AuthConfig is created for a push, if false it's for a pull
 * @param authConfigMap String-String Map holding configuration info from the plugin's configuration. Can be <code>null</code> in
 *                   which case the settings are consulted.
 * @param settings the global Maven settings object
 * @param user user to check for
 * @param registry registry to use, might be null in which case a default registry is checked,
 * @return the authentication configuration or <code>null</code> if none could be found
 *
 * @throws MojoFailureException
 */
private AuthConfig createStandardAuthConfig(boolean isPush, Map authConfigMap, Settings settings, String user, String registry) throws MojoExecutionException {
    AuthConfig ret;
    // Check first for specific configuration based on direction (pull or push), then for a default value
    for (LookupMode lookupMode : new LookupMode[] { getLookupMode(isPush), LookupMode.DEFAULT }) {
        // System properties docker.username and docker.password always take precedence
        ret = getAuthConfigFromSystemProperties(lookupMode);
        if (ret != null) {
            log.debug("AuthConfig: credentials from system properties");
            return ret;
        }
        // Check for openshift authentication either from the plugin config or from system props
        ret = getAuthConfigFromOpenShiftConfig(lookupMode, authConfigMap);
        if (ret != null) {
            log.debug("AuthConfig: OpenShift credentials");
            return ret;
        }
        // Get configuration from global plugin config
        ret = getAuthConfigFromPluginConfiguration(lookupMode, authConfigMap);
        if (ret != null) {
            log.debug("AuthConfig: credentials from plugin config");
            return ret;
        }
    }
    // ===================================================================
    // These are lookups based on registry only, so the direction (push or pull) doesn't matter:
    // Now lets lookup the registry & user from ~/.m2/setting.xml
    ret = getAuthConfigFromSettings(settings, user, registry);
    if (ret != null) {
        log.debug("AuthConfig: credentials from ~/.m2/setting.xml");
        return ret;
    }
    // No authentication found
    return null;
}
Also used : AuthConfig(io.fabric8.maven.docker.access.AuthConfig)

Example 63 with Check

use of io.fabric8.karaf.checks.Check in project docker-maven-plugin by fabric8io.

the class AuthConfigFactory method getAuthConfigFromOpenShiftConfig.

private AuthConfig getAuthConfigFromOpenShiftConfig(LookupMode lookupMode, Map authConfigMap) throws MojoExecutionException {
    Properties props = System.getProperties();
    String useOpenAuthModeProp = lookupMode.asSysProperty(AUTH_USE_OPENSHIFT_AUTH);
    // Check for system property
    if (props.containsKey(useOpenAuthModeProp)) {
        boolean useOpenShift = Boolean.valueOf(props.getProperty(useOpenAuthModeProp));
        if (useOpenShift) {
            AuthConfig ret = parseOpenShiftConfig();
            if (ret == null) {
                throw new MojoExecutionException("System property " + useOpenAuthModeProp + " " + "set, but not active user and/or token found in ~/.config/kube. " + "Please use 'oc login' for connecting to OpenShift.");
            }
            return ret;
        } else {
            return null;
        }
    }
    // Check plugin config
    Map mapToCheck = getAuthConfigMapToCheck(lookupMode, authConfigMap);
    if (mapToCheck != null && mapToCheck.containsKey(AUTH_USE_OPENSHIFT_AUTH) && Boolean.valueOf((String) mapToCheck.get(AUTH_USE_OPENSHIFT_AUTH))) {
        AuthConfig ret = parseOpenShiftConfig();
        if (ret == null) {
            throw new MojoExecutionException("Authentication configured for OpenShift, but no active user and/or " + "token found in ~/.config/kube. Please use 'oc login' for " + "connecting to OpenShift.");
        }
        return ret;
    } else {
        return null;
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 64 with Check

use of io.fabric8.karaf.checks.Check in project docker-maven-plugin by fabric8io.

the class HealthCheckChecker method check.

@Override
public boolean check() {
    try {
        final ContainerDetails container = docker.getContainer(containerId);
        if (container == null) {
            log.debug("HealthWaitChecker: Container %s not found");
            return false;
        }
        if (container.getHealthcheck() == null) {
            throw new IllegalArgumentException("Can not wait for healthstate of " + imageConfigDesc + ". No HEALTHCHECK configured.");
        }
        if (first) {
            log.info("%s: Waiting to become healthy", imageConfigDesc);
            log.debug("HealthWaitChecker: Waiting for healthcheck: '%s'", container.getHealthcheck());
            first = false;
        } else if (log.isDebugEnabled()) {
            log.debug("HealthWaitChecker: Waiting on healthcheck '%s'", container.getHealthcheck());
        }
        return container.isHealthy();
    } catch (DockerAccessException e) {
        log.warn("Error while checking health: %s", e.getMessage());
        return false;
    }
}
Also used : DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails)

Example 65 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class DeploymentAgent method handleRestartJvmFlag.

/**
 * Adds support for a directive to force a restart upon the first assignment of a specific profile to a container.
 * It creates an entry in zk so that a subsequent modification to the same profile, will not trigger a jvm restart.
 * The behavior is useful for situation when a profile provision .jars in lib/ folder, that are picked up only at
 * jvm boot time.
 *
 * @param profile
 * @param restart
 * @return
 */
protected boolean handleRestartJvmFlag(Profile profile, AtomicBoolean restart) {
    boolean result = false;
    List<String> profilesRequiringRestart = new ArrayList<>();
    ServiceReference<CuratorFramework> curatorServiceReference = systemBundleContext.getServiceReference(CuratorFramework.class);
    ServiceReference<FabricService> fabricServiceReference = systemBundleContext.getServiceReference(FabricService.class);
    if (curatorServiceReference != null && fabricServiceReference != null) {
        CuratorFramework curator = systemBundleContext.getService(curatorServiceReference);
        FabricService fs = systemBundleContext.getService(fabricServiceReference);
        String currentContainerName = fs.getCurrentContainerName();
        List<String> activeProfiles = fs.getCurrentContainer().getProfileIds();
        // check for jvm restart requests
        Map<String, String> agentProperties = profile.getConfiguration("io.fabric8.agent");
        Map<String, String> jvmRestartEntries = new HashMap<>();
        for (String key : agentProperties.keySet()) {
            if (key.startsWith("io.fabric8.agent.forceOneTimeJVMRestart")) {
                jvmRestartEntries.put(key, agentProperties.get(key));
                LOGGER.info("Found a profile carrying a one-time JVM restart request: {}", key);
            }
        }
        // clean old entries
        String basePath = ZkPath.CONTAINER_PROVISION_RESTART.getPath(currentContainerName);
        try {
            if (ZooKeeperUtils.exists(curator, basePath) != null) {
                List<String> zkPaths = ZooKeeperUtils.getAllChildren(curator, ZkPath.CONTAINER_PROVISION_RESTART.getPath(currentContainerName));
                for (String zkPath : zkPaths) {
                    String[] split = zkPath.split("/");
                    String prof = split[split.length - 1];
                    if (!activeProfiles.contains(prof)) {
                        LOGGER.info("Deleting old JVM restart request status: {}", zkPath);
                        ZooKeeperUtils.delete(curator, zkPath);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("Unable to check ZK connection", e);
        }
        for (String key : jvmRestartEntries.keySet()) {
            String[] split = key.split("\\.");
            String profileForcingRestart = split[split.length - 1];
            try {
                String zkPath = ZkPath.CONTAINER_PROVISION_RESTART_PROFILES.getPath(currentContainerName, profileForcingRestart);
                Stat exists = exists(curator, zkPath);
                if (exists == null) {
                    ZooKeeperUtils.create(curator, zkPath);
                    profilesRequiringRestart.add(profileForcingRestart);
                    result = true;
                }
            } catch (Exception e) {
                LOGGER.error("Unable to check ZK connection", e);
            }
        }
    }
    if (result) {
        System.setProperty("karaf.restart.jvm", "true");
        restart.set(true);
        LOGGER.warn("Profiles {} scheduled a JVM restart request. Automated JVM restart support is not universally available. If your jvm doesn't support it you are required to manually restart the container that has just been assigned the profile.", profilesRequiringRestart);
        try {
            bundleContext.getBundle(0).stop();
        } catch (BundleException e) {
            LOGGER.error("Error when forcing a JVM restart", e);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfigurationException(org.osgi.service.cm.ConfigurationException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) FabricService(io.fabric8.api.FabricService) BundleException(org.osgi.framework.BundleException)

Aggregations

Test (org.junit.Test)35 IOException (java.io.IOException)23 File (java.io.File)17 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)15 FabricService (io.fabric8.api.FabricService)11 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)11 Map (java.util.Map)10 Container (io.fabric8.api.Container)9 PatchException (io.fabric8.patch.management.PatchException)9 Expectations (mockit.Expectations)9 Profile (io.fabric8.api.Profile)8 TreeMap (java.util.TreeMap)7 Version (io.fabric8.api.Version)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)6 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 Check (io.fabric8.karaf.checks.Check)5 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)5 Patch (io.fabric8.patch.management.Patch)5