Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * SSL functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Ensure entry in SSH known hosts |
| 11 | * |
| 12 | * @param url url of remote host |
| 13 | */ |
| 14 | def ensureKnownHosts(url) { |
| 15 | uri = new URI(url) |
| 16 | port = uri.port ?: 22 |
| 17 | |
| 18 | sh "test -f ~/.ssh/known_hosts && grep ${uri.host} ~/.ssh/known_hosts || ssh-keyscan -p ${port} ${uri.host} >> ~/.ssh/known_hosts" |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Execute command with ssh-agent |
| 23 | * |
| 24 | * @param cmd Command to execute |
| 25 | */ |
| 26 | def runSshAgentCommand(cmd) { |
| 27 | sh(". ~/.ssh/ssh-agent.sh && ${cmd}") |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Setup ssh agent and add private key |
| 32 | * |
| 33 | * @param credentialsId Jenkins credentials name to lookup private key |
| 34 | */ |
| 35 | def prepareSshAgentKey(credentialsId) { |
iberezovskiy | 67af6c2 | 2016-12-26 18:17:21 +0400 | [diff] [blame] | 36 | def common = new com.mirantis.mk.common() |
| 37 | c = common.getSshCredentials(credentialsId) |
Sergey Kolekonov | ba20398 | 2016-12-21 18:32:17 +0400 | [diff] [blame] | 38 | sh("test -d ~/.ssh || mkdir -m 700 ~/.ssh") |
| 39 | sh('pgrep -l -u $USER -f | grep -e 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] | 40 | 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] | 41 | runSshAgentCommand("ssh-add ~/.ssh/id_rsa_${credentialsId}") |
| 42 | } |
| 43 | |
| 44 | return this; |