Add role for packaging kolla stack

Change-Id: I873aee77d25a696fc8b677c21be750d39acce500
diff --git a/playbooks/roles/package/tasks/binaries.yaml b/playbooks/roles/package/tasks/binaries.yaml
new file mode 100644
index 0000000..15fbf9e
--- /dev/null
+++ b/playbooks/roles/package/tasks/binaries.yaml
@@ -0,0 +1,28 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Create directory to store openstack binaries
+  file:
+    path: "{{ binaries_folder }}"
+    state: "{{ item }}"
+  with_items:
+    - absent
+    - directory
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/containers.yaml b/playbooks/roles/package/tasks/containers.yaml
new file mode 100644
index 0000000..f623bcc
--- /dev/null
+++ b/playbooks/roles/package/tasks/containers.yaml
@@ -0,0 +1,73 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Create directory to store container images
+  file:
+    path: "{{ containers_folder }}"
+    state: "{{ item }}"
+  with_items:
+    - absent
+    - directory
+
+- name: Pull kolla container images
+  command: "docker pull {{ kolla_docker_repo }}/{{ os_distro }}-{{ os_kolla_install_type }}-{{ image }}:{{ openstack_version }}"
+  loop: "{{ kolla_images }}"
+  loop_control:
+    loop_var: image
+  become: true
+  changed_when: false
+
+- name: Pull other container images
+  command: "docker pull {{ other_images[item.key].repo }}:{{ other_images[item.key].tag }}"
+  with_dict: "{{ other_images }}"
+  become: true
+  changed_when: false
+
+- name: Save kolla container images
+  command: |-
+    docker save {{ kolla_docker_repo }}/{{ os_distro }}-{{ os_kolla_install_type }}-{{ image }}:{{ openstack_version }}
+    -o {{ kolla_docker_repo }}_{{ os_distro }}-{{ os_kolla_install_type }}-{{ image }}_{{ openstack_version }}.tar
+  loop: "{{ kolla_images }}"
+  loop_control:
+    loop_var: image
+  args:
+    chdir: "{{ containers_folder }}"
+  become: true
+  changed_when: false
+
+- name: Save other container images
+  command: |-
+    docker save {{ other_images[item.key].repo }}:{{ other_images[item.key].tag }}
+    -o {{ other_images[item.key].repo | replace('/', '_') }}_{{ other_images[item.key].tag }}.tar
+  with_dict: "{{ other_images }}"
+  args:
+    chdir: "{{ containers_folder }}"
+  become: true
+  changed_when: false
+
+# NOTE (fdegir): archive fails due to wrong permissions so we fix them
+- name: Fix container image permissions
+  file:
+    path: "{{ containers_folder }}"
+    state: directory
+    recurse: true
+    mode: 0755
+  become: true
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/dib.yaml b/playbooks/roles/package/tasks/dib.yaml
new file mode 100644
index 0000000..887977b
--- /dev/null
+++ b/playbooks/roles/package/tasks/dib.yaml
@@ -0,0 +1,35 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Create directory to store images used for provisioning
+  file:
+    path: "{{ dib_folder }}"
+    state: "{{ item }}"
+  with_items:
+    - absent
+    - directory
+
+- name: Download distro images used for provisioning nodes
+  get_url:
+    url: "{{ item }}"
+    dest: "{{ dib_folder }}"
+    force: true
+  loop: "{{ dib_images }}"
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/fetch-component-vars.yaml b/playbooks/roles/package/tasks/fetch-component-vars.yaml
new file mode 100644
index 0000000..b1a4fc7
--- /dev/null
+++ b/playbooks/roles/package/tasks/fetch-component-vars.yaml
@@ -0,0 +1,46 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Clone '{{ component.key }}' repository and checkout version '{{ component.value.version }}'
+  git:
+    repo: "{{ component.value.src }}"
+    dest: "{{ tempdir.path }}/{{ component.key }}"
+    version: "{{ component.value.version }}"
+    refspec: "{{ component.value.refspec | default(omit) }}"
+    force: true
+  environment:
+    http_proxy: "{{ lookup('env','http_proxy') }}"
+    https_proxy: "{{ lookup('env','https_proxy') }}"
+    no_proxy: "{{ lookup('env','no_proxy') }}"
+
+- name: Ensure outdated '{{ component.key }}' vars file is removed
+  file:
+    path: "{{ inventory_path }}/group_vars/all/{{ component.key }}.yaml"
+  ignore_errors: true
+
+- name: Copy '{{ component.key }}' vars file to engine group_vars
+  copy:
+    src: "{{ tempdir.path }}/{{ component.key }}/vars/{{ component.key }}.yaml"
+    dest: "{{ inventory_path }}/group_vars/all/{{ component.key }}.yaml"
+    force: true
+
+- name: Include '{{ component.key }}' vars
+  include_vars: "{{ inventory_path }}/group_vars/all/{{ component.key }}.yaml"
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/git.yaml b/playbooks/roles/package/tasks/git.yaml
new file mode 100644
index 0000000..60ddff8
--- /dev/null
+++ b/playbooks/roles/package/tasks/git.yaml
@@ -0,0 +1,41 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Create directory to store git repositories
+  file:
+    path: "{{ git_folder }}"
+    state: "{{ item }}"
+  with_items:
+    - absent
+    - directory
+
+- name: Clone repositories
+  git:
+    repo: "{{ repositories[item.key].repo }}"
+    dest: "{{ git_folder }}/{{ repositories[item.key].dest }}"
+    version: "{{ repositories[item.key].version }}"
+    refspec: "{{ repositories[item.key].refspec | default(omit) }}"
+    force: true
+  with_dict: "{{ repositories }}"
+  environment:
+    http_proxy: "{{ lookup('env','http_proxy') }}"
+    https_proxy: "{{ lookup('env','https_proxy') }}"
+    no_proxy: "{{ lookup('env','no_proxy') }}"
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/install-packages-Debian.yml b/playbooks/roles/package/tasks/install-packages-Debian.yml
new file mode 100644
index 0000000..b69f4bd
--- /dev/null
+++ b/playbooks/roles/package/tasks/install-packages-Debian.yml
@@ -0,0 +1,61 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Load distribution variables
+  include_vars: '{{ ansible_os_family }}.yaml'
+
+- name: Add docker apt key
+  apt_key:
+    url: https://download.docker.com/linux/ubuntu/gpg
+    state: present
+  become: true
+
+# NOTE(fdegir): ansible apt_repository gives segmentation fault so failling back to command
+- name: Add docker apt repository
+  command: |-
+    add-apt-repository \
+    "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
+  changed_when: false
+  become: true
+
+- name: Run apt update
+  apt:
+    update_cache: true
+  become: true
+
+- name: Install packages
+  apt:
+    name: "{{ packages }}"
+    state: "{{ item }}"
+    force: true
+    install_recommends: true
+    autoremove: true
+    update_cache: true
+  with_items:
+    - absent
+    - present
+  become: true
+
+- name: Restart docker service
+  service:
+    name: "{{ docker_service_name }}"
+    state: restarted
+  become: true
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/main.yaml b/playbooks/roles/package/tasks/main.yaml
new file mode 100644
index 0000000..cfbe36d
--- /dev/null
+++ b/playbooks/roles/package/tasks/main.yaml
@@ -0,0 +1,102 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Create temporary folder to clone provisioner and installer repositories
+  tempfile:
+    state: directory
+  register: tempdir
+
+- name: Fetch vars files of provisioners and installers
+  include_tasks: fetch-component-vars.yaml
+  with_dict:
+    - "{{ provisioner }}"
+    - "{{ installer }}"
+  loop_control:
+    loop_var: component
+
+- name: Delete temporary folder
+  file:
+    path: "{{ tempdir.path }}"
+    state: absent
+
+- name: Create folder to store dependencies for offline deployment
+  file:
+    path: "{{ offline_pkg_folder }}"
+    state: "{{ item }}"
+  with_items:
+    - absent
+    - directory
+
+- name: Install packages on {{ ansible_os_family }}
+  include_tasks: "install-packages-{{ ansible_os_family }}.yml"
+
+# TODO (fdegir): we need to switch to build with dib here
+- name: Fetch operating system images for provisioning
+  include_tasks: dib.yaml
+
+# collect apt packages
+- name: Fetch operating system packages
+  include_tasks: "pkg-{{ ansible_os_family }}.yaml"
+
+# clone git repositories
+- name: Fetch git repositories
+  include_tasks: git.yaml
+
+# download binaries
+- name: Fetch binaries
+  include_tasks: binaries.yaml
+
+# download pip packages
+- name: Fetch pip python packages
+  include_tasks: pip.yaml
+
+# fetch kolla container images
+- name: Fetch container images
+  include_tasks: containers.yaml
+
+# ensure we don't have leftovers
+- name: Delete outdated files
+  file:
+    path: "{{ item }}"
+    state: absent
+  with_items:
+    - "{{ offline_pkg_file }}"
+    - "{{ offline_pkg_folder }}/install.sh"
+    - "{{ offline_installer_file }}"
+    - "/tmp/decompress.sh"
+
+- name: Copy decompress and install scripts
+  copy:
+    src: "{{ item.src }}"
+    dest: "{{ item.dest }}"
+    mode: 0755
+  with_items:
+    - {src: "install.sh", dest: "{{ offline_pkg_folder }}/install.sh"}
+    - {src: "decompress.sh", dest: "/tmp/decompress.sh"}
+
+# create tarball
+- name: Create engine installer file
+  script: build.sh
+  register: build_script
+
+- name: Log build script output to console
+  debug:
+    msg: "{{ build_script.stdout_lines }}"
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/pip.yaml b/playbooks/roles/package/tasks/pip.yaml
new file mode 100644
index 0000000..a4a4642
--- /dev/null
+++ b/playbooks/roles/package/tasks/pip.yaml
@@ -0,0 +1,40 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Create directory to store pip packages
+  file:
+    path: "{{ pip_folder }}"
+    state: "{{ item }}"
+  with_items:
+    - absent
+    - directory
+
+- name: Download pip packages using requirements.txt file
+  command: "pip download -r {{ engine_path }}/requirements.txt --no-cache"
+  changed_when: false
+  args:
+    chdir: "{{ pip_folder }}"
+
+- name: Copy pip.conf
+  template:
+    src: pip.conf.j2
+    dest: "{{ pip_folder }}/pip.conf"
+    force: true
+
+# vim: set ts=2 sw=2 expandtab:
diff --git a/playbooks/roles/package/tasks/pkg-Debian.yaml b/playbooks/roles/package/tasks/pkg-Debian.yaml
new file mode 100644
index 0000000..f844bf9
--- /dev/null
+++ b/playbooks/roles/package/tasks/pkg-Debian.yaml
@@ -0,0 +1,53 @@
+---
+# ============LICENSE_START=======================================================
+#  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
+# ================================================================================
+# 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.
+#
+# SPDX-License-Identifier: Apache-2.0
+# ============LICENSE_END=========================================================
+
+- name: Create directory to store apt packages
+  file:
+    path: "{{ pkg_folder }}/amd64"
+    state: "{{ item }}"
+  with_items:
+    - absent
+    - directory
+
+# NOTE (fdegir): docker version is taken and updated from engine versions file
+- name: Generate ubuntu.list file from template
+  template:
+    src: ubuntu.list.j2
+    dest: /tmp/ubuntu.list
+    force: true
+
+- name: Download apt packages using ubuntu.list file
+  shell: |
+    set -o pipefail
+    apt download $(grep -vE "^\s*#" /tmp/ubuntu.list | tr "\n" " ")
+  changed_when: false
+  args:
+    executable: /bin/bash
+    chdir: "{{ pkg_folder }}/amd64"
+
+- name: Generate Packages.gz file for apt packages
+  shell: |
+    set -o pipefail
+    dpkg-scanpackages amd64 | gzip -9c > amd64/Packages.gz
+  args:
+    executable: /bin/bash
+    creates: "{{ pkg_folder }}/amd64/Packages.gz"
+    chdir: "{{ pkg_folder }}"
+
+# vim: set ts=2 sw=2 expandtab: