Search in sources :

Example 1 with AdvancedArgument

use of com.openshift.jenkins.plugins.freestyle.model.AdvancedArgument in project jenkins-client-plugin by openshift.

the class BaseStep method runOcCommand.

protected boolean runOcCommand(final AbstractBuild build, final TaskListener listener, final String verb, final List verbArgs, final List userArgs, final List options, final OcProcessRunner runner) throws IOException, InterruptedException {
    final Map<String, String> overrides = consolidateEnvVars(listener, build, null);
    ClusterConfig c = getCluster(overrides);
    final String server, project, token, caContent;
    String selectedCAPath = "";
    boolean shouldSkipTLSVerify = false;
    ArrayList<String> advArgs = new ArrayList<String>();
    if (advancedArguments != null) {
        for (AdvancedArgument advArg : advancedArguments) {
            advArgs.add(advArg.getValue(overrides));
        }
    }
    if (c == null) {
        // if null, we assume the cluster is running the
        // Jenkins node.
        server = ClusterConfig.getHostClusterApiServerUrl();
        selectedCAPath = SERVICE_ACCOUNT_CA_PATH;
        caContent = null;
    } else {
        server = c.getServerUrl();
        if (c.isSkipTlsVerify()) {
            shouldSkipTLSVerify = true;
            caContent = null;
        } else {
            caContent = c.getServerCertificateAuthority();
        }
    }
    if (Strings.isNullOrEmpty(getProject(overrides))) {
        // for this step
        if (c != null) {
            // But a cluster definition was provided
            project = c.getDefaultProject();
            if (Strings.isNullOrEmpty(project)) {
                throw new IOException("No project defined in step or in cluster: " + getClusterName(overrides));
            }
        } else {
            project = new String(Files.readAllBytes(Paths.get(SERVICE_ACCOUNT_NAMESPACE_PATH)), StandardCharsets.UTF_8);
        }
    } else {
        project = this.getProject(overrides);
    }
    String actualCredentialsId = getCredentialsId(overrides);
    if (Strings.isNullOrEmpty(actualCredentialsId)) {
        // step.
        if (c != null) {
            // But a cluster definition was found
            actualCredentialsId = c.getCredentialsId();
            if (Strings.isNullOrEmpty(actualCredentialsId)) {
                throw new IOException("No credentials defined in step or in cluster: " + getClusterName(overrides));
            }
        }
    }
    if (!Strings.isNullOrEmpty(actualCredentialsId)) {
        OpenShiftTokenCredentials tokenSecret = CredentialsProvider.findCredentialById(actualCredentialsId, OpenShiftTokenCredentials.class, build, new ArrayList<DomainRequirement>());
        if (tokenSecret == null) {
            throw new IOException("Unable to find credential in Jenkins credential store: " + actualCredentialsId);
        }
        token = tokenSecret.getToken();
    } else {
        // We are running within a host cluster, so use mounted secret
        token = new String(Files.readAllBytes(Paths.get(SERVICE_ACCOUNT_TOKEN_PATH)), StandardCharsets.UTF_8);
    }
    final String finalSelectedCAPath = selectedCAPath;
    final boolean finalShouldSkipTLSVerify = shouldSkipTLSVerify;
    final List finalAdvArgs = advArgs;
    return withTempInput("serviceca", caContent, new WithTempInputRunnable() {

        @Override
        public boolean perform(String filename) throws IOException, InterruptedException {
            if (filename == null) {
                // this will be null if we are
                // running within the cluster or
                // TLS verify is disabled
                filename = finalSelectedCAPath;
            }
            final ClientCommandBuilder cmdBuilder = new ClientCommandBuilder(server, project, finalShouldSkipTLSVerify, filename, verb, finalAdvArgs, verbArgs, userArgs, options, token, Integer.parseInt(getLogLevel(overrides)));
            ProcessBuilder pb = new ProcessBuilder();
            pb.command(cmdBuilder.buildCommand(false));
            listener.getLogger().println("Executing: " + cmdBuilder.asString(true));
            return runner.perform(pb);
        }
    });
}
Also used : DomainRequirement(com.cloudbees.plugins.credentials.domains.DomainRequirement) ClientCommandBuilder(com.openshift.jenkins.plugins.util.ClientCommandBuilder) ArrayList(java.util.ArrayList) AdvancedArgument(com.openshift.jenkins.plugins.freestyle.model.AdvancedArgument) IOException(java.io.IOException) OpenShiftTokenCredentials(com.openshift.jenkins.plugins.OpenShiftTokenCredentials) ArrayList(java.util.ArrayList) List(java.util.List) ClusterConfig(com.openshift.jenkins.plugins.ClusterConfig)

Aggregations

DomainRequirement (com.cloudbees.plugins.credentials.domains.DomainRequirement)1 ClusterConfig (com.openshift.jenkins.plugins.ClusterConfig)1 OpenShiftTokenCredentials (com.openshift.jenkins.plugins.OpenShiftTokenCredentials)1 AdvancedArgument (com.openshift.jenkins.plugins.freestyle.model.AdvancedArgument)1 ClientCommandBuilder (com.openshift.jenkins.plugins.util.ClientCommandBuilder)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1