我正在尝试使用以下JClouds-Chef代码(v1.7.3)在全新的Linux VM上引导Chef Client,然后执行运行列表以使用应用程序堆栈实际配置该VM({{1 }}):
typical_app
当我运行时,我从SLF4J获得以下输出:
public class ChefPlugin {
public static void main(String[] args) {
ChefPlugin.provision();
System.out.println("And done!");
System.exit(0);
}
public static provision() {
String vmIp = "myapp01";
String vmSshUsername = "myadmin";
String vmSshPassword = "12345";
String endpoint = "https://mychefserver";
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");
System.out.println("Setup complete.");
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("typicalapp").build();
ArrayList<String> runList2 = new ArrayList<String>();
for(String item : runlist) {
runList2.add(item);
}
BootstrapConfig bootstrapConfig = BootstrapConfig.builder().runList(runList2).build();
System.out.println("Configured the bootstrapper.");
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();
System.out.println("Connected to SSH.");
try {
String rawScript = bootstrap.render(OsFamily.UNIX);
System.out.println("Raw script rendered.");
ExecResponse result = ssh.exec(rawScript);
System.out.println("Bootstrap script executed...");
} catch(Throwable t) {
System.out.println("Exception: " + t.getMessage());
} finally {
ssh.disconnect();
System.out.println("SSH closed.");
}
}
}
当我SSH到服务器(Setup complete.
Configured the bootstrapper.
[main] INFO net.schmizz.sshj.common.SecurityUtils - BouncyCastle registration succeeded
[main] WARN net.schmizz.sshj.DefaultConfig - Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy
[main] INFO net.schmizz.sshj.transport.TransportImpl - Client identity string: SSH-2.0-SSHJ_0_8_1_SNAPSHOT
[main] INFO net.schmizz.sshj.transport.TransportImpl - Server identity string: SSH-2.0-OpenSSH_6.6p1 Ubuntu-2ubuntu1
Connected to SSH.
Raw script rendered.
[main] INFO net.schmizz.sshj.connection.channel.direct.SessionChannel - Will request to exec `setupPublicCurl || exit 1
curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -X GET https://www.opscode.com/chef/install.sh |(bash)
mkdir -p /etc/chef
cat >> /etc/chef/client.rb <<-'END_OF_JCLOUDS_FILE'
require 'rubygems'
require 'ohai'
o = Ohai::System.new
o.all_plugins
node_name "jclouds-chef-" + o[:ipaddress]
log_level :info
log_location STDOUT
validation_client_name "chef-validator"
chef_server_url "https://mychefserver"
END_OF_JCLOUDS_FILE
cat >> /etc/chef/validation.pem <<-'END_OF_JCLOUDS_FILE'
-----BEGIN RSA PRIVATE KEY-----
<omitted for security purposes>
-----END RSA PRIVATE KEY-----
END_OF_JCLOUDS_FILE
cat >> /etc/chef/first-boot.json <<-'END_OF_JCLOUDS_FILE'
{"id":"jclouds-chef","run_list":["role[typical_app]"]}
END_OF_JCLOUDS_FILE
chef-client -j /etc/chef/first-boot.json
`
Bootstrap script executed...
[main] INFO net.schmizz.sshj.transport.TransportImpl - Disconnected - BY_APPLICATION
SSH closed.
And done!
)时,如果我运行myapp01
,我看到安装了Ruby。但是which ruby
没有输出,which chef-client
也没有输出。服务器上也没有which java
目录。这让我觉得我的代码只是部分工作,也许只在VM上安装Ruby而不是其他任何东西。除此之外,除非我在“并完成!”打印语句之后放置/etc/chef
,否则代码永远不会退出。这让我觉得有一个后台/工作线程(可能是在服务器上做某事的SSH进程)没有返回/完成。
这里没有任何错误或异常。
我的问题:
System.exit(0)
角色??要重现,请使用以下Maven POM来提取依赖关系,然后完全按原样运行上面的代码(只需使用您自己的Chef服务器和Linux VM)。
typical_app
更新:这是我运行@Ignasi Barrera建议更改后的<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<jclouds.version>1.7.3</jclouds.version>
</properties>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-sshj</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.api</groupId>
<artifactId>chef</artifactId>
<version>${jclouds.version}</version>
</dependency>
</dependencies>
</project>
文件:
/tmp/stderr
答案 0 :(得分:1)
这里的主要挑战是您尝试在不使用jclouds计算服务的情况下在节点上运行脚本。代码使用jclouds-chef生成一个Statement,但该假定该语句由jclouds计算服务执行,该服务有自己的机制来呈现它执行的脚本。
为了获得完整的原始脚本并能够使用原始SSH连接直接执行它,必须手动完成一些操作。特别是,jclouds-chef脚本假定定义了一些bash函数,并且在以这种方式呈现脚本时缺少这些函数。
您应该更改rawString
String的创建,如下所示:
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");
这样最终的脚本将具有所有相关的功能。另请注意,我没有直接运行脚本,而是更改了代码以上传它,然后在本地运行该文件。这将帮助您解决脚本失败时正在发生的事情。
另请注意,脚本将在每次运行时生成/etc/chef
目录的内容,因此在再次运行之前,您可能需要删除该目录。