[CORD-2336]
Retry download of switch images, cleanup + best practices

Change-Id: I87b296d449eb7eeec3dc66ff7c9e10602b9faebe
diff --git a/roles/maas/defaults/main.yml b/roles/maas/defaults/main.yml
index 53de249..8eb81ba 100644
--- a/roles/maas/defaults/main.yml
+++ b/roles/maas/defaults/main.yml
@@ -13,9 +13,23 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-accton_as5712_54x: 'https://github.com/opencord/OpenNetworkLinux/releases/download/2017-01-04.0024-8d23df5/ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER'
-accton_as5812_54x: 'https://github.com/opencord/OpenNetworkLinux/releases/download/2017-01-04.0024-8d23df5/ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER'
-accton_as6712_32x: 'https://github.com/opencord/OpenNetworkLinux/releases/download/2017-01-04.0024-8d23df5/ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER'
+switch_image_dir: "/var/www/html"
+
+# Add switch boot images to be downloaded to this list
+switch_boot_images:
+  - name: "ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER"
+    url: 'https://github.com/opencord/OpenNetworkLinux/releases/download/2017-01-04.0024-8d23df5/ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER'
+    checksum: "sha256:4ed90086cafc3899ddb10ab7bac8677d8ece27de3ed28ed385c12aba8d53c0a9"
+
+# Add specific models of switches here - the value of boot_image should be the
+# name of an image in the switch_boot_images list given above
+switch_models:
+  - name: "onie-installer-x86_64-accton_as5712_54x-r0"
+    boot_image: "ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER"
+  - name: "onie-installer-x86_64-accton_as5812_54x-r0"
+    boot_image: "ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER"
+  - name: "onie-installer-x86_64-accton_as6712_32x-r0"
+    boot_image: "ONL-2.0.0_ONL-OS_2017-01-04.0024-8d23df5_AMD64_INSTALLED_INSTALLER"
 
 pub_ssh_key_file_location: "{{ pub_ssh_key_location | default ('/etc/maas/.ssh') }}"
 
diff --git a/roles/maas/tasks/download.yml b/roles/maas/tasks/download.yml
deleted file mode 100644
index a2c814e..0000000
--- a/roles/maas/tasks/download.yml
+++ /dev/null
@@ -1,54 +0,0 @@
-
-# Copyright 2017-present Open Networking Foundation
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-
----
-# For some reason the ansible get_url retrieves HTML when we attempt to download the switch
-# boot resources from drop box, but using curl things work. So this include is all about
-# downloading the boot resource assuming it is not already on the local files (as detetermined
-# by the file size in bytes)
-
-- name: Get Remote Size
-  shell: curl -sSL -I "{{ item.url }}" | grep Content-Length | tail -1 | cut "-d " -f2
-  register: remote_size
-  changed_when: false
-  tags:
-    - switch_support
-    - skip_ansible_lint
-
-- name: Check Local File
-  stat:
-    path: "/var/www/html/{{ item.dest }}"
-  register: local_size
-  tags:
-    - switch_support
-
-- name: Download Switch Boot Resource
-  become: yes
-  command: curl -sSL "{{ item.url }}" -o "/var/www/html/{{ item.dest }}"
-  when: local_size.stat.exists == false or remote_size.stdout|int != local_size.stat.size
-  tags:
-    - switch_support
-    - skip_ansible_lint
-
-- name: Switch Boot Resources Permissions
-  become: yes
-  file:
-    path: "/var/www/html/{{ item.dest }}"
-    owner: root
-    group: root
-    mode: "0644"
-  tags:
-    - switch_support
diff --git a/roles/maas/tasks/main.yml b/roles/maas/tasks/main.yml
index 9a85b0e..4510464 100644
--- a/roles/maas/tasks/main.yml
+++ b/roles/maas/tasks/main.yml
@@ -1,4 +1,4 @@
-
+---
 # Copyright 2017-present Open Networking Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,8 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
----
 - name: Verify Network Bits on Network Specifications
   fail: msg="Network specification '{{ item }}' must include network bits"
   when: "item | match('^([0-9]{1,3}.){3}[0-9]{1,3}$')"
@@ -207,12 +205,33 @@
   changed_when: false
   failed_when: mgmt_ip_address.rc != 0 or mgmt_ip_address.stdout == ""
 
-- name: Switch Resource
-  include: download.yml
-  with_items:
-    - { url : "{{ accton_as6712_32x }}", dest : "onie-installer-x86_64-accton_as6712_32x-r0" }
-    - { url : "{{ accton_as5712_54x }}", dest : "onie-installer-x86_64-accton_as5712_54x-r0" }
-    - { url : "{{ accton_as5812_54x }}", dest : "onie-installer-x86_64-accton_as5812_54x-r0" }
+- name: Download Switch Boot Images
+  become: yes
+  get_url:
+    url: "{{ item.url }}"
+    checksum: "{{ item.checksum }}"
+    dest: "{{ switch_image_dir }}/{{ item.name }}"
+    owner: "root"
+    group: "root"
+    mode: "0644"
+  with_items: "{{ switch_boot_images }}"
+  register: boot_image_download_result
+  until: boot_image_download_result|success
+  retries: 5
+  delay: 10
+  tags:
+    - switch_support
+
+- name: Link boot images to model files
+  become: yes
+  file:
+    state: link
+    src: "{{ switch_image_dir }}/{{ item.boot_image }}"
+    dest: "{{ switch_image_dir }}/{{ item.name }}"
+    owner: "root"
+    group: "root"
+    mode: "0644"
+  with_items: "{{ switch_models }}"
   tags:
     - switch_support