Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 1 | /** |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 2 | * Update local mirror |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 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. |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 6 | * SALT_MASTER_URL Full Salt API address [https://10.10.10.1:6969]. |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 7 | * UPDATE_APTLY Option to update Aptly |
| 8 | * UPDATE_APTLY_MIRRORS List of mirrors |
| 9 | * PUBLISH_APTLY Publish aptly snapshots |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 10 | * RECREATE_APTLY_MIRRORS Recreate Aptly mirrors |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 11 | * RECREATE_APTLY_PUBLISHES Option to recreate Aptly publishes separated by comma |
| 12 | * FORCE_OVERWRITE_APTLY_PUBLISHES Option to force overwrite existing packages while publishing |
| 13 | * CLEANUP_APTLY Option to cleanup old Aptly snapshots |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 14 | * UPDATE_DEBMIRRORS Option to update Debmirrors |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 15 | * UPDATE_DOCKER_REGISTRY Option to update Docker Registry |
| 16 | * CLEANUP_DOCKER_CACHE Option to cleanup locally cached Docker images |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 17 | * UPDATE_GIT Option to update Git repositories |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 18 | * UPDATE_FILES Option to update static files |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 19 | * |
| 20 | **/ |
| 21 | |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 22 | common = new com.mirantis.mk.Common() |
| 23 | salt = new com.mirantis.mk.Salt() |
| 24 | python = new com.mirantis.mk.Python() |
| 25 | venvPepper = "venvPepper" |
| 26 | |
| 27 | @NonCPS |
| 28 | def Boolean dockerExists() { |
| 29 | def engine = salt.getPillar(venvPepper, 'I@aptly:server', "aptly:server:source:engine") |
| 30 | def matches = (engine =~ /:docker/) |
| 31 | try{ |
| 32 | def test = matches[position] |
| 33 | return false |
| 34 | }catch(Exception ex){ |
| 35 | return true |
| 36 | } |
| 37 | } |
| 38 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 39 | timeout(time: 12, unit: 'HOURS') { |
| 40 | node() { |
| 41 | try { |
| 42 | python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 43 | def dockerExists = dockerExists() |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 44 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 45 | if(UPDATE_APTLY.toBoolean()){ |
| 46 | stage('Update Aptly mirrors'){ |
| 47 | def aptlyMirrorArgs = "-s -v" |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 48 | |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 49 | if(RECREATE_APTLY_MIRRORS.toBoolean()) |
| 50 | { |
| 51 | if(dockerExists){ |
| 52 | salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true) |
| 53 | } |
| 54 | else{ |
| 55 | salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true, ['runas=aptly']) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | salt.enforceState(venvPepper, 'I@aptly:server', ['aptly.server'], true) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 60 | sleep(10) |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 61 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 62 | if(UPDATE_APTLY_MIRRORS != ""){ |
| 63 | common.infoMsg("Updating List of Aptly mirrors.") |
| 64 | UPDATE_APTLY_MIRRORS = UPDATE_APTLY_MIRRORS.replaceAll("\\s","") |
| 65 | def mirrors = UPDATE_APTLY_MIRRORS.tokenize(",") |
| 66 | for(mirror in mirrors){ |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 67 | if(dockerExists){ |
| 68 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs} -m ${mirror}\""], null, true) |
| 69 | }else{ |
| 70 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs} -m ${mirror}\"", 'runas=aptly'], null, true) |
| 71 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | else{ |
| 75 | common.infoMsg("Updating all Aptly mirrors.") |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 76 | |
| 77 | if(dockerExists){ |
| 78 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs}\""], null, true) |
| 79 | } |
| 80 | else{ |
| 81 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs}\"", 'runas=aptly'], null, true) |
| 82 | } |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 85 | } |
| 86 | if(PUBLISH_APTLY.toBoolean()){ |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 87 | def aptlyPublishArgs = "-v" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 88 | |
| 89 | common.infoMsg("Publishing all Aptly snapshots.") |
| 90 | |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 91 | salt.enforceState(venvPepper, 'I@aptly:server', ['aptly.publisher'], true) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 92 | sleep(10) |
| 93 | |
| 94 | if(CLEANUP_APTLY.toBoolean()){ |
| 95 | aptlyPublishArgs += "c" |
| 96 | } |
| 97 | if(RECREATE_APTLY_PUBLISHES.toBoolean()){ |
| 98 | aptlyPublishArgs += "r" |
| 99 | } |
| 100 | if(FORCE_OVERWRITE_APTLY_PUBLISHES.toBoolean()){ |
| 101 | aptlyPublishArgs += "f" |
| 102 | } |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 103 | if(dockerExists){ |
| 104 | aptlyPublishArgs += " -u http://10.99.0.1:8080" |
| 105 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=\"${aptlyPublishArgs}\""], null, true) |
| 106 | } |
| 107 | else{ |
| 108 | aptlyPublishArgs += "a" |
| 109 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=\"${aptlyPublishArgs}\"", 'runas=aptly'], null, true) |
| 110 | } |
| 111 | } |
| 112 | if(UPDATE_DEBMIRRORS.toBoolean()){ |
| 113 | stage('Update Debmirrors'){ |
| 114 | common.infoMsg("Updating Debmirrors") |
| 115 | salt.enforceState(venvPepper, 'I@debmirror:client', 'debmirror') |
| 116 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 117 | } |
| 118 | if(UPDATE_DOCKER_REGISTRY.toBoolean()){ |
| 119 | stage('Update Docker images'){ |
| 120 | common.infoMsg("Updating Docker images.") |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 121 | salt.enforceState(venvPepper, 'I@aptly:server', 'docker.client.registry') |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 122 | if(CLEANUP_DOCKER_CACHE.toBoolean()){ |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 123 | salt.cmdRun(venvPepper, 'I@aptly:server', 'docker system prune --all --force') |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 124 | } |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 127 | if(UPDATE_GIT.toBoolean()){ |
| 128 | stage('Update Git repositories'){ |
| 129 | common.infoMsg("Updating Git repositories.") |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 130 | salt.enforceState(venvPepper, 'I@aptly:server', ['git.server'], true) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 131 | } |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 132 | } |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 133 | if(UPDATE_FILES.toBoolean()){ |
| 134 | stage('Update static files'){ |
| 135 | common.infoMsg("Updating static files.") |
| 136 | salt.enforceState(venvPepper, 'I@aptly:server', ['linux.system.file'], true) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 137 | } |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 138 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 139 | } catch (Throwable e) { |
| 140 | // If there was an error or exception thrown, the build failed |
| 141 | currentBuild.result = "FAILURE" |
| 142 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 143 | throw e |
Richard Felkl | 07043e4 | 2017-11-28 15:03:42 +0100 | [diff] [blame] | 144 | } |
Richard Felkl | 4ae811f | 2017-10-10 18:17:27 +0200 | [diff] [blame] | 145 | } |
Jakub Josef | 2c21c6c | 2018-02-08 18:51:42 +0100 | [diff] [blame] | 146 | } |