static public String getSshPublicKey() { ensureSshKeysExist(); try { return IOUtils.toString(new FileInputStream(getSshPublicKeyFile())); } catch (IOException e) { throw new HalException(FATAL, "Failed to read ssh public key: " + e.getMessage(), e); } }
static String getInstanceIp(AccountDeploymentDetails<GoogleAccount> details, String instanceName) { Compute compute = getCompute(details); Instance instance = null; try { instance = compute.instances().get(details.getAccount().getProject(), "us-central1-f", instanceName).execute(); } catch (IOException e) { throw new HalException(FATAL, "Unable to get instance " + instanceName); } return instance.getNetworkInterfaces() .stream() .map(i -> i.getAccessConfigs().stream() .map(AccessConfig::getNatIP) .filter(ip -> !StringUtils.isEmpty(ip)) .findFirst() ).filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new HalException(FATAL, "No public IP associated with" + instanceName)); }
private static String getSshPublicKeyFile() { return getSshKeyFile() + ".pub"; }
String ip = getInstanceIp(details, instanceName); String keyFile = getSshKeyFile(); log.info("Opening port " + port + " against instance " + instanceName); boolean connected = false; proxy = openSshTunnel(ip, port, keyFile); connected = checkIfProxyIsOpen(proxy); closeSshTunnel(proxy); DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(10));
Compute compute = GoogleProviderUtils.getCompute(details); String project = account.getProject(); String zone = settings.getLocation(); .setNetwork(GoogleProviderUtils.ensureSpinnakerNetworkExists(details)) .setAccessConfigs(Collections.singletonList(accessConfig)); .setEmail(GoogleProviderUtils.defaultServiceAccount(details)) .setScopes(getScopes()); operation = compute.instanceTemplates().insert(project, template).execute(); instanceTemplateUrl = operation.getTargetLink(); GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation); } catch (IOException e) { throw new HalException(FATAL, "Failed to create instance template for " + settings.getArtifactId() + ": " + e.getMessage(), e); DaemonTaskHandler.message("Deploying the instance group manager"); operation = compute.instanceGroupManagers().insert(project, settings.getLocation(), manager).execute(); GoogleProviderUtils.waitOnZoneOperation(compute, project, settings.getLocation(), operation); } catch (IOException e) { throw new HalException(FATAL, "Failed to create instance group to run artifact " + settings.getArtifactId() + ": " + e.getMessage(), e);
String zone = settings.getLocation(); String project = details.getAccount().getProject(); Compute compute = GoogleProviderUtils.getCompute(details); InstanceGroupManager mig; try { GoogleProviderUtils.waitOnZoneOperation(compute, project, zone, compute.instanceGroupManagers().delete(project, zone, migName).execute()); } catch (IOException e) { throw new HalException(FATAL, "Failed to delete mig " + migName + " in " + zone, e); instanceTemplateName = instanceTemplateName.substring(instanceTemplateName.lastIndexOf('/') + 1); try { GoogleProviderUtils.waitOnGlobalOperation(compute, project, compute.instanceTemplates().delete(project, instanceTemplateName).execute()); } catch (IOException e) { throw new HalException(FATAL, "Failed to delete template " + instanceTemplateName + " in " + zone, e);
static String ensureSpinnakerNetworkExists(AccountDeploymentDetails<GoogleAccount> details) { String networkName = getNetworkName(); String project = details.getAccount().getProject(); Compute compute = getCompute(details); boolean exists = true; try { Operation operation = compute.networks().insert(project, network).execute(); networkUrl = operation.getTargetLink(); GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation); } catch (IOException e) { throw new HalException(FATAL, "Failed to create Spinnaker network: " + e.getMessage(), e);
static private void ensureSshKeysExist() { File sshKeyFile = new File(getSshKeyFile()); if (!sshKeyFile.exists()) { if (!sshKeyFile.getParentFile().exists()) { command.add("rsa"); command.add("-f"); // path to keyfile command.add(getSshKeyFile()); command.add("-C"); // username sshing into machine command.add("ubuntu"); File sshPublicKeyFile = new File(getSshPublicKeyFile()); String sshKeyContents = IOUtils.toString(new FileInputStream(sshPublicKeyFile)); command.add("chmod"); command.add("400"); command.add(getSshKeyFile());
String stack = name.getStack(); String detail = name.getDetail(); String network = GoogleProviderUtils.getNetworkName(); Map<String, String> metadata = getMetadata(details, runtimeSettings, configSources, version).stream() .reduce(new HashMap<String, String>(), String serviceAccountEmail = GoogleProviderUtils.defaultServiceAccount(details); List<String> scopes = getScopes(); String accountName = account.getName();
.setValue(GoogleProviderUtils.getSshPublicKey()); metadataItems.add(items);
String ip = getInstanceIp(details, instanceName); String keyFile = getSshKeyFile(); log.info("Opening port " + port + " against instance " + instanceName); boolean connected = false; proxy = openSshTunnel(ip, port, keyFile); connected = checkIfProxyIsOpen(proxy); closeSshTunnel(proxy); DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(10));
Compute compute = GoogleProviderUtils.getCompute(details); String project = account.getProject(); String zone = settings.getLocation(); .setNetwork(GoogleProviderUtils.ensureSpinnakerNetworkExists(details)) .setAccessConfigs(Collections.singletonList(accessConfig)); .setEmail(GoogleProviderUtils.defaultServiceAccount(details)) .setScopes(getScopes()); operation = compute.instanceTemplates().insert(project, template).execute(); instanceTemplateUrl = operation.getTargetLink(); GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation); } catch (IOException e) { throw new HalException(FATAL, "Failed to create instance template for " + settings.getArtifactId() + ": " + e.getMessage(), e); DaemonTaskHandler.message("Deploying the instance group manager"); operation = compute.instanceGroupManagers().insert(project, settings.getLocation(), manager).execute(); GoogleProviderUtils.waitOnZoneOperation(compute, project, settings.getLocation(), operation); } catch (IOException e) { throw new HalException(FATAL, "Failed to create instance group to run artifact " + settings.getArtifactId() + ": " + e.getMessage(), e);
String zone = settings.getLocation(); String project = details.getAccount().getProject(); Compute compute = GoogleProviderUtils.getCompute(details); InstanceGroupManager mig; try { GoogleProviderUtils.waitOnZoneOperation(compute, project, zone, compute.instanceGroupManagers().delete(project, zone, migName).execute()); } catch (IOException e) { throw new HalException(FATAL, "Failed to delete mig " + migName + " in " + zone, e); instanceTemplateName = instanceTemplateName.substring(instanceTemplateName.lastIndexOf('/') + 1); try { GoogleProviderUtils.waitOnGlobalOperation(compute, project, compute.instanceTemplates().delete(project, instanceTemplateName).execute()); } catch (IOException e) { throw new HalException(FATAL, "Failed to delete template " + instanceTemplateName + " in " + zone, e);
static String ensureSpinnakerNetworkExists(AccountDeploymentDetails<GoogleAccount> details) { String networkName = getNetworkName(); String project = details.getAccount().getProject(); Compute compute = getCompute(details); boolean exists = true; try { Operation operation = compute.networks().insert(project, network).execute(); networkUrl = operation.getTargetLink(); GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation); } catch (IOException e) { throw new HalException(FATAL, "Failed to create Spinnaker network: " + e.getMessage(), e);
static private void ensureSshKeysExist() { File sshKeyFile = new File(getSshKeyFile()); if (!sshKeyFile.exists()) { if (!sshKeyFile.getParentFile().exists()) { command.add("rsa"); command.add("-f"); // path to keyfile command.add(getSshKeyFile()); command.add("-C"); // username sshing into machine command.add("ubuntu"); File sshPublicKeyFile = new File(getSshPublicKeyFile()); String sshKeyContents = IOUtils.toString(new FileInputStream(sshPublicKeyFile)); command.add("chmod"); command.add("400"); command.add(getSshKeyFile());
String stack = name.getStack(); String detail = name.getDetail(); String network = GoogleProviderUtils.getNetworkName(); Map<String, String> metadata = getMetadata(details, runtimeSettings, configSources, version).stream() .reduce(new HashMap<String, String>(), String serviceAccountEmail = GoogleProviderUtils.defaultServiceAccount(details); List<String> scopes = getScopes(); String accountName = account.getName();
.setValue(GoogleProviderUtils.getSshPublicKey()); metadataItems.add(items);
static String getInstanceIp(AccountDeploymentDetails<GoogleAccount> details, String instanceName) { Compute compute = getCompute(details); Instance instance = null; try { instance = compute.instances().get(details.getAccount().getProject(), "us-central1-f", instanceName).execute(); } catch (IOException e) { throw new HalException(FATAL, "Unable to get instance " + instanceName); } return instance.getNetworkInterfaces() .stream() .map(i -> i.getAccessConfigs().stream() .map(AccessConfig::getNatIP) .filter(ip -> !StringUtils.isEmpty(ip)) .findFirst() ).filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new HalException(FATAL, "No public IP associated with" + instanceName)); }
static public String getSshPublicKey() { ensureSshKeysExist(); try { return IOUtils.toString(new FileInputStream(getSshPublicKeyFile())); } catch (IOException e) { throw new HalException(FATAL, "Failed to read ssh public key: " + e.getMessage(), e); } }
private static String getSshPublicKeyFile() { return getSshKeyFile() + ".pub"; }