This repository has been archived by the owner on Feb 4, 2019. It is now read-only.
/
CloudServersPublish.java
178 lines (150 loc) · 7.13 KB
/
CloudServersPublish.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* 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.jclouds.examples.rackspace.cloudservers;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.common.net.HostAndPort;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.openstack.nova.v2_0.domain.regionscoped.RegionAndId;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.sshj.config.SshjSshClientModule;
import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jclouds.compute.config.ComputeServiceProperties.POLL_INITIAL_PERIOD;
import static org.jclouds.compute.config.ComputeServiceProperties.POLL_MAX_PERIOD;
import static org.jclouds.examples.rackspace.cloudservers.Constants.*;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import static org.jclouds.util.Predicates2.retry;
/**
* This example creates a server, start a web server on it, and publish a page on the internet!
*/
public class CloudServersPublish implements Closeable {
private final ComputeService computeService;
private final int numServers;
/**
* To get a username and API key see
* http://jclouds.apache.org/guides/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
* The optional third argument (args[2]) is the number of Cloud Servers to start
*/
public static void main(String[] args) throws IOException {
getPublishedCloudServers(Arrays.asList(args));
}
public static Set<? extends NodeMetadata> getPublishedCloudServers(List<String> args) throws IOException {
CloudServersPublish cloudServersPublish = new CloudServersPublish(args);
Set<? extends NodeMetadata> nodes = null;
try {
nodes = cloudServersPublish.createServer();
cloudServersPublish.configureAndStartWebserver(nodes);
return nodes;
}
catch (Exception e) {
e.printStackTrace();
}
finally {
cloudServersPublish.close();
}
return nodes;
}
public CloudServersPublish(List<String> args) {
String username = args.get(0);
String apiKey = args.get(1);
numServers = args.size() == 3 ? Integer.valueOf(args.get(2)) : 1;
Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());
// These properties control how often jclouds polls for a status update
Properties overrides = new Properties();
overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.overrides(overrides)
.modules(modules)
.buildView(ComputeServiceContext.class);
computeService = context.getComputeService();
}
private Set<? extends NodeMetadata> createServer() throws RunNodesException, TimeoutException {
System.out.format("Create Server%n");
RegionAndId regionAndId = RegionAndId.fromRegionAndId(REGION, "performance1-1");
Template template = computeService.templateBuilder()
.locationId(REGION)
.osDescriptionMatches(".*Ubuntu 14.04.*")
.hardwareId(regionAndId.slashEncode())
.build();
// This method will continue to poll for the server status and won't return until this server is ACTIVE
// If you want to know what's happening during the polling, enable logging.
// See /jclouds-example/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java
Set<? extends NodeMetadata> nodes = computeService.createNodesInGroup(NAME, numServers, template);
for (NodeMetadata nodeMetadata: nodes) {
System.out.format(" %s%n", nodeMetadata);
}
return nodes;
}
private void configureAndStartWebserver(Set<? extends NodeMetadata> nodes) throws TimeoutException {
for (NodeMetadata nodeMetadata: nodes) {
String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
String privateAddress = nodeMetadata.getPrivateAddresses().iterator().next();
System.out.format(" Configure And Start Webserver%n");
awaitSsh(publicAddress);
String message = new StringBuilder()
.append("Hello from ").append(nodeMetadata.getHostname())
.append(" @ ").append(publicAddress).append("/").append(privateAddress)
.append(" in ").append(nodeMetadata.getLocation().getParent().getId())
.toString();
String script = new ScriptBuilder()
.addStatement(exec("apt-get -q -y update"))
.addStatement(exec("apt-get -q -y install apache2"))
.addStatement(exec("echo '" + message + "' > /var/www/index.html"))
.render(OsFamily.UNIX);
RunScriptOptions options = RunScriptOptions.Builder.blockOnComplete(true);
computeService.runScriptOnNode(nodeMetadata.getId(), script, options);
System.out.format(" Login: ssh %s@%s%n", nodeMetadata.getCredentials().getUser(), publicAddress);
System.out.format(" Password: %s%n", nodeMetadata.getCredentials().getOptionalPassword().get());
System.out.format(" Go to http://%s%n", publicAddress);
}
}
private void awaitSsh(String ip) throws TimeoutException {
SocketOpen socketOpen = computeService.getContext().utils().injector().getInstance(SocketOpen.class);
Predicate<HostAndPort> socketTester = retry(socketOpen, 300, 5, 5, SECONDS);
socketTester.apply(HostAndPort.fromParts(ip, 22));
}
/**
* Always close your service when you're done with it.
*/
public void close() throws IOException {
Closeables.close(computeService.getContext(), true);
}
}