Rewrite managing of virtualenvs

Change-Id: Icd729b1ac66661f27dcaa8423fc2d72c45a027c7
diff --git a/src/com/mirantis/system_qa/SharedPipeline.groovy b/src/com/mirantis/system_qa/SharedPipeline.groovy
index 8390c1b..efd8d49 100644
--- a/src/com/mirantis/system_qa/SharedPipeline.groovy
+++ b/src/com/mirantis/system_qa/SharedPipeline.groovy
@@ -686,8 +686,34 @@
     writeFile(file: filename, text: script, encoding: "UTF-8")
 }
 
+
+def withVirtualEnv(name, pythonVersion, installRepo = "", command = "") {
+    /// name - str, name of virtualenv, name of folder which will be located in /home/jenkins
+    /// pythonVersion - str, version of python defined through the ShiningPanda settings in the Jenkins (Jenkins ->
+    //Manage Jenkins -> Global Tool Configuration -> Python Installations)
+    /// installRepo - repo with tool which should be installed in that virtualenv
+    def homeDir = "/home/jenkins"
+    venvPath = "${homeDir}/${name}"
+    // check if venv exists
+
+    withPythonEnv("${pythonVersion}") {
+        if (!fileExists(venvPath)) {
+            sh """
+                virtualenv --python=\$(which python) ${venvPath}
+                . ${venvPath}/bin/activate
+                pip install -U ${installRepo}
+            """
+        }
+        sh """
+        . ${venvPath}/bin/activate
+        ${command}
+        deactivate
+        """
+    }
+}
+
+
 def upload_results_to_testrail(report_name, testPlanName, testSuiteName, methodname, testrail_name_template, reporter_extra_options=[]) {
-  def venvPath = '/home/jenkins/venv_testrail_reporter'
   def testPlanDesc = env.LAB_CONFIG_NAME
   def testrailURL = "https://mirantis.testrail.com"
   def testrailProject = "Mirantis Cloud Platform"
@@ -713,8 +739,6 @@
   ] + reporter_extra_options
 
   def script = """
-    . ${venvPath}/bin/activate
-    set -ex
     report ${reporterOptions.join(' ')} ${report_name}
   """
 
@@ -726,17 +750,26 @@
              passwordVariable: 'TESTRAIL_PASSWORD',
              usernameVariable: 'TESTRAIL_USER']
   ]) {
-    def ret = [:]
-    ret.stdout = ''
-    ret.exception = ''
-    try {
-        ret.stdout = run_cmd_stdout(script)
-    } catch (Exception ex) {
-        ret.exception = ("""\
-##### Report to '${testSuiteName}' failed: #####\n""" + ex.message + "\n\n")
-    }
-    return ret
-  }
+
+        def ret = [:]
+        ret.stdout = ''
+        ret.exception = ''
+        try {
+            sshagent(credentials: ['maintenance-team-ssh']) {
+                withVirtualEnv(
+                    "venv_testrail_reporter",
+                    "python3.7",
+                    "git+ssh://maintenance-ci-robot@gerrit.mcp.mirantis.com:29418/mcp/testrail-reporter",
+                    script
+                )}
+//             ret.stdout = run_cmd_stdout(script)
+        } catch (Exception ex) {
+            ret.exception = ("""\
+    ##### Report to '${testSuiteName}' failed: #####\n""" + ex.message + "\n\n")
+        }
+
+        return ret
+      }
 }
 
 
@@ -750,15 +783,12 @@
 }
 
 def mark_test_results(testPlanName, testSuiteName) {
-    def venvPath = '/home/jenkins/venv_testrail_analyzer'
     def testrailURL = "https://mirantis.testrail.com"
     def testrailProject = "Mirantis Cloud Platform"
     def configName = env.LAB_CONFIG_NAME
     def testRunName = "${configName} <${testSuiteName}>"
 
     def script = """
-    . ${venvPath}/bin/activate
-    set -ex
     export TESTRAIL_URL=${testrailURL}
     testrail-reporter analyze -p "${testrailProject}" -t "${testPlanName}" -r "${testRunName}" checklist.yaml
     """
@@ -775,7 +805,11 @@
     ret.stdout = ''
     ret.exception = ''
     try {
-        ret.stdout = run_cmd_stdout(script)
+        withVirtualEnv("venv_testrail_analyzer",
+                       "python3.7",
+                       "git+https://review.gerrithub.io/ibumarskov/testrail-reporter",
+                       script)
+//         ret.stdout = run_cmd_stdout(script)
     } catch (Exception ex) {
         ret.exception = ("""\
 ##### Report to '${testRunName}' failed: #####\n""" + ex.message + "\n\n")
@@ -783,6 +817,7 @@
     return ret
   }
 }
+
 def reboot_hardware_nodes(creds_name) {
     bm_ips_file = readFile("tcp_tests/templates/${creds_name}")
     bm_ips = bm_ips_file.split("\n")