blob: d3873ecbe0561f39e1069f67577d19384cd72f76 [file] [log] [blame]
Ales Komarek1fe5b8f2017-03-06 11:07:54 +01001/**
Ales Komarek374cc382017-03-16 08:49:01 +01002 * Update packages on given nodes
Ales Komarek1fe5b8f2017-03-06 11:07:54 +01003 *
4 * Expected parameters:
Ales Komarek374cc382017-03-16 08:49:01 +01005 * SALT_MASTER_CREDENTIALS Credentials to the Salt API.
6 * SALT_MASTER_URL Full Salt API address [https://10.10.10.1:8000].
7 * TARGET_SERVERS Salt compound target to match nodes to be updated [*, G@osfamily:debian].
8 * TARGET_PACKAGES Space delimited list of packages to be updates [package1=version package2=version], empty string means all updating all packages to the latest version.
9 * TARGET_SIZE_TEST Number of nodes to list package updates, empty string means all targetted nodes.
10 * TARGET_SIZE_SAMPLE Number of selected noded to live apply selected package update.
11 * TARGET_SIZE_BATCH Batch size for the complete live package update on all nodes, empty string means apply to all targetted nodes.
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010012 *
13**/
14
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010015def common = new com.mirantis.mk.Common()
16def salt = new com.mirantis.mk.Salt()
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010017
Ales Komarek374cc382017-03-16 08:49:01 +010018def saltMaster
19def targetAll = ['expression': TARGET_SERVERS, 'type': 'compound']
20def targetTestSubset
21def targetLiveSubset
22def targetLiveAll
23def minions
24def result
Ales Komarekef850442017-03-16 16:43:59 +010025def command
Ales Komarek374cc382017-03-16 08:49:01 +010026def packages
27
28node() {
29 try {
30
31 if (TARGET_PACKAGES != "") {
Ales Komarekef850442017-03-16 16:43:59 +010032 command = "pkg.install"
Jakub Josef4a013752017-03-16 17:37:51 +010033 packages = TARGET_PACKAGES.tokenize(' ')
Ales Komarek374cc382017-03-16 08:49:01 +010034 }
35 else {
Ales Komarekef850442017-03-16 16:43:59 +010036 command = "pkg.upgrade"
37 packages = null
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010038 }
39
Ales Komarek374cc382017-03-16 08:49:01 +010040 stage('Connect to Salt master') {
41 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010042 }
43
Ales Komarek374cc382017-03-16 08:49:01 +010044 stage('List target servers') {
45 minions = salt.getMinions(saltMaster, targetAll)
46 if (TARGET_SUBSET_TEST != "") {
47 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010048 }
Ales Komarek374cc382017-03-16 08:49:01 +010049 else {
50 targetTestSubset = minions.join(' or ')
51 }
52 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
53 targetLiveAll = minions.join(' or ')
54 common.infoMsg("Found nodes: ${targetLiveAll}")
55 common.infoMsg("Selected test nodes: ${targetTestSubset}")
56 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010057 }
58
Ales Komarek374cc382017-03-16 08:49:01 +010059 stage("List package upgrades") {
60 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
61 }
62
63 stage('Confirm live package upgrades on sample') {
Jakub Josef4a013752017-03-16 17:37:51 +010064 if(TARGET_PACKAGES==""){
65 timeout(time: 2, unit: 'HOURS') {
66 def userInput = input(
67 id: 'userInput', message: 'Insert package names for update', parameters: [
68 [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages']
69 ])
Jakub Josef57278002017-03-16 17:51:01 +010070 if(userInput!= ""){
71 packages = userInput.tokenize(" ")
Jakub Josef4a013752017-03-16 17:37:51 +010072 }
73 }
74 }else{
75 timeout(time: 2, unit: 'HOURS') {
76 input message: "Approve live package upgrades on ${targetLiveSubset} nodes?"
77 }
Ales Komarek374cc382017-03-16 08:49:01 +010078 }
79 }
80
81 stage('Apply package upgrades on sample') {
Ales Komarekef850442017-03-16 16:43:59 +010082 salt.runSaltProcessStep(saltMaster, targetLiveSubset, command, packages, null, true)
Ales Komarek374cc382017-03-16 08:49:01 +010083
84 }
85
86 stage('Confirm package upgrades on all nodes') {
87 timeout(time: 2, unit: 'HOURS') {
88 input message: "Approve live package upgrades on ${targetLiveAll} nodes?"
89 }
90 }
91
92 stage('Apply package upgrades on all nodes') {
Ales Komarekef850442017-03-16 16:43:59 +010093 salt.runSaltProcessStep(saltMaster, targetLiveAll, command, packages, null, true)
Ales Komarek374cc382017-03-16 08:49:01 +010094 }
95
96 } catch (Throwable e) {
97 // If there was an error or exception thrown, the build failed
98 currentBuild.result = "FAILURE"
99 throw e
100 } finally {
101 // common.sendNotification(currentBuild.result,"",["slack"])
Ales Komarek1fe5b8f2017-03-06 11:07:54 +0100102 }
103}