blob: cde839c8baef224c54514a26ffd59e4fceb76134 [file] [log] [blame]
Jiri Broulik9b73d6c2017-06-02 12:27:05 +02001/**
2 * Update packages on given nodes
3 *
4 * Expected parameters:
5 * 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_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.
12 *
13**/
14
15def common = new com.mirantis.mk.Common()
16def salt = new com.mirantis.mk.Salt()
17
18def saltMaster
19def targetAll = ['expression': TARGET_SERVERS, 'type': 'compound']
20def targetTestSubset
21def targetLiveSubset
22def targetLiveAll
23def minions
24def result
25def packages
26def command
27def commandKwargs
28
29node() {
30 try {
31
32 stage('Connect to Salt master') {
33 saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
34 }
35
36 stage('List target servers') {
37 minions = salt.getMinions(saltMaster, targetAll)
38
39 if (minions.isEmpty()) {
40 throw new Exception("No minion was targeted")
41 }
42
43 if (TARGET_SUBSET_TEST != "") {
44 targetTestSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_TEST)).join(' or ')
45 } else {
46 targetTestSubset = minions.join(' or ')
47 }
48 targetLiveSubset = minions.subList(0, Integer.valueOf(TARGET_SUBSET_LIVE)).join(' or ')
49
50 targetLiveAll = minions.join(' or ')
51 common.infoMsg("Found nodes: ${targetLiveAll}")
52 common.infoMsg("Selected test nodes: ${targetTestSubset}")
53 common.infoMsg("Selected sample nodes: ${targetLiveSubset}")
54 }
55
56 stage("Add new repos on sample") {
57 salt.enforceState(saltMaster, targetTestSubset, 'linux.system')
58 }
59
60 stage("List package upgrades") {
61 salt.runSaltProcessStep(saltMaster, targetTestSubset, 'pkg.list_upgrades', [], null, true)
62 }
63
64 stage('Confirm live package upgrades on sample') {
65 if(TARGET_PACKAGES==""){
66 timeout(time: 2, unit: 'HOURS') {
67 def userInput = input(
68 id: 'userInput', message: 'Insert package names for update', parameters: [
69 [$class: 'TextParameterDefinition', defaultValue: '', description: 'Package names (or *)', name: 'packages']
70 ])
71 if(userInput!= "" && userInput!= "*"){
72 TARGET_PACKAGES = userInput
73 }
74 }
75 }else{
76 timeout(time: 2, unit: 'HOURS') {
77 input message: "Approve live package upgrades on ${targetLiveSubset} nodes?"
78 }
79 }
80 }
81
82 if (TARGET_PACKAGES != "") {
83 command = "pkg.install"
84 packages = TARGET_PACKAGES.tokenize(' ')
85 commandKwargs = ['only_upgrade': 'true']
86 }else {
87 command = "pkg.upgrade"
88 packages = null
89 }
90
91 stage('Apply package upgrades on sample') {
92 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveSubset, 'type': 'compound'], command, null, packages, commandKwargs)
93 salt.printSaltCommandResult(out)
94 }
95
96 stage('Confirm package upgrades on all nodes') {
97 timeout(time: 2, unit: 'HOURS') {
98 input message: "Approve live package upgrades on ${targetLiveAll} nodes?"
99 }
100 }
101
102 stage('Apply package upgrades on all nodes') {
103 out = salt.runSaltCommand(saltMaster, 'local', ['expression': targetLiveAll, 'type': 'compound'], command, null, packages, commandKwargs)
104 salt.printSaltCommandResult(out)
105 }
106
107 } catch (Throwable e) {
108 // If there was an error or exception thrown, the build failed
109 currentBuild.result = "FAILURE"
110 throw e
111 }
112}