Search in sources :

Example 1 with ProjectConfig

use of io.fabric8.devops.ProjectConfig in project fabric8 by fabric8io.

the class Configuration method findNamespaceForEnvironment.

/**
 * Resolves a logical environment name for a project, such as <code>Testing</code> to the physical projcect/team specific
 * namespace value.
 *
 * It tries to find a fabric8.yml file in this folder or a parent folder and loads it and tries to use it to find the
 * namespace for the given environment or uses environment variables to resolve the environment name -> physical namespace
 * @return the namespace
 */
private static String findNamespaceForEnvironment(String environment, Map<String, String> map, KubernetesClient kubernetesClient, String developNamespace, boolean failOnMissingEnvironmentNamespace) {
    String namespace = null;
    if (!Strings.isNullOrBlank(environment)) {
        namespace = Environments.namespaceForEnvironment(kubernetesClient, environment, developNamespace);
        if (Strings.isNotBlank(namespace)) {
            return namespace;
        }
        String basedir = System.getProperty("basedir", ".");
        File folder = new File(basedir);
        ProjectConfig projectConfig = ProjectConfigs.findFromFolder(folder);
        if (projectConfig != null) {
            LinkedHashMap<String, String> environments = projectConfig.getEnvironments();
            if (environments != null) {
                namespace = environments.get(environment);
            }
        }
        String key = environment.toLowerCase() + ".namespace";
        if (Strings.isNullOrBlank(namespace)) {
            // lets try find an environment variable or system property
            namespace = getStringProperty(key, map, null);
        }
        if (Strings.isNullOrBlank(namespace)) {
            if (failOnMissingEnvironmentNamespace) {
                throw new IllegalStateException("A fabric8 environment '" + environment + "' has been specified, but no matching namespace was found in the fabric8.yml file or '" + key + "' system property");
            } else {
                return developNamespace;
            }
        }
    }
    return namespace;
}
Also used : ProjectConfig(io.fabric8.devops.ProjectConfig) File(java.io.File)

Example 2 with ProjectConfig

use of io.fabric8.devops.ProjectConfig in project fabric8 by fabric8io.

the class DevOpsConnector method execute.

/**
 * For a given project this operation will try to update the associated DevOps resources
 *
 * @throws Exception
 */
public void execute() throws Exception {
    loadConfigFile();
    KubernetesClient kubernetes = getKubernetes();
    String name = projectName;
    if (Strings.isNullOrBlank(name)) {
        if (projectConfig != null) {
            name = projectConfig.getBuildName();
        }
        if (Strings.isNullOrBlank(name)) {
            name = jenkinsJob;
        }
        if (Strings.isNullOrBlank(name)) {
            name = ProjectRepositories.createBuildName(username, repoName);
            if (projectConfig != null) {
                projectConfig.setBuildName(name);
            }
        }
    }
    if (Strings.isNullOrBlank(projectName)) {
        projectName = name;
    }
    Map<String, String> labels = new HashMap<>();
    labels.put("user", username);
    labels.put("repo", repoName);
    getLog().info("build name " + name);
    taiga = null;
    taigaProject = null;
    try {
        taiga = createTaiga();
        taigaProject = createTaigaProject(taiga);
    } catch (Exception e) {
        getLog().error("Failed to load or lazily create the Taiga project: " + e, e);
    }
    getLog().info("taiga " + taiga);
    LetsChatClient letschat = null;
    try {
        letschat = createLetsChat();
    } catch (Exception e) {
        getLog().error("Failed to load or lazily create the LetsChat client: " + e, e);
    }
    getLog().info("letschat " + letschat);
    /*
         * Create Gerrit Git to if isGerritReview is enabled
         */
    if (projectConfig != null && projectConfig.hasCodeReview()) {
        try {
            createGerritRepo(repoName, gerritUser, gerritPwd, gerritGitInitialCommit, gerritGitRepoDesription);
        } catch (Exception e) {
            getLog().error("Failed to create GerritGit repo : " + e, e);
        }
    }
    Map<String, String> annotations = new HashMap<>();
    jenkinsJobUrl = null;
    String jenkinsUrl = null;
    try {
        jenkinsUrl = getJenkinsServiceUrl(true);
        if (Strings.isNotBlank(jenkinsUrl)) {
            if (Strings.isNotBlank(jenkinsMonitorView)) {
                String url = URLUtils.pathJoin(jenkinsUrl, "/view", jenkinsMonitorView);
                annotationLink(annotations, "fabric8.link.jenkins.monitor/", url, "Monitor");
            }
            if (Strings.isNotBlank(jenkinsPipelineView)) {
                String url = URLUtils.pathJoin(jenkinsUrl, "/view", jenkinsPipelineView);
                annotationLink(annotations, "fabric8.link.jenkins.pipeline/", url, "Pipeline");
            }
            if (Strings.isNotBlank(name)) {
                jenkinsJobUrl = URLUtils.pathJoin(jenkinsUrl, "/job", name);
                annotationLink(annotations, "fabric8.link.jenkins.job/", jenkinsJobUrl, "Job");
            }
        }
    } catch (Exception e) {
        getLog().warn("Could not find the Jenkins URL!: " + e, e);
    }
    getLog().info("jenkins " + jenkinsUrl);
    if (!annotationLink(annotations, "fabric8.link.issues/", issueTrackerUrl, issueTrackerLabel)) {
        String taigaLink = getProjectPageLink(taiga, taigaProject, this.taigaProjectLinkPage);
        annotationLink(annotations, "fabric8.link.taiga/", taigaLink, taigaProjectLinkLabel);
    }
    if (!annotationLink(annotations, "fabric8.link.team/", teamUrl, teamLabel)) {
        String taigaTeamLink = getProjectPageLink(taiga, taigaProject, this.taigaTeamLinkPage);
        annotationLink(annotations, "fabric8.link.taiga.team/", taigaTeamLink, taigaTeamLinkLabel);
    }
    annotationLink(annotations, "fabric8.link.releases/", releasesUrl, releasesLabel);
    String chatRoomLink = getChatRoomLink(letschat);
    annotationLink(annotations, "fabric8.link.letschat.room/", chatRoomLink, letschatRoomLinkLabel);
    annotationLink(annotations, "fabric8.link.repository.browse/", repositoryBrowseLink, repositoryBrowseLabel);
    ProjectConfigs.defaultEnvironments(projectConfig, namespace);
    String consoleUrl = getServiceUrl(ServiceNames.FABRIC8_CONSOLE, namespace, fabric8ConsoleNamespace);
    if (projectConfig != null) {
        Map<String, String> environments = projectConfig.getEnvironments();
        updateEnvironmentConfigMap(environments, kubernetes, annotations, consoleUrl);
    }
    addLink("Git", getGitUrl());
    Controller controller = createController();
    OpenShiftClient openShiftClient = controller.getOpenShiftClientOrJenkinshift();
    BuildConfig buildConfig = null;
    if (openShiftClient != null) {
        try {
            buildConfig = openShiftClient.buildConfigs().withName(projectName).get();
        } catch (Exception e) {
            log.error("Failed to load build config for " + namespace + "/" + projectName + ". " + e, e);
        }
        log.info("Loaded build config for " + namespace + "/" + projectName + " " + buildConfig);
    }
    // if we have loaded a build config then lets assume its correct!
    boolean foundExistingGitUrl = false;
    if (buildConfig != null) {
        BuildConfigSpec spec = buildConfig.getSpec();
        if (spec != null) {
            BuildSource source = spec.getSource();
            if (source != null) {
                GitBuildSource git = source.getGit();
                if (git != null) {
                    gitUrl = git.getUri();
                    log.info("Loaded existing BuildConfig git url: " + gitUrl);
                    foundExistingGitUrl = true;
                }
                LocalObjectReference sourceSecret = source.getSourceSecret();
                if (sourceSecret != null) {
                    gitSourceSecretName = sourceSecret.getName();
                }
            }
        }
        if (!foundExistingGitUrl) {
            log.warn("Could not find a git url in the loaded BuildConfig: " + buildConfig);
        }
        log.info("Loaded gitSourceSecretName: " + gitSourceSecretName);
    }
    log.info("gitUrl is: " + gitUrl);
    if (buildConfig == null) {
        buildConfig = new BuildConfig();
    }
    ObjectMeta metadata = getOrCreateMetadata(buildConfig);
    metadata.setName(projectName);
    metadata.setLabels(labels);
    putAnnotations(metadata, annotations);
    Map<String, String> currentAnnotations = metadata.getAnnotations();
    if (!currentAnnotations.containsKey(Annotations.Builds.GIT_CLONE_URL)) {
        currentAnnotations.put(Annotations.Builds.GIT_CLONE_URL, gitUrl);
    }
    String localGitUrl = getLocalGitUrl();
    if (!currentAnnotations.containsKey(Annotations.Builds.LOCAL_GIT_CLONE_URL) && Strings.isNotBlank(localGitUrl)) {
        currentAnnotations.put(Annotations.Builds.LOCAL_GIT_CLONE_URL, localGitUrl);
    }
    // lets switch to the local git URL to avoid DNS issues in forge or jenkins
    if (Strings.isNotBlank(localGitUrl)) {
        gitUrl = localGitUrl;
    }
    Builds.configureDefaultBuildConfig(buildConfig, name, gitUrl, foundExistingGitUrl, buildImageStream, buildImageTag, s2iCustomBuilderImage, secret, jenkinsUrl);
    try {
        getLog().info("About to apply build config: " + new JSONObject(KubernetesHelper.toJson(buildConfig)).toString(4));
        controller.applyBuildConfig(buildConfig, "maven");
        getLog().info("Created build configuration for " + name + " in namespace: " + controller.getNamespace() + " at " + kubernetes.getMasterUrl());
    } catch (Exception e) {
        getLog().error("Failed to create BuildConfig for " + KubernetesHelper.toJson(buildConfig) + ". " + e, e);
    }
    this.jenkinsJobName = name;
    if (isRegisterWebHooks()) {
        registerWebHooks();
        getLog().info("webhooks done");
    }
    if (modifiedConfig) {
        if (basedir == null) {
            getLog().error("Could not save updated " + ProjectConfigs.FILE_NAME + " due to missing basedir");
        } else {
            try {
                ProjectConfigs.saveToFolder(basedir, projectConfig, true);
                getLog().info("Updated " + ProjectConfigs.FILE_NAME);
            } catch (IOException e) {
                getLog().error("Could not save updated " + ProjectConfigs.FILE_NAME + ": " + e, e);
            }
        }
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) IOException(java.io.IOException) Controller(io.fabric8.kubernetes.api.Controller) GitBuildSource(io.fabric8.openshift.api.model.GitBuildSource) SAXException(org.xml.sax.SAXException) WebApplicationException(javax.ws.rs.WebApplicationException) AuthenticationException(org.apache.http.auth.AuthenticationException) ConnectException(java.net.ConnectException) MalformedChallengeException(org.apache.http.auth.MalformedChallengeException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BuildSource(io.fabric8.openshift.api.model.BuildSource) GitBuildSource(io.fabric8.openshift.api.model.GitBuildSource) JSONObject(org.json.JSONObject) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) LetsChatClient(io.fabric8.letschat.LetsChatClient) BuildConfigSpec(io.fabric8.openshift.api.model.BuildConfigSpec)

Example 3 with ProjectConfig

use of io.fabric8.devops.ProjectConfig in project fabric8 by fabric8io.

the class YamlTest method testParseYaml.

@Test
public void testParseYaml() throws Exception {
    String basedir = System.getProperty("basedir", ".");
    File file = new File(basedir, "src/test/resources/fabric8.yml");
    assertThat(file).exists();
    ProjectConfig config = ProjectConfigs.parseProjectConfig(file);
    System.out.println("Parsed: " + config);
    assertThat(config.getChatRoom()).isEqualTo("myroom");
    assertThat(config.getIssueProjectName()).isEqualTo("foo");
    assertThat(config.getPipeline()).isEqualTo("maven/CanaryReleaseThenStage.groovy");
    LinkedHashMap<String, String> environments = config.getEnvironments();
    // lets assert that things are in the correct order...
    List<Pair<String, String>> expectedEnvironents = Arrays.asList(new Pair<String, String>("Testing", "gogsadmin-james4-testing"), new Pair<String, String>("Staging", "gogsadmin-james4-staging"), new Pair<String, String>("Production", "gogsadmin-james4-prod"));
    int idx = 0;
    Set<Map.Entry<String, String>> entries = environments.entrySet();
    for (Map.Entry<String, String> entry : entries) {
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println("Found environment " + key + " = " + value);
        Pair<String, String> actual = new Pair<>(key, value);
        assertTrue("Too many entries - found unexpected value: " + actual, expectedEnvironents.size() > idx);
        Pair<String, String> expected = expectedEnvironents.get(idx++);
        assertEquals("environment " + idx, expected, actual);
    }
}
Also used : File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(io.fabric8.utils.Pair) Test(org.junit.Test)

Example 4 with ProjectConfig

use of io.fabric8.devops.ProjectConfig in project fabric8 by fabric8io.

the class DevOpsConnector method loadConfigFile.

protected void loadConfigFile() {
    if (projectConfig == null) {
        GitRepoClient gitRepo = getGitRepoClient();
        boolean hasLocalConfig = false;
        if (basedir != null && basedir.isDirectory()) {
            projectConfig = ProjectConfigs.loadFromFolder(basedir);
            if (!projectConfig.isEmpty() || ProjectConfigs.hasConfigFile(basedir)) {
                hasLocalConfig = true;
            }
        }
        if (!hasLocalConfig && tryLoadConfigFileFromRemoteGit && Strings.isNotBlank(repoName) && gitRepo != null) {
            try {
                InputStream input = gitRepo.getRawFile(username, repoName, branch, ProjectConfigs.FILE_NAME);
                if (input != null) {
                    try {
                        getLog().info("Parsing " + ProjectConfigs.FILE_NAME + " from the git repo " + repoName + " user " + username + " in branch " + branch);
                        projectConfig = ProjectConfigs.parseProjectConfig(input);
                    } catch (IOException e) {
                        getLog().warn("Failed to parse " + ProjectConfigs.FILE_NAME + " from the repo " + repoName + " for user " + username + " branch: " + branch + ". " + e, e);
                    }
                }
            } catch (Exception e) {
                getLog().warn("Failed to load " + ProjectConfigs.FILE_NAME + " from the repo " + repoName + " for user " + username + " branch: " + branch + ". " + e, e);
            }
        }
    }
    if (projectConfig != null) {
        String chatRoom = projectConfig.getChatRoom();
        if (Strings.isNotBlank(chatRoom)) {
            getLog().info("Found chat room: " + chatRoom);
            letschatRoomExpression = chatRoom;
        }
        String issueProjectName = projectConfig.getIssueProjectName();
        if (Strings.isNotBlank(issueProjectName)) {
            taigaProjectName = issueProjectName;
        }
    } else {
        getLog().info("No fabric8.yml file found for " + basedir);
    }
    if (Strings.isNullOrBlank(gitUrl)) {
        try {
            gitUrl = GitHelpers.extractGitUrl(basedir);
        } catch (IOException e) {
            getLog().warn("Could not load git URL from directory: " + e, e);
        }
    }
    if (Strings.isNullOrBlank(taigaProjectName)) {
        taigaProjectName = repoName;
    }
    if (Strings.isNullOrBlank(taigaProjectSlug)) {
        // TODO should we upper case it or anything?
        taigaProjectSlug = taigaProjectName;
    }
}
Also used : GitRepoClient(io.fabric8.repo.git.GitRepoClient) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) WebApplicationException(javax.ws.rs.WebApplicationException) AuthenticationException(org.apache.http.auth.AuthenticationException) ConnectException(java.net.ConnectException) MalformedChallengeException(org.apache.http.auth.MalformedChallengeException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 5 with ProjectConfig

use of io.fabric8.devops.ProjectConfig in project fabric8 by fabric8io.

the class DevOpsConnector method addLink.

public void addLink(String label, String url) {
    if (projectConfig == null) {
        projectConfig = new ProjectConfig();
    }
    projectConfig.addLink(label, url);
    modifiedConfig = true;
}
Also used : ProjectConfig(io.fabric8.devops.ProjectConfig)

Aggregations

ProjectConfig (io.fabric8.devops.ProjectConfig)3 File (java.io.File)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 AuthenticationException (org.apache.http.auth.AuthenticationException)2 MalformedChallengeException (org.apache.http.auth.MalformedChallengeException)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 SAXException (org.xml.sax.SAXException)2 Controller (io.fabric8.kubernetes.api.Controller)1 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 LetsChatClient (io.fabric8.letschat.LetsChatClient)1 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)1 BuildConfigSpec (io.fabric8.openshift.api.model.BuildConfigSpec)1 BuildSource (io.fabric8.openshift.api.model.BuildSource)1 GitBuildSource (io.fabric8.openshift.api.model.GitBuildSource)1