Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame^] | 5 | * SSH functions |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Ensure entry in SSH known hosts |
| 11 | * |
| 12 | * @param url url of remote host |
| 13 | */ |
| 14 | def ensureKnownHosts(url) { |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame^] | 15 | def hostArray = getKnownHost(url) |
| 16 | sh "test -f ~/.ssh/known_hosts && grep ${hostArray[0]} ~/.ssh/known_hosts || ssh-keyscan -p ${hostArray[1]} ${hostArray[0]} >> ~/.ssh/known_hosts" |
| 17 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 18 | |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame^] | 19 | @NonCPS |
| 20 | def getKnownHost(url){ |
| 21 | // test for git@github.com:organization/repository like URLs |
| 22 | def p = ~/.+@(.+\..+)\:{1}.*/ |
| 23 | def result = p.matcher(url) |
| 24 | def host = "" |
| 25 | if (result.matches()) { |
| 26 | host = result.group(1) |
| 27 | port = 22 |
| 28 | } else { |
| 29 | parsed = new URI(url) |
| 30 | host = parsed.host |
| 31 | port = parsed.port && parsed.port > 0 ? parsed.port: 22 |
| 32 | } |
| 33 | return [host,port] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Execute command with ssh-agent |
| 38 | * |
| 39 | * @param cmd Command to execute |
| 40 | */ |
| 41 | def runSshAgentCommand(cmd) { |
Sergey Kulanov | f36af07 | 2017-01-20 13:35:57 +0200 | [diff] [blame] | 42 | // if file exists, then we started ssh-agent |
| 43 | if (fileExists("$HOME/.ssh/ssh-agent.sh")) { |
| 44 | sh(". ~/.ssh/ssh-agent.sh && ${cmd}") |
| 45 | } else { |
| 46 | // we didn't start ssh-agent in prepareSshAgentKey() because some ssh-agent |
| 47 | // is running. Let's re-use already running agent and re-construct |
| 48 | // * SSH_AUTH_SOCK |
| 49 | // * SSH_AGENT_PID |
| 50 | sh """ |
| 51 | export SSH_AUTH_SOCK=`find /tmp/ -type s -name agent.\\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*' | head -n 1` |
| 52 | export SSH_AGENT_PID=`echo \${SSH_AUTH_SOCK} | cut -d. -f2` |
| 53 | ${cmd} |
| 54 | """ |
| 55 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame^] | 59 | * Execute command with ssh-agent (shortcut for runSshAgentCommand) |
| 60 | * |
| 61 | * @param cmd Command to execute |
| 62 | */ |
| 63 | def agentSh(cmd) { |
| 64 | runSshAgentCommand(cmd) |
| 65 | } |
| 66 | |
| 67 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 68 | * Setup ssh agent and add private key |
| 69 | * |
| 70 | * @param credentialsId Jenkins credentials name to lookup private key |
| 71 | */ |
| 72 | def prepareSshAgentKey(credentialsId) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 73 | def common = new com.mirantis.mk.Common() |
iberezovskiy | 67af6c2 | 2016-12-26 18:17:21 +0400 | [diff] [blame] | 74 | c = common.getSshCredentials(credentialsId) |
Sergey Kulanov | f36af07 | 2017-01-20 13:35:57 +0200 | [diff] [blame] | 75 | // create ~/.ssh and delete file ssh-agent.sh which can be stale |
| 76 | sh('mkdir -p -m 700 ~/.ssh && rm -f ~/.ssh/ssh-agent.sh') |
| 77 | sh('pgrep -l -u $USER -f ssh-agent\$ >/dev/null || ssh-agent|grep -v "Agent pid" > ~/.ssh/ssh-agent.sh') |
Sergey Kulanov | 6307d34 | 2016-12-27 14:29:31 +0200 | [diff] [blame] | 78 | sh("set +x; echo '${c.getPrivateKey()}' > ~/.ssh/id_rsa_${credentialsId} && chmod 600 ~/.ssh/id_rsa_${credentialsId}; set -x") |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 79 | runSshAgentCommand("ssh-add ~/.ssh/id_rsa_${credentialsId}") |
| 80 | } |
| 81 | |
| 82 | return this; |