Repository: jclouds-examples Updated Branches: refs/heads/master a7c2a0d9c -> b183a1874
Simplify interface of Google examples and fix the name and comment of userName variable. Project: http://git-wip-us.apache.org/repos/asf/jclouds-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-examples/commit/b183a187 Tree: http://git-wip-us.apache.org/repos/asf/jclouds-examples/tree/b183a187 Diff: http://git-wip-us.apache.org/repos/asf/jclouds-examples/diff/b183a187 Branch: refs/heads/master Commit: b183a18747d7bce8f51bc05fc70acfa2cb5edfcd Parents: a7c2a0d Author: Marek Wieczorek <[email protected]> Authored: Sat Sep 6 12:10:06 2014 +0200 Committer: Andrew Phillips <[email protected]> Committed: Tue Sep 30 08:46:45 2014 -0400 ---------------------------------------------------------------------- google/pom.xml | 2 +- .../google/computeengine/CreateServer.java | 53 +++++++++++++------- .../google/computeengine/ExecuteCommand.java | 41 ++++++++++----- 3 files changed, 65 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/b183a187/google/pom.xml ---------------------------------------------------------------------- diff --git a/google/pom.xml b/google/pom.xml index ddd879a..a9dbcdf 100644 --- a/google/pom.xml +++ b/google/pom.xml @@ -26,7 +26,7 @@ <name>google-examples</name> <properties> - <jclouds.version>1.7.2</jclouds.version> + <jclouds.version>1.8.0</jclouds.version> </properties> <dependencies> http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/b183a187/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java ---------------------------------------------------------------------- diff --git a/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java b/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java index 63cf54b..fb1ebbf 100644 --- a/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java +++ b/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java @@ -65,38 +65,55 @@ public class CreateServer implements Closeable { * The second argument (args[1]) is a path to your service account private key PEM file without a password. It is * used for server-to-server interactions (https://developers.google.com/console/help/new/#serviceaccounts). * The key is not transmitted anywhere. - * The third argument (args[2]) is your Google user name. - * The fourth argument (args[3]) is a path to the file containing your SSH public key - * (https://developers.google.com/compute/docs/instances#sshkeys). The key is needed to authorize your access to - * the machine. - * The fifth argument (args[4]) is a path to the file containing your SSH private key, which is required to perform - * any operations on your machine via SSH (https://developers.google.com/compute/docs/instances#sshkeys). - * The key is not transmitted anywhere. * * Example: * * java org.jclouds.examples.google.computeengine.CreateServer \ * [email protected] \ - * /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \ - * planetnik \ - * /home/planetnik/.ssh/google_compute_engine.pub - * /home/planetnik/.ssh/google_compute_engine + * /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem */ - public static void main(final String[] args) throws IOException { + public static void main(final String[] args) { String serviceAccountEmailAddress = args[0]; - String serviceAccountKey = Files.toString(new File(args[1]), Charset.defaultCharset()); - String googleUserName = args[2]; - String sshPublicKey = Files.toString(new File(args[3]), Charset.defaultCharset()); - String sshPrivateKey = Files.toString(new File(args[4]), Charset.defaultCharset()); + String serviceAccountKey = null; + try { + serviceAccountKey = Files.toString(new File(args[1]), Charset.defaultCharset()); + } catch (IOException e) { + System.err.println("Cannot open service account private key PEM file: " + args[1] + "\n" + e.getMessage()); + System.exit(1); + } + String userName = System.getProperty("user.name"); + String sshPublicKey = null; + String sshPrivateKey = null; + String sshPublicKeyFileName = System.getProperty("user.home") + File.separator + ".ssh" + File.separator + + "google_compute_engine.pub"; + String sshPrivateKeyFileName = System.getProperty("user.home") + File.separator + ".ssh" + File.separator + + "google_compute_engine"; + try { + sshPublicKey = Files.toString(new File(sshPublicKeyFileName), Charset.defaultCharset()); + sshPrivateKey = Files.toString(new File(sshPrivateKeyFileName), Charset.defaultCharset()); + } catch (IOException e) { + System.err.println("Unable to load your SSH keys.\n" + e.getMessage() + + "\nYour public key, which is required to authorize your access to the machine is expected to be at " + + sshPublicKeyFileName + " , and your private key, which is required to perform any operations " + + "on your machine via SSH, is expected to be at " + sshPrivateKeyFileName + + " .\nSee https://developers.google.com/compute/docs/instances#sshkeys for more details.\n" + + e.getMessage()); + System.exit(1); + } CreateServer createServer = new CreateServer(serviceAccountEmailAddress, serviceAccountKey); try { - createServer.createServer(googleUserName, sshPublicKey, sshPrivateKey); + createServer.createServer(userName, sshPublicKey, sshPrivateKey); } catch (Exception e) { e.printStackTrace(); } finally { - createServer.close(); + try { + createServer.close(); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } } } http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/b183a187/google/src/main/java/org/jclouds/examples/google/computeengine/ExecuteCommand.java ---------------------------------------------------------------------- diff --git a/google/src/main/java/org/jclouds/examples/google/computeengine/ExecuteCommand.java b/google/src/main/java/org/jclouds/examples/google/computeengine/ExecuteCommand.java index 888ebdf..88727a9 100644 --- a/google/src/main/java/org/jclouds/examples/google/computeengine/ExecuteCommand.java +++ b/google/src/main/java/org/jclouds/examples/google/computeengine/ExecuteCommand.java @@ -67,9 +67,6 @@ public class ExecuteCommand implements Closeable { * (https://developers.google.com/compute/docs/instances#start_vm). * The fourth argument (args[3]) is the zone where your instance is located * (https://developers.google.com/compute/docs/zones). - * The fifth argument (args[4]) is your Google user name. - * The sixth argument (args[5]) is a path to the file containing your SSH private key - * (https://developers.google.com/compute/docs/instances#sshkeys). * * Example: * @@ -77,17 +74,32 @@ public class ExecuteCommand implements Closeable { * [email protected] \ * /home/planetnik/Work/Cloud/OSS/certificate/gcp-oss.pem \ * planetnik-main \ - * europe-west1-a \ - * planetnik \ - * /home/planetnik/.ssh/google_compute_engine + * europe-west1-a */ - public static void main(final String[] args) throws IOException { + public static void main(final String[] args) { String serviceAccountEmailAddress = args[0]; - String serviceAccountKey = Files.toString(new File(args[1]), Charset.defaultCharset()); + String serviceAccountKey = null; + try { + serviceAccountKey = Files.toString(new File(args[1]), Charset.defaultCharset()); + } catch (IOException e) { + System.err.println("Cannot open service account private key PEM file: " + args[1] + "\n" + e.getMessage()); + System.exit(1); + } String instanceName = args[2]; String zone = args[3]; - String googleUserName = args[4]; - String sshPrivateKey = Files.toString(new File(args[5]), Charset.defaultCharset()); + String userName = System.getProperty("user.name"); + String sshPrivateKeyFileName = System.getProperty("user.home") + File.separator + ".ssh" + File.separator + + "google_compute_engine"; + String sshPrivateKey = null; + try { + sshPrivateKey = Files.toString(new File(sshPrivateKeyFileName), Charset.defaultCharset()); + } catch (IOException e) { + System.err.println("Unable to load your SSH private key at " + sshPrivateKeyFileName + + "\nIt is required to perform any operations on your machine via SSH.\n" + + "See https://developers.google.com/compute/docs/instances#sshkeys for more details.\n" + + e.getMessage()); + System.exit(1); + } ExecuteCommand executeApplication = new ExecuteCommand(serviceAccountEmailAddress, serviceAccountKey); @@ -100,11 +112,16 @@ public class ExecuteCommand implements Closeable { System.err.format("Error: Instance %s could not be located in zone %s.%n", instanceName, zone); System.exit(1); } - executeApplication.executeSimpleCommand(instance, googleUserName, sshPrivateKey); + executeApplication.executeSimpleCommand(instance, userName, sshPrivateKey); } catch (Exception e) { e.printStackTrace(); } finally { - executeApplication.close(); + try { + executeApplication.close(); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } } }
