gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:41:35 25 4
gpt4 key购买 nike

我正在使用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();
}
}
}

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

是不是就这么简单:

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

最佳答案

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

但请注意,Chef Server 中必须已存在环境。如果您想在新环境中创建节点,也可以通过编程方式创建它们,如下所示:

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

关于java - 如何将环境与 JClouds-Chef API 合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25049159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com