Search in sources :

Example 1 with UserDetails

use of io.fabric8.project.support.UserDetails in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method executeInternal.

@Override
public void executeInternal() throws MojoExecutionException, MojoFailureException {
    if (!basedir.isDirectory() || !basedir.exists()) {
        throw new MojoExecutionException("No directory for base directory: " + basedir);
    }
    // lets check for a git repo
    String gitRemoteURL = null;
    Repository repository = null;
    try {
        repository = GitUtils.findRepository(basedir);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to find local git repository in current directory: " + e, e);
    }
    try {
        gitRemoteURL = GitUtils.getRemoteAsHttpsURL(repository);
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to get the current git branch: " + e, e);
    }
    try {
        clusterAccess = new ClusterAccess(this.namespace);
        if (Strings.isNullOrBlank(projectName)) {
            projectName = basedir.getName();
        }
        KubernetesClient kubernetes = clusterAccess.createDefaultClient(log);
        KubernetesResourceUtil.validateKubernetesMasterUrl(kubernetes.getMasterUrl());
        String namespace = clusterAccess.getNamespace();
        OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinsShift(kubernetes, namespace);
        if (gitRemoteURL != null) {
            // lets check we don't already have this project imported
            String branch = repository.getBranch();
            BuildConfig buildConfig = findBuildConfigForGitRepo(openShiftClient, namespace, gitRemoteURL, branch);
            if (buildConfig != null) {
                logBuildConfigLink(kubernetes, namespace, buildConfig, log);
                throw new MojoExecutionException("Project already imported into build " + getName(buildConfig) + " for URI: " + gitRemoteURL + " and branch: " + branch);
            } else {
                Map<String, String> annotations = new HashMap<>();
                annotations.put(Annotations.Builds.GIT_CLONE_URL, gitRemoteURL);
                buildConfig = createBuildConfig(kubernetes, namespace, projectName, gitRemoteURL, annotations);
                openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
                ensureExternalGitSecretsAreSetupFor(kubernetes, namespace, gitRemoteURL);
                logBuildConfigLink(kubernetes, namespace, buildConfig, log);
            }
        } else {
            // lets create an import a new project
            UserDetails userDetails = createGogsUserDetails(kubernetes, namespace);
            userDetails.setBranch(branchName);
            BuildConfigHelper.CreateGitProjectResults createGitProjectResults;
            try {
                createGitProjectResults = BuildConfigHelper.importNewGitProject(kubernetes, userDetails, basedir, namespace, projectName, remoteName, "Importing project from mvn fabric8:import", false);
            } catch (WebApplicationException e) {
                Response response = e.getResponse();
                if (response.getStatus() > 400) {
                    String message = getEntityMessage(response);
                    log.warn("Could not create the git repository: %s %s", e, message);
                    log.warn("Are your username and password correct in the Secret %s/%s?", secretNamespace, gogsSecretName);
                    log.warn("To re-enter your password rerun this command with -Dfabric8.passsword.retry=true");
                    throw new MojoExecutionException("Could not create the git repository. " + "Are your username and password correct in the Secret " + secretNamespace + "/" + gogsSecretName + "?" + e + message, e);
                } else {
                    throw e;
                }
            }
            BuildConfig buildConfig = createGitProjectResults.getBuildConfig();
            openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
            logBuildConfigLink(kubernetes, namespace, buildConfig, log);
        }
    } catch (KubernetesClientException e) {
        KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) IOException(java.io.IOException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Response(javax.ws.rs.core.Response) Repository(org.eclipse.jgit.lib.Repository) UserDetails(io.fabric8.project.support.UserDetails) BuildConfigHelper(io.fabric8.project.support.BuildConfigHelper) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BuildConfigHelper.createBuildConfig(io.fabric8.project.support.BuildConfigHelper.createBuildConfig) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with UserDetails

use of io.fabric8.project.support.UserDetails in project fabric8 by fabric8io.

the class BuildConfigHelper method importNewGitProject.

public static CreateGitProjectResults importNewGitProject(KubernetesClient kubernetesClient, UserDetails userDetails, File basedir, String namespace, String projectName, String origin, String message, boolean apply, boolean useLocalGitAddress) throws GitAPIException, JsonProcessingException {
    GitUtils.disableSslCertificateChecks();
    InitCommand initCommand = Git.init();
    initCommand.setDirectory(basedir);
    Git git = initCommand.call();
    LOG.info("Initialised an empty git configuration repo at {}", basedir.getAbsolutePath());
    PersonIdent personIdent = userDetails.createPersonIdent();
    String user = userDetails.getUser();
    String address = userDetails.getAddress();
    String internalAddress = userDetails.getInternalAddress();
    String branch = userDetails.getBranch();
    // lets create the repository
    GitRepoClient repoClient = userDetails.createRepoClient();
    CreateRepositoryDTO createRepository = new CreateRepositoryDTO();
    createRepository.setName(projectName);
    String fullName = null;
    RepositoryDTO repository = repoClient.createRepository(createRepository);
    if (repository != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got repository: " + toJson(repository));
        }
        fullName = repository.getFullName();
    }
    if (Strings.isNullOrBlank(fullName)) {
        fullName = user + "/" + projectName;
    }
    String htmlUrl = URLUtils.pathJoin(resolveToRoot(address), user, projectName);
    String localCloneUrl = URLUtils.pathJoin(resolveToRoot(internalAddress), user, projectName + ".git");
    String cloneUrl = htmlUrl + ".git";
    String defaultCloneUrl = cloneUrl;
    // lets default to using the local git clone URL
    if (useLocalGitAddress && Strings.isNotBlank(internalAddress)) {
        defaultCloneUrl = localCloneUrl;
    }
    // now lets import the code and publish
    GitUtils.configureBranch(git, branch, origin, defaultCloneUrl);
    GitUtils.addDummyFileToEmptyFolders(basedir);
    LOG.info("About to git commit and push to: " + defaultCloneUrl + " and remote name " + origin);
    GitUtils.doAddCommitAndPushFiles(git, userDetails, personIdent, branch, origin, message, true);
    Map<String, String> annotations = new HashMap<>();
    annotations.put(Annotations.Builds.GIT_CLONE_URL, cloneUrl);
    annotations.put(Annotations.Builds.LOCAL_GIT_CLONE_URL, localCloneUrl);
    BuildConfig buildConfig;
    if (apply) {
        buildConfig = createAndApplyBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
    } else {
        buildConfig = createBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
    }
    return new CreateGitProjectResults(buildConfig, fullName, htmlUrl, localCloneUrl, cloneUrl);
}
Also used : Git(org.eclipse.jgit.api.Git) CreateRepositoryDTO(io.fabric8.repo.git.CreateRepositoryDTO) PersonIdent(org.eclipse.jgit.lib.PersonIdent) HashMap(java.util.HashMap) InitCommand(org.eclipse.jgit.api.InitCommand) GitRepoClient(io.fabric8.repo.git.GitRepoClient) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) RepositoryDTO(io.fabric8.repo.git.RepositoryDTO) CreateRepositoryDTO(io.fabric8.repo.git.CreateRepositoryDTO)

Example 3 with UserDetails

use of io.fabric8.project.support.UserDetails in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method createGogsUserDetails.

protected UserDetails createGogsUserDetails(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
    String url = getGogsURL(kubernetes, namespace);
    Secret secret = null;
    UserDetails userDetails;
    if (Strings.isNullOrBlank(url)) {
        return createGithubProjectAndUserDetails(kubernetes, namespace);
    }
    log.debug("Got gogs URL: " + url);
    String gogsSecretName = getGogsSecretName(namespace);
    if (Strings.isNullOrBlank(gitUserName) || Strings.isNullOrBlank(gitPassword)) {
        secret = findOrCreateGitSecret(kubernetes, gogsSecretName, GOGS_REPO_HOST);
    }
    if (Strings.isNullOrBlank(gitUserName)) {
        gitUserName = getGogsSecretField(kubernetes, secret, GOGS_REPO_HOST, "username");
    }
    if (Strings.isNullOrBlank(gitPassword)) {
        gitPassword = getGogsSecretField(kubernetes, secret, GOGS_REPO_HOST, "password");
    }
    if (Strings.isNullOrBlank(gitEmail)) {
        gitEmail = findEmailFromDotGitConfig();
    }
    createOrUpdateSecret(kubernetes, secret);
    ConfigMap configMap = getSecretGitConfigMap(kubernetes, namespace, secretNamespace);
    updateSecretGitConfigMap(kubernetes, secretNamespace, configMap, GOGS_REPO_HOST, gogsSecretName);
    log.info("git username: " + gitUserName + " password: " + hidePassword(gitPassword) + " email: " + gitEmail);
    return new UserDetails(url, url, gitUserName, gitPassword, gitEmail);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) UserDetails(io.fabric8.project.support.UserDetails) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Example 4 with UserDetails

use of io.fabric8.project.support.UserDetails in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method createGithubProjectAndUserDetails.

protected UserDetails createGithubProjectAndUserDetails(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
    log.info("Creating Project on GitHub");
    String url = "https://api.github.com";
    UserDetails userDetails = null;
    String secretNamespace = getSecretNamespace();
    ensureNamespaceExists(kubernetes, secretNamespace);
    ConfigMap configMap = getSecretGitConfigMap(kubernetes, namespace, secretNamespace);
    String host = convertToValidDnsLabel(currentUserName() + "-github.com");
    String currentSecretName = configMap.getData().get(host);
    if (currentSecretName == null) {
        currentSecretName = createGitSecretName(namespace, host);
    }
    Secret secret = findOrCreateGitSecret(kubernetes, currentSecretName, host);
    // FIXME: Rename the methods to be generic
    // if empty or retrying lets re-enter the user/pwd
    gitUserName = getGogsSecretField(kubernetes, secret, host, "username");
    gitPassword = getGogsSecretField(kubernetes, secret, host, "password");
    if (Strings.isNullOrBlank(gitEmail)) {
        gitEmail = findEmailFromDotGitConfig();
    }
    createOrUpdateSecret(kubernetes, secret);
    updateSecretGitConfigMap(kubernetes, secretNamespace, configMap, host, currentSecretName);
    return new UserDetails("https://api.github.com", "https://api.github.com", gitUserName, gitPassword, gitEmail);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) UserDetails(io.fabric8.project.support.UserDetails) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Aggregations

UserDetails (io.fabric8.project.support.UserDetails)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 Secret (io.fabric8.kubernetes.api.model.Secret)2 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)2 HashMap (java.util.HashMap)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 ClusterAccess (io.fabric8.maven.core.access.ClusterAccess)1 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)1 BuildConfigHelper (io.fabric8.project.support.BuildConfigHelper)1 BuildConfigHelper.createBuildConfig (io.fabric8.project.support.BuildConfigHelper.createBuildConfig)1 CreateRepositoryDTO (io.fabric8.repo.git.CreateRepositoryDTO)1 GitRepoClient (io.fabric8.repo.git.GitRepoClient)1 RepositoryDTO (io.fabric8.repo.git.RepositoryDTO)1 IOException (java.io.IOException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 PrompterException (org.codehaus.plexus.components.interactivity.PrompterException)1