LAVA: Add data server role

Use a local Apache to serve large and/or confidential data files
required by Fairphone's internal test runs. Also create a defined folder
structure on the host to store device-type-specific files.

This change does not introduce any device types. To actually create
device-type-specific folders, populate the device_types list.

Issue: INFRA-175
Issue: INFRA-173
Change-Id: Icfa7a00d1c3619e069dd5451c510662d103ddf7c
Test: ansible-lint lava-data.yaml
diff --git a/group_vars/lava-data.yaml b/group_vars/lava-data.yaml
new file mode 100644
index 0000000..3df3df9
--- /dev/null
+++ b/group_vars/lava-data.yaml
@@ -0,0 +1,22 @@
+apache_conf_dir: "/etc/apache2"
+apache_lava_site_config_name: "lava-data.conf"
+
+lava_data_http_port: 8080
+lava_www_srv_ports:
+  - "{{ lava_srv_http_port }}"
+  - "{{ lava_data_http_port }}"
+
+# Document root for the Apache site. This will symlink to lava_srv_data_root.
+apache_lava_document_root: "/var/www/lava-data"
+# Root path for LAVA data server.
+lava_srv_data_root: "/srv/lava-data"
+# Android-specific third party test packages.
+lava_data_android_dir: "{{ lava_srv_data_root }}/android"
+# Device-type-specific data.
+lava_data_device_type_dir: "{{ lava_srv_data_root }}/{{ device_type }}"
+# Images for device deployment.
+lava_data_images_device_type_dir: "{{ lava_data_device_type_dir }}/images/rel"
+# OTA images.
+lava_data_otas_device_type_dir: "{{ lava_data_device_type_dir }}/otas/rel"
+# Images required for test automation (prepared userdata images).
+lava_data_automation_device_type_dir: "{{ lava_data_device_type_dir }}/automation"
diff --git a/group_vars/lava-device-types.yaml b/group_vars/lava-device-types.yaml
new file mode 100644
index 0000000..80618b7
--- /dev/null
+++ b/group_vars/lava-device-types.yaml
@@ -0,0 +1 @@
+device_types: []
diff --git a/inventories/lava-servers.ini b/inventories/lava-servers.ini
index b263c10..7992ae6 100644
--- a/inventories/lava-servers.ini
+++ b/inventories/lava-servers.ini
@@ -1,4 +1,5 @@
 ; lava-master ansible_ssh_user=root ansible_ssh_host=TODO
+; lava-data ansible_ssh_user=root ansible_ssh_host=TODO
 
 [lava-workers]
 ; lava-worker1 ansible_ssh_user=root ansible_ssh_host=TODO
diff --git a/lava-data.yaml b/lava-data.yaml
new file mode 100644
index 0000000..36eeabe
--- /dev/null
+++ b/lava-data.yaml
@@ -0,0 +1,9 @@
+---
+- hosts: lava-data
+  become: yes
+  roles:
+    - lava-data
+  vars_files:
+    - group_vars/lava-common.yaml
+    - group_vars/lava-device-types.yaml
+    - group_vars/lava-data.yaml
diff --git a/roles/lava-data/handlers/main.yaml b/roles/lava-data/handlers/main.yaml
new file mode 100644
index 0000000..684b336
--- /dev/null
+++ b/roles/lava-data/handlers/main.yaml
@@ -0,0 +1,6 @@
+---
+
+- name: restart apache
+  systemd:
+    name: apache2
+    state: restarted
diff --git a/roles/lava-data/tasks/lava-data-apache.yaml b/roles/lava-data/tasks/lava-data-apache.yaml
new file mode 100644
index 0000000..3794f37
--- /dev/null
+++ b/roles/lava-data/tasks/lava-data-apache.yaml
@@ -0,0 +1,34 @@
+- name: Deploy LAVA data host Apache config.
+  template:
+    src: ./templates/apache-site-lava-data.conf.j2
+    dest: "{{ apache_conf_dir }}/sites-available/{{ apache_lava_site_config_name }}"
+    mode: u=rw,g=r,o=r
+  notify: restart apache
+
+- name: Deploy Apache ports config.
+  template:
+    src: ./templates/apache-ports.conf.j2
+    dest: "{{ apache_conf_dir }}/ports.conf"
+    mode: u=rw,g=r,o=r
+  notify: restart apache
+
+- name: Create Apache document root symlink.
+  file:
+    src: "{{ lava_srv_data_root }}"
+    dest: "{{ apache_lava_document_root }}"
+    state: link
+    owner: root
+    group: root
+  notify: restart apache
+
+- name: Ensure Apache LAVA data site is enabled.
+  file:
+    src: "{{ apache_conf_dir }}/sites-available/{{ apache_lava_site_config_name }}"
+    dest: "{{ apache_conf_dir }}/sites-enabled/{{ apache_lava_site_config_name }}"
+    state: link
+  notify: restart apache
+
+- name: Ensure Apache is running.
+  systemd:
+    state: started
+    name: apache2
diff --git a/roles/lava-data/tasks/lava-data-paths.yaml b/roles/lava-data/tasks/lava-data-paths.yaml
new file mode 100644
index 0000000..2f42a8d
--- /dev/null
+++ b/roles/lava-data/tasks/lava-data-paths.yaml
@@ -0,0 +1,45 @@
+# Create folder structure that stores files for test deployment and execution.
+# Both, LAVA device deployment and test actions will access these files via a
+# local web server.
+
+- name: Create Android data dir.
+  file:
+    path: "{{ lava_data_android_dir }}"
+    state: directory
+    owner: root
+    group: root
+    mode: u=rwX,g=rX,o=rX
+    recurse: yes
+
+- name: Create images data dir per device type.
+  vars:
+    device_type: "{{ item }}"
+  file:
+    path: "{{ lava_data_images_device_type_dir }}"
+    state: directory
+    owner: root
+    group: root
+    mode: u=rwX,g=rX,o=rX
+  loop: "{{ device_types }}"
+
+- name: Create OTAs data dir per device type type.
+  vars:
+    device_type: "{{ item }}"
+  file:
+    path: "{{ lava_data_otas_device_type_dir }}"
+    state: directory
+    owner: root
+    group: root
+    mode: u=rwX,g=rX,o=rX
+  loop: "{{ device_types }}"
+
+- name: Create automation data dir per device type.
+  vars:
+    device_type: "{{ item }}"
+  file:
+    path: "{{ lava_data_automation_device_type_dir }}"
+    state: directory
+    owner: root
+    group: root
+    mode: u=rwX,g=rX,o=rX
+  loop: "{{ device_types }}"
diff --git a/roles/lava-data/tasks/main.yaml b/roles/lava-data/tasks/main.yaml
new file mode 100644
index 0000000..e010152
--- /dev/null
+++ b/roles/lava-data/tasks/main.yaml
@@ -0,0 +1,2 @@
+- import_tasks: lava-data-paths.yaml
+- import_tasks: lava-data-apache.yaml
diff --git a/roles/lava-data/templates/apache-ports.conf.j2 b/roles/lava-data/templates/apache-ports.conf.j2
new file mode 100644
index 0000000..84c300b
--- /dev/null
+++ b/roles/lava-data/templates/apache-ports.conf.j2
@@ -0,0 +1,11 @@
+{% for port in lava_www_srv_ports %}
+Listen {{ port }}
+{% endfor %}
+
+<IfModule ssl_module>
+	Listen 443
+</IfModule>
+
+<IfModule mod_gnutls.c>
+	Listen 443
+</IfModule>
diff --git a/roles/lava-data/templates/apache-site-lava-data.conf.j2 b/roles/lava-data/templates/apache-site-lava-data.conf.j2
new file mode 100644
index 0000000..2b415fd
--- /dev/null
+++ b/roles/lava-data/templates/apache-site-lava-data.conf.j2
@@ -0,0 +1,6 @@
+<VirtualHost *:{{ lava_data_http_port }}>
+    DocumentRoot {{ apache_lava_document_root }}
+
+    ErrorLog ${APACHE_LOG_DIR}/lava-data-error.log
+    CustomLog ${APACHE_LOG_DIR}/lava-data-access.log combined
+</VirtualHost>