Modified local repo updates

Related: PROD-18333

Change-Id: I219f962f34b1534e7667f918ac4fab24d43b167a
diff --git a/update-mirror-image.groovy b/update-mirror-image.groovy
index e37d537..2f71d2a 100644
--- a/update-mirror-image.groovy
+++ b/update-mirror-image.groovy
@@ -1,37 +1,62 @@
 /**
- * Update mirror image
+ * Update local mirror
  *
  * Expected parameters:
  *   SALT_MASTER_CREDENTIALS            Credentials to the Salt API.
- *   SALT_MASTER_URL                    Full Salt API address [https://10.10.10.1:8000].
+ *   SALT_MASTER_URL                    Full Salt API address [https://10.10.10.1:6969].
  *   UPDATE_APTLY                       Option to update Aptly
  *   UPDATE_APTLY_MIRRORS               List of mirrors
  *   PUBLISH_APTLY                      Publish aptly snapshots
+ *   RECREATE_APTLY_MIRRORS             Recreate Aptly mirrors
  *   RECREATE_APTLY_PUBLISHES           Option to recreate Aptly publishes separated by comma
  *   FORCE_OVERWRITE_APTLY_PUBLISHES    Option to force overwrite existing packages while publishing
  *   CLEANUP_APTLY                      Option to cleanup old Aptly snapshots
+ *   UPDATE_DEBMIRRORS                  Option to update Debmirrors
  *   UPDATE_DOCKER_REGISTRY             Option to update Docker Registry
  *   CLEANUP_DOCKER_CACHE               Option to cleanup locally cached Docker images
- *   UPDATE_PYPI                        Option to update Python Packages
  *   UPDATE_GIT                         Option to update Git repositories
- *   UPDATE_IMAGES                      Option to update VM images
+ *   UPDATE_FILES                       Option to update static files
  *
 **/
 
-def common = new com.mirantis.mk.Common()
-def salt = new com.mirantis.mk.Salt()
-def python = new com.mirantis.mk.Python()
-def venvPepper = "venvPepper"
+common = new com.mirantis.mk.Common()
+salt = new com.mirantis.mk.Salt()
+python = new com.mirantis.mk.Python()
+venvPepper = "venvPepper"
+
+@NonCPS
+def Boolean dockerExists() {
+  def engine = salt.getPillar(venvPepper, 'I@aptly:server', "aptly:server:source:engine")
+  def matches = (engine =~ /:docker/)
+  try{
+      def test = matches[position]
+      return false
+  }catch(Exception ex){
+      return true
+  }
+}
+
 timeout(time: 12, unit: 'HOURS') {
     node() {
         try {
             python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
+            def dockerExists = dockerExists()
 
             if(UPDATE_APTLY.toBoolean()){
                 stage('Update Aptly mirrors'){
                     def aptlyMirrorArgs = "-s -v"
 
-                    salt.enforceState(venvPepper, '*apt*', ['aptly.server'], true)
+                    if(RECREATE_APTLY_MIRRORS.toBoolean())
+                    {
+                        if(dockerExists){
+                            salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true)
+                        }
+                        else{
+                            salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true, ['runas=aptly'])
+                        }
+                    }
+
+                    salt.enforceState(venvPepper, 'I@aptly:server', ['aptly.server'], true)
                     sleep(10)
 
                     if(UPDATE_APTLY_MIRRORS != ""){
@@ -39,21 +64,31 @@
                         UPDATE_APTLY_MIRRORS = UPDATE_APTLY_MIRRORS.replaceAll("\\s","")
                         def mirrors = UPDATE_APTLY_MIRRORS.tokenize(",")
                         for(mirror in mirrors){
-                            salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs} -m ${mirror}\"", 'runas=aptly'], null, true)
+                            if(dockerExists){
+                                salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs} -m ${mirror}\""], null, true)
+                            }else{
+                                salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs} -m ${mirror}\"", 'runas=aptly'], null, true)
+                            }
                         }
                     }
                     else{
                         common.infoMsg("Updating all Aptly mirrors.")
-                        salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs}\"", 'runas=aptly'], null, true)
+
+                        if(dockerExists){
+                            salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs}\""], null, true)
+                        }
+                        else{
+                            salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=\"${aptlyMirrorArgs}\"", 'runas=aptly'], null, true)
+                        }
                     }
                 }
             }
             if(PUBLISH_APTLY.toBoolean()){
-                def aptlyPublishArgs = "-av"
+                def aptlyPublishArgs = "-v"
 
                 common.infoMsg("Publishing all Aptly snapshots.")
 
-                salt.enforceState(venvPepper, '*apt*', ['aptly.publisher'], true)
+                salt.enforceState(venvPepper, 'I@aptly:server', ['aptly.publisher'], true)
                 sleep(10)
 
                 if(CLEANUP_APTLY.toBoolean()){
@@ -65,33 +100,40 @@
                 if(FORCE_OVERWRITE_APTLY_PUBLISHES.toBoolean()){
                     aptlyPublishArgs += "f"
                 }
-                salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=\"${aptlyPublishArgs}\"", 'runas=aptly'], null, true)
+                if(dockerExists){
+                    aptlyPublishArgs += " -u http://10.99.0.1:8080"
+                    salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=\"${aptlyPublishArgs}\""], null, true)
+                }
+                else{
+                    aptlyPublishArgs += "a"
+                    salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=\"${aptlyPublishArgs}\"", 'runas=aptly'], null, true)
+                }
+            }
+            if(UPDATE_DEBMIRRORS.toBoolean()){
+                stage('Update Debmirrors'){
+                    common.infoMsg("Updating Debmirrors")
+                    salt.enforceState(venvPepper, 'I@debmirror:client', 'debmirror')
+                }
             }
             if(UPDATE_DOCKER_REGISTRY.toBoolean()){
                 stage('Update Docker images'){
                     common.infoMsg("Updating Docker images.")
-                    salt.enforceState(venvPepper, '*apt*', 'docker.client.registry')
+                    salt.enforceState(venvPepper, 'I@aptly:server', 'docker.client.registry')
                     if(CLEANUP_DOCKER_CACHE.toBoolean()){
-                        salt.cmdRun(venvPepper, '*apt*', 'docker system prune --all --force')
+                        salt.cmdRun(venvPepper, 'I@aptly:server', 'docker system prune --all --force')
                     }
                 }
             }
-            if(UPDATE_PYPI.toBoolean()){
-                stage('Update PyPi packages'){
-                    common.infoMsg("Updating PyPi packages.")
-                    salt.cmdRun(venvPepper, '*apt*', 'pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt')
-                }
-            }
             if(UPDATE_GIT.toBoolean()){
                 stage('Update Git repositories'){
                     common.infoMsg("Updating Git repositories.")
-                    salt.enforceState(venvPepper, '*apt*', ['git.server'], true)
+                    salt.enforceState(venvPepper, 'I@aptly:server', ['git.server'], true)
                 }
             }
-            if(UPDATE_IMAGES.toBoolean()){
-                stage('Update VM images'){
-                    common.infoMsg("Updating VM images.")
-                    salt.runSaltProcessStep(venvPepper, '*apt*', '/srv/scripts/update-images.sh')
+            if(UPDATE_FILES.toBoolean()){
+                stage('Update static files'){
+                    common.infoMsg("Updating static files.")
+                    salt.enforceState(venvPepper, 'I@aptly:server', ['linux.system.file'], true)
                 }
             }
         } catch (Throwable e) {
diff --git a/upgrade-mcp-release.groovy b/upgrade-mcp-release.groovy
index d423347..70256b7 100644
--- a/upgrade-mcp-release.groovy
+++ b/upgrade-mcp-release.groovy
@@ -10,14 +10,28 @@
  */
 
 // Load shared libs
-def salt = new com.mirantis.mk.Salt()
-def common = new com.mirantis.mk.Common()
-def python = new com.mirantis.mk.Python()
-def venvPepper = "venvPepper"
+salt = new com.mirantis.mk.Salt()
+common = new com.mirantis.mk.Common()
+python = new com.mirantis.mk.Python()
+venvPepper = "venvPepper"
+
+@NonCPS
+def Boolean dockerExists() {
+  def engine = salt.getPillar(venvPepper, 'I@aptly:server', "aptly:server:source:engine")
+  def matches = (engine =~ /:docker/)
+  try{
+      def test = matches[position]
+      return false
+  }catch(Exception ex){
+      return true
+  }
+}
+
 timeout(time: 12, unit: 'HOURS') {
     node("python") {
         try {
             python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
+            def dockerExists = dockerExists()
 
             stage("Update Reclass"){
                 common.infoMsg("Updating reclass model")
@@ -29,20 +43,31 @@
             if(UPDATE_LOCAL_REPOS.toBoolean()){
                 stage("Update local repos"){
                     common.infoMsg("Updating local repositories")
-                    salt.cmdRun(venvPepper, '*apt*', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true, ['runas=aptly'])
-                    salt.enforceState(venvPepper, '*apt*', 'aptly', true)
-                    salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", 'runas=aptly'], null, true)
-                    salt.runSaltProcessStep(venvPepper, '*apt*', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-afrv", 'runas=aptly'], null, true)
+                    if(dockerExists){
+                        salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true)
+                    }
+                    else{
+                       salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true, ['runas=aptly'])
+                    }
 
-                    salt.enforceState(venvPepper, '*apt*', 'docker.client.registry', true)
+                    salt.enforceState(venvPepper, 'I@aptly:server', 'aptly', true)
 
-                    salt.cmdRun(venvPepper, '*apt*', "export HOME='/root';export MCP_VERSION='${MCP_VERSION}';/srv/scripts/debmirror.sh")
+                    if(dockerExists){
+                        salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv"], null, true)
+                        salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-frv -u http://10.99.0.1:8080"], null, true)
+                    }
+                    else{
+                        salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", 'runas=aptly'], null, true)
+                        salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-afrv", 'runas=aptly'], null, true)
+                    }
 
-                    salt.enforceState(venvPepper, '*apt*', 'git server', true)
+                    salt.enforceState(venvPepper, 'I@aptly:server', 'docker.client.registry', true)
 
-                    salt.cmdRun(venvPepper, '*apt*', 'pip2pi /srv/pypi_mirror/packages/ -r /srv/pypi_mirror/requirements.txt')
+                    salt.enforceState(venvPepper, 'I@aptly:server', 'debmirror', true)
 
-                    salt.cmdRun(venvPepper, '*apt*', '/srv/scripts/update-images.sh')
+                    salt.enforceState(venvPepper, 'I@aptly:server', 'git.server', true)
+
+                    salt.enforceState(venvPepper, 'I@aptly:server', 'linux.system.file', true)
                 }
             }