我正在使用优秀的JClouds-Chef API在我们的数据中心中引导和配置VM。
我有以下代码:
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
并查看节点,我看到它是运行列表和食谱是空的。换句话说,即使我专门为运行列表添加了一个角色,它似乎也没有在主厨服务器端添加内容。
这可能会发生什么?
答案 0 :(得分:0)
Chef Server中是否存在角色?
与环境一样,它必须存在。如果没有,您必须首先使用ChefApi
以与创建环境类似的方式创建它。
如果角色已存在,请检查VM中执行的脚本的输出,看它是否包含错误。