blob: b37fe225c830f316a2161f10730fbaf0ab60cc6c [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
Filip Pytloun43896ed2017-03-29 14:23:32 +020027def commandKwargs
Ales Komarek374cc382017-03-16 08:49:01 +010028
29node() {
30 try {
31
Ales Komarek374cc382017-03-16 08:49:01 +010032 stage('Connect to Salt master') {
33 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010034 }
35
Ales Komarek374cc382017-03-16 08:49:01 +010036 stage('List target servers') {
37 minions = salt.getMinions(saltMaster, targetAll)
38 if (TARGET_SUBSET_TEST != "") {
39 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010040 }
Ales Komarek374cc382017-03-16 08:49:01 +010041 else {
42 targetTestSubset = minions.join(' or ')
43 }
44 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
45 targetLiveAll = minions.join(' or ')
46 common.infoMsg("Found nodes: ${targetLiveAll}")
47 common.infoMsg("Selected test nodes: ${targetTestSubset}")
48 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010049 }
50
Ales Komarek374cc382017-03-16 08:49:01 +010051 stage("List package upgrades") {
52 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
53 }
54
55 stage('Confirm live package upgrades on sample') {
Jakub Josef4a013752017-03-16 17:37:51 +010056 if(TARGET_PACKAGES==""){
57 timeout(time: 2, unit: 'HOURS') {
58 def userInput = input(
59 id: 'userInput', message: 'Insert package names for update', parameters: [
60 [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages']
61 ])
Jakub Joseff080c142017-03-16 18:22:18 +010062 if(userInput!= "" && userInput!= "*"){
63 TARGET_PACKAGES = userInput
Jakub Josef4a013752017-03-16 17:37:51 +010064 }
65 }
66 }else{
67 timeout(time: 2, unit: 'HOURS') {
68 input message: "Approve live package upgrades on ${targetLiveSubset} nodes?"
69 }
Ales Komarek374cc382017-03-16 08:49:01 +010070 }
Jakub Josefc5407c42017-03-16 18:31:10 +010071 }
72
73 if (TARGET_PACKAGES != "") {
Filip Pytloun43896ed2017-03-29 14:23:32 +020074 command = "pkg.install"
Jakub Josefc5407c42017-03-16 18:31:10 +010075 packages = TARGET_PACKAGES.tokenize(' ')
Filip Pytloun43896ed2017-03-29 14:23:32 +020076 commandKwargs = ['only_upgrade': 'true']
Jakub Josefc5407c42017-03-16 18:31:10 +010077 }else {
78 command = "pkg.upgrade"
79 packages = null
Ales Komarek374cc382017-03-16 08:49:01 +010080 }
81
82 stage('Apply package upgrades on sample') {
Filip Pytloun43896ed2017-03-29 14:23:32 +020083 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, packages, commandKwargs)
84 salt.printSaltCommandResult(out)
Ales Komarek374cc382017-03-16 08:49:01 +010085 }
86
87 stage('Confirm package upgrades on all nodes') {
88 timeout(time: 2, unit: 'HOURS') {
89 input message: "Approve live package upgrades on ${targetLiveAll} nodes?"
90 }
91 }
92
93 stage('Apply package upgrades on all nodes') {
Filip Pytloun43896ed2017-03-29 14:23:32 +020094 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, packages, commandKwargs)
95 salt.printSaltCommandResult(out)
Ales Komarek374cc382017-03-16 08:49:01 +010096 }
97
98 } catch (Throwable e) {
99 // If there was an error or exception thrown, the build failed
100 currentBuild.result = "FAILURE"
101 throw e
102 } finally {
103 // common.sendNotification(currentBuild.result,"",["slack"])
Ales Komarek1fe5b8f2017-03-06 11:07:54 +0100104 }
105}