All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.oracle.bedrock.runtime.k8s.helm.HelmCommand Maven / Gradle / Ivy

The newest version!
/*
 * File: HelmCommand.java
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * The contents of this file are subject to the terms and conditions of 
 * the Common Development and Distribution License 1.0 (the "License").
 *
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the License by consulting the LICENSE.txt file
 * distributed with this file, or by consulting https://oss.oracle.com/licenses/CDDL
 *
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file LICENSE.txt.
 *
 * MODIFICATIONS:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 */

package com.oracle.bedrock.runtime.k8s.helm;

import com.oracle.bedrock.Option;
import com.oracle.bedrock.OptionsByType;
import com.oracle.bedrock.runtime.Application;
import com.oracle.bedrock.runtime.LocalPlatform;
import com.oracle.bedrock.runtime.Platform;
import com.oracle.bedrock.runtime.SimpleApplication;
import com.oracle.bedrock.runtime.options.Arguments;
import com.oracle.bedrock.runtime.options.EnvironmentVariables;
import com.oracle.bedrock.runtime.options.Executable;

import java.io.File;
import java.net.URL;
import java.util.Objects;

/**
 * An immutable representation of a Helm command that can be executed
 * by a Bedrock {@link Platform}.
 * 

* Copyright (c) 2018. All Rights Reserved. Oracle Corporation.
* Oracle is a registered trademark of Oracle Corporation and/or its affiliates. * * @param the type of the command * * @author Jonathan Knight */ public abstract class HelmCommand implements CLI { /** * The default location of the Helm executable. */ public static final String DEFAULT_HELM = System.getProperty("bedrock.helm", "/usr/local/bin/helm"); /** * The Helm executable to run when executing this command. */ private final String helmExecutable; /** * The Helm command to execute. */ private final String[] commands; /** * The arguments to this command. */ private final Arguments arguments; /** * The flags to this command. */ private final Arguments flags; /** * The environment variables to this command. */ private final EnvironmentVariables environmentVariables; /** * {@code true} if flags come before arguments in the command line for this command. */ private final boolean flagsFirst; /** * Create a Helm command. * * @param helmExecutable the Helm executable to invoke * @param arguments the command arguments * @param flags the command flags * @param env the environment variables to apply to the command execution * @param flagsFirst {@code true} to indicate that flags come before arguments * @param commands the Helm command to execute */ HelmCommand(String helmExecutable, Arguments arguments, Arguments flags, EnvironmentVariables env, boolean flagsFirst, String... commands) { this.helmExecutable = helmExecutable == null || helmExecutable.trim().isEmpty() ? DEFAULT_HELM : helmExecutable; this.arguments = Objects.requireNonNull(arguments); this.flags = Objects.requireNonNull(flags); this.environmentVariables = Objects.requireNonNull(env); this.flagsFirst = flagsFirst; this.commands = Objects.requireNonNull(commands); } /** * Obtain the Helm executable to run. * * @return the Helm executable to run */ public String getHelmLocation() { return helmExecutable == null ? DEFAULT_HELM : helmExecutable; } /** * Determine whether the Helm executable that this command * would use exists. * * @return {@code true} if the Helm executable exists */ public boolean helmExists() { File file = new File(helmExecutable); return file.exists() && file.canExecute(); } /** * Obtain the Helm command that will be executed. * * @return the Helm command that will be executed */ public String[] getCommands() { return commands; } /** * Obtain the arguments to this command. * * @return the arguments to this command */ public Arguments getArguments() { return arguments; } /** * Obtain the environment variables for this command. * * @return the environment variables for this command */ public EnvironmentVariables getEnvironment() { return environmentVariables; } /** * Obtain the flags to this command. * * @return the flags to this command */ public Arguments getFlags() { return flags; } /** * Determine whether the flags appear at the start or end of the command line. * * @return {@code true} if the flags appear at the start or end of * the command line */ public boolean isFlagsFirst() { return flagsFirst; } /** * Execute this Helm command on the {@link LocalPlatform}. * * @param options any options to apply to the process * * @return the {@link Application} representing the Helm command execution */ public Application execute(Option... options) { return LocalPlatform.get().launch(this, options); } /** * Execute this Helm command on the {@link LocalPlatform} and wait for the command to complete. * * @param options any options to apply to the process * * @return the return code from the completed command. */ public int executeAndWait(Option... options) { try (Application app = LocalPlatform.get().launch(this, options)) { return app.waitFor(options); } } @Override public Class getImplementationClass(Platform platform, OptionsByType options) { return SimpleApplication.class; } @Override public void onLaunching(Platform platform, OptionsByType optionsByType) { optionsByType.add(Executable.named(getHelmLocation())); Arguments args = optionsByType.get(Arguments.class); for (String cmd : commands) { args = args.with(cmd); } if (flagsFirst) { args = args.with(flags) .with(arguments); } else { args = args.with(arguments) .with(flags); } optionsByType.add(args); if (environmentVariables != null) { EnvironmentVariables env = optionsByType.get(EnvironmentVariables.class); optionsByType.add(env.with(environmentVariables)); } } @Override public void onLaunch(Platform platform, OptionsByType optionsByType) { // there is nothing to do here } @Override public void onLaunched(Platform platform, Application application, OptionsByType optionsByType) { // there is nothing to do here } /** * A generic Helm command. */ public static class Template extends HelmCommand