aboutsummaryrefslogtreecommitdiff
path: root/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/Shell.groovy
blob: bddff008b2d4c36810dcf1485f3bf9de002c9f49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.bigtop.itest.shell

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

class Shell {
  static private Log LOG = LogFactory.getLog(Shell.class)

  static private final String DEFAULT_SHELL = "/bin/bash -s";

  String shell;
  String user;

  String script;
  List<String> out;
  List<String> err;
  int ret;

  Shell(String sh) {
    shell = sh;
  }

  Shell(String sh, String u) {
    shell = sh;
    user = u;
  }

  Shell() {
    shell = DEFAULT_SHELL;
  }

  void setUser(String u) {
    user = u
  }

  /**
   * Execute shell script consisting of as many Strings as we have arguments,
   * possibly under an explicit username (requires sudoers privileges).
   * NOTE: individual strings are concatenated into a single script as though
   * they were delimited with new line character. All quoting rules are exactly
   * what one would expect in standalone shell script.
   *
   * After executing the script its return code can be accessed as getRet(),
   * stdout as getOut() and stderr as getErr(). The script itself can be accessed
   * as getScript()
   * WARNING: it isn't thread safe
   * @param args shell script split into multiple Strings
   * @return Shell object for chaining
   */
  Shell exec(Object... args) {
    def proc = user ? "sudo -u $user PATH=${System.getenv('PATH')} $shell".execute() :
                                    "$shell".execute()
    script = args.join("\n")
    if (LOG.isTraceEnabled()) {
        LOG.trace("${shell} << __EOT__\n${script}\n__EOT__");
    }

    Thread.start {
      def writer = new PrintWriter(new BufferedOutputStream(proc.out))
      writer.println(script)
      writer.close()
    }
    ByteArrayOutputStream baosErr = new ByteArrayOutputStream(4096);
    proc.consumeProcessErrorStream(baosErr);
    out = proc.in.readLines()

    // Possibly a bug in String.split as it generates a 1-element array on an
    // empty String
    if (baosErr.size() != 0) {
      err = baosErr.toString().split('\n');
    }
    else {
      err = new ArrayList<String>();
    }

    proc.waitFor()
    ret = proc.exitValue()

    if (LOG.isTraceEnabled()) {
        if (ret != 0) {
           LOG.trace("return: $ret");
        }
        if (out.size() != 0) {
           LOG.trace("\n<stdout>\n${out.join('\n')}\n</stdout>");
        }
        if (err.size() != 0) {
           LOG.trace("\n<stderr>\n${err.join('\n')}\n</stderr>");
        }
    }

    return this
  }

  /**
   * Fix up the return code so that a value of 255 is mapped back to -1
   * @return twos complement return code from an unsigned byte
   */
  int signCorrectedReturnCode() {
    return (ret << 24) >> 24
  }

  /**
   * String operation returns the exit code and any script built up
   * @return a description of the contents of the instance.
   */
  @Override
  String toString() {
    return signCorrectedReturnCode() + " =>\"" + (script ?: "(no script)") +"\""
  }

  /**
   * Dump the command, return code and outputs to the log.
   * stdout is logged at info; stderr at error.
   */
  void dumpOutput() {
    LOG.error(toString())
    LOG.error("return code = ${signCorrectedReturnCode()}")
    if (out.size() != 0) {
      LOG.info("\n<stdout>\n${out.join('\n')}\n</stdout>");
    }
    if (err.size() != 0) {
      LOG.error("\n<stderr>\n${err.join('\n')}\n</stderr>");
    }
  }

}