Merge branch 'feature/23745'

This commit is contained in:
Roberto Cirillo 2022-10-04 16:21:46 +02:00
commit 15b62f11b0
2 changed files with 69 additions and 84 deletions

11
.project Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>gCubeDeployer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

142
Jenkinsfile vendored
View File

@ -1,4 +1,9 @@
#!groovy
import groovy.io.FileType
import static groovy.io.FileType.FILES
import jenkins.model.*
import hudson.FilePath;
import jenkins.model.Jenkins;
/**
* Deploy components to the D4Science Infrastructure (dev-only)
@ -9,30 +14,13 @@
// related jenkins job: https://jenkins.d4science.org/job/gCubeDeployer/
def agent_root_folder = '/var/lib/jenkins'
def agent_deploy_filename = 'deploy.csv'
def agent_deploy_backup_filename = 'deploy.bck'
def deployList
def backupList
if (params.deployFile) {
println "Using custom deploy file"
deployList = params.deployFile
} else {
println "Using local deploy file"
//load the report from local
deployList = agent_root_folder+'/'+agent_deploy_filename;
println "Load from local file ${deployList}"
}
backupList = agent_root_folder+'/'+agent_deploy_backup_filename;
//locate the targetHost file
//String targetHostURL = "https://code-repo.d4science.org/gCubeCI/gCubeDeployer/raw/branch/master/open/gcube-${gCube_release_version}.yaml"
//if (verbose)
// println "Querying ${targetHostURL}"
//load the release file
//def text = targetHostURL.toURL().getText()
pipeline {
agent {
@ -51,9 +39,12 @@ pipeline {
environment {
AGENT_ROOT_FOLDER = "${agent_root_folder}"
DEPLOY_ROOT_FOLDER ="${agent_root_folder}/ansible-repos/ansible-playbooks/d4science-ghn-cluster/CD"
DEPLOY_FILE = "${agent_root_folder}/${agent_deploy_filename}"
BACKUP_FILE = "${agent_root_folder}/${agent_deploy_backup_filename}"
PENDING_DEPLOY_FOLDER="${agent_root_folder}/CD/"
CD_ROOT_FOLDER = "${agent_root_folder}/ansible-repos/ansible-playbooks/d4science-ghn-cluster/CD"
PIPELINE_BUILD_NUMBER = "${env.BUILD_NUMBER}"
DEPLOY_FILE = "${PENDING_DEPLOY_FOLDER}deploy.${PIPELINE_BUILD_NUMBER}.csv"
BACKUP_FILE = "${PENDING_DEPLOY_FOLDER}deploy.${PIPELINE_BUILD_NUMBER}.bck"
DEPLOY_FOLDER= "${WORKSPACE}/CD-${PIPELINE_BUILD_NUMBER}"
TRIGGER_JOB= "${params.TRIGGER_JOB}"
TRIGGER_VERSION= "${params.TRIGGER_VERSION}"
TRIGGER_HOST="${params.TRIGGER_HOST}"
@ -84,6 +75,9 @@ pipeline {
steps {
sh '''
date=`date`;
mkdir -p ${PENDING_DEPLOY_FOLDER}
mkdir -p "${DEPLOY_FOLDER}"
find "${PENDING_DEPLOY_FOLDER}" -type f -exec mv --target-directory="${DEPLOY_FOLDER}" '\'{'\'} '\'+
'''
}
}
@ -92,37 +86,55 @@ pipeline {
allOf{
triggeredBy 'TimerTrigger'
// maybe we can add a new condition in order to consider the manual execution of this pipeline
environment name: 'IS_CRON', value: 'True'
environment name: 'IS_CRON_TMP', value: 'True'
}
}
steps {
echo 'Cron build enabled. Deploy from system ongoing'
script {
// parse the report and extract the data
def components = getComponentsFromCSV(deployList)
if (components.size() > 0) {
for (component in components) {
stage(component){
println "Deploy on going of component: $component"
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
checkup("${component.name}", "${component.version}", "${component.host}");
deploy("${component.name}", "${component.version}", "${component.host}");
}
}
}
}else{
println "No components found"
}
def deployFolder="CD-${env.BUILD_NUMBER}";
println ("searching files in folder ${deployFolder}");
def files = findFiles(glob: "${deployFolder}/*.csv")
if (files == null){
println ("Nothing to do");
}else{
def serviceList = []
for (def file : files){
def records = readCSV file: "${file.path}"
for (def record : records) {
println("Processing record: "+record)
if(!serviceList.contains(record.get(0))){
stage(record.get(0)){
println "Deploy on going of component: ${record.get(0)}"
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
checkup(record.get(0), record.get(1), record.get(2));
deploy(record.get(0), record.get(1), record.get(2));
}
}
serviceList << record.get(0)
}else{
echo "${record.get(0)} already deployed. Deployment skipped."
}
}
sh "rm ${file.path}"
}
sh "rm -Rf ${deployFolder}"
}
}
cleanup(DEPLOY_FILE,BACKUP_FILE);
}
}
stage('Nothing to do ') {
when{
allOf{
triggeredBy 'TimerTrigger'
environment name: 'IS_CRON', value: 'False'
anyOf{
allOf{
triggeredBy 'TimerTrigger'
environment name: 'IS_CRON_TMP', value: 'False'
}
}
}
steps {
echo 'Do Nothing: cron build disabled'
@ -130,10 +142,11 @@ pipeline {
}
stage('New pending deploy ') {
when{
environment name: 'IS_CRON', value: 'True'
environment name: 'IS_CRON_TMP', value: 'True'
anyOf{
triggeredBy 'BuildUpstreamCause'
triggeredBy 'UpstreamCause'
triggeredBy 'UserIdCause'
}
}
steps {
@ -145,8 +158,7 @@ pipeline {
else
echo "${TRIGGER_JOB},${TRIGGER_VERSION},${TRIGGER_HOST}" >> ${DEPLOY_FILE}
fi
'''
'''
// the following catch give always an error in order to return a feedback UNSTABLE to the final user since the deploy is still not performed
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
sh("""
@ -157,7 +169,7 @@ pipeline {
}
stage('Deploy from job ') {
when{
environment name: 'IS_CRON', value: 'False';
environment name: 'IS_CRON_TMP', value: 'False';
expression {
env.TRIGGER_CD.toBoolean()
}
@ -180,7 +192,7 @@ pipeline {
stage('Deploy manually ') {
when{
allOf{
environment name: 'IS_CRON', value: 'False';
environment name: 'IS_CRON_TMP', value: 'False';
triggeredBy 'UserIdCause'
}
}
@ -223,34 +235,11 @@ pipeline {
}
}
//parse a csv file formatted in this way: ComponentName,ComponentVersion, ComponentHost
def getComponentsFromCSV(def deployList) {
def components = []
if (fileExists("${deployList}")) {
echo 'file found'
readFile("${deployList}").split('\n').each { line, count->
if (line.startsWith('#'))
return
def fields = line.split(',')
components.add([
name : fields[0],
version : fields[1],
host : fields[2]
]
)
}
} else {
echo ' File Not found. Failing.'
}
return components
}
//launch ansible deploy
def deploy(String service, String version, String host){
def now = new Date();
println("Going to deploy the service "+service+" with version: "+version+" on target: "+host);
def statusCode = sh( script: "cd $DEPLOY_ROOT_FOLDER;./deployService.sh $service $version $host;", returnStdout: true);
def statusCode = sh( script: "cd $CD_ROOT_FOLDER;./deployService.sh $service $version $host;", returnStdout: true);
sh("""
echo " last exit code \$?";
""")
@ -267,18 +256,3 @@ def checkup(String service, String version, String host){
esac
""")
}
//clean and update the local deploy file
def cleanup(def DEPLOY_FILE, def BACKUP_FILE){
sh '''
echo "cleanup $DEPLOY_FILE";
if [ -f ${DEPLOY_FILE} ]; then
if [ -f ${BACKUP_FILE} ]; then
echo "backup found: ${BACKUP_FILE} going to replace it";
rm ${BACKUP_FILE};
fi
mv ${DEPLOY_FILE} ${BACKUP_FILE};
else
echo "deploy file empty"
fi
'''
}