Search in sources :

Example 6 with SSHKeyPair

use of io.cdap.cdap.runtime.spi.ssh.SSHKeyPair in project cdap by caskdata.

the class ExistingDataprocProvisioner method createCluster.

@Override
public Cluster createCluster(ProvisionerContext context) throws Exception {
    Map<String, String> contextProperties = createContextProperties(context);
    DataprocConf conf = DataprocConf.create(contextProperties);
    if (context.getRuntimeMonitorType() == RuntimeMonitorType.SSH) {
        String sshUser = contextProperties.get(SSH_USER);
        String sshKey = contextProperties.get(SSH_KEY);
        if (Strings.isNullOrEmpty(sshUser) || Strings.isNullOrEmpty(sshKey)) {
            throw new DataprocRuntimeException("SSH User and key are required for monitoring through SSH.");
        }
        SSHKeyPair sshKeyPair = new SSHKeyPair(new SSHPublicKey(sshUser, ""), () -> sshKey.getBytes(StandardCharsets.UTF_8));
        // The ssh context shouldn't be null, but protect it in case there is platform bug
        Optional.ofNullable(context.getSSHContext()).ifPresent(c -> c.setSSHKeyPair(sshKeyPair));
    }
    String clusterName = contextProperties.get(CLUSTER_NAME);
    try (DataprocClient client = DataprocClient.fromConf(conf, false)) {
        try {
            client.updateClusterLabels(clusterName, getSystemLabels());
        } catch (DataprocRuntimeException e) {
            // Only log the stacktrace if trace log level is enabled
            if (LOG.isTraceEnabled()) {
                LOG.trace("Cannot update cluster labels due to {}", e.getMessage(), e);
            } else {
                LOG.debug("Cannot update cluster labels due to {}", e.getMessage());
            }
        }
        return client.getCluster(clusterName).filter(c -> c.getStatus() == ClusterStatus.RUNNING).orElseThrow(() -> new DataprocRuntimeException("Dataproc cluster " + clusterName + " does not exist or not in running state."));
    }
}
Also used : RuntimeMonitorType(io.cdap.cdap.runtime.spi.RuntimeMonitorType) PollingStrategies(io.cdap.cdap.runtime.spi.provisioner.PollingStrategies) Logger(org.slf4j.Logger) Cluster(io.cdap.cdap.runtime.spi.provisioner.Cluster) SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) ProvisionerContext(io.cdap.cdap.runtime.spi.provisioner.ProvisionerContext) ProvisionerSpecification(io.cdap.cdap.runtime.spi.provisioner.ProvisionerSpecification) LoggerFactory(org.slf4j.LoggerFactory) ClusterStatus(io.cdap.cdap.runtime.spi.provisioner.ClusterStatus) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Strings(com.google.common.base.Strings) PollingStrategy(io.cdap.cdap.runtime.spi.provisioner.PollingStrategy) Map(java.util.Map) SSHPublicKey(io.cdap.cdap.runtime.spi.ssh.SSHPublicKey) Optional(java.util.Optional) SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) SSHPublicKey(io.cdap.cdap.runtime.spi.ssh.SSHPublicKey)

Example 7 with SSHKeyPair

use of io.cdap.cdap.runtime.spi.ssh.SSHKeyPair in project cdap by caskdata.

the class ElasticMapReduceProvisioner method createCluster.

@Override
public Cluster createCluster(ProvisionerContext context) throws Exception {
    // Generates and set the ssh key
    // or 'hadoop'
    SSHKeyPair sshKeyPair = context.getSSHContext().generate("ec2-user");
    context.getSSHContext().setSSHKeyPair(sshKeyPair);
    EMRConf conf = EMRConf.fromProvisionerContext(context);
    String clusterName = getClusterName(context.getProgramRunInfo());
    try (EMRClient client = EMRClient.fromConf(conf)) {
        // if it already exists, it means this is a retry. We can skip actually making the request
        Optional<ClusterSummary> existing = client.getUnterminatedClusterByName(clusterName);
        if (existing.isPresent()) {
            return client.getCluster(existing.get().getId()).get();
        }
        String clusterId = client.createCluster(clusterName);
        return new Cluster(clusterId, ClusterStatus.CREATING, Collections.emptyList(), Collections.emptyMap());
    }
}
Also used : SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) ClusterSummary(com.amazonaws.services.elasticmapreduce.model.ClusterSummary) Cluster(io.cdap.cdap.runtime.spi.provisioner.Cluster)

Example 8 with SSHKeyPair

use of io.cdap.cdap.runtime.spi.ssh.SSHKeyPair in project cdap by caskdata.

the class RemoteHadoopConf method fromProperties.

/**
 * Create the conf from a property map while also performing validation.
 */
public static RemoteHadoopConf fromProperties(Map<String, String> properties) {
    String host = getString(properties, "host");
    String user = getString(properties, "user");
    String privateKey = getString(properties, "sshKey");
    SSHKeyPair keyPair = new SSHKeyPair(new SSHPublicKey(user, ""), () -> privateKey.getBytes(StandardCharsets.UTF_8));
    return new RemoteHadoopConf(keyPair, host, properties.get("initializationAction"), properties.get("kerberosPrincipal"), properties.get("kerberosKeytabPath"));
}
Also used : SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) SSHPublicKey(io.cdap.cdap.runtime.spi.ssh.SSHPublicKey)

Example 9 with SSHKeyPair

use of io.cdap.cdap.runtime.spi.ssh.SSHKeyPair in project cdap by caskdata.

the class ProvisioningService method createDeprovisionTask.

private Runnable createDeprovisionTask(ProvisioningTaskInfo taskInfo, Provisioner provisioner, Consumer<ProgramRunId> taskCleanup) {
    Map<String, String> properties = taskInfo.getProvisionerProperties();
    ProvisionerContext context;
    SSHKeyPair sshKeyPair = null;
    try {
        sshKeyPair = createSSHKeyPair(taskInfo);
    } catch (IOException e) {
        LOG.warn("Failed to load ssh key. No SSH key will be available for the deprovision task", e);
    }
    ProgramRunId programRunId = taskInfo.getProgramRunId();
    Map<String, String> systemArgs = taskInfo.getProgramOptions().getArguments().asMap();
    try {
        SSHContext sshContext = new DefaultSSHContext(Networks.getAddress(cConf, Constants.NETWORK_PROXY_ADDRESS), null, sshKeyPair);
        context = createContext(cConf, taskInfo.getProgramOptions(), programRunId, taskInfo.getUser(), properties, sshContext);
    } catch (InvalidMacroException e) {
        runWithProgramLogging(programRunId, systemArgs, () -> LOG.error("Could not evaluate macros while deprovisoning. " + "The cluster will be marked as orphaned.", e));
        provisionerNotifier.orphaned(programRunId);
        return () -> {
        };
    }
    DeprovisionTask task = new DeprovisionTask(taskInfo, transactionRunner, 300, provisioner, context, provisionerNotifier, locationFactory);
    ProvisioningTaskKey taskKey = new ProvisioningTaskKey(programRunId, ProvisioningOp.Type.DEPROVISION);
    return () -> taskExecutor.submit(taskKey, () -> callWithProgramLogging(programRunId, systemArgs, () -> {
        try {
            long delay = task.executeOnce();
            if (delay < 0) {
                taskCleanup.accept(programRunId);
            }
            return delay;
        } catch (InterruptedException e) {
            // We can get interrupted if the task is cancelled or CDAP is stopped. In either case, just return.
            // If it was cancelled, state cleanup is left to the caller. If it was CDAP master stopping, the task
            // will be resumed on master startup
            LOG.debug("Deprovision task for program run {} interrupted.", programRunId);
            throw e;
        } catch (Exception e) {
            // Otherwise, if there was an error deprovisioning, run the cleanup
            LOG.info("Deprovision task for program run {} failed.", programRunId, e);
            taskCleanup.accept(programRunId);
            throw e;
        }
    }));
}
Also used : SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) SSHContext(io.cdap.cdap.runtime.spi.ssh.SSHContext) IOException(java.io.IOException) InvalidMacroException(io.cdap.cdap.api.macro.InvalidMacroException) NotFoundException(io.cdap.cdap.common.NotFoundException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) RetryableProvisionException(io.cdap.cdap.runtime.spi.provisioner.RetryableProvisionException) IOException(java.io.IOException) InvalidMacroException(io.cdap.cdap.api.macro.InvalidMacroException) ProvisionerContext(io.cdap.cdap.runtime.spi.provisioner.ProvisionerContext) DeprovisionTask(io.cdap.cdap.internal.provision.task.DeprovisionTask) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId)

Example 10 with SSHKeyPair

use of io.cdap.cdap.runtime.spi.ssh.SSHKeyPair in project cdap by caskdata.

the class ProvisioningService method createSSHKeyPair.

/**
 * Creates a {@link SSHKeyPair} based on the given {@link ProvisioningTaskInfo}.
 *
 * @param taskInfo the task info containing information about the ssh keys
 * @return a {@link SSHKeyPair} or {@code null} if ssh key information are not present in the task info
 */
@Nullable
private SSHKeyPair createSSHKeyPair(ProvisioningTaskInfo taskInfo) throws IOException {
    // Check if there is ssh user property in the Cluster
    String sshUser = Optional.ofNullable(taskInfo.getCluster()).map(Cluster::getProperties).map(p -> p.get(Constants.RuntimeMonitor.SSH_USER)).orElse(null);
    if (sshUser == null) {
        return null;
    }
    Location keysDir = locationFactory.create(taskInfo.getSecureKeysDir());
    Location publicKeyLocation = keysDir.append(Constants.RuntimeMonitor.PUBLIC_KEY);
    Location privateKeyLocation = keysDir.append(Constants.RuntimeMonitor.PRIVATE_KEY);
    if (!publicKeyLocation.exists() || !privateKeyLocation.exists()) {
        return null;
    }
    return new LocationBasedSSHKeyPair(keysDir, sshUser);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Provisioner(io.cdap.cdap.runtime.spi.provisioner.Provisioner) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) RetryStrategies(io.cdap.cdap.common.service.RetryStrategies) ProvisionerDetail(io.cdap.cdap.proto.provisioner.ProvisionerDetail) SparkCompatReader(io.cdap.cdap.internal.app.spark.SparkCompatReader) Future(java.util.concurrent.Future) ProgramStateWriter(io.cdap.cdap.app.runtime.ProgramStateWriter) Gson(com.google.gson.Gson) InvalidMacroException(io.cdap.cdap.api.macro.InvalidMacroException) Map(java.util.Map) MacroParserOptions(io.cdap.cdap.api.macro.MacroParserOptions) Cancellable(org.apache.twill.common.Cancellable) Capabilities(io.cdap.cdap.runtime.spi.provisioner.Capabilities) Threads(org.apache.twill.common.Threads) ProvisionerSpecification(io.cdap.cdap.runtime.spi.provisioner.ProvisionerSpecification) Collection(java.util.Collection) Set(java.util.Set) KeyedExecutor(io.cdap.cdap.common.async.KeyedExecutor) LoggingContextAccessor(io.cdap.cdap.common.logging.LoggingContextAccessor) ProvisioningTask(io.cdap.cdap.internal.provision.task.ProvisioningTask) StructuredTableContext(io.cdap.cdap.spi.data.StructuredTableContext) SecureStore(io.cdap.cdap.api.security.store.SecureStore) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) LoggingContext(io.cdap.cdap.common.logging.LoggingContext) List(java.util.List) SecurityRequestContext(io.cdap.cdap.security.spi.authentication.SecurityRequestContext) Type(java.lang.reflect.Type) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) Optional(java.util.Optional) Constants(io.cdap.cdap.common.conf.Constants) ProfileId(io.cdap.cdap.proto.id.ProfileId) ProgramOptionConstants(io.cdap.cdap.internal.app.runtime.ProgramOptionConstants) DeprovisionTask(io.cdap.cdap.internal.provision.task.DeprovisionTask) NotFoundException(io.cdap.cdap.common.NotFoundException) RuntimeJobManager(io.cdap.cdap.runtime.spi.runtimejob.RuntimeJobManager) Retries(io.cdap.cdap.common.service.Retries) RuntimeMonitorType(io.cdap.cdap.runtime.spi.RuntimeMonitorType) Cluster(io.cdap.cdap.runtime.spi.provisioner.Cluster) SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) Location(org.apache.twill.filesystem.Location) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ProvisionerContext(io.cdap.cdap.runtime.spi.provisioner.ProvisionerContext) Loggers(io.cdap.cdap.common.logging.Loggers) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ClusterStatus(io.cdap.cdap.runtime.spi.provisioner.ClusterStatus) SSHContext(io.cdap.cdap.runtime.spi.ssh.SSHContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) MacroParser(io.cdap.cdap.internal.app.runtime.plugin.MacroParser) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProvisionTask(io.cdap.cdap.internal.provision.task.ProvisionTask) ProgramOptions(io.cdap.cdap.app.runtime.ProgramOptions) PluginRequirement(io.cdap.cdap.internal.pipeline.PluginRequirement) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) SystemArguments(io.cdap.cdap.internal.app.runtime.SystemArguments) VersionInfo(io.cdap.cdap.runtime.spi.VersionInfo) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) RetryableProvisionException(io.cdap.cdap.runtime.spi.provisioner.RetryableProvisionException) LoggingContextHelper(io.cdap.cdap.logging.context.LoggingContextHelper) Logger(org.slf4j.Logger) SparkCompat(io.cdap.cdap.runtime.spi.SparkCompat) Networks(io.cdap.cdap.common.utils.Networks) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) LocationFactory(org.apache.twill.filesystem.LocationFactory) ProjectInfo(io.cdap.cdap.common.utils.ProjectInfo) ProvisionerSystemContext(io.cdap.cdap.runtime.spi.provisioner.ProvisionerSystemContext) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Requirements(io.cdap.cdap.api.plugin.Requirements) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) LogSamplers(io.cdap.cdap.common.logging.LogSamplers) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) MacroEvaluator(io.cdap.cdap.api.macro.MacroEvaluator) Cluster(io.cdap.cdap.runtime.spi.provisioner.Cluster) Location(org.apache.twill.filesystem.Location) Nullable(javax.annotation.Nullable)

Aggregations

SSHKeyPair (io.cdap.cdap.runtime.spi.ssh.SSHKeyPair)14 Cluster (io.cdap.cdap.runtime.spi.provisioner.Cluster)8 ProvisionerContext (io.cdap.cdap.runtime.spi.provisioner.ProvisionerContext)6 RetryableProvisionException (io.cdap.cdap.runtime.spi.provisioner.RetryableProvisionException)6 SSHContext (io.cdap.cdap.runtime.spi.ssh.SSHContext)6 SSHPublicKey (io.cdap.cdap.runtime.spi.ssh.SSHPublicKey)6 IOException (java.io.IOException)6 InvalidMacroException (io.cdap.cdap.api.macro.InvalidMacroException)4 NotFoundException (io.cdap.cdap.common.NotFoundException)4 DeprovisionTask (io.cdap.cdap.internal.provision.task.DeprovisionTask)4 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)4 RuntimeMonitorType (io.cdap.cdap.runtime.spi.RuntimeMonitorType)4 ClusterStatus (io.cdap.cdap.runtime.spi.provisioner.ClusterStatus)4 ProvisionerSpecification (io.cdap.cdap.runtime.spi.provisioner.ProvisionerSpecification)4 ConnectException (java.net.ConnectException)4 SocketTimeoutException (java.net.SocketTimeoutException)4 Map (java.util.Map)4 Optional (java.util.Optional)4 TimeUnit (java.util.concurrent.TimeUnit)4 Logger (org.slf4j.Logger)4