From 4cb30fa83673cf66f70b7979f15d1c896691856c Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 1 Mar 2019 15:27:08 +0100 Subject: [PATCH] =?UTF-8?q?Refactorisation=20de=20l'API=20pour=20les=20op?= =?UTF-8?q?=C3=A9rations=20sur=20OpenNebula?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pipelines/debian-test-package.jenkinsfile | 2 +- vars/nebula.groovy | 41 ++++++++++++++--------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/pipelines/debian-test-package.jenkinsfile b/pipelines/debian-test-package.jenkinsfile index ec4dbfa..aaa513b 100644 --- a/pipelines/debian-test-package.jenkinsfile +++ b/pipelines/debian-test-package.jenkinsfile @@ -47,7 +47,7 @@ pipeline { stage("Test package installation") { steps { script { - nebula.runInNewVM([ + nebula.runScriptInNewVM([ vmTemplate: params.vmTemplate, script: """ set -xeo pipefail diff --git a/vars/nebula.groovy b/vars/nebula.groovy index bc1e3ef..a509a19 100644 --- a/vars/nebula.groovy +++ b/vars/nebula.groovy @@ -133,7 +133,15 @@ def initWithCredentials(String urlCredentialsId, String userCredentialsId, Closu } -def runInNewVM(Map args) { +def runScriptInNewVM(Map args) { + def script = args.get("script", "") + runInNewVM(args) { shell -> + shell(script) + } +} + + +def runInNewVM(Map args, Closure body) { def urlCredentialsId = args.get('urlCredentialsId', 'opennebula-dev-url') def userCredentialsId = args.get('userCredentialsId', 'kipp-credentials') @@ -141,7 +149,6 @@ def runInNewVM(Map args) { def vmTemplate = args.get('vmTemplate', '') def terminateOnExit = args.get('terminateOnExit', true) def shell = args.get("shell", "/bin/sh") - def script = args.get('script', '') // On récupère les identifiants de connexion SSH pour la VM withCredentials([ @@ -162,20 +169,24 @@ def runInNewVM(Map args) { sleep(5) } - // On créait un script temporaire à exécuter sur la machine distante - def now = System.currentTimeMillis() - def tempScriptFile = "script_${env.BUILD_ID}_${now}.sh" - writeFile(file: tempScriptFile, text: """ - #!${shell} - ${script.stripIndent()} - """) + def remoteShell = { script -> + // On créait un script temporaire à exécuter sur la machine distante + def now = System.currentTimeMillis() + def tempScriptFile = "script_${env.BUILD_ID}_${now}.sh" + writeFile(file: tempScriptFile, text: """ + #!${shell} + ${script.stripIndent()} + """) + // On transfère le script sur la machine distante et on l'exécute + sh """ + scp ${sshArgs} '${tempScriptFile}' 'root@${host}:/tmp/${tempScriptFile}' + ssh ${sshArgs} root@${host} 'chmod +x /tmp/${tempScriptFile}; /tmp/${tempScriptFile}' + """ + } - // On transfère le script sur la machine distante et on l'exécute - sh """ - scp ${sshArgs} '${tempScriptFile}' 'root@${host}:/tmp/${tempScriptFile}' - ssh ${sshArgs} root@${host} 'chmod +x /tmp/${tempScriptFile}; /tmp/${tempScriptFile}' - """ + body(remoteShell) + } } } -} +} \ No newline at end of file