blob: 24ccd52216d0df897d47e880fa3ebd61f8bfcfce [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package 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 */
14def 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 */
26def 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 */
35def prepareSshAgentKey(credentialsId) {
iberezovskiy67af6c22016-12-26 18:17:21 +040036 def common = new com.mirantis.mk.common()
37 c = common.getSshCredentials(credentialsId)
Sergey Kolekonovba203982016-12-21 18:32:17 +040038 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 Kulanov6307d342016-12-27 14:29:31 +020040 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 +040041 runSshAgentCommand("ssh-add ~/.ssh/id_rsa_${credentialsId}")
42}
43
44return this;