# prebuilt nexus blob in installation time.
 # Component name must match with tar filename!
 # e.g.
-# aaa-component-0.0.1.tar is expected in aux_data_path for aaa-component image
+# aaa/bbb-component-0.0.1.tar are expected in aux_data_path for component images.
 #runtime_images:
-#    aaa-component-0.0.1:
+#  aaa-component-0.0.1:
 #    registry: "nexus3.onap.org:10001"
 #    path:     "/onap/components/aaa-component"
 #    tag:      "latest"
-runtime_images:
+#  bbb-component-0.0.1:
+#    registry: "nexus3.onap.org:10001"
+#    path:     "/onap/components/bbb-component"
+#    tag:      "latest"
+runtime_images: {}
 
 ###############################
 # Application specific params #
 
     - docker
     - dns
     - vncserver
-    - role: nexus
-      vars:
-        phase: install
     - nginx
-  tasks:
-    - name: "wait for nexus to come up"
-      uri:
-        url: "{{ nexus_url }}/service/metrics/healthcheck"
-        user: admin
-        password: admin123
-        force_basic_auth: yes
-        method: GET
-      register: nexus_wait
-      until: not nexus_wait.failed
-      retries: 30
-      delay: 10
-
-- name: Nexus changes in runtime
-  hosts: infrastructure
-  roles:
-    - role: nexus
-      vars:
-        phase: configure
-      when: populate_nexus | bool
-    - role: nexus
-      vars:
-        phase: runtime-populate
-      when: runtime_images is defined and runtime_images is not none
+    - nexus
 
 - name: Setup base for Kubernetes nodes
   hosts: kubernetes
 
-#Defaults to install, can be set to configure.
-phase: install
+---
+# By default prepopulated nexus binary blob used.
+populate_nexus: false
+# By dafault no additional docker images pushed to nexus at runtime
+# but all images are pre-populated either at buildtime or at install time (populate_nexus).
+runtime_images: {}
 
   uri:
     url: "{{ nexus_url }}/service/rest/v1/script/configure"
     method: GET
-    force_basic_auth: yes
+    force_basic_auth: true
     user: admin
     password: admin123
     status_code: [200, 404]
       uri:
         url: "{{ nexus_url }}/service/rest/v1/script"
         method: POST
-        force_basic_auth: yes
+        force_basic_auth: true
         user: admin
         password: admin123
         body_format: json
       uri:
         url: "{{ nexus_url }}/service/rest/v1/script/configure/run"
         method: POST
-        force_basic_auth: yes
+        force_basic_auth: true
         user: admin
         password: admin123
         body_format: raw
 
       docker_image:
         name: "{{ runtime_images[component].registry }}{{ runtime_images[component].path }}"
         tag: "{{ runtime_images[component].tag }}"
-        push: yes
+        push: true
         load_path: "{{ item.path }}"
         timeout: 120
+      changed_when: false  # for idenpotence
 
     path: "{{ app_data_path }}/nexus_data"
     owner: 200
     group: 200
-    recurse: yes
+    recurse: true
 
 - name: Load nexus image
   docker_image:
       - "{{ app_data_path }}/nexus_data:/nexus-data:rw"
     state: started
     restart_policy: unless-stopped
+
+- name: Wait for nexus to come up
+  uri:
+    url: "{{ nexus_url }}/service/metrics/healthcheck"
+    user: admin
+    password: admin123
+    force_basic_auth: true
+    method: GET
+  retries: 30
+  delay: 10
+  register: nexus_wait
+  until: not nexus_wait.failed
 
 ---
-- include_tasks: "{{ phase }}.yml"
+- include_tasks: install.yml
+- include_tasks: configure.yml
+  when: populate_nexus | bool
+- include_tasks: runtime-populate.yml
+  when:
+    - runtime_images is defined
+    - runtime_images is not none
+    - runtime_images.keys() | length > 0
 
 # need to iterate over those tasks in include
 - include: "insert-images.yml"
   with_items: "{{ tar_images.files }}"
-
 
+---
 nexus_url: "https://nexus.{{ hostvars[groups.infrastructure[0]].ansible_nodename }}"
 
     proxy_send_timeout 120;
     proxy_read_timeout 300;
 
-    upstream nexus {
-        server nexus:8081;
-    }
-
-    upstream registry {
-        server nexus:8082;
-    }
-
 # http simulations
     server {
         listen 80;
 
 # nexus simulations
     server {
+        resolver 127.0.0.11 valid=30s;
         listen 80;
         listen 443 ssl;
         server_name {% for host in simulated_hosts.nexus -%}
         client_max_body_size 3G;
 
         location / {
+            set $upstream_nexus nexus:8081;
+            set $upstream_registry nexus:8082;
             # redirect to docker registry
             if ($http_user_agent ~ docker ) {
-                proxy_pass http://registry;
+                proxy_pass http://$upstream_registry;
             }
-            proxy_pass http://nexus;
+            proxy_pass http://$upstream_nexus;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;