blob: 10f3a855f8d4ac00bdaf50156d93eb0789f9c6f4 [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**/
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020014pepperEnv = "pepperEnv"
15salt = new com.mirantis.mk.Salt()
Ales Komarek1fe5b8f2017-03-06 11:07:54 +010016def common = new com.mirantis.mk.Common()
chnyda625f4b42017-10-11 14:10:31 +020017def python = new com.mirantis.mk.Python()
Ales Komarek374cc382017-03-16 08:49:01 +010018def targetTestSubset
19def targetLiveSubset
20def targetLiveAll
21def minions
22def result
23def packages
Jakub Joseff080c142017-03-16 18:22:18 +010024def command
Filip Pytloun43896ed2017-03-29 14:23:32 +020025def commandKwargs
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020026def installSaltStack(target, pkgs){
27 salt.runSaltProcessStep(pepperEnv, target, 'pkg.install', ["force_yes=True", "pkgs='$pkgs'"], null, true, 30)
28}
29
Jakub Josefa63f9862018-01-11 17:58:38 +010030timeout(time: 12, unit: 'HOURS') {
31 node() {
32 try {
Ales Komarek374cc382017-03-16 08:49:01 +010033
Jakub Josefa63f9862018-01-11 17:58:38 +010034 stage('Setup virtualenv for Pepper') {
35 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Tomáš Kukráld73cef02017-04-05 15:24:57 +020036 }
37
Jakub Josefa63f9862018-01-11 17:58:38 +010038 stage('List target servers') {
39 minions = salt.getMinions(pepperEnv, TARGET_SERVERS)
40
41 if (minions.isEmpty()) {
42 throw new Exception("No minion was targeted")
43 }
44
45 if (TARGET_SUBSET_TEST != "") {
46 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
47 } else {
48 targetTestSubset = minions.join(' or ')
49 }
50 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
51
52 targetLiveAll = minions.join(' or ')
53 common.infoMsg("Found nodes: ${targetLiveAll}")
54 common.infoMsg("Selected test nodes: ${targetTestSubset}")
55 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
Ales Komarek374cc382017-03-16 08:49:01 +010056 }
Tomáš Kukráld73cef02017-04-05 15:24:57 +020057
Jakub Josefa63f9862018-01-11 17:58:38 +010058 stage("List package upgrades") {
59 common.infoMsg("Listing all the packages that have a new update available on test nodes: ${targetTestSubset}")
60 salt.runSaltProcessStep(pepperEnv, targetTestSubset, 'pkg.list_upgrades', [], null, true)
61 if(TARGET_PACKAGES != "" && TARGET_PACKAGES != "*"){
62 common.infoMsg("Note that only the ${TARGET_PACKAGES} would be installed from the above list of available updates on the ${targetTestSubset}")
63 }
Sam Stoelingaf52a0422017-08-01 12:26:21 -070064 }
Ales Komarek374cc382017-03-16 08:49:01 +010065
Jakub Josefa63f9862018-01-11 17:58:38 +010066 stage('Confirm live package upgrades on sample') {
67 if(TARGET_PACKAGES==""){
68 timeout(time: 2, unit: 'HOURS') {
69 def userInput = input(
70 id: 'userInput', message: 'Insert package names for update', parameters: [
71 [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages']
72 ])
73 if(userInput!= "" && userInput!= "*"){
74 TARGET_PACKAGES = userInput
75 }
76 }
77 }else{
78 timeout(time: 2, unit: 'HOURS') {
79 input message: "Approve live package upgrades on ${targetLiveSubset} nodes?"
Jakub Josef4a013752017-03-16 17:37:51 +010080 }
81 }
Jakub Josefa63f9862018-01-11 17:58:38 +010082 }
83
84 if (TARGET_PACKAGES != "") {
85 command = "pkg.install"
86 packages = TARGET_PACKAGES.tokenize(' ')
87 commandKwargs = ['only_upgrade': 'true']
88 }else {
89 command = "pkg.upgrade"
90 packages = null
91 }
92
93 stage('Apply package upgrades on sample') {
Pavel Cizinskyaf889c32018-08-15 15:20:42 +020094 if(packages == null || packages.contains("salt-master") || packages.contains("salt-common") || packages.contains("salt-minion") || packages.contains("salt-api")){
95 def saltTargets = (targetLiveSubset.split(' or ').collect{it as String})
96 for(int i = 0; i < saltTargets.size(); i++ ){
97 common.infoMsg("During salt-minion upgrade on cfg node, pipeline lose connectivy to salt-master for 2 min. If pipeline ended with error rerun pipeline again.")
98 common.retry(10, 5) {
99 if(salt.minionsReachable(pepperEnv, 'I@salt:master', "I@salt:master and ${saltTargets[i]}")){
100 installSaltStack("I@salt:master and ${saltTargets[i]}", '["salt-master", "salt-common", "salt-api", "salt-minion"]')
101 }
102 if(salt.minionsReachable(pepperEnv, 'I@salt:master', "I@salt:minion and not I@salt:master and ${saltTargets[i]}")){
103 installSaltStack("I@salt:minion and not I@salt:master and ${saltTargets[i]}", '["salt-minion"]')
104 }
105 }
106 }
107 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100108 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, packages, commandKwargs)
109 salt.printSaltCommandResult(out)
Martin Polreich6cb53622018-08-15 16:45:29 +0200110 for(value in out.get("return")[0].values()){
111 if (value.containsKey('result') && value.result == false) {
112 throw new Exception("The package upgrade on sample node has failed. Please check the Salt run result above for more information.")
113 }
114 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100115 }
116
117 stage('Confirm package upgrades on all nodes') {
Jakub Josef4a013752017-03-16 17:37:51 +0100118 timeout(time: 2, unit: 'HOURS') {
Jakub Josefa63f9862018-01-11 17:58:38 +0100119 input message: "Approve live package upgrades on ${targetLiveAll} nodes?"
Jakub Josef4a013752017-03-16 17:37:51 +0100120 }
Ales Komarek374cc382017-03-16 08:49:01 +0100121 }
Jakub Josefc5407c42017-03-16 18:31:10 +0100122
Jakub Josefa63f9862018-01-11 17:58:38 +0100123 stage('Apply package upgrades on all nodes') {
Pavel Cizinskyaf889c32018-08-15 15:20:42 +0200124
125 if(packages == null || packages.contains("salt-master") || packages.contains("salt-common") || packages.contains("salt-minion") || packages.contains("salt-api")){
126 def saltTargets = (targetLiveAll.split(' or ').collect{it as String})
127 for(int i = 0; i < saltTargets.size(); i++ ){
128 common.infoMsg("During salt-minion upgrade on cfg node, pipeline lose connectivy to salt-master for 2 min. If pipeline ended with error rerun pipeline again.")
129 common.retry(10, 5) {
130 if(salt.minionsReachable(pepperEnv, 'I@salt:master', "I@salt:master and ${saltTargets[i]}")){
131 installSaltStack("I@salt:master and ${saltTargets[i]}", '["salt-master", "salt-common", "salt-api", "salt-minion"]')
132 }
133 if(salt.minionsReachable(pepperEnv, 'I@salt:master', "I@salt:minion and not I@salt:master and ${saltTargets[i]}")){
134 installSaltStack("I@salt:minion and not I@salt:master and ${saltTargets[i]}", '["salt-minion"]')
135 }
136 }
137 }
138 }
139
Jakub Josefa63f9862018-01-11 17:58:38 +0100140 out = salt.runSaltCommand(pepperEnv, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, packages, commandKwargs)
141 salt.printSaltCommandResult(out)
Martin Polreich6cb53622018-08-15 16:45:29 +0200142 for(value in out.get("return")[0].values()){
143 if (value.containsKey('result') && value.result == false) {
144 throw new Exception("The package upgrade on sample node has failed. Please check the Salt run result above for more information.")
145 }
146 }
147 common.warningMsg("Pipeline has finished successfully, but please, check if any packages have been kept back.")
Ales Komarek374cc382017-03-16 08:49:01 +0100148 }
Ales Komarek374cc382017-03-16 08:49:01 +0100149
Jakub Josefa63f9862018-01-11 17:58:38 +0100150 } catch (Throwable e) {
151 // If there was an error or exception thrown, the build failed
152 currentBuild.result = "FAILURE"
153 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
154 throw e
Ales Komarek374cc382017-03-16 08:49:01 +0100155 }
Ales Komarek1fe5b8f2017-03-06 11:07:54 +0100156 }
157}