【问题标题】:How to incorporate Environments with JClouds-Chef API?如何将环境与 JClouds-Chef API 结合起来?
【发布时间】:2014-07-31 00:32:56
【问题描述】:

我正在使用JClouds-Chef 来:

  1. 引导新配置的 Linux VM; 然后
  2. 在该节点上运行 chef-client 来配置它

重要的是要注意,我目前配置 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();
        }
    }
}

除了现有角色之外,我们的内部“厨师”(我们的开发人员)现在希望将厨师“environments”的概念添加到我们所有的食谱中。这样我们就可以为每个节点指定特定于环境的角色。我的问题:JClouds-Chef API 是否处理环境?如果是这样,我该如何修改代码以包含特定于环境的角色?

是不是就这么简单:

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

【问题讨论】:

    标签: java chef-infra roles environment jclouds


    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2021-12-22
      • 2021-09-18
      • 1970-01-01
      • 2018-12-08
      • 2012-03-17
      • 2017-12-15
      相关资源
      最近更新 更多