blob: 4526d6d4ac90b28e10cee1994b91e3c531a962c2 [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 {
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 Kolekonovba203982016-12-21 18:32:17 +040034}
35
36/**
37 * Execute command with ssh-agent
38 *
39 * @param cmd Command to execute
40 */
41def runSshAgentCommand(cmd) {
Sergey Kulanovf36af072017-01-20 13:35:57 +020042 // 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 Kolekonovba203982016-12-21 18:32:17 +040056}
57
58/**
Filip Pytloun49d66302017-03-06 10:26:22 +010059 * Execute command with ssh-agent (shortcut for runSshAgentCommand)
60 *
61 * @param cmd Command to execute
62 */
63def agentSh(cmd) {
64 runSshAgentCommand(cmd)
65}
66
67/**
Sergey Kolekonovba203982016-12-21 18:32:17 +040068 * Setup ssh agent and add private key
69 *
70 * @param credentialsId Jenkins credentials name to lookup private key
71 */
72def prepareSshAgentKey(credentialsId) {
iberezovskiyd4240b52017-02-20 17:18:28 +040073 def common = new com.mirantis.mk.Common()
iberezovskiy67af6c22016-12-26 18:17:21 +040074 c = common.getSshCredentials(credentialsId)
Sergey Kulanovf36af072017-01-20 13:35:57 +020075 // 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 Kulanov6307d342016-12-27 14:29:31 +020078 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 +040079 runSshAgentCommand("ssh-add ~/.ssh/id_rsa_${credentialsId}")
80}
81
82return this;