Cleanup ssh and git functions
Change-Id: Ic6a3f5c484167275dbd449a5ff7246016053eadf
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 881f706..ffa72d3 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -17,17 +17,6 @@
}
/**
- * Parse HEAD of current directory and return commit hash
- */
-def getGitCommit() {
- git_commit = sh (
- script: 'git rev-parse HEAD',
- returnStdout: true
- ).trim()
- return git_commit
-}
-
-/**
* Return workspace.
* Currently implemented by calling pwd so it won't return relevant result in
* dir context
@@ -218,106 +207,6 @@
throw new Exception("Could not find credentials for ID ${id}")
}
-/**
- * Setup ssh agent and add private key
- *
- * @param credentialsId Jenkins credentials name to lookup private key
- */
-def prepareSshAgentKey(credentialsId) {
- c = getSshCredentials(credentialsId)
- sh("test -d ~/.ssh || mkdir -m 700 ~/.ssh")
- sh('pgrep -l -u $USER -f | grep -e ssh-agent\$ >/dev/null || ssh-agent|grep -v "Agent pid" > ~/.ssh/ssh-agent.sh')
- sh("set +x; echo '${c.getPrivateKey()}' > ~/.ssh/id_rsa_${credentialsId} && chmod 600 ~/.ssh/id_rsa_${credentialsId}; set -x")
- agentSh("ssh-add ~/.ssh/id_rsa_${credentialsId}")
-}
-
-/**
- * Execute command with ssh-agent
- *
- * @param cmd Command to execute
- */
-def agentSh(cmd) {
- sh(". ~/.ssh/ssh-agent.sh && ${cmd}")
-}
-
-/**
- * Ensure entry in SSH known hosts
- *
- * @param url url of remote host
- */
-def ensureKnownHosts(url) {
- def hostArray = getKnownHost(url)
- sh "test -f ~/.ssh/known_hosts && grep ${hostArray[0]} ~/.ssh/known_hosts || ssh-keyscan -p ${hostArray[1]} ${hostArray[0]} >> ~/.ssh/known_hosts"
-}
-
-@NonCPS
-def getKnownHost(url){
- // test for git@github.com:organization/repository like URLs
- def p = ~/.+@(.+\..+)\:{1}.*/
- def result = p.matcher(url)
- def host = ""
- if (result.matches()) {
- host = result.group(1)
- port = 22
- } else {
- parsed = new URI(url)
- host = parsed.host
- port = parsed.port && parsed.port > 0 ? parsed.port: 22
- }
- return [host,port]
-}
-
-/**
- * Mirror git repository, merge target changes (downstream) on top of source
- * (upstream) and push target or both if pushSource is true
- *
- * @param sourceUrl Source git repository
- * @param targetUrl Target git repository
- * @param credentialsId Credentials id to use for accessing source/target
- * repositories
- * @param branches List or comma-separated string of branches to sync
- * @param followTags Mirror tags
- * @param pushSource Push back into source branch, resulting in 2-way sync
- * @param pushSourceTags Push target tags into source or skip pushing tags
- * @param gitEmail Email for creation of merge commits
- * @param gitName Name for creation of merge commits
- */
-def mirrorGit(sourceUrl, targetUrl, credentialsId, branches, followTags = false, pushSource = false, pushSourceTags = false, gitEmail = 'jenkins@localhost', gitName = 'Jenkins') {
- if (branches instanceof String) {
- branches = branches.tokenize(',')
- }
-
- prepareSshAgentKey(credentialsId)
- ensureKnownHosts(targetUrl)
- sh "git config user.email '${gitEmail}'"
- sh "git config user.name '${gitName}'"
-
- sh "git remote | grep target || git remote add target ${TARGET_URL}"
- agentSh "git remote update --prune"
-
- for (i=0; i < branches.size; i++) {
- branch = branches[i]
- sh "git branch | grep ${branch} || git checkout -b ${branch} origin/${branch}"
- sh "git branch | grep ${branch} && git checkout ${branch} && git reset --hard origin/${branch}"
-
- sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'"
- followTagsArg = followTags ? "--follow-tags" : ""
- agentSh "git push ${followTagsArg} target HEAD:${branch}"
-
- if (pushSource == true) {
- followTagsArg = followTags && pushSourceTags ? "--follow-tags" : ""
- agentSh "git push ${followTagsArg} origin HEAD:${branch}"
- }
- }
-
- if (followTags == true) {
- agentSh "git push target --tags"
-
- if (pushSourceTags == true) {
- agentSh "git push origin --tags"
- }
- }
-}
/**
* Tests Jenkins instance for existence of plugin with given name
@@ -415,4 +304,4 @@
}
}
}
-}
\ No newline at end of file
+}
diff --git a/src/com/mirantis/mk/Git.groovy b/src/com/mirantis/mk/Git.groovy
index 55218cc..4bfc3d7 100644
--- a/src/com/mirantis/mk/Git.groovy
+++ b/src/com/mirantis/mk/Git.groovy
@@ -40,6 +40,20 @@
}
/**
+ * Get remote URL
+ *
+ * @param name Name of remote (default origin)
+ * @param type Type (fetch or push, default fetch)
+ */
+def getGitRemote(name = 'origin', type = 'fetch') {
+ gitRemote = sh (
+ script: "git remote | grep ${name} | grep ${type} | awk '{print \$2}'",
+ returnStdout: true
+ ).trim()
+ return gitRemote
+}
+
+/**
* Change actual working branch of repo
*
* @param path Path to the git repository
@@ -118,27 +132,54 @@
}
/**
- * Mirror git repository
+ * Mirror git repository, merge target changes (downstream) on top of source
+ * (upstream) and push target or both if pushSource is true
+ *
+ * @param sourceUrl Source git repository
+ * @param targetUrl Target git repository
+ * @param credentialsId Credentials id to use for accessing source/target
+ * repositories
+ * @param branches List or comma-separated string of branches to sync
+ * @param followTags Mirror tags
+ * @param pushSource Push back into source branch, resulting in 2-way sync
+ * @param pushSourceTags Push target tags into source or skip pushing tags
+ * @param gitEmail Email for creation of merge commits
+ * @param gitName Name for creation of merge commits
*/
-def mirrorReporitory(sourceUrl, targetUrl, credentialsId, branches, followTags = false, gitEmail = 'jenkins@localhost', gitUsername = 'Jenkins') {
- def ssl = new com.mirantis.mk.Ssl()
+def mirrorGit(sourceUrl, targetUrl, credentialsId, branches, followTags = false, pushSource = false, pushSourceTags = false, gitEmail = 'jenkins@localhost', gitName = 'Jenkins') {
if (branches instanceof String) {
branches = branches.tokenize(',')
}
- ssl.prepareSshAgentKey(credentialsId)
- ssl.ensureKnownHosts(targetUrl)
- sh "git remote | grep target || git remote add target ${targetUrl}"
- agentSh "git remote update --prune"
+ def ssh = new com.mirantis.mk.Ssh()
+ ssh.prepareSshAgentKey(credentialsId)
+ ssh.ensureKnownHosts(targetUrl)
+ sh "git config user.email '${gitEmail}'"
+ sh "git config user.name '${gitName}'"
+
+ sh "git remote | grep target || git remote add target ${TARGET_URL}"
+ ssh.agentSh "git remote update --prune"
+
for (i=0; i < branches.size; i++) {
branch = branches[i]
sh "git branch | grep ${branch} || git checkout -b ${branch} origin/${branch}"
sh "git branch | grep ${branch} && git checkout ${branch} && git reset --hard origin/${branch}"
- sh "git config --global user.email '${gitEmail}'"
- sh "git config --global user.name '${gitUsername}'"
sh "git ls-tree target/${branch} && git merge --no-edit --ff target/${branch} || echo 'Target repository is empty, skipping merge'"
followTagsArg = followTags ? "--follow-tags" : ""
- agentSh "git push ${followTagsArg} target HEAD:${branch}"
+ ssh.agentSh "git push ${followTagsArg} target HEAD:${branch}"
+
+ if (pushSource == true) {
+ followTagsArg = followTags && pushSourceTags ? "--follow-tags" : ""
+ agentSh "git push ${followTagsArg} origin HEAD:${branch}"
+ }
+ }
+
+ if (followTags == true) {
+ ssh.agentSh "git push target --tags"
+
+ if (pushSourceTags == true) {
+ ssh.agentSh "git push origin --tags"
+ }
}
}
diff --git a/src/com/mirantis/mk/Ssl.groovy b/src/com/mirantis/mk/Ssh.groovy
similarity index 66%
rename from src/com/mirantis/mk/Ssl.groovy
rename to src/com/mirantis/mk/Ssh.groovy
index c9bec04..4526d6d 100644
--- a/src/com/mirantis/mk/Ssl.groovy
+++ b/src/com/mirantis/mk/Ssh.groovy
@@ -2,7 +2,7 @@
/**
*
- * SSL functions
+ * SSH functions
*
*/
@@ -12,10 +12,25 @@
* @param url url of remote host
*/
def ensureKnownHosts(url) {
- uri = new URI(url)
- port = uri.port ?: 22
+ def hostArray = getKnownHost(url)
+ sh "test -f ~/.ssh/known_hosts && grep ${hostArray[0]} ~/.ssh/known_hosts || ssh-keyscan -p ${hostArray[1]} ${hostArray[0]} >> ~/.ssh/known_hosts"
+}
- sh "test -f ~/.ssh/known_hosts && grep ${uri.host} ~/.ssh/known_hosts || ssh-keyscan -p ${port} ${uri.host} >> ~/.ssh/known_hosts"
+@NonCPS
+def getKnownHost(url){
+ // test for git@github.com:organization/repository like URLs
+ def p = ~/.+@(.+\..+)\:{1}.*/
+ def result = p.matcher(url)
+ def host = ""
+ if (result.matches()) {
+ host = result.group(1)
+ port = 22
+ } else {
+ parsed = new URI(url)
+ host = parsed.host
+ port = parsed.port && parsed.port > 0 ? parsed.port: 22
+ }
+ return [host,port]
}
/**
@@ -41,6 +56,15 @@
}
/**
+ * Execute command with ssh-agent (shortcut for runSshAgentCommand)
+ *
+ * @param cmd Command to execute
+ */
+def agentSh(cmd) {
+ runSshAgentCommand(cmd)
+}
+
+/**
* Setup ssh agent and add private key
*
* @param credentialsId Jenkins credentials name to lookup private key