如何将环境与JClouds-Chef API合并?

时间:2014-07-31 00:32:56

标签: java chef roles environment jclouds

我正在使用JClouds-Chef

  1. 引导新配置的Linux VM;然后然后
  2. 在该节点上运行chef-client进行配置
  3. 值得注意的是,我当前正在配置Chef的所有人都是一个角色(它只需要一切;其他一切都在Chef服务器上为我设置):

    public class ChefClient {
        public configure() {
            String vmIp = "myapp01.example.com";
            String vmSshUsername = "myuser";
            String vmSshPassword = "12345";
    
            String endpoint = "https://mychefserver.example.com";
            String client = "myuser";
            String validator = "chef-validator";
            String clientCredential = Files.toString(new File("C:\\Users\\myuser\\sandbox\\chef\\myuser.pem"), Charsets.UTF_8);
            String validatorCredential = Files.toString(new File("C:\\Users\\myuser\\sandbox\\chef\\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);
            ChefService chef = ctx.getChefService();
    
            List<String> runlist = new RunListBuilder().addRole("platformcontrol_dev").build();
    
            ArrayList<String> runList2 = new ArrayList<String>();
            for(String item : runlist) {
                runList2.add(item);
            }
    
            BootstrapConfig bootstrapConfig = BootstrapConfig.builder().runList(runList2).build();
    
            chef.updateBootstrapConfigForGroup("jclouds-chef", bootstrapConfig);
    
            Statement bootstrap = chef.createBootstrapScriptForGroup("jclouds-chef");
    
            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 {
                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("bash /tmp/chef-bootstrap.sh");
            } catch(Throwable t) {
                println "Exception: " + t.message
            } finally {
                ssh.disconnect();
            }
        }
    }
    

    我们的内部&#34;厨师&#34; (我们的开发人员)现在想要添加Chef&#34; environments&#34;的概念。我们的所有食谱以及现有的角色。这样我们就可以为每个节点指定特定于环境的角色。我的问题:JClouds-Chef API是否处理环境?如果是这样,我如何修改代码以包含特定于环境的角色?

    是否就像:

    一样简单
    BootstrapConfig bootstrapConfig = BootstrapConfig.builder()
        .environment("name-of-env-here?").runList(runList2).build();
    

1 个答案:

答案 0 :(得分:1)

是的,就这么简单。这将告诉引导脚本在指定环境中注册节点。

但请注意,Chef服务器中的环境必须已存在。如果要在新环境中创建节点,还可以按以下方式以编程方式创建节点:

ChefApi api = ctx.unwrapApi(ChefApi.class);
if (api.getEnvironment("environment-name") == null) {
    Environment env = Environment.builder()
        .name("environment-name")
        .description("Some description")
        .build();
    api.createEnvironment(env);
}