Alexandr Lovtsov | 58e8159 | 2019-05-15 11:56:29 +0300 | [diff] [blame] | 1 | /** |
| 2 | * Update packages on given server nodes |
| 3 | * |
| 4 | * Expected parameters: |
| 5 | * DRIVE_TRAIN_PARAMS Yaml, DriveTrain releated params: |
| 6 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
| 7 | * SALT_MASTER_URL Full Salt API address [https://10.10.10.1:8000] |
| 8 | * IGNORE_SERVER_STATUS Does not validate server availability/status before update |
| 9 | * IGNORE_SERVER_VERSION Does not validate that all servers have been updated |
| 10 | * TARGET_SERVERS Salt compound target to match nodes to be updated [*, G@osfamily:debian] |
| 11 | */ |
| 12 | |
| 13 | // Convert parameters from yaml to env variables |
| 14 | params = readYaml text: env.DRIVE_TRAIN_PARAMS |
| 15 | for (key in params.keySet()) { |
| 16 | value = params[key] |
| 17 | env.setProperty(key, value) |
| 18 | } |
| 19 | |
| 20 | @NonCPS |
| 21 | def getNextNode() { |
| 22 | for (n in hudson.model.Hudson.instance.slaves) { |
| 23 | node_name = n.getNodeName() |
| 24 | if (node_name != env.SLAVE_NAME) { |
| 25 | return node_name |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | def update() { |
| 31 | def pEnv = "pepperEnv" |
| 32 | def salt = new com.mirantis.mk.Salt() |
| 33 | def common = new com.mirantis.mk.Common() |
| 34 | def python = new com.mirantis.mk.Python() |
| 35 | def pkg_name = 'glusterfs-client' |
| 36 | |
| 37 | /** |
| 38 | * - choose only those hosts where update is available. Exclude minion on which job is running |
| 39 | * - validate that all gluasterfs servers are in normal working state. Can be skipped with option |
| 40 | * - validate that glusterfs on all servers has been updated, otherwise stop update. Can be skipped with option |
| 41 | * - run update state on one client at a time |
| 42 | */ |
| 43 | |
| 44 | try { |
| 45 | |
| 46 | stage('Setup virtualenv for Pepper') { |
| 47 | python.setupPepperVirtualenv(pEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 48 | } |
| 49 | |
| 50 | stage('List target servers') { |
| 51 | all_minions = salt.getMinions(pEnv, TARGET_SERVERS) |
| 52 | |
| 53 | if (all_minions.isEmpty()) { |
| 54 | throw new Exception("No minion was targeted") |
| 55 | } |
| 56 | |
| 57 | minions = [] |
| 58 | for (minion in all_minions) { |
| 59 | latest_version = salt.getReturnValues(salt.runSaltProcessStep(pEnv, minion, 'pkg.latest_version', [pkg_name, 'show_installed=True'])).split('\n')[0] |
| 60 | current_version = salt.getReturnValues(salt.runSaltProcessStep(pEnv, minion, 'pkg.version', [pkg_name])).split('\n')[0] |
| 61 | slave_container_id = salt.getReturnValues(salt.cmdRun(pEnv, minion, "which docker >/dev/null && docker ps --filter name=jenkins_${env.NODE_NAME} --filter status=running -q", false)).split('\n')[0] |
| 62 | if (latest_version != current_version) { |
| 63 | if (!slave_container_id.isEmpty() && !minion.startsWith('cfg')) { |
| 64 | env.SLAVE_NAME = env.NODE_NAME |
| 65 | env.SLAVE_MINION = minion |
| 66 | } else { |
| 67 | minions.add(minion) |
| 68 | } |
| 69 | } else { |
| 70 | common.infoMsg("${pkg_name} has been already upgraded or newer version is not available on ${minion}. Skip upgrade") |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | if (!minions.isEmpty()) { |
| 75 | if (!IGNORE_SERVER_STATUS.toBoolean()){ |
| 76 | stage('Validate servers availability') { |
| 77 | salt.commandStatus(pEnv, 'I@glusterfs:server', "gluster pool list | fgrep localhost", 'Connected', true, true, null, true, 1) |
| 78 | common.successMsg("All glusterfs servers are available") |
| 79 | } |
| 80 | } else { |
| 81 | common.warningMsg("Check of glusterfs servers availability has been disabled") |
| 82 | } |
| 83 | if (!IGNORE_SERVER_VERSION.toBoolean()){ |
| 84 | stage('Check that all glusterfs servers have been updated') { |
| 85 | latest_version = salt.getReturnValues(salt.runSaltProcessStep(pEnv, minions[0], 'pkg.latest_version', [pkg_name, 'show_installed=True'])).split('\n')[0].split('-')[0] |
| 86 | salt.commandStatus(pEnv, 'I@glusterfs:server', "glusterfsd --version | head -n1 | awk '{print \$2}' | egrep '^${latest_version}' || echo none", latest_version, true, true, null, true, 1) |
| 87 | common.successMsg('All glusterfs servers have been updated to desired version') |
| 88 | } |
| 89 | } else { |
| 90 | common.warningMsg("Check of glusterfs servers' version has been disabled") |
| 91 | } |
| 92 | // Actual update |
| 93 | for (tgt in minions) { |
| 94 | stage("Update glusterfs on ${tgt}") { |
| 95 | salt.runSaltProcessStep(pEnv, tgt, 'state.apply', ['glusterfs.update.client']) |
| 96 | } |
| 97 | } |
| 98 | } else if (env.SLAVE_MINION == null) { |
| 99 | common.warningMsg("No hosts to update glusterfs on") |
| 100 | } |
| 101 | } catch (Throwable e) { |
| 102 | // If there was an error or exception thrown, the build failed |
| 103 | currentBuild.result = "FAILURE" |
| 104 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 105 | salt.runSaltProcessStep(pEnv, TARGET_SERVERS, 'state.apply', ['glusterfs']) |
| 106 | throw e |
| 107 | } |
| 108 | } |
| 109 | timeout(time: 12, unit: 'HOURS') { |
| 110 | node() { |
| 111 | update() |
| 112 | } |
| 113 | // Perform an update from another slave to finish update on previous slave host |
| 114 | if (env.SLAVE_NAME != null && !env.SLAVE_NAME.isEmpty()) { |
| 115 | node(getNextNode()) { |
| 116 | update() |
| 117 | } |
| 118 | } |
| 119 | } |