blob: 0ce3e268a734ca1c8eec6b0ce4c32dfc2195a5a8 [file] [log] [blame]
vitalygusev22701372018-09-25 17:27:35 +04001/**
2 *
3 * Upgrade Stacklight packages and components
4 *
5 * Requred parameters:
6 * SALT_MASTER_URL URL of Salt master
7 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
8 *
9 * STAGE_UPGRADE_SYSTEM_PART Set to True if upgrade of system part (telegraf, fluentd, prometheus-relay) is desired
10 * STAGE_UPGRADE_ES_KIBANA Set to True if Elasticsearch and Kibana upgrade is desired
11 * STAGE_UPGRADE_DOCKER_COMPONENTS Set to True if upgrade for components running in Docker Swarm is desired
12 *
13 */
14
vitalygusev4b331fa2018-12-04 19:03:47 +040015common = new com.mirantis.mk.Common()
16salt = new com.mirantis.mk.Salt()
17python = new com.mirantis.mk.Python()
18command = 'cmd.run'
19pepperEnv = "pepperEnv"
20errorOccured = false
vitalygusev22701372018-09-25 17:27:35 +040021
22def upgrade(master, target, service, pckg, state) {
vitalygusev4b331fa2018-12-04 19:03:47 +040023 stage("Upgrade ${service}") {
vitalygusev25872e12018-12-14 18:45:29 +040024 salt.runSaltProcessStep(master, "${target}", 'saltutil.refresh_pillar', [], null, true)
vitalygusev22701372018-09-25 17:27:35 +040025 salt.enforceState(master, "${target}", 'linux.system.repo', true)
vitalygusev25872e12018-12-14 18:45:29 +040026 common.infoMsg("Upgrade ${service} package(s)")
vitalygusev22701372018-09-25 17:27:35 +040027 try {
vitalygusev25872e12018-12-14 18:45:29 +040028 salt.runSaltProcessStep(master, "${target}", command, ["apt-get install -y -o Dpkg::Options::=\"--force-confold\" ${pckg}"], null, true)
vitalygusev22701372018-09-25 17:27:35 +040029 } catch (Exception er) {
30 errorOccured = true
vitalygusev25872e12018-12-14 18:45:29 +040031 common.errorMsg("[ERROR] ${pckg} package(s) was not upgraded.")
32 throw er
vitalygusev22701372018-09-25 17:27:35 +040033 }
vitalygusev4b331fa2018-12-04 19:03:47 +040034 common.infoMsg("Run ${state} state on ${target} nodes")
vitalygusev22701372018-09-25 17:27:35 +040035 try {
vitalygusev25328892018-10-17 11:40:27 +040036 salt.enforceState(master, "${target}", ["${state}"], true)
vitalygusev22701372018-09-25 17:27:35 +040037 } catch (Exception er) {
38 errorOccured = true
vitalygusev4b331fa2018-12-04 19:03:47 +040039 common.errorMsg("[ERROR] ${state} state was executed and failed. Please fix it manually.")
vitalygusev25872e12018-12-14 18:45:29 +040040 throw er
vitalygusev22701372018-09-25 17:27:35 +040041 }
vitalygusev25872e12018-12-14 18:45:29 +040042 common.infoMsg("Check ${service} service(s) status on the target nodes")
43 for (s in service.split(" ")){
44 salt.runSaltProcessStep(master, "${target}", "service.status", "${s}", null, true)
45 }
46 }
47}
48
49def verify_es_is_green(master) {
50 common.infoMsg('Verify that the Elasticsearch cluster status is green')
51 try {
52 def retries_wait = 20
53 def retries = 15
54 def elasticsearch_vip
55 def pillar = salt.getPillar(master, "I@elasticsearch:client", 'elasticsearch:client:server:host')
56 if(!pillar['return'].isEmpty()) {
57 elasticsearch_vip = pillar['return'][0].values()[0]
58 } else {
59 errorOccured = true
60 common.errorMsg('[ERROR] Elasticsearch VIP address could not be retrieved')
61 }
62 pillar = salt.getPillar(master, "I@elasticsearch:client", 'elasticsearch:client:server:port')
63 def elasticsearch_port
64 if(!pillar['return'].isEmpty()) {
65 elasticsearch_port = pillar['return'][0].values()[0]
66 } else {
67 errorOccured = true
68 common.errorMsg('[ERROR] Elasticsearch VIP port could not be retrieved')
69 }
70 common.retry(retries,retries_wait) {
71 common.infoMsg('Waiting for Elasticsearch to become green..')
72 salt.cmdRun(master, "I@elasticsearch:client", "curl -sf ${elasticsearch_vip}:${elasticsearch_port}/_cat/health | awk '{print \$4}' | grep green")
73 }
74 } catch (Exception er) {
75 errorOccured = true
76 common.errorMsg("[ERROR] Elasticsearch cluster status is not \'green\'. Please fix it manually.")
77 throw er
vitalygusev22701372018-09-25 17:27:35 +040078 }
vitalygusev22701372018-09-25 17:27:35 +040079}
80
81def upgrade_es_kibana(master) {
vitalygusev25872e12018-12-14 18:45:29 +040082 def elasticsearch_version
83 def es_pillar = salt.getPillar(master, "I@elasticsearch:client", '_param:elasticsearch_version')
84 if(!es_pillar['return'].isEmpty()) {
85 elasticsearch_version = es_pillar['return'][0].values()[0]
86 }
vitalygusev4b331fa2018-12-04 19:03:47 +040087 stage('Upgrade elasticsearch') {
vitalygusev25872e12018-12-14 18:45:29 +040088 if (elasticsearch_version == '5') {
89 try {
90 common.infoMsg('Upgrade the Elasticsearch package')
91 salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["systemctl stop elasticsearch"], null, true)
92 salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["apt-get --only-upgrade install elasticsearch"], null, true)
93 salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["systemctl daemon-reload"], null, true)
94 salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["systemctl start elasticsearch"], null, true)
95 salt.runSaltProcessStep(master, '*', 'saltutil.sync_all', [], null, true)
96 verify_es_is_green(master)
97 } catch (Exception er) {
vitalygusev22701372018-09-25 17:27:35 +040098 errorOccured = true
vitalygusev25872e12018-12-14 18:45:29 +040099 common.errorMsg("[ERROR] Elasticsearch upgrade failed. Please fix it manually.")
100 throw er
vitalygusev22701372018-09-25 17:27:35 +0400101 }
vitalygusev25872e12018-12-14 18:45:29 +0400102 } else {
103 try {
104 salt.runSaltProcessStep(master, "*", 'saltutil.refresh_pillar', [], null, true)
105 salt.enforceState(master, "I@elasticsearch:server", 'linux.system.repo', true)
106 salt.runSaltProcessStep(master, 'I@elasticsearch:client', command, ["apt-get install -y -o Dpkg::Options::=\"--force-confold\" python-elasticsearch"], null, true)
107 salt.enforceState(master, "I@elasticsearch:server", 'salt.minion', true)
108 salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["systemctl stop elasticsearch"], null, true)
109 salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["export ES_PATH_CONF=/etc/elasticsearch; apt-get install -y -o Dpkg::Options::=\"--force-confold\" elasticsearch"], null, true)
110 salt.enforceState(master, "I@elasticsearch:server", 'elasticsearch.server', true)
111 verify_es_is_green(master)
112 salt.enforceState(master, "I@elasticsearch:client", 'elasticsearch.client.update_index_templates', true)
113 salt.enforceState(master, "I@elasticsearch:client", 'elasticsearch.client', true)
114 } catch (Exception er) {
vitalygusev22701372018-09-25 17:27:35 +0400115 errorOccured = true
vitalygusev25872e12018-12-14 18:45:29 +0400116 common.errorMsg("[ERROR] Elasticsearch upgrade failed. Please fix it manually.")
117 throw er
vitalygusev22701372018-09-25 17:27:35 +0400118 }
vitalygusev22701372018-09-25 17:27:35 +0400119 }
120 }
vitalygusev4b331fa2018-12-04 19:03:47 +0400121 stage('Upgrade kibana') {
vitalygusev25872e12018-12-14 18:45:29 +0400122 def kibana_version
123 def kibana_pillar = salt.getPillar(master, "I@kibana:client", '_param:kibana_version')
124 if(!kibana_pillar['return'].isEmpty()) {
125 kibana_version = kibana_pillar['return'][0].values()[0]
126 }
127 if (kibana_version == '5') {
128 try {
129 common.infoMsg('Upgrade the Kibana package')
130 salt.runSaltProcessStep(master, 'I@kibana:server', command, ["systemctl stop kibana"], null, true)
131 salt.runSaltProcessStep(master, 'I@kibana:server', command, ["apt-get --only-upgrade install kibana"], null, true)
132 salt.runSaltProcessStep(master, 'I@kibana:server', command, ["systemctl start kibana"], null, true)
133 } catch (Exception er) {
134 errorOccured = true
135 common.errorMsg("[ERROR] Kibana upgrade failed. Please fix it manually.")
136 throw er
137 }
138 } else {
139 try {
140 salt.runSaltProcessStep(master, 'I@kibana:server', command, ["systemctl stop kibana"], null, true)
141 salt.enforceStateWithExclude(pepperEnv, "I@kibana:server", "kibana.server", "[{'id': 'kibana_service'}]")
142 salt.runSaltProcessStep(master, 'I@kibana:server', command, ["apt-get install -y -o Dpkg::Options::=\"--force-confold\" kibana"], null, true)
143 salt.enforceState(master, "I@kibana:server", 'kibana.server', true)
144 salt.enforceState(master, "I@kibana:client", 'kibana.client', true)
145 } catch (Exception er) {
146 errorOccured = true
147 common.errorMsg("[ERROR] Kibana upgrade failed. Please fix it manually.")
148 throw er
149 }
vitalygusev22701372018-09-25 17:27:35 +0400150 }
vitalygusev22701372018-09-25 17:27:35 +0400151
vitalygusev4b331fa2018-12-04 19:03:47 +0400152 common.infoMsg("Check kibana status on the target nodes")
153 salt.runSaltProcessStep(master, "I@kibana:server", "service.status", ["kibana"], null, true)
vitalygusev22701372018-09-25 17:27:35 +0400154 }
155}
156timeout(time: 12, unit: 'HOURS') {
157 node("python") {
158
159 stage('Setup virtualenv for Pepper') {
160 python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
161 }
162
vitalygusev25872e12018-12-14 18:45:29 +0400163 salt.enforceState(pepperEnv, '*', 'salt.minion.grains')
164 salt.runSaltProcessStep(pepperEnv, '*', 'saltutil.refresh_modules')
165 salt.runSaltProcessStep(pepperEnv, '*', 'mine.update')
166 sleep(30)
167
168
vitalygusev22701372018-09-25 17:27:35 +0400169 if (STAGE_UPGRADE_SYSTEM_PART.toBoolean() == true && !errorOccured) {
170 upgrade(pepperEnv, "I@telegraf:agent or I@telegraf:remote_agent", "telegraf", "telegraf", "telegraf")
vitalygusev4b331fa2018-12-04 19:03:47 +0400171 upgrade(pepperEnv, "I@fluentd:agent", "td-agent", "td-agent td-agent-additional-plugins", "fluentd")
vitalygusev22701372018-09-25 17:27:35 +0400172 if (salt.testTarget(pepperEnv, "I@prometheus:relay")) {
vitalygusev25872e12018-12-14 18:45:29 +0400173 upgrade(pepperEnv, "I@prometheus:relay", "prometheus prometheus-relay", "prometheus-bin prometheus-relay", "prometheus")
174 salt.runSaltProcessStep(pepperEnv, "I@prometheus:relay", "service.restart", "prometheus", null, true)
vitalygusev22701372018-09-25 17:27:35 +0400175 }
176 if (salt.testTarget(pepperEnv, "I@prometheus:exporters:libvirt")) {
177 upgrade(pepperEnv, "I@prometheus:exporters:libvirt", "libvirt-exporter", "libvirt-exporter", "prometheus")
178 }
179 if (salt.testTarget(pepperEnv, "I@prometheus:exporters:jmx")) {
180 upgrade(pepperEnv, "I@prometheus:exporters:jmx", "jmx-exporter", "jmx-exporter", "prometheus")
181 }
vitalygusev25328892018-10-17 11:40:27 +0400182 }
183
184 if (STAGE_UPGRADE_ES_KIBANA.toBoolean() == true && !errorOccured) {
185 upgrade_es_kibana(pepperEnv)
vitalygusev22701372018-09-25 17:27:35 +0400186 }
187
188 if (STAGE_UPGRADE_DOCKER_COMPONENTS.toBoolean() == true && !errorOccured) {
189
vitalygusev4b331fa2018-12-04 19:03:47 +0400190 stage('Upgrade docker components') {
vitalygusev22701372018-09-25 17:27:35 +0400191
192 try {
vitalygusev4b331fa2018-12-04 19:03:47 +0400193 common.infoMsg('Disable and remove the previous versions of monitoring services')
194 salt.runSaltProcessStep(pepperEnv, 'I@docker:swarm:role:master and I@prometheus:server', command, ["docker stack rm monitoring"], null, true)
195 common.infoMsg('Rebuild the Prometheus configuration')
vitalygusev22701372018-09-25 17:27:35 +0400196 salt.enforceState(pepperEnv, 'I@docker:swarm and I@prometheus:server', 'prometheus')
vitalygusev4b331fa2018-12-04 19:03:47 +0400197 common.infoMsg('Disable and remove the previous version of Grafana')
198 salt.runSaltProcessStep(pepperEnv, 'I@docker:swarm:role:master and I@prometheus:server', command, ["docker stack rm dashboard"], null, true)
199 common.infoMsg('Start the monitoring services')
vitalygusev22701372018-09-25 17:27:35 +0400200 salt.enforceState(pepperEnv, 'I@docker:swarm:role:master and I@prometheus:server', 'docker')
201 salt.runSaltProcessStep(pepperEnv, '*', 'saltutil.sync_all', [], null, true)
vitalygusev4b331fa2018-12-04 19:03:47 +0400202 common.infoMsg('Refresh the Grafana dashboards')
vitalygusev22701372018-09-25 17:27:35 +0400203 salt.enforceState(pepperEnv, 'I@grafana:client', 'grafana.client')
204 } catch (Exception er) {
205 errorOccured = true
vitalygusev4b331fa2018-12-04 19:03:47 +0400206 common.errorMsg("[ERROR] Upgrade of docker components failed. Please fix it manually.")
vitalygusev25872e12018-12-14 18:45:29 +0400207 throw er
vitalygusev22701372018-09-25 17:27:35 +0400208 }
209 }
210 }
211 }
212}