Merge "Implemented batch option on salt states"
diff --git a/src/com/mirantis/mk/Debian.groovy b/src/com/mirantis/mk/Debian.groovy
index ac600e4..4b0c765 100644
--- a/src/com/mirantis/mk/Debian.groovy
+++ b/src/com/mirantis/mk/Debian.groovy
@@ -41,9 +41,9 @@
             apt-get update && apt-get install -y build-essential devscripts equivs sudo &&
             groupadd -g ${jenkinsGID} jenkins &&
             useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
-            sudo -H -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} &&
+            sudo -H -E -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} &&
             mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control
-            sudo -H -u jenkins debuild --no-lintian -uc -us -b'""")
+            sudo -H -E -u jenkins debuild --no-lintian -uc -us -b'""")
 }
 
 /*
@@ -92,7 +92,7 @@
  * @param image Image name to use for build (default debian:sid)
  * @param snapshot Generate snapshot version (default false)
  */
-def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") {
+def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitName='Jenkins', gitEmail='jenkins@dummy.org', revisionPostfix="") {
     def common = new com.mirantis.mk.Common()
     def jenkinsUID = common.getJenkinsUid()
     def jenkinsGID = common.getJenkinsGid()
@@ -111,8 +111,8 @@
             groupadd -g ${jenkinsGID} jenkins &&
             useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
             cd ${dir} &&
-            sudo -H -u jenkins git config --global user.name "${gitName}" &&
-            sudo -H -u jenkins git config --global user.email "${gitEmail}" &&
+            sudo -H -E -u jenkins git config --global user.name "${gitName}" &&
+            sudo -H -E -u jenkins git config --global user.email "${gitEmail}" &&
             [[ "${snapshot}" == "false" ]] || (
                 VERSION=`dpkg-parsechangelog --count 1 | grep Version: | sed "s,Version: ,,g"` &&
                 UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` &&
@@ -124,16 +124,16 @@
                     NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" &&
                     NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix &&
                     echo "Generating new upstream version \$NEW_UPSTREAM_VERSION" &&
-                    sudo -H -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH &&
-                    sudo -H -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION
+                    sudo -H -E -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH &&
+                    sudo -H -E -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION
                 else
                     NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix
                 fi &&
-                sudo -H -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution &&
-                sudo -H -u jenkins git add -u debian/changelog &&
-                sudo -H -u jenkins git commit -m "New snapshot version \$NEW_VERSION"
+                sudo -H -E -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution &&
+                sudo -H -E -u jenkins git add -u debian/changelog &&
+                sudo -H -E -u jenkins git commit -m "New snapshot version \$NEW_VERSION"
             ) &&
-            sudo -H -u jenkins gbp buildpackage -nc --git-force-create --git-notify=false --git-ignore-branch --git-ignore-new --git-verbose --git-export-dir=../build-area -sa -S -uc -us '""")
+            sudo -H -E -u jenkins gbp buildpackage -nc --git-force-create --git-notify=false --git-ignore-branch --git-ignore-new --git-verbose --git-export-dir=../build-area -sa -S -uc -us '""")
 }
 
 /*
diff --git a/src/com/mirantis/mk/Git.groovy b/src/com/mirantis/mk/Git.groovy
index 22ca53d..1c8b19f 100644
--- a/src/com/mirantis/mk/Git.groovy
+++ b/src/com/mirantis/mk/Git.groovy
@@ -48,20 +48,6 @@
 }
 
 /**
- * Get remote URL
- *
- * @param name  Name of remote (default any)
- * @param type  Type (fetch or push, default fetch)
- */
-def getGitRemote(name = '', type = 'fetch') {
-    gitRemote = sh (
-        script: "git remote -v | grep '${name}' | grep ${type} | awk '{print \$2}' | head -1",
-        returnStdout: true
-    ).trim()
-    return gitRemote
-}
-
-/**
  * Change actual working branch of repo
  *
  * @param path            Path to the git repository
@@ -78,13 +64,48 @@
 }
 
 /**
+ * Get remote URL
+ *
+ * @param name  Name of remote (default any)
+ * @param type  Type (fetch or push, default fetch)
+ */
+def getGitRemote(name = '', type = 'fetch') {
+    gitRemote = sh (
+        script: "git remote -v | grep '${name}' | grep ${type} | awk '{print \$2}' | head -1",
+        returnStdout: true
+    ).trim()
+    return gitRemote
+}
+
+/**
+ * Create new working branch for repo
+ *
+ * @param path            Path to the git repository
+ * @param branch          Branch desired to switch to
+ */
+def createGitBranch(path, branch) {
+    def git_cmd
+    dir(path) {
+        git_cmd = sh (
+            script: "git checkout -b ${branch}",
+            returnStdout: true
+        ).trim()
+    }
+    return git_cmd
+}
+
+/**
  * Commit changes to the git repo
  *
  * @param path            Path to the git repository
  * @param message         A commit message
  */
 def commitGitChanges(path, message) {
+    def git_cmd
     dir(path) {
+        sh "git config --global user.email 'jenkins@localhost'"
+        sh "git config --global user.name 'jenkins-slave'"
+
         sh(
             script: 'git add -A',
             returnStdout: true
@@ -101,20 +122,25 @@
 /**
  * Push git changes to remote repo
  *
- * @param path            Path to the git repository
+ * @param path            Path to the local git repository
  * @param branch          Branch on the remote git repository
  * @param remote          Name of the remote repository
+ * @param credentialsId   Credentials with write permissions
  */
-def pushGitChanges(path, branch = 'master', remote = 'origin') {
+def pushGitChanges(path, branch = 'master', remote = 'origin', credentialsId = null) {
+    def ssh = new com.mirantis.mk.Ssh()
     dir(path) {
-        git_cmd = sh(
-            script: "git push ${remote} ${branch}",
-            returnStdout: true
-        ).trim()
+        if (credentialsId == null) {
+            sh script: "git push ${remote} ${branch}"
+        }
+        else {
+            ssh.prepareSshAgentKey(credentialsId)
+            ssh.runSshAgentCommand("git push ${remote} ${branch}")
+        }
     }
-    return git_cmd
 }
 
+
 /**
  * Mirror git repository, merge target changes (downstream) on top of source
  * (upstream) and push target or both if pushSource is true
diff --git a/src/com/mirantis/mk/Python.groovy b/src/com/mirantis/mk/Python.groovy
index c38347c..1844d42 100644
--- a/src/com/mirantis/mk/Python.groovy
+++ b/src/com/mirantis/mk/Python.groovy
@@ -226,14 +226,14 @@
  *
  * @param path        Path where virtualenv is created
  */
-def buildCookiecutterTemplate (template, context, path = none) {
-    contextFile = "default_context.json"
-    contextString = "parameters:\n"
+def buildCookiecutterTemplate(template, context, outputDir = '.', path = none) {
+    configFile = "default_config.yaml"
+    configString = "default_context:\n"
     for (parameter in context) {
-      contextString = "${contextString}  ${parameter.key}: ${parameter.value}\n"
+      configString = "${configString}  ${parameter.key}: ${parameter.value}\n"
     }
-    writeFile file: contextFile, text: contextString
-    command = ". ./${work_dir}/bin/activate; cookiecutter --config-file ${cookiecutter_context_file} --overwrite-if-exists --verbose --no-input ${template_dir}"
+    writeFile file: configFile, text: configString
+    command = ". ${path}/bin/activate; cookiecutter --config-file ${configFile} --output-dir ${outputDir} --overwrite-if-exists --verbose --no-input ${template}"
     output = sh (returnStdout: true, script: command)
     echo("[Cookiecutter build] Output: ${output}")
 }