view src/org/thermostat/qa2/framework/services/ShellService.java @ 192:5d335809cd51

Test fixes changes: - Broken CliClientAgentInfoTest is now using more reliable way to find agent-id. - Now, when pattern is not found in tomcat log(s), it is printed, so that problem can be more easily found and fixed. - Fixing problem with race condition caused by asynchronous nature of shutdown tomcat script - Making ShellService class cleaner and more robust
author Zdenek Zambersky <zzambers@redhat.com>
date Tue, 14 Jul 2015 19:00:56 +0200
parents 0d34a379de82
children a74aa91ab44c
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.services;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.thermostat.qa2.framework.NativeProcess;
import org.thermostat.qa2.framework.Shell;
import org.thermostat.qa2.framework.utils.CommonUtilities;
import org.thermostat.qa2.framework.utils.FileUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
public abstract class ShellService extends AbstractProcessService {

    String commands;
    Map<String, String> environment = new HashMap();
    String pidFile;

    public ShellService(String... commands) {
        this.commands = "";
        for (String command : commands) {
            this.commands += command + " ";
        }
    }

    public void addEnvVariable(String name, String value) {
        environment.put(name, value);
    }

    @Override
    public NativeProcess startServiceProcess() throws Exception {
        pidFile = "." + File.separator + getServiceName().replace(" ", "_") + ".pid";
        pidFile = FileUtilities.getUniqueFile(pidFile);
        Shell shell = new Shell(File.separator + "bin" + File.separator + "bash");
        shell.setLabel(getServiceName());
        shell.start();
        // enable job controll so that service process runs in separate process group
        shell.writeln("set -m");
        for (Entry<String, String> entry : environment.entrySet()) {
            shell.writeln("export " + entry.getKey() + "=" + entry.getValue());
        }
        shell.writeln(commands + " &");
        shell.writeln("PROCESS_PID=$!");
        shell.writeln("echo ${PROCESS_PID} > " + pidFile);
        shell.writeln("wait ${PROCESS_PID}");
        shell.writeln("exit $?");
        return shell;
    }

    @Override
    public void stopServiceImpl() throws Exception {
        int timeout = 30;
        for (int i = 0; i < timeout; ++i) {
            if (!this.isRunning()) {
                // stopped itself sooner then can be killed
                break;
            } else if (new File(pidFile).exists()) { // pid file exists
                List<String> list = FileUtilities.getLineListFromFile(pidFile);
                String pid;
                // pid file was written to
                if (list.size() > 0 && (pid = list.get(0)) != null && pid.length() > 0) {
                    // send TERM signal to process group of service process
                    NativeProcess process = new NativeProcess("kill", "--", "-" + pid);
                    process.setLabel(getServiceName());
                    process.start();
                    process.waitForRaw();
                    break;
                }
            }
            CommonUtilities.sleep(1000);
        }
        thread.join();
    }

    @Override
    public void runAfterInWrappingThread() throws Exception {
        NativeProcess process = new NativeProcess("rm", pidFile);
        process.setLabel(getServiceName());
        process.start();
        process.waitForRaw();
    }

}