view src/org/thermostat/qa2/framework/utils/ThermostatUtilities.java @ 191:2650531d6d93

Reflecting shell parameter changes done in thermostat
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 14 Jul 2015 18:53:20 +0200
parents 877b403e8344
children 5aa76ad23f06
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA give you
 permission to link this code with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.framework.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.thermostat.qa2.framework.NativeProcess;
import org.thermostat.qa2.framework.Shell;
import org.thermostat.qa2.framework.ThermostatQAConfig;
import org.thermostat.qa2.framework.ThermostatQAConfig.Login;
import org.thermostat.qa2.framework.services.BackupService;
import org.thermostat.qa2.framework.services.ThermostatStorage;

/**
 *
 * @author Zdeněk Žamberský
 */
public class ThermostatUtilities {

    /* runs thermostat setup (creates thermostat user home dir (database)),
     backup service (on thermostat user home) is started  before if requested */
    public static void setupThermostat(String setupTarget, boolean backupUserHome) throws Exception {
        String thermostatUserHome = ThermostatQAConfig.getThermostatUserHome(setupTarget);
        if (new File(thermostatUserHome).exists()) {
            if (backupUserHome) {
                BackupService backupService = new BackupService(thermostatUserHome);
                backupService.start();
            }
            ProcessUtilities.run("rm", "-rf", "--", thermostatUserHome);
        }
        runThermostatSetup(ThermostatQAConfig.getThermostatBinDir(setupTarget), thermostatUserHome);
    }

    public static void runThermostatSetup(String executableDir, String thermostatUserHome) throws Exception {
        NativeProcess process;
        CommonUtilities.printHeading("Running Thermostat setup ...");
        String setupPath1 = executableDir + File.separator + "thermostat-devsetup";
        String setupPath2 = executableDir + File.separator + "thermostat-setup";
        String setupPath3 = executableDir + File.separator + "thermostat-setup-user-home";
        if (new File(setupPath1).exists()) {
            process = new NativeProcess(setupPath1);
        } else if (new File(setupPath2).exists()) {
            Login mongoLogin = ThermostatQAConfig.getMongoLogin();
            process = new NativeProcess("bash", "-c", "echo -e \"yes\\n" + mongoLogin.getUsername() + "\\n" + mongoLogin.getPassword() + "\\n" + mongoLogin.getPassword() + "\\n\" | " + setupPath2);
        } else if (new File(setupPath3).exists()) {
            process = new NativeProcess(setupPath3);
        } else {
            throw new Exception("script to setup thermostat cannot be found in: " + executableDir);
        }
        process.setLabel("Thermostat setup");
        process.setEnvironmentVariable("USER_THERMOSTAT_HOME", thermostatUserHome);
        process.start();
        process.waitFor();
    }

    /* does all required tasks to setup storage as requested,
     it does not modify existing storage (database),
     it can also start backup services if reqested */
    public static void setupStorage(String setupTarget, boolean web, boolean badAgentLogin, boolean badClientLogin, boolean agentSaveOnExit, boolean backupThermostatConfig, boolean backupUserHome) throws Exception {
        boolean packedThermostat = ThermostatQAConfig.isThermostatPackaged(setupTarget);
        if (web && !packedThermostat) {
            deployThermostatWebApp(setupTarget);
            String tomcatHome = ThermostatQAConfig.getTomcatHome();
            CommonUtilities.printHeading("Cleaning up tomcat logs");
            ProcessUtilities.shellRun("rm -rf -- \"" + tomcatHome + File.separator + "logs" + File.separator + "\"" + "*");
        }
        if (backupThermostatConfig) {
            BackupService backupService = new BackupService(ThermostatQAConfig.getThermostatEtcDir(setupTarget));
            backupService.setRunAsRoot(packedThermostat);
            backupService.start();
        }
        if (backupUserHome) {
            String thermostatUserHome = ThermostatQAConfig.getThermostatUserHome(setupTarget);
            if (new File(thermostatUserHome).exists()) {
                BackupService backupService = new BackupService(thermostatUserHome);
                backupService.start();
            }
        }

        copyThermostatConfigFiles(setupTarget, web, badAgentLogin, badClientLogin, agentSaveOnExit);
    }

    public static void setupDefaultMongoUsers(String target) throws Exception {
        ThermostatStorage storage = new ThermostatStorage(target);
        storage.start();
        List<Login> users = new ArrayList();
        users.add(ThermostatQAConfig.getAgentLogin());
        users.add(ThermostatQAConfig.getClientLogin());
        setupMongoUsers("127.0.0.1:" + ThermostatQAConfig.mongoPort, users);
        storage.stop();
    }

    public static void setupMongoUsers(String address, List<Login> users) throws Exception {
        CommonUtilities.printHeading("Setting up mongo users ...");
        Shell mongo = new Shell("mongo", address);
        mongo.setLabel("Mongo users setup");
        mongo.start();
        mongo.writeln("use thermostat");
        Login login = ThermostatQAConfig.getMongoLogin();
        mongo.writeln("db.auth(\"" + login.getUsername() + "\", \"" + login.getPassword() + "\")");
        for (Login user : users) {
            mongo.writeln("db.addUser(\"" + user.getUsername() + "\", \"" + user.getPassword() + "\")");
        }
        mongo.writeln("quit()");
        mongo.waitFor();
    }

    private static boolean webStorage = false;

    public static void copyThermostatConfigFiles(String setupTarget, boolean web, boolean badAgentLogin, boolean badClientLogin, boolean saveOnExit) throws Exception {
        CommonUtilities.printHeading("Copying thermostat config files ...");
        webStorage = web;
        String thermostatUserConfigDir = ThermostatQAConfig.getThermostatUserHome(setupTarget) + File.separator + "etc";
        String thermostatConfigDir = ThermostatQAConfig.getThermostatEtcDir(setupTarget);
        FileUtilities.printLineListToFile(thermostatUserConfigDir + File.separator + "agent.auth", getAgentAuth(badAgentLogin));
        FileUtilities.printLineListToFile(thermostatUserConfigDir + File.separator + "agent.properties", getAgentProperties(web, saveOnExit));
        FileUtilities.printLineListToFile(thermostatUserConfigDir + File.separator + "client.properties", getClientProperties(web, badClientLogin));

        boolean needsRoot = ThermostatQAConfig.isThermostatPackaged(setupTarget);
        String usersFile = thermostatConfigDir + File.separator + "thermostat-users.properties";
        String rolesFile = thermostatConfigDir + File.separator + "thermostat-roles.properties";
        if (needsRoot) {
            String usersTmpFile = FileUtilities.getUniqueFile(ThermostatQAConfig.getBackupDir() + File.separator + "thermostat-users.properties");
            String rolesTmpFile = FileUtilities.getUniqueFile(ThermostatQAConfig.getBackupDir() + File.separator + "thermostat-roles.properties");

            FileUtilities.printLineListToFile(usersTmpFile, getThermostatUsers());
            FileUtilities.printLineListToFile(rolesTmpFile, getThermostatRoles());

            ProcessUtilities.run("sudo", "-n", "mv", "-f", usersTmpFile, usersFile);
            ProcessUtilities.run("sudo", "-n", "mv", "-f", rolesTmpFile, rolesFile);
        } else {
            FileUtilities.printLineListToFile(usersFile, getThermostatUsers());
            FileUtilities.printLineListToFile(rolesFile, getThermostatRoles());
        }
    }

    public static final String storageTemplatesDir = "templates" + File.separator + "storage-config";
    public static final String usernamePattern = "${USERNAME}";
    public static final String passwordPattern = "${PASSWORD}";

    public static List<String> getAgentAuth(boolean badLogin) throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "agent.auth");
        Map<String, List<String>> patterns = new HashMap();

        ThermostatQAConfig.Login login = badLogin ? ThermostatQAConfig.getAgentBadLogin() : ThermostatQAConfig.getAgentLogin();
        CommonUtilities.addReplacementToMap(patterns, usernamePattern, login.getUsername());
        CommonUtilities.addReplacementToMap(patterns, passwordPattern, login.getPassword());

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String saveOnExitPattern = "${SAVE_ON_EXIT}";
    public static final String dbUrlPattern = "${DB_URL}";

    public static List<String> getAgentProperties(boolean web, boolean saveOnExit) throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "agent.properties");
        Map<String, List<String>> patterns = new HashMap();

        CommonUtilities.addReplacementToMap(patterns, saveOnExitPattern, java.lang.Boolean.toString(saveOnExit));
        CommonUtilities.addReplacementToMap(patterns, dbUrlPattern, ThermostatQAConfig.getStroageUrl(web));

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String connectionUrlPattern = "${CONNECTION_URL}";

    public static List<String> getClientProperties(boolean web, boolean badLogin) throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "client.properties");
        Map<String, List<String>> patterns = new HashMap();

        ThermostatQAConfig.Login login = badLogin ? ThermostatQAConfig.getClientBadLogin() : ThermostatQAConfig.getClientLogin();
        CommonUtilities.addReplacementToMap(patterns, usernamePattern, login.getUsername());
        CommonUtilities.addReplacementToMap(patterns, connectionUrlPattern, ThermostatQAConfig.getStroageUrl(web).replace(":", "\\:"));

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String usersPattern = "${USERS}";

    public static List<String> getThermostatUsers() throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "thermostat-users.properties");
        Map<String, List<String>> patterns = new HashMap();

        List<String> userLines = new ArrayList();
        Login agentLogin = ThermostatQAConfig.getAgentLogin();
        Login clientLogin = ThermostatQAConfig.getClientLogin();
        userLines.add(agentLogin.getUsername() + "=" + agentLogin.getPassword());
        userLines.add(clientLogin.getUsername() + "=" + clientLogin.getPassword());
        patterns.put(usersPattern, userLines);

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static final String agentUsernamePattern = "${AGENT_USERNAME}";
    public static final String clientUsernamePattern = "${CLIENT_USERNAME}";

    public static List<String> getThermostatRoles() throws IOException {
        List<String> lines = FileUtilities.getLineListFromFile(storageTemplatesDir + File.separator + "thermostat-roles.properties");
        Map<String, List<String>> patterns = new HashMap();

        Login agentLogin = ThermostatQAConfig.getAgentLogin();
        Login clientLogin = ThermostatQAConfig.getClientLogin();
        CommonUtilities.addReplacementToMap(patterns, agentUsernamePattern, agentLogin.getUsername());
        CommonUtilities.addReplacementToMap(patterns, clientUsernamePattern, clientLogin.getUsername());

        List<String> output = new ArrayList();
        CommonUtilities.replacePatterns(lines, output, patterns);
        return output;
    }

    public static boolean isWebStorageConfigured() {
        return webStorage;
    }

    public static void deployThermostatWebApp(String setupTarget) throws Exception {
        CommonUtilities.printHeading("Deploying thermostat web app ...");
        String deployDir = ThermostatQAConfig.getTomcatHome() + File.separator + "webapps";
        String deployedAppPath = deployDir + File.separator + "thermostat";
        if (new File(deployedAppPath).exists()) {
            ProcessUtilities.shellRun("rm -rf " + deployedAppPath);
        }
        String webAppPath = ThermostatQAConfig.getWebAppPath(setupTarget);
        String name = new File(webAppPath).getName();

        ProcessUtilities.shellRun("cp -r " + webAppPath + " " + deployDir);
        ProcessUtilities.shellRun("mv " + deployDir + File.separator + name + " " + deployedAppPath);
    }

    /* Zero, negative or positive value is returned for first version equal to,
       lower or higher than second */
    public static int compareVersions(String version1, String version2) {
        String version1lc = version1.toLowerCase();
        String version2lc = version2.toLowerCase();

        int index1;
        int index2;
        int index1Old = -1;
        int index2Old = -1;
        String component1;
        String component2;
        do {
            index1 = version1.indexOf(".", index1Old + 1);
            index2 = version2.indexOf(".", index2Old + 1);
            component1 = index1 >= 0 ? version1lc.substring(index1Old + 1, index1) : version1lc.substring(index1Old + 1);
            component2 = index2 >= 0 ? version2lc.substring(index2Old + 1, index2) : version2lc.substring(index2Old + 1);
            if (!component1.equals(component2)) {
                if (component1.equals("head")) {
                    return 1;
                }
                if (component2.equals("head")) {
                    return -1;
                }
                int sub = Integer.valueOf(component1) - Integer.valueOf(component2);
                if (sub != 0) {
                    return sub;
                }
            }
            index1Old = index1;
            index2Old = index2;
        } while (index1 >= 0 && index2 >= 0);
        return 0;
    }

    /* Starts thermostat shell executes requested command in it,
     writes username/password, exits shell and returns output as line list */
    public static List<String> getShellCommandOutput(String target, String command) throws Exception {
        ThermostatQAConfig.Login login = ThermostatQAConfig.getClientLogin();
        String username = login.getUsername();
        String password = login.getPassword();

        String[] thermostatCmds = {ThermostatQAConfig.getThermostatExecutablePath(target), "shell"};
        Shell thermostat = new Shell(thermostatCmds);
        thermostat.setEnvironmentVariable("USER_THERMOSTAT_HOME", ThermostatQAConfig.getThermostatUserHome(target));
        thermostat.setStdOutMode(NativeProcess.MODE_LOG_AND_BUFFER);
        thermostat.start();
        thermostat.writeln(command);
        thermostat.writeln(username);
        thermostat.writeln(password);
        thermostat.writeln("exit");
        thermostat.waitFor();
        return thermostat.getStdoutLines();
    }

    public static List<String> getListVmsOutput() throws Exception {
        return getListVmsOutput("tested");
    }

    public static List<String> getListVmsOutput(String target) throws Exception {
        return getShellCommandOutput(target, "list-vms");
    }

    public static String getRunningVmLine() throws Exception {
        return ThermostatUtilities.getRunningVmLine("tested", null);
    }

    public static String getRunningVmLine(String name) throws Exception {
        return ThermostatUtilities.getRunningVmLine("tested", name);
    }

    public static String getRunningVmLine(String target, String name) throws Exception {
        List<String> vmsLines = ThermostatUtilities.getListVmsOutput(target);
        String vmLine = null;

        for (String line : vmsLines) {
            if (name == null) {
                if (line.contains("RUNNING") && !line.contains("com.redhat.thermostat.main.Thermostat") && !line.contains("org.netbeans.main")) {
                    vmLine = line;
                    break;
                }
            } else {
                if (line.contains("RUNNING") && line.contains(name)) {
                    vmLine = line;
                    break;
                }
            }
        }
        return vmLine;
    }

    public static List<String> getListHeapDumpsOutput(String target) throws Exception {
        return getShellCommandOutput(target, "list-heap-dumps");
    }

    public static String findLastHeapDump(String hostId, String vmId) throws Exception {
        return findLastHeapDump("tested", hostId, vmId);
    }

    public static String findLastHeapDump(String target, String hostId, String vmId) throws Exception {
        List<String> lines = getListHeapDumpsOutput(target);
        int start = 0;
        for (String currentLine : lines) {
            if (currentLine.contains("HOST ID") && currentLine.contains("VM ID") && currentLine.contains("HEAP ID")) {
                break;
            }
            ++start;
        }
        int size = lines.size();
        for (int i = size - 1; i > start; --i) {
            String line = lines.get(i);
            if (line.contains(hostId) && line.contains(vmId)) {
                return line;
            }
        }
        return null;
    }

    public static List<String> gc(String vmId) throws Exception {
        return getShellCommandOutput("tested", "gc --vmId " + vmId);
    }

    public static List<String> killVm(String hostId, String vmId) throws Exception {
        if (compareVersions(ThermostatQAConfig.getThermostatVersion("tested"), "head") == 0) {
            return getShellCommandOutput("tested", "kill-vm --vmId " + vmId);
        } else {
            return getShellCommandOutput("tested", "kill-vm --vmId " + vmId + " --hostId " + hostId);
        }
    }

    public static String getHeapDumpLine(String hostId, String vmId) throws Exception {
        return getHeapDumpLine("tested", hostId, vmId);
    }

    public static String getHeapDumpLine(String target, String hostId, String vmId) throws Exception {
        List<String> heapLines = ThermostatUtilities.getListHeapDumpsOutput(target);
        String heapLine = null;

        for (String line : heapLines) {
            if (line.contains(hostId) && line.contains(vmId)) {
                heapLine = line;
                break;
            }
        }
        return heapLine;
    }

    public static List<String> showHistogram(String heapId) throws Exception {
        return getShellCommandOutput("tested", "show-heap-histogram --heapId " + heapId);
    }

    public static List<String> findObjects(String heapId, String pattern) throws Exception {
        return getShellCommandOutput("tested", "find-objects --heapId " + heapId + " " + pattern);
    }

    public static List<String> findRoot(String heapId, String objectId) throws Exception {
        return getShellCommandOutput("tested", "find-root --objectId " + objectId + " --heapId " + heapId);
    }

    public static void dumpHeap(String hostId, String vmId) throws Exception {
        dumpHeap("tested", hostId, vmId);
    }

    public static void dumpHeap(String target, String hostId, String vmId) throws Exception {
        if (compareVersions(ThermostatQAConfig.getThermostatVersion(target), "head") == 0) {
            getShellCommandOutput(target, "dump-heap --vmId " + vmId);
        } else {
            getShellCommandOutput(target, "dump-heap --hostId " + hostId + " --vmId " + vmId);
        }
    }

    public static List<String> saveHeapDumpToFile(String heapId, String file) throws Exception {
        return getShellCommandOutput("tested", "save-heap-dump-to-file --file " + file + " --heapId " + heapId);
    }

    public static List<String> profileVm(String target, String hostId, String vmId, String action) throws Exception {
        return getShellCommandOutput(target, "profile-vm --hostId " + hostId + " --vmId " + vmId + " " + action);
    }

}