0
我使用Jclouds庫來引導我的虛擬機,而不是用「刀引導」引導廚師節點,我發現下面的代碼與Jclouds
包com.sagemcom.administration.chef;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import org.jclouds.Constants;
import org.jclouds.ContextBuilder;
import org.jclouds.chef.ChefContext;
import org.jclouds.chef.ChefService;
import org.jclouds.chef.config.ChefProperties;
import org.jclouds.chef.domain.BootstrapConfig;
import org.jclouds.chef.util.RunListBuilder;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.ssh.SshClient;
import org.jclouds.sshj.config.SshjSshClientModule;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import com.google.common.net.HostAndPort;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
public class ChefProvisioner {
public static void main(String[] args) throws IOException {
ChefProvisioner.provision();
}
public static void provision() throws IOException {
// Configuration
String vmIp = "192.168.221.160";
String vmSshUsername = "root";
String vmSshPassword = "omar";
String endpoint = "https://api.chef.io/organizations/scompany";
String client = "omar";
String validator = "scompany-validator";
String clientCredential = Files.toString(new File("C:\\chef\\omar.pem"), Charsets.UTF_8);
String validatorCredential = Files.toString(new File("C:\\chef\\scompany-validator.pem"), Charsets.UTF_8);
Properties props = new Properties();
props.put(ChefProperties.CHEF_VALIDATOR_NAME, validator);
props.put(ChefProperties.CHEF_VALIDATOR_CREDENTIAL, validatorCredential);
props.put(Constants.PROPERTY_RELAX_HOSTNAME, "true");
props.put(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
/* *** First, create the context to connect to the Chef Server *** */
// Create the context and configure the SSH driver to use. sshj in this example
ChefContext ctx = ContextBuilder.newBuilder("chef")
.endpoint(endpoint)
.credentials(client, clientCredential)
.overrides(props)
.modules(ImmutableSet.of(new SshjSshClientModule())) //
.buildView(ChefContext.class);
ChefService chef = ctx.getChefService();
/* *** Second, generate the bootstrap script *** */
// Generate the bootsrap configuration
List<String> runlist = new RunListBuilder().addRole("myrole").build();
BootstrapConfig bootstrapConfig = BootstrapConfig.builder().runList(runlist).build();
chef.updateBootstrapConfigForGroup("jclouds-chef", bootstrapConfig);
Statement bootstrap = chef.createBootstrapScriptForGroup("jclouds-chef");
/* *** Finally create an SSH connection manually and run the script on the VM *** */
SshClient.Factory sshFactory = ctx.unwrap().utils()
.injector().getInstance(Key.get(new TypeLiteral<SshClient.Factory>() {}));
SshClient ssh = sshFactory.create(HostAndPort.fromParts(vmIp, 22),
LoginCredentials.builder().user(vmSshUsername).password(vmSshPassword).build());
ssh.connect();
try {
String rawScript = bootstrap.render(org.jclouds.scriptbuilder.domain.OsFamily.UNIX);
ExecResponse result = ssh.exec(rawScript);
} finally {
ssh.disconnect();
}
}
}
當我運行這段代碼,我得到這個例外
juin 29, 2016 10:29:26 AM org.jclouds.logging.jdk.JDKLogger logError
GRAVE: Cannot retry after server error, command has exceeded retry limit 5: [method=org.jclouds.chef.ChefApi.public abstract void org.jclouds.chef.ChefApi.createDatabag(java.lang.String)[bootstrap], request=POST https://api.chef.io/data HTTP/1.1]
Exception in thread "main" org.jclouds.http.HttpResponseException: api.chef.io connecting to POST https://api.chef.io/data HTTP/1.1
at org.jclouds.http.internal.BaseHttpCommandExecutorService.invoke(BaseHttpCommandExecutorService.java:113)
at org.jclouds.rest.internal.InvokeHttpMethod$InvokeAndTransform.call(InvokeHttpMethod.java:160)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at java.lang.Thread.getStackTrace(Unknown Source)
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:126)
at org.jclouds.rest.internal.InvokeHttpMethod.invokeWithTimeout(InvokeHttpMethod.java:126)
at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:71)
at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:44)
at org.jclouds.rest.internal.DelegatesToInvocationFunction.handle(DelegatesToInvocationFunction.java:156)
at org.jclouds.rest.internal.DelegatesToInvocationFunction.invoke(DelegatesToInvocationFunction.java:123)
at com.sun.proxy.$Proxy52.createDatabag(Unknown Source)
at org.jclouds.chef.internal.BaseChefService.updateBootstrapConfigForGroup(BaseChefService.java:167)
at com.sagemcom.administration.chef.ChefProvisioner.provision(ChefProvisioner.java:77)
at com.sagemcom.administration.chef.ChefProvisioner.main(ChefProvisioner.java:34)
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
at org.jclouds.rest.internal.InvokeHttpMethod.invokeWithTimeout(InvokeHttpMethod.java:126)
at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:71)
at org.jclouds.rest.internal.InvokeHttpMethod.apply(InvokeHttpMethod.java:44)
at org.jclouds.rest.internal.DelegatesToInvocationFunction.handle(DelegatesToInvocationFunction.java:156)
at org.jclouds.rest.internal.DelegatesToInvocationFunction.invoke(DelegatesToInvocationFunction.java:123)
at com.sun.proxy.$Proxy52.createDatabag(Unknown Source)
at org.jclouds.chef.internal.BaseChefService.updateBootstrapConfigForGroup(BaseChefService.java:167)
at com.sagemcom.administration.chef.ChefProvisioner.provision(ChefProvisioner.java:77)
at com.sagemcom.administration.chef.ChefProvisioner.main(ChefProvisioner.java:34)
Caused by: java.net.UnknownHostException: api.chef.io
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.writePayloadToConnection(JavaUrlHttpCommandExecutorService.java:294)
at org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.convert(JavaUrlHttpCommandExecutorService.java:170)
at org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.convert(JavaUrlHttpCommandExecutorService.java:64)
at org.jclouds.http.internal.BaseHttpCommandExecutorService.invoke(BaseHttpCommandExecutorService.java:91)
at org.jclouds.rest.internal.InvokeHttpMethod$InvokeAndTransform.call(InvokeHttpMethod.java:160)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
這是例外,因爲我使用的託管廚師? 謝謝
謝謝@Ignasi,我修正了這個錯誤,但現在當我運行代碼時,Statement bootstrap返回null,並且我在這一行中得到了NullPointerException:Statement bootstrap = chef.createBootstrapScriptForGroup(「jclouds-chef」);與廚師服務器的連接沒有問題,廚師服務器上的asucces生成了bootstrap腳本,對此例外有任何想法。 – Omar
你能提供更新的代碼和新的堆棧跟蹤嗎? –
您好,Ignasi 我仍然使用相同的代碼,公司內的代理導致舊錯誤, 現在角色被添加到runlist,但發生了NullPointerException ' – Omar