blob: 504e5d3f35f0a02823352a4c73224ab928b890b1 [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) {
36 c = getSshCredentials(credentialsId)
37 sh("test -d ~/.ssh || mkdir -m 700 ~/.ssh")
38 sh('pgrep -l -u $USER -f | grep -e ssh-agent\$ >/dev/null || ssh-agent|grep -v "Agent pid" > ~/.ssh/ssh-agent.sh')
39 sh("echo '${c.getPrivateKey()}' > ~/.ssh/id_rsa_${credentialsId} && chmod 600 ~/.ssh/id_rsa_${credentialsId}")
40 runSshAgentCommand("ssh-add ~/.ssh/id_rsa_${credentialsId}")
41}
42
43return this;