package de.unibi.cebitec.bibigrid.core.model;

import java.util.ArrayList;
import java.util.List;

/**
 * Host configuration for the ansible scripts.
 * <p/>
 * The {@link #toString() toString} method outputs the configuration in ini format
 * ready to use for ansible.
 *
 * @author mfriedrichs(at)techfak.uni-bielefeld.de
 */
public class AnsibleHostsConfig {
    private static final String PYTHON_INTERPRETER = "ansible_python_interpreter=python3";
    private final Configuration config;
    private final List<String> workerIps;

    public AnsibleHostsConfig(Configuration config, List<Instance> workerInstances) {
        this.config = config;
        workerIps = new ArrayList<>();
        for (Instance instance : workerInstances) {
            workerIps.add(instance.getPrivateIp());
        }
    }

    @Override
    public String toString() {
        StringBuilder hostsConfig = new StringBuilder();
        hostsConfig.append("# The content of this file (ansible_hosts) should be generated by ");
        hostsConfig.append("BiBiGrid during instance initialization.\n");
        hostsConfig.append("# Depending on the used base image the ansible_user must be changed. ");
        hostsConfig.append("The local ip address of each worker must\n");
        hostsConfig.append("# be inserted in the worker section\n\n");
        hostsConfig.append("[master]\n");
        hostsConfig.append("localhost ansible_connection=local " + PYTHON_INTERPRETER + "\n\n");
        hostsConfig.append("[workers]\n");
        for (String workerIp : workerIps) {
            hostsConfig.append(workerIp).append(" ansible_connection=ssh " + PYTHON_INTERPRETER + " ansible_user=");
            hostsConfig.append(config.getSshUser()).append("\n");
        }
        return hostsConfig.toString();
    }
}

@@ -436,16 +436,25 @@
 rpreter=
+/usr/bin/
 python3%22

fix async task execution by specifying full path of ansible interpreter