blob: ea2259c6632c81d1fa81ba935a3e2216a53125b7 [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.
Jiri Broulik9b73d6c2017-06-02 12:27:05 +02009 * TARGET_SUBSET_TEST Number of nodes to list package updates, empty string means all targetted nodes.
10 * TARGET_SUBSET_LIVE Number of selected nodes to live apply selected package update.
11 * TARGET_BATCH_LIVE 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
Ales Komarek374cc382017-03-16 08:49:01 +010019def targetTestSubset
20def targetLiveSubset
21def targetLiveAll
22def minions
23def result
24def packages
Jakub Joseff080c142017-03-16 18:22:18 +010025def command
Filip Pytloun43896ed2017-03-29 14:23:32 +020026def commandKwargs
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') {
Tomáš Kukrálc86c8b42017-07-13 10:26:51 +020036 minions = salt.getMinions(saltMaster, TARGET_SERVERS)
Tomáš Kukráld73cef02017-04-05 15:24:57 +020037
38 if (minions.isEmpty()) {
39 throw new Exception("No minion was targeted")
40 }
41
Ales Komarek374cc382017-03-16 08:49:01 +010042 if (TARGET_SUBSET_TEST != "") {
43 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
Tomáš Kukráld73cef02017-04-05 15:24:57 +020044 } else {
Ales Komarek374cc382017-03-16 08:49:01 +010045 targetTestSubset = minions.join(' or ')
46 }
47 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
Tomáš Kukráld73cef02017-04-05 15:24:57 +020048
Ales Komarek374cc382017-03-16 08:49:01 +010049 targetLiveAll = minions.join(' or ')
50 common.infoMsg("Found nodes: ${targetLiveAll}")
51 common.infoMsg("Selected test nodes: ${targetTestSubset}")
52 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010053 }
54
Ales Komarek374cc382017-03-16 08:49:01 +010055 stage("List package upgrades") {
56 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
57 }
58
59 stage('Confirm live package upgrades on sample') {
Jakub Josef4a013752017-03-16 17:37:51 +010060 if(TARGET_PACKAGES==""){
61 timeout(time: 2, unit: 'HOURS') {
62 def userInput = input(
63 id: 'userInput', message: 'Insert package names for update', parameters: [
64 [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages']
65 ])
Jakub Joseff080c142017-03-16 18:22:18 +010066 if(userInput!= "" && userInput!= "*"){
67 TARGET_PACKAGES = userInput
Jakub Josef4a013752017-03-16 17:37:51 +010068 }
69 }
70 }else{
71 timeout(time: 2, unit: 'HOURS') {
72 input message: "Approve live package upgrades on ${targetLiveSubset} nodes?"
73 }
Ales Komarek374cc382017-03-16 08:49:01 +010074 }
Jakub Josefc5407c42017-03-16 18:31:10 +010075 }
76
77 if (TARGET_PACKAGES != "") {
Filip Pytloun43896ed2017-03-29 14:23:32 +020078 command = "pkg.install"
Jakub Josefc5407c42017-03-16 18:31:10 +010079 packages = TARGET_PACKAGES.tokenize(' ')
Filip Pytloun43896ed2017-03-29 14:23:32 +020080 commandKwargs = ['only_upgrade': 'true']
Jakub Josefc5407c42017-03-16 18:31:10 +010081 }else {
82 command = "pkg.upgrade"
83 packages = null
Ales Komarek374cc382017-03-16 08:49:01 +010084 }
85
86 stage('Apply package upgrades on sample') {
Filip Pytloun43896ed2017-03-29 14:23:32 +020087 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, packages, commandKwargs)
88 salt.printSaltCommandResult(out)
Ales Komarek374cc382017-03-16 08:49:01 +010089 }
90
91 stage('Confirm package upgrades on all nodes') {
92 timeout(time: 2, unit: 'HOURS') {
93 input message: "Approve live package upgrades on ${targetLiveAll} nodes?"
94 }
95 }
96
97 stage('Apply package upgrades on all nodes') {
Filip Pytloun43896ed2017-03-29 14:23:32 +020098 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, packages, commandKwargs)
99 salt.printSaltCommandResult(out)
Ales Komarek374cc382017-03-16 08:49:01 +0100100 }
101
102 } catch (Throwable e) {
103 // If there was an error or exception thrown, the build failed
104 currentBuild.result = "FAILURE"
105 throw e
Ales Komarek1fe5b8f2017-03-06 11:07:54 +0100106 }
107}