Bootstrap chef node with Jclouds

2019-08-17 01:57发布

问题:

i'm using the Jclouds library to bootstrap my Vms instead of used "knife bootstrap" , i find the following code

package 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();
            }


        }

}

When I run this code, I get this exceptions

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)

Is this exception because i used hosted-chef ? thanks

回答1:

I don't think so. The host where you are running the jclouds core from does not seem to be able to resolve the api.chef.io domain name.

As a side note, if you are manually generating the bootstrap script you might want to consider doing it this way.



标签: chef jclouds