Refactor var functions of Pipeline library

Move all named functions under com.mirantis.mcp package

Change-Id: I98002e038173fce2325d069e951b221c61109e69
diff --git a/src/com/mirantis/mcp/Calico.groovy b/src/com/mirantis/mcp/Calico.groovy
new file mode 100644
index 0000000..eaa3360
--- /dev/null
+++ b/src/com/mirantis/mcp/Calico.groovy
@@ -0,0 +1,122 @@
+package com.mirantis.mcp
+
+/**
+ * Build Calico containers
+ *
+ * @param body Closure
+ *        body includes next parameters:
+ *          - dockerRepo String, repo with docker images
+ *          - artifactoryUrl String, URL to repo with calico-binaries
+ *          - imageTag String, tag of images
+ *          - nodeImage String, Calico Node image name
+ *          - ctlImage String, Calico CTL image name
+ *          - buildImage String, Calico Build image name
+ *          - felixImage String, Calico Felix image name
+ *          - confdBuildId String, Version of Calico Confd
+ *          - confdUrl String, URL to Calico Confd
+ *          - birdUrl, URL to Calico Bird
+ *          - birdBuildId, Version of Calico Bird
+ *          - bird6Url, URL to Calico Bird6
+ *          - birdclUrl, URL to Calico BirdCL
+ *
+ * Usage example:
+ *
+ * def calicoFunc = new com.mirantis.mcp.Calico()
+ * calicoFunc.buildCalicoContainers {
+ *     dockerRepo = 'mcp-k8s-ci.docker.mirantis.net'
+ *     artifactoryURL = 'https://artifactory.mcp.mirantis.net/artifactory/sandbox'
+ *     nodeImage = 'mcp-k8s-ci.docker.mirantis.net/calico/node'
+ *     ctlImage = 'mcp-k8s-ci.docker.mirantis.net/calico/ctl'
+ * }
+ *
+ */
+def buildCalicoContainers = { body ->
+  // evaluate the body block, and collect configuration into the object
+  def config = [:]
+  body.resolveStrategy = Closure.DELEGATE_FIRST
+  body.delegate = config
+  body()
+
+
+  def dockerRepo = config.dockerRepo
+  def projectNamespace = "mirantis/projectcalico"
+  def artifactoryUrl = config.artifactoryURL
+
+  if (! dockerRepo ) {
+      error('dockerRepo parameter have to be set.')
+  }
+
+  if (! artifactoryUrl ) {
+      error('artifactoryUrl parameter have to be set.')
+  }
+
+  def imgTag = config.imageTag ?: getGitDescribe(true) + "-" + getDatetime()
+
+  def nodeImage = config.nodeImage ?: "${dockerRepo}/${projectNamespace}/calico/node"
+  def nodeName = "${nodeImage}:${imgTag}"
+
+  def ctlImage = config.ctlImage ?: "${dockerRepo}/${projectNamespace}/calico/ctl"
+  def ctlName = "${ctlImage}:${imgTag}"
+
+   // calico/build goes from libcalico
+  def buildImage = config.buildImage ?: "${dockerRepo}/${projectNamespace}/calico/build:latest"
+  // calico/felix goes from felix
+  def felixImage = config.felixImage ?: "${dockerRepo}/${projectNamespace}/calico/felix:latest"
+
+  def confdBuildId = config.confdBuildId ?: "${artifactoryUrl}/${projectNamespace}/confd/latest".toURL().text.trim()
+  def confdUrl = config.confdUrl ?: "${artifactoryUrl}/${projectNamespace}/confd/confd-${confdBuildId}"
+
+  def birdBuildId = config.birdBuildId ?: "${artifactoryUrl}/${projectNamespace}/bird/latest".toURL().text.trim()
+  def birdUrl = config.birdUrl ?: "${artifactoryUrl}/${projectNamespace}/bird/bird-${birdBuildId}"
+  def bird6Url = config.bird6Url ?: "${artifactoryUrl}/${projectNamespace}/bird/bird6-${birdBuildId}"
+  def birdclUrl = config.birdclUrl ?: "${artifactoryUrl}/${projectNamespace}/bird/birdcl-${birdBuildId}"
+
+  // add LABELs to dockerfiles
+  setDockerfileLabels("./calicoctl/Dockerfile.calicoctl",
+                     ["docker.imgTag=${imgTag}",
+                      "calico.buildImage=${buildImage}",
+                      "calico.birdclUrl=${birdclUrl}"])
+
+  setDockerfileLabels("./calico_node/Dockerfile",
+                     ["docker.imgTag=${imgTag}",
+                      "calico.buildImage=${buildImage}",
+                      "calico.felixImage=${felixImage}",
+                      "calico.confdUrl=${confdUrl}",
+                      "calico.birdUrl=${birdUrl}",
+                      "calico.bird6Url=${bird6Url}",
+                      "calico.birdclUrl=${birdclUrl}"])
+
+  // Start build section
+  stage ('Build calico/ctl image'){
+    sh """
+      make calico/ctl \
+        CTL_CONTAINER_NAME=${ctlName} \
+        PYTHON_BUILD_CONTAINER_NAME=${buildImage} \
+        BIRDCL_URL=${birdclUrl}
+    """
+  }
+
+
+  stage('Build calico/node'){
+    sh """
+      make calico/node \
+        NODE_CONTAINER_NAME=${nodeName} \
+        PYTHON_BUILD_CONTAINER_NAME=${buildImage} \
+        FELIX_CONTAINER_NAME=${felixImage} \
+        CONFD_URL=${confdUrl} \
+        BIRD_URL=${birdUrl} \
+        BIRD6_URL=${bird6Url} \
+        BIRDCL_URL=${birdclUrl}
+    """
+  }
+
+
+  return [
+    CTL_CONTAINER_NAME:"${ctlName}",
+    NODE_CONTAINER_NAME:"${nodeName}",
+    CALICO_NODE_IMAGE_REPO:"${nodeImage}",
+    CALICOCTL_IMAGE_REPO:"${ctlImage}",
+    CALICO_VERSION: "${imgTag}"
+  ]
+
+}
diff --git a/src/com/mirantis/mcp/Common.groovy b/src/com/mirantis/mcp/Common.groovy
index 847c851..c8c5ea5 100644
--- a/src/com/mirantis/mcp/Common.groovy
+++ b/src/com/mirantis/mcp/Common.groovy
@@ -17,4 +17,16 @@
 def getDatetime(format = "yyyyMMddHHmmss") {
     def now = new Date();
     return now.format(format, TimeZone.getTimeZone('UTC'));
-}
\ No newline at end of file
+}
+
+/**
+ * Run tox with or without specified environment
+ * @param env String, name of environment
+ */
+def runTox(String env = null) {
+  if (env) {
+    sh "tox -v -e ${env}"
+  } else {
+    sh "tox -v"
+  }
+}
diff --git a/src/com/mirantis/mcp/Git.groovy b/src/com/mirantis/mcp/Git.groovy
index 0cb9533..4bf2056 100644
--- a/src/com/mirantis/mcp/Git.groovy
+++ b/src/com/mirantis/mcp/Git.groovy
@@ -33,3 +33,119 @@
     }
     return git_commit
 }
+
+/**
+ * Execute git clone+checkout stage for some project,
+ * through SSH
+ *
+ * @param body Closure
+ *        body includes next parameters:
+ *          - credentialsId, id of user which should make checkout
+ *          - branch, branch of project
+ *          - host, gerrit-ci hostname
+ *          - project, name of project
+ *          - targetDir, target directory of cloned repo
+ *          - withMerge, prevent detached mode in repo
+ *
+ * Usage example:
+ *
+ * def gitFunc = new com.mirantis.mcp.Git()
+ * gitFunc.gitSSHCheckout {
+ *   credentialsId = 'mcp-ci-gerrit'
+ *   branch = 'mcp-0.1'
+ *   host = 'ci.mcp-ci.local'
+ *   project = 'project'
+ * }
+ */
+def gitSSHCheckout = { body ->
+  // evaluate the body block, and collect configuration into the object
+  def config = [:]
+  body.resolveStrategy = Closure.DELEGATE_FIRST
+  body.delegate = config
+  body()
+
+  def merge = config.withMerge ?: false
+  def targetDir = config.targetDir ?: "./"
+  def port = config.port ?: "29418"
+
+  // default parameters
+  def scmExtensions = [
+    [$class: 'CleanCheckout'],
+    [$class: 'RelativeTargetDirectory', relativeTargetDir: "${targetDir}"]
+  ]
+
+  // https://issues.jenkins-ci.org/browse/JENKINS-6856
+  if (merge) {
+    scmExtensions.add([$class: 'LocalBranch', localBranch: "${config.branch}"])
+  }
+
+  checkout(
+    scm: [
+      $class: 'GitSCM',
+      branches: [[name: "${config.branch}"]],
+      extensions: scmExtensions,
+      userRemoteConfigs: [[
+        credentialsId: "${config.credentialsId}",
+        name: 'origin',
+        url: "ssh://${config.credentialsId}@${config.host}:${port}/${config.project}.git"
+      ]]
+    ]
+  )
+}
+
+/**
+ * Execute git clone and checkout stage from gerrit review
+ *
+ * @param body Closure
+ *        body includes next parameters:
+ *          - credentialsId, id of user which should make checkout
+ *          - withMerge, prevent detached mode in repo
+ *          - withWipeOut, wipe repository and force clone
+ *
+ * Usage example:
+ *
+ * def gitFunc = new com.mirantis.mcp.Git()
+ * gitFunc.gerritPatchsetCheckout {
+ *   credentialsId = 'mcp-ci-gerrit'
+ *   withMerge = true
+ * }
+ */
+def gerritPatchsetCheckout = { body ->
+  // evaluate the body block, and collect configuration into the object
+  def config = [:]
+  body.resolveStrategy = Closure.DELEGATE_FIRST
+  body.delegate = config
+  body()
+
+
+  def merge = config.withMerge ?: false
+  def wipe = config.withWipeOut ?: false
+
+  // default parameters
+  def scmExtensions = [
+    [$class: 'CleanCheckout'],
+    [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
+  ]
+  // if we need to "merge" code from patchset to GERRIT_BRANCH branch
+  if (merge) {
+    scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
+  }
+  // we need wipe workspace before checkout
+  if (wipe) {
+    scmExtensions.add([$class: 'WipeWorkspace'])
+  }
+
+  checkout(
+    scm: [
+      $class: 'GitSCM',
+      branches: [[name: "${GERRIT_BRANCH}"]],
+      extensions: scmExtensions,
+      userRemoteConfigs: [[
+        credentialsId: "${config.credentialsId}",
+        name: 'gerrit',
+        url: "ssh://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git",
+        refspec: "${GERRIT_REFSPEC}"
+      ]]
+    ]
+  )
+}