Add waiter to run_build method

Change-Id: Id2afcb0b79512883fdb0c66a2b069a89e7ac85ba
diff --git a/src/com/mirantis/system_qa/SharedPipeline.groovy b/src/com/mirantis/system_qa/SharedPipeline.groovy
index 0483965..53927f7 100644
--- a/src/com/mirantis/system_qa/SharedPipeline.groovy
+++ b/src/com/mirantis/system_qa/SharedPipeline.groovy
@@ -7,7 +7,7 @@
     def common = new com.mirantis.mk.Common()
     common.printMsg("Run shell command:\n" + cmd, "blue")
     def VENV_PATH='/home/jenkins/fuel-devops30'
-    script = """\
+    def script = """\
         set -ex;
         . ${VENV_PATH}/bin/activate;
         bash -c '${cmd.stripIndent()}'
@@ -20,18 +20,24 @@
     common.printMsg("Run shell command:\n" + cmd, "blue")
     def VENV_PATH='/home/jenkins/fuel-devops30'
     def stderr_path = "/tmp/${JOB_NAME}_${BUILD_NUMBER}_stderr.log"
-    script = """\
-        set +x;
-        echo 'activate python virtualenv ${VENV_PATH}';
-        . ${VENV_PATH}/bin/activate;
-        bash -c 'set -ex; set -ex; ${cmd.stripIndent()}' 2>${stderr_path}
+    def script = """#!/bin/bash
+        set +x
+        echo 'activate python virtualenv ${VENV_PATH}'
+        . ${VENV_PATH}/bin/activate
+        bash -c -e -x '${cmd.stripIndent()}' 2>${stderr_path}
     """
-    def result
     try {
-        return sh(script: script, returnStdout: returnStdout)
+        def stdout = sh(script: script, returnStdout: returnStdout)
+        def stderr = readFile("${stderr_path}")
+        def error_message = "\n<<<<<< STDERR: >>>>>>\n" + stderr
+        common.printMsg(error_message, "yellow")
+        common.printMsg("", "reset")
+        return stdout
     } catch (e) {
         def stderr = readFile("${stderr_path}")
         def error_message = e.message + "\n<<<<<< STDERR: >>>>>>\n" + stderr
+        common.printMsg(error_message, "red")
+        common.printMsg("", "reset")
         throw new Exception(error_message)
     } finally {
         sh(script: "rm ${stderr_path} || true")
diff --git a/tcp_tests/managers/jenkins/client.py b/tcp_tests/managers/jenkins/client.py
index 6404432..39c698e 100644
--- a/tcp_tests/managers/jenkins/client.py
+++ b/tcp_tests/managers/jenkins/client.py
@@ -1,4 +1,5 @@
 from __future__ import print_function
+import datetime
 import time
 
 import jenkins
@@ -81,6 +82,24 @@
         num = self.__client.build_job(name, params)
         time.sleep(2)  # wait while job is started
 
+        def is_build_queued():
+            try:
+                item = self.__client.get_queue_item(num)
+                ts = item['inQueueSince'] / 1000
+                since_time = datetime.datetime.fromtimestamp(ts)
+                print("Build in the queue since {}".format(since_time))
+                return True
+            except jenkins.JenkinsException:
+                if verbose:
+                    print("Build have not been queued {} yet".format(num))
+
+        helpers.wait(
+            is_build_queued,
+            timeout=timeout,
+            interval=30,
+            timeout_msg='Timeout waiting to queue the build '
+                        'for {} job'.format(name))
+
         def is_blocked():
             queued = self.__client.get_queue_item(num)
             status = not queued['blocked']
@@ -96,6 +115,26 @@
             interval=30,
             timeout_msg='Timeout waiting to run the job [{}]'.format(name))
         build_id = self.__client.get_queue_item(num)['executable']['number']
+
+        def is_build_started():
+            try:
+                build = self.__client.get_build_info(name, build_id)
+                ts = float(build['timestamp']) / 1000
+                start_time = datetime.datetime.fromtimestamp(ts)
+                print("the build {} in {} have started at {} UTC".format(
+                    build_id, name, start_time))
+                return True
+            except jenkins.JenkinsException:
+                if verbose:
+                    print("the build {} in {} have not strated yet".format(
+                        build_id, name))
+        helpers.wait(
+            is_build_started,
+            timeout=timeout,
+            interval=30,
+            timeout_msg='Timeout waiting to run build of '
+                        'the job [{}]'.format(name))
+
         return name, build_id
 
     def wait_end_of_build(self, name, build_id, timeout=600, interval=5,