001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2017-2018 microBean.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.helm.maven;
018
019import java.util.Objects;
020
021import java.util.concurrent.Callable;
022import java.util.concurrent.Future;
023
024import hapi.release.ReleaseOuterClass.Release;
025
026import hapi.services.tiller.Tiller.UninstallReleaseRequest;
027import hapi.services.tiller.Tiller.UninstallReleaseResponse;
028
029import org.apache.maven.plugin.logging.Log;
030
031import org.apache.maven.plugins.annotations.Mojo;
032import org.apache.maven.plugins.annotations.Parameter;
033
034import org.microbean.helm.ReleaseManager;
035
036/**
037 * Uninstalls a release.
038 *
039 * @author <a href="https://about.me/lairdnelson"
040 * target="_parent">Laird Nelson</a>
041 */
042@Mojo(name = "uninstall")
043public class UninstallReleaseMojo extends AbstractSingleReleaseMojo {
044
045
046  /*
047   * Instance fields.
048   */
049
050
051  /**
052   * Whether <a
053   * href="https://github.com/kubernetes/helm/blob/master/docs/charts_hooks.md#hooks">release
054   * hooks</a> should be disabled.
055   */
056  @Parameter(defaultValue = "false")
057  private boolean disableHooks;
058
059  /**
060   * The default timeout, in seconds, to use for Kubernetes
061   * operations; set to {@code 300} by default for parity with the
062   * {@code helm} command line program.
063   */
064  @Parameter(defaultValue = "300")
065  private long timeout; // in seconds
066
067  /**
068   * Whether the release being uninstalled should be purged entirely
069   * instead of being marked as deleted.
070   */
071  @Parameter(defaultValue = "false")
072  private boolean purge;
073
074
075  /*
076   * Constructors.
077   */
078
079
080  /**
081   * Creates a new {@link UninstallReleaseMojo}.
082   */
083  public UninstallReleaseMojo() {
084    super();
085  }
086
087
088  /*
089   * Instance methods.
090   */
091  
092
093  /**
094   * {@inheritDoc}
095   *
096   * <p>This implementation <a
097   * href="https://docs.helm.sh/using_helm/#helm-delete-deleting-a-release">uninstalls</a>
098   * the release with the {@linkplain #getReleaseName() supplied
099   * release name}.</p>
100   */
101  @Override
102  protected void execute(final Callable<ReleaseManager> releaseManagerCallable) throws Exception {
103    Objects.requireNonNull(releaseManagerCallable);
104    final Log log = this.getLog();
105    assert log != null;
106
107    final UninstallReleaseRequest.Builder requestBuilder = UninstallReleaseRequest.newBuilder();
108    assert requestBuilder != null;
109
110    requestBuilder.setDisableHooks(this.getDisableHooks());
111
112    final String releaseName = this.getReleaseName();
113    if (releaseName != null) {
114      requestBuilder.setName(releaseName);
115    }
116
117    requestBuilder.setPurge(this.getPurge());
118    requestBuilder.setTimeout(this.getTimeout());
119
120    final ReleaseManager releaseManager = releaseManagerCallable.call();
121    if (releaseManager == null) {
122      throw new IllegalStateException("releaseManagerCallable.call() == null");
123    }
124
125    if (log.isInfoEnabled()) {
126      log.info("Uninstalling release " + requestBuilder.getName());
127    }
128    final Future<UninstallReleaseResponse> uninstallReleaseResponseFuture = releaseManager.uninstall(requestBuilder.build());
129    assert uninstallReleaseResponseFuture != null;
130    final UninstallReleaseResponse uninstallReleaseResponse = uninstallReleaseResponseFuture.get();
131    assert uninstallReleaseResponse != null;
132    if (log.isInfoEnabled()) {
133      final Release release = uninstallReleaseResponse.getRelease();
134      assert release != null;
135      log.info("Uninstalled release " + release.getName());
136    }
137    
138  }
139
140  /**
141   * Returns {@code true} if <a
142   * href="https://github.com/kubernetes/helm/blob/master/docs/charts_hooks.md#hooks">chart
143   * hooks</a> are disabled.
144   *
145   * @return {@code true} if <a
146   * href="https://github.com/kubernetes/helm/blob/master/docs/charts_hooks.md#hooks">chart
147   * hooks</a> are disabled
148   *
149   * @see #setDisableHooks(boolean)
150   */
151  public boolean getDisableHooks() {
152    return this.disableHooks;
153  }
154
155  /**
156   * Sets whether <a
157   * href="https://github.com/kubernetes/helm/blob/master/docs/charts_hooks.md#hooks">chart
158   * hooks</a> are disabled.
159   *
160   * @param disableHooks if {@code true}, <a
161   * href="https://github.com/kubernetes/helm/blob/master/docs/charts_hooks.md#hooks">chart
162   * hooks</a> will be disabled
163   *
164   * @see #getDisableHooks()
165   */
166  public void setDisableHooks(final boolean disableHooks) {
167    this.disableHooks = disableHooks;
168  }
169
170  /**
171   * Returns whether the release being uninstalled should be purged entirely
172   * instead of being marked as deleted.
173   *
174   * @return {@code true} if the release being uninstalled should be
175   * purged entirely instead of being marked as deleted; {@code false}
176   * if it should be marked as deleted
177   *
178   * @see #setPurge(boolean)
179   */
180  public boolean getPurge() {
181    return this.purge;
182  }
183
184  /**
185   * Sets whether the release being uninstalled should be purged
186   * entirely instead of being marked as deleted.
187   *
188   * @param purge if {@code true} the release being uninstalled will
189   * be purged entirely
190   *
191   * @see #getPurge()
192   */
193  public void setPurge(final boolean purge) {
194    this.purge = purge;
195  }
196
197  /**
198   * Returns the timeout value, in seconds, for Kubernetes operations.
199   *
200   * @return the timeout value, in seconds, for Kubernetes operations
201   *
202   * @see #setTimeout(long)
203   */
204  public long getTimeout() {
205    return this.timeout;
206  }
207
208  /**
209   * Sets the timeout value, in seconds, for Kubernetes operations.
210   *
211   * @param timeoutInSeconds the timeout value, in seconds, for Kubernetes
212   * operations
213   *
214   * @see #getTimeout()
215   */
216  public void setTimeout(final long timeoutInSeconds) {
217    this.timeout = timeoutInSeconds;
218  }
219
220}