如何使用JClouds-Chef指定客户端版本?

时间:2014-12-12 19:14:15

标签: java chef jclouds

我几个月来一直使用以下JClouds-Chef 1.7.3代码从头开始引导新VM:

public class Bootstrapper {
    public static void main(String[] args) {
        Bootstrapper b = new Bootstrapper();
        b.bootstrap();
    }

    public void bootstrap() {
        String endpoint = "https://mychef.example.com"
        String client = "myuser"
        String validator = "chef-validator"

        String clientCredential = Files.toString(new File("/etc/myuser/myuser.pem"), Charsets.UTF_8)
        String validatorCredential = Files.toString(new File("/etc/myuser/chef-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")

        ChefContext ctx = ContextBuilder.newBuilder("chef")
            .endpoint(endpoint)
            .credentials(client, clientCredential)
            .overrides(props)
            .modules(ImmutableSet.of(new SshjSshClientModule())) //
            .buildView(ChefContext.class);

        ChefApi api = ctx.unwrapApi(ChefApi.class)
        MyEnvProvider environmentProvider = new MyEnvProvider()

        Environment devEnv = environmentProvider.provideEnvironment()
        api.createEnvironment(devEnv)

        List<String> runlist = new RunListBuilder().addRole("myrole").build()
        BootstrapConfig bootstrapConfig = BootstrapConfig.builder().environment("myenv").runList(runlist).build()

        String vmIp = "myapp01.example.com"
        String vmSshUsername = "myuser"
        String vmSshPassword = "12345"
        ChefService chef = chefContext.getChefService()
        chef.updateBootstrapConfigForGroup(chefGroup, bootstrapConfig)

        Statement bootstrap = chef.createBootstrapScriptForGroup(chefGroup)

        SshClient.Factory sshFactory = chefContext.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()

        StringBuilder rawScript = new StringBuilder()

        Map<String, String> resolvedFunctions = ScriptBuilder.resolveFunctionDependenciesForStatements(
            new HashMap<String, String>(), ImmutableSet.of(bootstrap), OsFamily.UNIX)

        ScriptBuilder.writeFunctions(resolvedFunctions, OsFamily.UNIX, rawScript)
        rawScript.append(bootstrap.render(OsFamily.UNIX))

        ssh.put("/tmp/chef-bootstrap.sh", rawScript.toString())
        ExecResponse result = ssh.exec("sudo bash /tmp/chef-bootstrap.sh")

        ssh.disconnect()
        api.close()
        ctx.close()
    }
}

我刚刚在几周内第一次运行此代码,一切都被打破了。现在看来这个代码的结果是使用Chef 12的节点,而mychef.example.com是Chef 11服务器,这就是问题的根源。

所以我问:如何配置此代码以便继续安装/引导Chef 11.x节点?

请注意:这是代码问题,不是系统管理员问题,因此属于StackOverflow。

1 个答案:

答案 0 :(得分:2)

jclouds docs list how to configure the process

在这种情况下,您需要类似ChefProperties.CHEF_VERSION = '11.16.4'的内容。我觉得我应该指出,Chef-client 12可以与Chef Server 11一起使用。如果你正在运行Enterprise Chef 11,你只需要在其上更新一个配置变量以允许客户端12,因为它被错误地限制为只有11 .X。