<commit_before>package org.ovirt.engine.core.vdsbroker.vdsbroker;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.ovirt.engine.core.common.businessentities.VmInit;
import org.ovirt.engine.core.common.businessentities.VmInitNetwork;
import org.ovirt.engine.core.utils.JsonHelper;
import org.ovirt.engine.core.utils.log.Log;
import org.ovirt.engine.core.utils.log.LogFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

public class CloudInitHandler {
    private static final Log log = LogFactory.getLog(CloudInitHandler.class);

    private final VmInit vmInit;
    private final Map<String, Object> metaData;
    private final Map<String, Object> userData;
    private final Map<String, byte[]> files;
    private int nextFileIndex;
    private String interfaces;

    private final String passwordKey = "password";

    private enum CloudInitFileMode {
        FILE,
        NETWORK;
    }

    public CloudInitHandler(VmInit vmInit) {
        this.vmInit = vmInit;
        metaData = new HashMap<String, Object>();
        userData = new HashMap<String, Object>();
        files = new HashMap<String, byte[]>();
        nextFileIndex = 0;
    }


    public Map<String, byte[]> getFileData()
            throws UnsupportedEncodingException, IOException, JsonGenerationException, JsonMappingException {
        if (vmInit != null) {
            try {
                storeHostname();
                storeAuthorizedKeys();
                storeRegenerateKeys();
                storeNetwork();
                storeTimeZone();
                storeRootPassword();
                storeUserName();
            } catch (IllegalArgumentException ex) {
                throw new IllegalArgumentException("Malformed input", ex);
            }
        }

        // Add other required/supplemental data
        storeExecutionParameters();

        String metaDataStr = mapToJson(metaData);
        String userDataStr = mapToYaml(userData);

        if (vmInit != null && vmInit.getCustomScript() != null) {
            userDataStr += vmInit.getCustomScript();
        }

        // add #cloud-config for user data file head
        if (StringUtils.isNotBlank(userDataStr)) {
            userDataStr = "#cloud-config\n" + userDataStr;
        }

        files.put("openstack/latest/meta_data.json", metaDataStr.getBytes("UTF-8"));
        files.put("openstack/latest/user_data", userDataStr.getBytes("UTF-8"));

        // mask password for log if exists
        if (metaDataStr.contains(passwordKey) && vmInit != null && vmInit.getRootPassword() != null) {
            String oldStr = String.format("\"%s\" : \"%s\"", passwordKey, vmInit.getRootPassword());
            String newStr = String.format("\"%s\" : ***", passwordKey);
            metaDataStr = metaDataStr.replace(oldStr, newStr);
        }
        log.debugFormat("cloud-init meta-data:\n{0}", metaDataStr);
        log.debugFormat("cloud-init user-data:\n{0}", userDataStr);

        return files;
    }


    private void storeHostname() {
        if (!StringUtils.isEmpty(vmInit.getHostname())) {
            metaData.put("hostname", vmInit.getHostname());
            metaData.put("name", vmInit.getHostname());
        }
    }

    private void storeAuthorizedKeys() {
        if (!StringUtils.isEmpty(vmInit.getAuthorizedKeys())) {
            metaData.put("public_keys", normalizeAuthorizedKeys(vmInit.getAuthorizedKeys()));
        }
    }

    private List<String> normalizeAuthorizedKeys(String authorizedKeys) {
        List<String> keys = new ArrayList<String>();
        for (String key : vmInit.getAuthorizedKeys().split("(\\r?\\n|\\r)+")) {
            if (!StringUtils.isEmpty(key)) {
                keys.add(key);
            }
        }
        return keys;
    }

    private void storeRegenerateKeys() {
        if (vmInit.getRegenerateKeys() != null && vmInit.getRegenerateKeys()) {
            // Create new system ssh keys
            userData.put("ssh_deletekeys", "True");
        }
    }

    private void storeNetwork() throws UnsupportedEncodingException {
        StringBuilder output = new StringBuilder();

        if (vmInit.getNetworks() != null) {
            List<VmInitNetwork> networks = vmInit.getNetworks();

            for (VmInitNetwork iface: networks) {
                output.append("iface " + iface.getName() + " inet "
                        + iface.getBootProtocol().getDisplayName() + "\n");
                if (StringUtils.isNotEmpty(iface.getIp())) {
                    output.append("  address " + iface.getIp() + "\n");
                }
                if (StringUtils.isNotEmpty(iface.getNetmask())) {
                    output.append("  netmask " + iface.getNetmask() + "\n");
                }
                if (StringUtils.isNotEmpty(iface.getGateway())) {
                    output.append("  gateway " + iface.getGateway() + "\n");
                }

                // As of cloud-init 0.7.1, you can't set DNS servers without also setting NICs
                if (vmInit.getDnsServers() != null) {
                    output.append("  dns-nameservers")
                        .append(" ").append(vmInit.getDnsServers());
                    output.append("\n");
                }

                if (vmInit.getDnsSearch() != null) {
                    output.append("  dns-search")
                        .append(" ").append(vmInit.getDnsSearch());
                    output.append("\n");
                }
            }

            output.append("auto");
            for (VmInitNetwork iface : networks) {
                output.append(" " + iface.getName());
            }
            output.append("\n");
        }

        interfaces = output.toString();<commit_msg>core: 'Start-on-boot' of a nic should be respected<commit_after>package org.ovirt.engine.core.vdsbroker.vdsbroker;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.ovirt.engine.core.common.businessentities.VmInit;
import org.ovirt.engine.core.common.businessentities.VmInitNetwork;
import org.ovirt.engine.core.utils.JsonHelper;
import org.ovirt.engine.core.utils.log.Log;
import org.ovirt.engine.core.utils.log.LogFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

public class CloudInitHandler {
    private static final Log log = LogFactory.getLog(CloudInitHandler.class);

    private final VmInit vmInit;
    private final Map<String, Object> metaData;
    private final Map<String, Object> userData;
    private final Map<String, byte[]> files;
    private int nextFileIndex;
    private String interfaces;

    private final String passwordKey = "password";

    private enum CloudInitFileMode {
        FILE,
        NETWORK;
    }

    public CloudInitHandler(VmInit vmInit) {
        this.vmInit = vmInit;
        metaData = new HashMap<String, Object>();
        userData = new HashMap<String, Object>();
        files = new HashMap<String, byte[]>();
        nextFileIndex = 0;
    }


    public Map<String, byte[]> getFileData()
            throws UnsupportedEncodingException, IOException, JsonGenerationException, JsonMappingException {
        if (vmInit != null) {
            try {
                storeHostname();
                storeAuthorizedKeys();
                storeRegenerateKeys();
                storeNetwork();
                storeTimeZone();
                storeRootPassword();
                storeUserName();
            } catch (IllegalArgumentException ex) {
                throw new IllegalArgumentException("Malformed input", ex);
            }
        }

        // Add other required/supplemental data
        storeExecutionParameters();

        String metaDataStr = mapToJson(metaData);
        String userDataStr = mapToYaml(userData);

        if (vmInit != null && vmInit.getCustomScript() != null) {
            userDataStr += vmInit.getCustomScript();
        }

        // add #cloud-config for user data file head
        if (StringUtils.isNotBlank(userDataStr)) {
            userDataStr = "#cloud-config\n" + userDataStr;
        }

        files.put("openstack/latest/meta_data.json", metaDataStr.getBytes("UTF-8"));
        files.put("openstack/latest/user_data", userDataStr.getBytes("UTF-8"));

        // mask password for log if exists
        if (metaDataStr.contains(passwordKey) && vmInit != null && vmInit.getRootPassword() != null) {
            String oldStr = String.format("\"%s\" : \"%s\"", passwordKey, vmInit.getRootPassword());
            String newStr = String.format("\"%s\" : ***", passwordKey);
            metaDataStr = metaDataStr.replace(oldStr, newStr);
        }
        log.debugFormat("cloud-init meta-data:\n{0}", metaDataStr);
        log.debugFormat("cloud-init user-data:\n{0}", userDataStr);

        return files;
    }


    private void storeHostname() {
        if (!StringUtils.isEmpty(vmInit.getHostname())) {
            metaData.put("hostname", vmInit.getHostname());
            metaData.put("name", vmInit.getHostname());
        }
    }

    private void storeAuthorizedKeys() {
        if (!StringUtils.isEmpty(vmInit.getAuthorizedKeys())) {
            metaData.put("public_keys", normalizeAuthorizedKeys(vmInit.getAuthorizedKeys()));
        }
    }

    private List<String> normalizeAuthorizedKeys(String authorizedKeys) {
        List<String> keys = new ArrayList<String>();
        for (String key : vmInit.getAuthorizedKeys().split("(\\r?\\n|\\r)+")) {
            if (!StringUtils.isEmpty(key)) {
                keys.add(key);
            }
        }
        return keys;
    }

    private void storeRegenerateKeys() {
        if (vmInit.getRegenerateKeys() != null && vmInit.getRegenerateKeys()) {
            // Create new system ssh keys
            userData.put("ssh_deletekeys", "True");
        }
    }

    private void storeNetwork() throws UnsupportedEncodingException {
        StringBuilder output = new StringBuilder();

        if (vmInit.getNetworks() != null) {
            List<VmInitNetwork> networks = vmInit.getNetworks();

            for (VmInitNetwork iface: networks) {
                if (Boolean.TRUE.equals(iface.getStartOnBoot())) {
                    output.append("auto ").append(iface.getName()).append("\n");
                }

                output.append("iface " + iface.getName() + " inet "
                        + iface.getBootProtocol().getDisplayName() + "\n");
                if (StringUtils.isNotEmpty(iface.getIp())) {
                    output.append("  address " + iface.getIp() + "\n");
                }
                if (StringUtils.isNotEmpty(iface.getNetmask())) {
                    output.append("  netmask " + iface.getNetmask() + "\n");
                }
                if (StringUtils.isNotEmpty(iface.getGateway())) {
                    output.append("  gateway " + iface.getGateway() + "\n");
                }

                // As of cloud-init 0.7.1, you can't set DNS servers without also setting NICs
                if (vmInit.getDnsServers() != null) {
                    output.append("  dns-nameservers")
                        .append(" ").append(vmInit.getDnsServers());
                    output.append("\n");
                }

                if (vmInit.getDnsSearch() != null) {
                    output.append("  dns-search")
                        .append(" ").append(vmInit.getDnsSearch());
                    output.append("\n");
                }
            }
        }

        interfaces = output.toString();
{'fraction_non_alphanumeric': 0.12456633074508508, 'fraction_numerical': 0.001321658681645465, 'mean_word_length': 2.179359243697479, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5569133', 'n_tokens_mistral': 3401, 'n_tokens_neox': 3282, 'n_words': 646}