vitalygusev | 2270137 | 2018-09-25 17:27:35 +0400 | [diff] [blame^] | 1 | /** |
| 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 | |
| 15 | def common = new com.mirantis.mk.Common() |
| 16 | def salt = new com.mirantis.mk.Salt() |
| 17 | def python = new com.mirantis.mk.Python() |
| 18 | |
| 19 | def pepperEnv = "pepperEnv" |
| 20 | def targetLiveSubset |
| 21 | def targetLiveAll |
| 22 | def minions |
| 23 | def result |
| 24 | def args |
| 25 | def commandKwargs |
| 26 | def probe = 1 |
| 27 | def errorOccured = false |
| 28 | def command = 'cmd.run' |
| 29 | |
| 30 | def upgrade(master, target, service, pckg, state) { |
| 31 | stage("Change ${target} repos") { |
| 32 | salt.runSaltProcessStep(master, "${target}", 'saltutil.refresh_pillar', [], null, true, 5) |
| 33 | salt.enforceState(master, "${target}", 'linux.system.repo', true) |
| 34 | } |
| 35 | stage("Update ${pckg} package") { |
| 36 | common.infoMsg("Upgrade ${service} package") |
| 37 | try { |
| 38 | salt.runSaltProcessStep(master, "${target}", command, ["apt-get install --only-upgrade ${pckg}"], null, true) |
| 39 | } catch (Exception er) { |
| 40 | errorOccured = true |
| 41 | common.errorMsg("${pckg} package is not upgraded.") |
| 42 | return |
| 43 | } |
| 44 | } |
| 45 | stage("Run ${state} on ${target}") { |
| 46 | try { |
| 47 | salt.enforceState(master, '${target}', '${state}') |
| 48 | } catch (Exception er) { |
| 49 | errorOccured = true |
| 50 | common.errorMsg('${state} state was executed and failed. Please fix it manually.') |
| 51 | } |
| 52 | } |
| 53 | out = salt.runSaltCommand(master, 'local', ['expression': '${target}', 'type': 'compound'], command, null, 'systemctl status ${service}.service', null) |
| 54 | salt.printSaltCommandResult(out) |
| 55 | |
| 56 | common.warningMsg('Please check \'systemctl status ${service}.service\' on ${target} nodes if ${service} is running.') |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | def upgrade_es_kibana(master) { |
| 61 | stage('Elasticsearch upgrade') { |
| 62 | try { |
| 63 | salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["systemctl stop elasticsearch"], null, true) |
| 64 | salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["apt-get --only-upgrade install elasticsearch"], null, true) |
| 65 | salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["systemctl daemon-reload"], null, true) |
| 66 | salt.runSaltProcessStep(master, 'I@elasticsearch:server', command, ["systemctl start elasticsearch"], null, true) |
| 67 | salt.runSaltProcessStep(master, '*', 'saltutil.sync_all', [], null, true) |
| 68 | } catch (Exception er) { |
| 69 | errorOccured = true |
| 70 | common.errorMsg("Elasticsearch upgrade failed. Please fix it manually.") |
| 71 | return |
| 72 | } |
| 73 | } |
| 74 | stage('Verify that the Elasticsearch cluster status is green') { |
| 75 | try { |
| 76 | def retries_wait = 20 |
| 77 | def retries = 15 |
| 78 | def elasticsearch_vip |
| 79 | if(!pillar['return'].isEmpty()) { |
| 80 | elasticsearch_vip = pillar['return'][0].values()[0] |
| 81 | } else { |
| 82 | errorOccured = true |
| 83 | common.errorMsg('[ERROR] Elasticsearch VIP address could not be retrieved') |
| 84 | } |
| 85 | pillar = salt.getPillar(master, "I@elasticsearch:client", 'elasticsearch:client:server:port') |
| 86 | def elasticsearch_port |
| 87 | if(!pillar['return'].isEmpty()) { |
| 88 | elasticsearch_port = pillar['return'][0].values()[0] |
| 89 | } else { |
| 90 | errorOccured = true |
| 91 | common.errorMsg('[ERROR] Elasticsearch VIP port could not be retrieved') |
| 92 | } |
| 93 | common.retry(retries,retries_wait) { |
| 94 | common.infoMsg('Waiting for Elasticsearch to become green..') |
| 95 | salt.cmdRun(master, "I@elasticsearch:client", "curl -sf ${elasticsearch_vip}:${elasticsearch_port}/_cat/health | awk '{print \$4}' | grep green") |
| 96 | } |
| 97 | } catch (Exception er) { |
| 98 | errorOccured = true |
| 99 | common.errorMsg("Elasticsearch cluster status is not \'green\'. Please fix it manually.") |
| 100 | return |
| 101 | } |
| 102 | } |
| 103 | stage('Kibana upgrade') { |
| 104 | try { |
| 105 | salt.runSaltProcessStep(master, 'I@kibana:server', command, ["systemctl stop kibana"], null, true) |
| 106 | salt.runSaltProcessStep(master, 'I@kibana:server', command, ["apt-get --only-upgrade install kibana"], null, true) |
| 107 | salt.runSaltProcessStep(master, 'I@kibana:server', command, ["systemctl start kibana"], null, true) |
| 108 | } catch (Exception er) { |
| 109 | errorOccured = true |
| 110 | common.errorMsg("Kibana upgrade failed. Please fix it manually.") |
| 111 | return |
| 112 | } |
| 113 | out = salt.runSaltCommand(master, 'local', ['expression': 'I@kibana:server', 'type': 'compound'], command, null, 'systemctl status kibana.service', null) |
| 114 | salt.printSaltCommandResult(out) |
| 115 | |
| 116 | common.warningMsg('Please check if kibana service is running.') |
| 117 | return |
| 118 | } |
| 119 | } |
| 120 | timeout(time: 12, unit: 'HOURS') { |
| 121 | node("python") { |
| 122 | |
| 123 | stage('Setup virtualenv for Pepper') { |
| 124 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 125 | } |
| 126 | |
| 127 | if (STAGE_UPGRADE_SYSTEM_PART.toBoolean() == true && !errorOccured) { |
| 128 | upgrade(pepperEnv, "I@telegraf:agent or I@telegraf:remote_agent", "telegraf", "telegraf", "telegraf") |
| 129 | upgrade(pepperEnv, "I@fluentd:agent", "td-agent", "td-agent", "fluentd") |
| 130 | if (salt.testTarget(pepperEnv, "I@prometheus:relay")) { |
| 131 | upgrade(pepperEnv, "I@prometheus:relay", "prometheus-relay", "prometheus-relay", "prometheus") |
| 132 | } |
| 133 | if (salt.testTarget(pepperEnv, "I@prometheus:exporters:libvirt")) { |
| 134 | upgrade(pepperEnv, "I@prometheus:exporters:libvirt", "libvirt-exporter", "libvirt-exporter", "prometheus") |
| 135 | } |
| 136 | if (salt.testTarget(pepperEnv, "I@prometheus:exporters:jmx")) { |
| 137 | upgrade(pepperEnv, "I@prometheus:exporters:jmx", "jmx-exporter", "jmx-exporter", "prometheus") |
| 138 | } |
| 139 | if (STAGE_UPGRADE_ES_KIBANA.toBoolean() == true && !errorOccured) { |
| 140 | upgrade_es_kibana(pepperEnv) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (STAGE_UPGRADE_DOCKER_COMPONENTS.toBoolean() == true && !errorOccured) { |
| 145 | |
| 146 | stage('Docker components upgrade') { |
| 147 | |
| 148 | try { |
| 149 | salt.runSaltProcessStep(pepperEnv, 'I@docker:swarm:role:master and I@prometheus:server', command, ["docker stack rm monitoring"], null, true) |
| 150 | salt.enforceState(pepperEnv, 'I@docker:swarm and I@prometheus:server', 'prometheus') |
| 151 | salt.runSaltProcessStep(pepperEnv, 'I@docker:swarm:role:master and I@prometheus:server', command, ["docker stack rm dashboard"], null, true) |
| 152 | salt.enforceState(pepperEnv, 'I@docker:swarm:role:master and I@prometheus:server', 'docker') |
| 153 | salt.runSaltProcessStep(pepperEnv, '*', 'saltutil.sync_all', [], null, true) |
| 154 | salt.enforceState(pepperEnv, 'I@grafana:client', 'grafana.client') |
| 155 | } catch (Exception er) { |
| 156 | errorOccured = true |
| 157 | common.errorMsg("Upgrade of docker components failed. Please fix it manually.") |
| 158 | return |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |