private void setupHostsFile() throws IOException { lock.lock(); try { if (AnsibleGlobalProperty.KEEP_HOSTS_FILE_IN_MEMORY) { if (!hostIPs.contains(targetIp)) { hostIPs.add(targetIp); FileUtils.writeStringToFile(hostsFile, StringUtils.join(hostIPs, "\n"), false); logger.debug(String.format("add target ip[%s] to %s", targetIp, AnsibleConstant.INVENTORY_FILE)); } } else { if (!findIpInHostFile()) { FileUtils.writeStringToFile(hostsFile, String.format("%s\n", targetIp), true); logger.debug(String.format("add target ip[%s] to %s", targetIp, AnsibleConstant.INVENTORY_FILE)); } else { logger.debug(String.format("found target ip[%s] in %s", targetIp, AnsibleConstant.INVENTORY_FILE)); } } } finally { lock.unlock(); } }
public void prepare() { DebugUtils.Assert(targetIp != null, "targetIp cannot be null"); try { setupHostsFile(); } catch (IOException e) { throw new CloudRuntimeException(e); } } }
private void run(Completion completion) { new PrepareAnsible().setTargetIp(msg.getTargetIp()).prepare();
public void run(Completion completion) { try { if (!isNeedRun()) { completion.success(); return; } putArgument("pip_url", String.format("http://%s:8080/zstack/static/pypi/simple", restf.getHostName())); putArgument("trusted_host", restf.getHostName()); putArgument("yum_server", String.format("%s:8080", restf.getHostName())); putArgument("remote_user", username); if (password != null && !password.isEmpty()) { putArgument("remote_pass", password); } putArgument("remote_port", Integer.toString(sshPort)); logger.debug(String.format("starts to run ansible[%s]", playBookPath == null ? playBookName : playBookPath)); new PrepareAnsible().setTargetIp(targetIp).prepare(); setupPublicKey(); callAnsible(completion); } catch (SshException e) { throw new OperationFailureException(operr("User name or password or port number may be problematic")); } catch (Exception e) { throw new CloudRuntimeException(e); } }