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) |
Sergey Galkin | 2576c7b | 2017-07-04 14:27:36 +0400 | [diff] [blame] | 16 | sh "test -f ~/.ssh/known_hosts && grep ${hostArray[0]} ~/.ssh/known_hosts || ssh-keyscan -H -p ${hostArray[1]} ${hostArray[0]} >> ~/.ssh/known_hosts" |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 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 |
Filip Pytloun | a02d502 | 2017-08-14 11:17:55 +0200 | [diff] [blame] | 22 | def p = ~/[a-z0-9\._\-]+@(.+\..+)\:{1}.*/ |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 23 | def result = p.matcher(url) |
| 24 | def host = "" |
| 25 | if (result.matches()) { |
| 26 | host = result.group(1) |
| 27 | port = 22 |
| 28 | } else { |
Jakub Josef | 3e8dd47 | 2017-03-22 15:45:05 +0100 | [diff] [blame] | 29 | // test for protocol |
| 30 | if(url.indexOf("://") == -1){ |
| 31 | url="ssh://" + url |
| 32 | } |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 33 | parsed = new URI(url) |
| 34 | host = parsed.host |
| 35 | port = parsed.port && parsed.port > 0 ? parsed.port: 22 |
| 36 | } |
| 37 | return [host,port] |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Execute command with ssh-agent |
| 42 | * |
| 43 | * @param cmd Command to execute |
Jakub Josef | 4386349 | 2017-04-04 17:03:55 +0200 | [diff] [blame] | 44 | * @return STDOUT output |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 45 | */ |
| 46 | def runSshAgentCommand(cmd) { |
Sergey Kulanov | f36af07 | 2017-01-20 13:35:57 +0200 | [diff] [blame] | 47 | // if file exists, then we started ssh-agent |
| 48 | if (fileExists("$HOME/.ssh/ssh-agent.sh")) { |
Jakub Josef | 4386349 | 2017-04-04 17:03:55 +0200 | [diff] [blame] | 49 | return sh(script:". ~/.ssh/ssh-agent.sh && ${cmd}", returnStdout:true) |
Sergey Kulanov | f36af07 | 2017-01-20 13:35:57 +0200 | [diff] [blame] | 50 | } else { |
| 51 | // we didn't start ssh-agent in prepareSshAgentKey() because some ssh-agent |
| 52 | // is running. Let's re-use already running agent and re-construct |
| 53 | // * SSH_AUTH_SOCK |
| 54 | // * SSH_AGENT_PID |
Jakub Josef | 4386349 | 2017-04-04 17:03:55 +0200 | [diff] [blame] | 55 | return sh(script:""" |
Sergey Kulanov | f36af07 | 2017-01-20 13:35:57 +0200 | [diff] [blame] | 56 | export SSH_AUTH_SOCK=`find /tmp/ -type s -name agent.\\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*' | head -n 1` |
| 57 | export SSH_AGENT_PID=`echo \${SSH_AUTH_SOCK} | cut -d. -f2` |
| 58 | ${cmd} |
Jakub Josef | 4386349 | 2017-04-04 17:03:55 +0200 | [diff] [blame] | 59 | """, returnStdout: true) |
Sergey Kulanov | f36af07 | 2017-01-20 13:35:57 +0200 | [diff] [blame] | 60 | } |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 64 | * Execute command with ssh-agent (shortcut for runSshAgentCommand) |
| 65 | * |
| 66 | * @param cmd Command to execute |
Jakub Josef | 4386349 | 2017-04-04 17:03:55 +0200 | [diff] [blame] | 67 | * @return STDOUT output |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 68 | */ |
| 69 | def agentSh(cmd) { |
Jakub Josef | 4386349 | 2017-04-04 17:03:55 +0200 | [diff] [blame] | 70 | return runSshAgentCommand(cmd) |
Filip Pytloun | 49d6630 | 2017-03-06 10:26:22 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 74 | * Setup ssh agent and add private key |
| 75 | * |
| 76 | * @param credentialsId Jenkins credentials name to lookup private key |
| 77 | */ |
| 78 | def prepareSshAgentKey(credentialsId) { |
iberezovskiy | d4240b5 | 2017-02-20 17:18:28 +0400 | [diff] [blame] | 79 | def common = new com.mirantis.mk.Common() |
iberezovskiy | 67af6c2 | 2016-12-26 18:17:21 +0400 | [diff] [blame] | 80 | c = common.getSshCredentials(credentialsId) |
Sergey Kulanov | f36af07 | 2017-01-20 13:35:57 +0200 | [diff] [blame] | 81 | // create ~/.ssh and delete file ssh-agent.sh which can be stale |
| 82 | sh('mkdir -p -m 700 ~/.ssh && rm -f ~/.ssh/ssh-agent.sh') |
Filip Pytloun | f1b2344 | 2017-08-17 11:24:25 +0200 | [diff] [blame] | 83 | sh('pgrep -l -u `id -u` -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] | 84 | 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] | 85 | runSshAgentCommand("ssh-add ~/.ssh/id_rsa_${credentialsId}") |
| 86 | } |
| 87 | |
| 88 | return this; |