org.apache.brooklyn.entity.software.base.AbstractSoftwareProcessWinRmDriver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-software-base Show documentation
Show all versions of brooklyn-software-base Show documentation
Base classes for Brooklyn software process entities
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.brooklyn.entity.software.base;
import static org.apache.brooklyn.util.JavaGroovyEquivalents.elvis;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import javax.xml.ws.WebServiceException;
import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.mgmt.Task;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
import org.apache.brooklyn.core.entity.Entities;
import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.entity.software.base.lifecycle.NativeWindowsScriptRunner;
import org.apache.brooklyn.entity.software.base.lifecycle.WinRmExecuteHelper;
import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
import org.apache.brooklyn.util.core.internal.winrm.WinRmTool;
import org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse;
import org.apache.brooklyn.util.core.mutex.WithMutexes;
import org.apache.brooklyn.util.core.task.Tasks;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.exceptions.ReferenceWithError;
import org.apache.brooklyn.util.repeat.Repeater;
import org.apache.brooklyn.util.stream.Streams;
import org.apache.brooklyn.util.text.Strings;
import org.apache.brooklyn.util.time.Duration;
import org.apache.cxf.interceptor.Fault;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
public abstract class AbstractSoftwareProcessWinRmDriver extends AbstractSoftwareProcessDriver implements NativeWindowsScriptRunner {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSoftwareProcessWinRmDriver.class);
AttributeSensor WINDOWS_USERNAME = Sensors.newStringSensor("windows.username",
"Default Windows username to be used when connecting to the Entity's VM");
AttributeSensor WINDOWS_PASSWORD = Sensors.newStringSensor("windows.password",
"Default Windows password to be used when connecting to the Entity's VM");
public AbstractSoftwareProcessWinRmDriver(EntityLocal entity, WinRmMachineLocation location) {
super(entity, location);
entity.sensors().set(WINDOWS_USERNAME, location.config().get(WinRmMachineLocation.USER));
entity.sensors().set(WINDOWS_PASSWORD, location.config().get(WinRmMachineLocation.PASSWORD));
}
/** @see #newScript(Map, String) */
protected WinRmExecuteHelper newScript(String phase) {
return newScript(Maps.newLinkedHashMap(), phase);
}
protected WinRmExecuteHelper newScript(String command, String psCommand, String phase) {
return newScript(command, psCommand, phase, null);
}
protected WinRmExecuteHelper newScript(String command, String psCommand, String phase, String ntDomain) {
return newScript(phase)
.setNtDomain(ntDomain)
.setCommand(command)
.setPsCommand(psCommand)
.failOnNonZeroResultCode()
.gatherOutput();
}
protected WinRmExecuteHelper newScript(Map flags, String phase) {
if (!Entities.isManaged(getEntity()))
throw new IllegalStateException(getEntity() + " is no longer managed; cannot create script to run here (" + phase + ")");
WinRmExecuteHelper s = new WinRmExecuteHelper(this, phase + " " + elvis(entity, this));
return s;
}
@Override
public void runPreInstallCommand() {
if (Strings.isNonBlank(getEntity().getConfig(BrooklynConfigKeys.PRE_INSTALL_COMMAND)) || Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND))) {
newScript(
getEntity().getConfig(BrooklynConfigKeys.PRE_INSTALL_COMMAND),
getEntity().getConfig(VanillaWindowsProcess.PRE_INSTALL_POWERSHELL_COMMAND),
"pre-install-command")
.useMutex(getLocation().mutexes(), "installation lock at host", "installing "+elvis(entity,this))
.execute();
}
if (entity.getConfig(VanillaWindowsProcess.PRE_INSTALL_REBOOT_REQUIRED)) {
rebootAndWait();
}
}
@Override
public void setup() {
// Default to no-op
}
@Override
public void runPostInstallCommand() {
if (Strings.isNonBlank(entity.getConfig(BrooklynConfigKeys.POST_INSTALL_COMMAND)) || Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND))) {
newScript(
getEntity().getConfig(BrooklynConfigKeys.POST_INSTALL_COMMAND),
getEntity().getConfig(VanillaWindowsProcess.POST_INSTALL_POWERSHELL_COMMAND),
"post-install-command")
.useMutex(getLocation().mutexes(), "installation lock at host", "installing "+elvis(entity,this))
.execute();
}
}
@Override
public void runPreCustomizeCommand() {
if (Strings.isNonBlank(getEntity().getConfig(BrooklynConfigKeys.PRE_CUSTOMIZE_COMMAND)) || Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.PRE_CUSTOMIZE_POWERSHELL_COMMAND))) {
executeCommandInTask(
getEntity().getConfig(BrooklynConfigKeys.PRE_CUSTOMIZE_COMMAND),
getEntity().getConfig(VanillaWindowsProcess.PRE_CUSTOMIZE_POWERSHELL_COMMAND),
"pre-customize-command");
}
}
@Override
public void runPostCustomizeCommand() {
if (Strings.isNonBlank(entity.getConfig(BrooklynConfigKeys.POST_CUSTOMIZE_COMMAND)) || Strings.isNonBlank(getEntity().getConfig(VanillaWindowsProcess.POST_CUSTOMIZE_POWERSHELL_COMMAND))) {
executeCommandInTask(
getEntity().getConfig(BrooklynConfigKeys.POST_CUSTOMIZE_COMMAND),
getEntity().getConfig(VanillaWindowsProcess.POST_CUSTOMIZE_POWERSHELL_COMMAND),
"post-customize-command");
}
}
@Override
public void runPreLaunchCommand() {
if (Strings.isNonBlank(entity.getConfig(BrooklynConfigKeys.PRE_LAUNCH_COMMAND)) || Strings.isNonBlank(entity.getConfig(VanillaWindowsProcess.PRE_LAUNCH_POWERSHELL_COMMAND))) {
executeCommandInTask(
getEntity().getConfig(BrooklynConfigKeys.PRE_LAUNCH_COMMAND),
getEntity().getConfig(VanillaWindowsProcess.PRE_LAUNCH_POWERSHELL_COMMAND),
"pre-launch-command");
}
}
@Override
public void runPostLaunchCommand() {
if (Strings.isNonBlank(entity.getConfig(BrooklynConfigKeys.POST_LAUNCH_COMMAND)) || Strings.isNonBlank(entity.getConfig(VanillaWindowsProcess.POST_LAUNCH_POWERSHELL_COMMAND))) {
executeCommandInTask(
getEntity().getConfig(BrooklynConfigKeys.POST_LAUNCH_COMMAND),
getEntity().getConfig(VanillaWindowsProcess.POST_LAUNCH_POWERSHELL_COMMAND),
"post-launch-command");
}
}
@Override
public void copyPreInstallResources() {
final WithMutexes mutexSupport = getLocation().mutexes();
String mutexId = "installation lock at host";
mutexSupport.acquireMutex(mutexId, "pre-installing " + elvis(entity, this));
try {
super.copyPreInstallResources();
} catch (Exception e) {
LOG.warn("Error copying pre-install resources", e);
throw Exceptions.propagate(e);
} finally {
mutexSupport.releaseMutex(mutexId);
}
}
@Override
public void copyInstallResources() {
final WithMutexes mutexSupport = getLocation().mutexes();
String mutexId = "installation lock at host";
mutexSupport.acquireMutex(mutexId, "installing " + elvis(entity, this));
try {
super.copyInstallResources();
} catch (Exception e) {
LOG.warn("Error copying install resources", e);
throw Exceptions.propagate(e);
} finally {
mutexSupport.releaseMutex(mutexId);
}
}
@Override
public void copyCustomizeResources() {
final WithMutexes mutexSupport = getLocation().mutexes();
String mutexId = "installation lock at host";
mutexSupport.acquireMutex(mutexId, "customizing " + elvis(entity, this));
try {
super.copyCustomizeResources();
} catch (Exception e) {
LOG.warn("Error copying customize resources", e);
throw Exceptions.propagate(e);
} finally {
mutexSupport.releaseMutex(mutexId);
}
}
@Override
public WinRmMachineLocation getLocation() {
return (WinRmMachineLocation)super.getLocation();
}
public WinRmMachineLocation getMachine() {
return getLocation();
}
protected int executeCommandInTask(String command, String psCommand, String phase) {
return executeCommandInTask(command, psCommand, phase, null);
}
protected int executeCommandInTask(String command, String psCommand, String phase, String ntDomain) {
WinRmExecuteHelper helper = newScript(command, psCommand, phase, ntDomain);
return helper.execute();
}
@Override
public int executeNativeCommand(Map flags, String command, String phase) {
return executeNativeOrPsCommand(flags, command, null, phase, true);
}
@Override
public int executePsCommand(Map flags, String command, String phase) {
return executeNativeOrPsCommand(flags, null, command, phase, true);
}
/**
* @deprecated since 0.5.0; instead rely on {@link org.apache.brooklyn.api.entity.drivers.downloads.DownloadResolverManager} to inc
*
*
* {@code
* DownloadResolver resolver = Entities.newDownloader(this);
* List urls = resolver.getTargets();
* }
*
*/
@Deprecated
protected String getEntityVersionLabel() {
return getEntityVersionLabel("_");
}
/**
* @deprecated since 0.5.0; instead rely on {@link org.apache.brooklyn.api.entity.drivers.downloads.DownloadResolverManager} to inc
*/
@Deprecated
protected String getEntityVersionLabel(String separator) {
return elvis(entity.getEntityType().getSimpleName(),
entity.getClass().getName())+(getVersion() != null ? separator+getVersion() : "");
}
@Override
public String getRunDir() {
// TODO: This needs to be tidied, and read from the appropriate flags (if set)
return "$HOME\\brooklyn-managed-processes\\apps\\" + entity.getApplicationId() + "\\entities\\" + getEntityVersionLabel()+"_"+entity.getId();
}
@Override
public String getInstallDir() {
// TODO: This needs to be tidied, and read from the appropriate flags (if set)
return "$HOME\\brooklyn-managed-processes\\installs\\" + entity.getApplicationId() + "\\" + getEntityVersionLabel()+"_"+entity.getId();
}
@Override
public int copyResource(Map