【问题标题】:JClouds-Chef not adding role to runlistJClouds-Chef 未将角色添加到运行列表
【发布时间】:2014-08-14 19:59:32
【问题描述】:

我正在使用出色的 JClouds-Chef API 在我们的数据中心引导和配置虚拟机。

我有以下代码:

String appName = "myapp";
String myRole = "my_app";
String chefGroup = "fizzbuzz";
String endpoint = "https://mychefserver";
String client = "myuser";
String validator = "chef-validator";
String clientCredential = Files.toString(new File("/etc/chef/myuser.pem"), Charsets.UTF_8);
String validatorCredential = Files.toString(new File("/etc/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);

ChefApi api = ctx.unwrapApi(ChefApi.class);
// ChefEnvironmentProvider is a custom class that builds environments.
ChefEnvironmentProvider environmentProvider = new ChefEnvironmentProvider(appName);

Environment devEnv = environmentProvider.provideEnvironment("dev");
Environment testEnv = environmentProvider.provideEnvironment("test");
api.createEnvironment(devEnv);
api.createEnvironment(testEnv);

List<String> runlist = new RunListBuilder().addRole(myRole).build();
//      BootstrapConfig bootstrapConfig = BootstrapConfig.builder().environment(devEnv).runList(runlist).build();
BootstrapConfig bootstrapConfig = BootstrapConfig.builder().runList(runlist).build();

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

Statement bootstrap = chef.createBootstrapScriptForGroup(chefGroup);

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("sudo bash /tmp/chef-bootstrap.sh");
} catch(Throwable t) {
    log.error(t.message);
} finally {
    ssh.disconnect();
}

当我运行此代码时,我没有遇到任何异常,但是如果我进入 http://mychefserver 并查看节点,我看到它是 Run List 和 Recipes em> 是空的。 换句话说,即使我专门向运行列表添加了一个角色,它似乎并没有在厨师服务器端添加东西。

这里会发生什么?

【问题讨论】:

    标签: chef-infra jclouds


    【解决方案1】:

    Chef Server 中是否存在该角色?

    就像环境一样,它必须存在。如果没有,您必须先使用ChefApi 以与创建环境类似的方式创建它。

    如果角色已经存在,检查在VM中执行的脚本的输出,看看它是否包含错误。

    【讨论】:

    • 感谢@Ignasi Barrera - 是的,它存在,但我的环境中有一个错误的 URL 引用。您手动 SSH 进入机器并运行 chef 的建议让我能够看到我的配置有什么问题 - 谢谢!
    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2019-01-17
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多