Skip to content
Permalink
Browse files
Update compute-basic to use JSON credential
  • Loading branch information
danbroudy authored and nacx committed May 3, 2015
1 parent d19ff7d commit 58761e38eaca7963ac5d5cb9eaa34cbdb120fd16
Showing 2 changed files with 25 additions and 16 deletions.
@@ -89,9 +89,11 @@ To list all images (the *groupname* parameter is not used):
java -jar target/compute-basics-jar-with-dependencies.jar \
google-compute-engine \
your-project-service-account-email@developer.gserviceaccount.com \
/path/to/private-key.pem \
/path/to/json-key.json \
mygroup add

For more information on service accounts and getting a JSON key see [here](https://developers.google.com/console/help/new/#serviceaccounts)

## License

Copyright (C) 2009-2014 The Apache Software Foundation
@@ -6,9 +6,9 @@
* 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
@@ -33,6 +33,7 @@
import static org.jclouds.compute.predicates.NodePredicates.TERMINATED;
import static org.jclouds.compute.predicates.NodePredicates.inGroup;
import static org.jclouds.scriptbuilder.domain.Statements.exec;

import java.io.File;
import java.io.IOException;
import java.util.Map;
@@ -54,8 +55,10 @@
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.enterprise.config.EnterpriseConfigurationModule;
import org.jclouds.googlecloud.GoogleCredentialsFromJson;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.providers.ProviderMetadata;
import org.jclouds.providers.Providers;
@@ -65,6 +68,7 @@

import com.google.common.base.Charsets;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
@@ -85,15 +89,15 @@ public class MainApp {
public static enum Action {
ADD, RUN, EXEC, DESTROY, LISTIMAGES, LISTNODES;
}

public static final Map<String, ApiMetadata> allApis = Maps.uniqueIndex(Apis.viewableAs(ComputeServiceContext.class),
Apis.idFunction());

public static final Map<String, ProviderMetadata> appProviders = Maps.uniqueIndex(Providers.viewableAs(ComputeServiceContext.class),
Providers.idFunction());

public static final Set<String> allKeys = ImmutableSet.copyOf(Iterables.concat(appProviders.keySet(), allApis.keySet()));

public static int PARAMETERS = 5;
public static String INVALID_SYNTAX = "Invalid number of parameters. Syntax is: provider identity credential groupName (add|exec|run|destroy)";

@@ -114,7 +118,7 @@ public static void main(String[] args) {

// For GCE, the credential parameter is the path to the private key file
if (providerIsGCE)
credential = getPrivateKeyFromFile(credential);
credential = getCredentialFromJsonKeyFile(credential);

if (action == Action.RUN && args.length < PARAMETERS + 1)
throw new IllegalArgumentException("please pass the local file to run as the last parameter");
@@ -124,7 +128,7 @@ public static void main(String[] args) {
if (!file.exists())
throw new IllegalArgumentException("file must exist! " + file);
}

String minRam = System.getProperty("minRam");

// note that you can check if a provider is present ahead of time
@@ -143,12 +147,12 @@ public static void main(String[] args) {
// that tested to work with java, which tends to be Ubuntu or CentOS
TemplateBuilder templateBuilder = compute.templateBuilder();

// If you want to up the ram and leave everything default, you can
// If you want to up the ram and leave everything default, you can
// just tweak minRam
if (minRam != null)
templateBuilder.minRam(Integer.parseInt(minRam));


// note this will create a user with the same name as you on the
// node. ex. you can connect via ssh publicip
Statement bootInstructions = AdminAccess.standard();
@@ -185,7 +189,7 @@ public static void main(String[] args) {
case RUN:
System.out.printf(">> running [%s] on group %s as %s%n", file, groupName, login.identity);

// when running a sequence of commands, you probably want to have jclouds use the default behavior,
// when running a sequence of commands, you probably want to have jclouds use the default behavior,
// which is to fork a background process.
responses = compute.runScriptOnNodesMatching(//
inGroup(groupName),
@@ -238,9 +242,12 @@ public static void main(String[] args) {
}
}

private static String getPrivateKeyFromFile(String filename) {
private static String getCredentialFromJsonKeyFile(String filename) {
try {
return Files.toString(new File(filename), Charsets.UTF_8);
String fileContents = Files.toString(new File(filename), UTF_8);
Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(fileContents);
String credential = credentialSupplier.get().credential;
return credential;
} catch (IOException e) {
System.err.println("Exception reading private key from '%s': " + filename);
e.printStackTrace();
@@ -271,7 +278,7 @@ private static ComputeService initComputeService(String provider, String identit
.credentials(identity, credential)
.modules(modules)
.overrides(properties);

System.out.printf(">> initializing %s%n", builder.getApiMetadata());

return builder.buildView(ComputeServiceContext.class).getComputeService();

0 comments on commit 58761e3

Please sign in to comment.