blob: 4969da3be33362f9d3b346c86fd3e8461e940100 [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
Filip Pytloun49d66302017-03-06 10:26:22 +01005 * SSH functions
Sergey Kolekonovba203982016-12-21 18:32:17 +04006 *
7 */
8
9/**
10 * Ensure entry in SSH known hosts
11 *
12 * @param url url of remote host
13 */
14def ensureKnownHosts(url) {
Filip Pytloun49d66302017-03-06 10:26:22 +010015 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 Kolekonovba203982016-12-21 18:32:17 +040018
Filip Pytloun49d66302017-03-06 10:26:22 +010019@NonCPS
20def 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 {
Jakub Josef3e8dd472017-03-22 15:45:05 +010029 // test for protocol
30 if(url.indexOf("://") == -1){
31 url="ssh://" + url
32 }
Filip Pytloun49d66302017-03-06 10:26:22 +010033 parsed = new URI(url)
34 host = parsed.host
35 port = parsed.port && parsed.port > 0 ? parsed.port: 22
36 }
37 return [host,port]
Sergey Kolekonovba203982016-12-21 18:32:17 +040038}
39
40/**
41 * Execute command with ssh-agent
42 *
43 * @param cmd Command to execute
44 */
45def runSshAgentCommand(cmd) {
Sergey Kulanovf36af072017-01-20 13:35:57 +020046 // if file exists, then we started ssh-agent
47 if (fileExists("$HOME/.ssh/ssh-agent.sh")) {
48 sh(". ~/.ssh/ssh-agent.sh && ${cmd}")
49 } else {
50 // we didn't start ssh-agent in prepareSshAgentKey() because some ssh-agent
51 // is running. Let's re-use already running agent and re-construct
52 // * SSH_AUTH_SOCK
53 // * SSH_AGENT_PID
54 sh """
55 export SSH_AUTH_SOCK=`find /tmp/ -type s -name agent.\\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*' | head -n 1`
56 export SSH_AGENT_PID=`echo \${SSH_AUTH_SOCK} | cut -d. -f2`
57 ${cmd}
58 """
59 }
Sergey Kolekonovba203982016-12-21 18:32:17 +040060}
61
62/**
Filip Pytloun49d66302017-03-06 10:26:22 +010063 * Execute command with ssh-agent (shortcut for runSshAgentCommand)
64 *
65 * @param cmd Command to execute
66 */
67def agentSh(cmd) {
68 runSshAgentCommand(cmd)
69}
70
71/**
Sergey Kolekonovba203982016-12-21 18:32:17 +040072 * Setup ssh agent and add private key
73 *
74 * @param credentialsId Jenkins credentials name to lookup private key
75 */
76def prepareSshAgentKey(credentialsId) {
iberezovskiyd4240b52017-02-20 17:18:28 +040077 def common = new com.mirantis.mk.Common()
iberezovskiy67af6c22016-12-26 18:17:21 +040078 c = common.getSshCredentials(credentialsId)
Sergey Kulanovf36af072017-01-20 13:35:57 +020079 // create ~/.ssh and delete file ssh-agent.sh which can be stale
80 sh('mkdir -p -m 700 ~/.ssh && rm -f ~/.ssh/ssh-agent.sh')
81 sh('pgrep -l -u $USER -f ssh-agent\$ >/dev/null || ssh-agent|grep -v "Agent pid" > ~/.ssh/ssh-agent.sh')
Sergey Kulanov6307d342016-12-27 14:29:31 +020082 sh("set +x; echo '${c.getPrivateKey()}' > ~/.ssh/id_rsa_${credentialsId} && chmod 600 ~/.ssh/id_rsa_${credentialsId}; set -x")
Sergey Kolekonovba203982016-12-21 18:32:17 +040083 runSshAgentCommand("ssh-add ~/.ssh/id_rsa_${credentialsId}")
84}
85
86return this;