blob: 6c31c95034d150a506b04e8a5d6be8c0e058469f [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
25def packages
Jakub Joseff080c142017-03-16 18:22:18 +010026def command
Ales Komarek374cc382017-03-16 08:49:01 +010027
28node() {
29 try {
30
Ales Komarek374cc382017-03-16 08:49:01 +010031 stage('Connect to Salt master') {
32 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010033 }
34
Ales Komarek374cc382017-03-16 08:49:01 +010035 stage('List target servers') {
36 minions = salt.getMinions(saltMaster, targetAll)
37 if (TARGET_SUBSET_TEST != "") {
38 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010039 }
Ales Komarek374cc382017-03-16 08:49:01 +010040 else {
41 targetTestSubset = minions.join(' or ')
42 }
43 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
44 targetLiveAll = minions.join(' or ')
45 common.infoMsg("Found nodes: ${targetLiveAll}")
46 common.infoMsg("Selected test nodes: ${targetTestSubset}")
47 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010048 }
49
Ales Komarek374cc382017-03-16 08:49:01 +010050 stage("List package upgrades") {
51 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
52 }
53
54 stage('Confirm live package upgrades on sample') {
Jakub Josef4a013752017-03-16 17:37:51 +010055 if(TARGET_PACKAGES==""){
56 timeout(time: 2, unit: 'HOURS') {
57 def userInput = input(
58 id: 'userInput', message: 'Insert package names for update', parameters: [
59 [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages']
60 ])
Jakub Joseff080c142017-03-16 18:22:18 +010061 if(userInput!= "" && userInput!= "*"){
62 TARGET_PACKAGES = userInput
Jakub Josef4a013752017-03-16 17:37:51 +010063 }
64 }
65 }else{
66 timeout(time: 2, unit: 'HOURS') {
67 input message: "Approve live package upgrades on ${targetLiveSubset} nodes?"
68 }
Ales Komarek374cc382017-03-16 08:49:01 +010069 }
Jakub Josefc5407c42017-03-16 18:31:10 +010070 }
71
72 if (TARGET_PACKAGES != "") {
73 command = "pkg.install";
74 packages = TARGET_PACKAGES.tokenize(' ')
75 }else {
76 command = "pkg.upgrade"
77 packages = null
Ales Komarek374cc382017-03-16 08:49:01 +010078 }
79
80 stage('Apply package upgrades on sample') {
Ales Komarekef850442017-03-16 16:43:59 +010081 salt.runSaltProcessStep(saltMaster, targetLiveSubset, command, packages, null, true)
Ales Komarek374cc382017-03-16 08:49:01 +010082 }
83
84 stage('Confirm package upgrades on all nodes') {
85 timeout(time: 2, unit: 'HOURS') {
86 input message: "Approve live package upgrades on ${targetLiveAll} nodes?"
87 }
88 }
89
90 stage('Apply package upgrades on all nodes') {
Ales Komarekef850442017-03-16 16:43:59 +010091 salt.runSaltProcessStep(saltMaster, targetLiveAll, command, packages, null, true)
Ales Komarek374cc382017-03-16 08:49:01 +010092 }
93
94 } catch (Throwable e) {
95 // If there was an error or exception thrown, the build failed
96 currentBuild.result = "FAILURE"
97 throw e
98 } finally {
99 // common.sendNotification(currentBuild.result,"",["slack"])
Ales Komarek1fe5b8f2017-03-06 11:07:54 +0100100 }
101}