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

Java 如何使用JClouds Chef指定客户端版本?,java,chef-infra,jclouds,Java,Chef Infra,Jclouds,几个月来,我一直在使用以下代码从头开始引导新的虚拟机: public class Bootstrapper { public static void main(String[] args) { Bootstrapper b = new Bootstrapper(); b.bootstrap(); } public void bootstrap() { String endpoint = "https://mychef.ex

几个月来,我一直在使用以下代码从头开始引导新的虚拟机:

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()
    }
}
公共类引导程序{
公共静态void main(字符串[]args){
引导程序b=新引导程序();
b、 bootstrap();
}
公共无效引导(){
字符串端点=”https://mychef.example.com"
String client=“myuser”
字符串验证器=“厨师验证器”
字符串clientCredential=Files.toString(新文件(“/etc/myuser/myuser.pem”),Charsets.UTF_8)
String validatorCredential=Files.toString(新文件(“/etc/myuser/chef validator.pem”),Charsets.UTF_8)
Properties props=新属性()
props.put(ChefProperties.CHEF\u VALIDATOR\u NAME,VALIDATOR);
道具放置(ChefProperties.CHEF\u验证器\u凭证,验证器凭证)
props.put(Constants.PROPERTY\u RELAX\u主机名,“true”)
props.put(Constants.PROPERTY\u TRUST\u ALL\u CERTS,“true”)
ChefContext ctx=ContextBuilder.newBuilder(“厨师”)
.endpoint(端点)
.凭据(客户端、客户端凭据)
.覆盖(道具)
.modules(ImmutableSet.of(新的SSHJSHClientModule())//
.buildView(ChefContext.class);
ChefApi api=ctx.unwapapi(ChefApi.class)
MyEnvProvider environmentProvider=新的MyEnvProvider()
Environment-devEnv=environmentProvider.provideEnvironment()
api.createEnvironment(devEnv)
List runlist=new RunListBuilder().addRole(“myrole”).build()
BootstrapConfig BootstrapConfig=BootstrapConfig.builder().environment(“myenv”).runList(runList.build())
字符串vmIp=“myapp01.example.com”
字符串vmSshUsername=“myuser”
字符串vmshPassword=“12345”
ChefService chef=chefContext.getChefService()
chef.updateBootstrapConfigForGroup(chefGroup,bootstrapConfig)
语句bootstrap=chef.createbootstrapsscriptforgroup(chefGroup)
SshClient.Factory sshFactory=chefContext.unwrap().utils()
.injector().getInstance(Key.get(new-TypeLiteral(){}))
SshClient ssh=sshFactory.create(HostAndPort.fromParts(vmIp,22),
LoginCredentials.builder().user(vmSshUsername).password(vmshpassword.build())
ssh.connect()
StringBuilder rawScript=新建StringBuilder()
映射resolvedFunctions=ScriptBuilder.resolveFunctionDependenciesForStatements(
新的HashMap(),ImmutableSet.of(bootstrap),OsFamily.UNIX)
ScriptBuilder.writeFunctions(resolvedFunctions、OsFamily.UNIX、rawScript)
append(bootstrap.render(OsFamily.UNIX))
put(“/tmp/chef bootstrap.sh”,rawScript.toString())
ExecResponse result=ssh.exec(“sudobash/tmp/chef bootstrap.sh”)
ssh.disconnect()
api.close()
ctx.close()
}
}
我几周来第一次运行了这段代码,但一切都被破坏了。现在看来,这段代码的结果是使用Chef 12的节点,而
mychef.example.com
是Chef 11服务器,这是问题的根源

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

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

问题


在这种情况下,您可能需要类似于
ChefProperties.CHEF\u VERSION='11.16.4'
。我觉得我应该指出,chef client 12与chef Server 11配合得很好。如果您运行的是Enterprise Chef 11,您只需要更新其上的配置变量以允许客户端12,因为它被错误地限制为仅11.x。

考虑到从jclouds 1.7开始,Chef默认使用Omnibus安装程序安装,这将安装最新版本的Chef客户端,不考虑
chefproperty.CHEF\u版本的属性值。(顺便问一下,有没有办法通过综合脚本安装Chef客户端的具体版本?)。如果要控制安装的确切Chef版本,则需要设置
chefproperty。使用\u OMNIBUS
false
,并相应地配置所述的@coderanger属性。是的,在运行脚本时传递a。还请记住,您可以将其设置为
11
,以获得最新的11.x版本。谢谢@coderanger!我刚刚创建了一个补丁,用于在使用Omnibus安装程序时添加对自定义版本的支持(通过设置相同的
CHEF_version
属性)。简而言之,您可以期望它可用:如果我同时配置
chefConfig.put(ChefProperties.CHEF\u USE\u OMNIBUS,false);chefConfig.put(ChefProperties.CHEF_版本,“11.16.4”)它不是从chef url下载chef客户端rpm。。。有什么想法吗?