Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Update mirror image |
| 3 | * |
| 4 | * Expected parameters: |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 5 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API. |
| 6 | * SALT_MASTER_URL Full Salt API address [https://10.10.10.1:8000]. |
| 7 | * UPDATE_APTLY Option to update Aptly |
| 8 | * UPDATE_APTLY_MIRRORS List of mirrors |
| 9 | * PUBLISH_APTLY Publish aptly snapshots |
| 10 | * RECREATE_APTLY_PUBLISHES Option to recreate Aptly publishes separated by comma |
| 11 | * FORCE_OVERWRITE_APTLY_PUBLISHES Option to force overwrite existing packages while publishing |
| 12 | * CLEANUP_APTLY Option to cleanup old Aptly snapshots |
| 13 | * UPDATE_DOCKER_REGISTRY Option to update Docker Registry |
| 14 | * CLEANUP_DOCKER_CACHE Option to cleanup locally cached Docker images |
| 15 | * UPDATE_PYPI Option to update Python Packages |
| 16 | * UPDATE_GIT Option to update Git repositories |
| 17 | * UPDATE_IMAGES Option to update VM images |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 18 | * |
| 19 | **/ |
| 20 | |
| 21 | def common = new com.mirantis.mk.Common() |
| 22 | def salt = new com.mirantis.mk.Salt() |
| 23 | def python = new com.mirantis.mk.Python() |
| 24 | def venvPepper = "venvPepper" |
| 25 | |
| 26 | node() { |
| 27 | try { |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 28 | python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 29 | |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 30 | if(UPDATE_APTLY.toBoolean()){ |
| 31 | stage('Update Aptly mirrors'){ |
| 32 | def aptlyMirrorArgs = "-s -v" |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 33 | |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 34 | salt.enforceState(venvPepper, '*apt*', ['aptly.server'], true) |
| 35 | sleep(10) |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 36 | |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 37 | if(UPDATE_APTLY_MIRRORS != ""){ |
| 38 | common.infoMsg("Updating List of Aptly mirrors.") |
| 39 | UPDATE_APTLY_MIRRORS = UPDATE_APTLY_MIRRORS.replaceAll("\\s","") |
| 40 | def mirrors = UPDATE_APTLY_MIRRORS.tokenize(",") |
| 41 | for(mirror in mirrors){ |
| 42 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs} -m ${mirror}\"", 'runas=aptly'], null, true) |
| 43 | } |
| 44 | } |
| 45 | else{ |
| 46 | common.infoMsg("Updating all Aptly mirrors.") |
| 47 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs}\"", 'runas=aptly'], null, true) |
| 48 | } |
| 49 | } |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 50 | } |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 51 | if(PUBLISH_APTLY.toBoolean()){ |
| 52 | def aptlyPublishArgs = "-av" |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 53 | |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 54 | common.infoMsg("Publishing all Aptly snapshots.") |
| 55 | |
| 56 | salt.enforceState(venvPepper, '*apt*', ['aptly.publisher'], true) |
| 57 | sleep(10) |
| 58 | |
| 59 | if(CLEANUP_APTLY.toBoolean()){ |
| 60 | aptlyPublishArgs += "c" |
| 61 | } |
| 62 | if(RECREATE_APTLY_PUBLISHES.toBoolean()){ |
| 63 | aptlyPublishArgs += "r" |
| 64 | } |
| 65 | if(FORCE_OVERWRITE_APTLY_PUBLISHES.toBoolean()){ |
| 66 | aptlyPublishArgs += "f" |
| 67 | } |
| 68 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=\"${aptlyPublishArgs}\"", 'runas=aptly'], null, true) |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 69 | } |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 70 | if(UPDATE_DOCKER_REGISTRY.toBoolean()){ |
| 71 | stage('Update Docker images'){ |
| 72 | common.infoMsg("Updating Docker images.") |
| 73 | salt.enforceState(venvPepper, '*apt*', ['docker.client.registry'], true) |
| 74 | if(CLEANUP_DOCKER_CACHE.toBoolean()){ |
| 75 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['docker system prune --all --force'], null, true) |
| 76 | } |
| 77 | } |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 78 | } |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 79 | if(UPDATE_PYPI.toBoolean()){ |
| 80 | stage('Update PyPi packages'){ |
| 81 | common.infoMsg("Updating PyPi packages.") |
| 82 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt'], null, true) |
| 83 | } |
| 84 | } |
| 85 | if(UPDATE_GIT.toBoolean()){ |
| 86 | stage('Update Git repositories'){ |
| 87 | common.infoMsg("Updating Git repositories.") |
| 88 | salt.enforceState(venvPepper, '*apt*', ['git.server'], true) |
| 89 | } |
| 90 | } |
| 91 | if(UPDATE_IMAGES.toBoolean()){ |
| 92 | stage('Update VM images'){ |
| 93 | common.infoMsg("Updating VM images.") |
| 94 | salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.run', ['/srv/scripts/update-images.sh'], null, true) |
| 95 | } |
| 96 | } |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 97 | } catch (Throwable e) { |
| 98 | // If there was an error or exception thrown, the build failed |
| 99 | currentBuild.result = "FAILURE" |
| 100 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 101 | throw e |
| 102 | } |
| 103 | } |