Feature 8623

This feature offers a OSM installer to OpenStack by using Ansible

Change-Id: I0d609dc227f8968614b4e9a358cb80961b69fb3e
Signed-off-by: Antonio Marsico <antonio.marsico@bt.com>
diff --git a/installers/openstack/README.md b/installers/openstack/README.md
new file mode 100644
index 0000000..a8e4356
--- /dev/null
+++ b/installers/openstack/README.md
@@ -0,0 +1,38 @@
+<!--
+Copyright 2020 British Telecommunications plc
+
+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.
+Author: Antonio Marsico (antonio.marsico@bt.com)
+-->
+
+
+# User guide
+
+This short guide explains how to use directly the Ansible playbook to install OSM to an OpenStack infrastructure.
+
+## Prerequisites
+The ansible playbook requires `ansible` and `openstacksdk` to be executed. `python-openstackclient` is not mandatory but highly recommended. They are part of Python pip and can be installed as follows:
+
+`$ sudo -H pip install python-openstackclient "openstacksdk<1" "ansible>=2.9,<3"`
+
+## Execute the playbook
+
+In order to execute the playbook, it is required an OpenStack openrc file. It can be downloaded from the OpenStack web interface Horizon.
+
+After that, it can be loaded with the following command:
+
+`$ . openrc`
+
+Then, all the credentials are loaded in the bash environment. Now it is possible to execute the playbook to configure OpenStack and install OSM:
+
+`$ ansible-playbook -e external_network_name=<your openstack external network> site.yml`
\ No newline at end of file
diff --git a/installers/openstack/ansible.cfg b/installers/openstack/ansible.cfg
new file mode 100644
index 0000000..b85fcb9
--- /dev/null
+++ b/installers/openstack/ansible.cfg
@@ -0,0 +1,17 @@
+#   Copyright 2020 British Telecommunications plc
+#
+#   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.
+#   Author: Antonio Marsico (antonio.marsico@bt.com)
+
+[defaults]
+host_key_checking = False
diff --git a/installers/openstack/group_vars/all.yml b/installers/openstack/group_vars/all.yml
new file mode 100644
index 0000000..ce0f9c1
--- /dev/null
+++ b/installers/openstack/group_vars/all.yml
@@ -0,0 +1,47 @@
+---
+
+#   Copyright 2020 British Telecommunications plc
+#
+#   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.
+#   Author: Antonio Marsico (antonio.marsico@bt.com)
+
+osm_installer_path: /usr/share/osm-devops/installers/install_osm.sh
+
+prefix: server
+
+cloud_name:
+
+setup_volume: false
+
+external_network_name:
+
+installer_args:
+
+os_key_name: ansible-key
+
+os_flavor:
+  name: medium-4-cpu-8-gb-40-disk
+  cpu: 4
+  ram: 8192
+  disk: 40
+
+servers:
+  - name: osm
+    user: ubuntu
+    image: ubuntu1804
+    image_url: https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img
+    flavor: "{{os_flavor.name}}"
+    key: "{{os_key_name}}"
+    nics: "net-name={{external_network_name}}"
+    meta:
+      group: appservers
\ No newline at end of file
diff --git a/installers/openstack/roles/create_instances/tasks/main.yml b/installers/openstack/roles/create_instances/tasks/main.yml
new file mode 100644
index 0000000..03c0b4c
--- /dev/null
+++ b/installers/openstack/roles/create_instances/tasks/main.yml
@@ -0,0 +1,61 @@
+---
+#   Copyright 2020 British Telecommunications plc
+#
+#   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.
+#   Author: Antonio Marsico (antonio.marsico@bt.com)
+
+- name: Launch OSM instances
+  os_server:
+    cloud: "{{ cloud_name }}"
+    name: "{{ prefix }}-{{ item.name }}"
+    state: present
+    key_name: "{{ item.key }}"
+    nics: "{{ item.nics }}"
+    image: "{{ item.image }}"
+    flavor: "{{ item.flavor }}"
+    security_groups:
+      - default
+      - "{{external_network_name}}_access"
+  with_items: "{{ servers }}"
+  register: "os_hosts"
+  when: setup_volume|bool == False
+
+- name: Launch OSM instances with a volume
+  os_server:
+    cloud: "{{ cloud_name }}"
+    name: "{{ prefix }}-{{ item.name }}"
+    state: present
+    key_name: "{{ item.key }}"
+    nics: "{{ item.nics }}"
+    image: "{{ item.image }}"
+    flavor: "{{ item.flavor }}"
+    boot_from_volume: yes
+    terminate_volume: yes
+    security_groups:
+      - default
+      - "{{external_network_name}}_access"
+  with_items: "{{ servers }}"
+  register: "os_hosts_with_volume"
+  when: setup_volume|bool == True
+
+- set_fact:
+    os_hosts: "{{ os_hosts_with_volume }}"
+  when: setup_volume|bool == True
+
+- name: Add OSM host to the local Ansible inventory
+  add_host:
+    name: "{{ item.openstack.accessIPv4 }}"
+    groups: "{{ item['item']['meta']['group'] }}"
+    ansible_private_key_file: "~/.ssh/{{ os_key_name }}"
+    ansible_user: "{{ item['item']['user'] }}"
+  with_items: "{{ os_hosts.results }}"
diff --git a/installers/openstack/roles/osm_installation/tasks/main.yml b/installers/openstack/roles/osm_installation/tasks/main.yml
new file mode 100644
index 0000000..c254574
--- /dev/null
+++ b/installers/openstack/roles/osm_installation/tasks/main.yml
@@ -0,0 +1,63 @@
+---
+#   Copyright 2020 British Telecommunications plc
+#
+#   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.
+#   Author: Antonio Marsico (antonio.marsico@bt.com)
+
+- name: Waiting target connection to become reachable/usable
+  wait_for_connection:
+
+- name: Gathering facts
+  setup:
+
+- name: Copy OSM installer
+  copy:
+    src: "{{ osm_installer_path }}"
+    dest: install_osm.sh
+    mode: '744'
+
+- name: Check OSM reachability
+  uri:
+    url: http://localhost
+  ignore_errors: yes
+  register: curl_result
+
+- name: Pre-configure iptables persistent
+  debconf: name=iptables-persistent question={{ item }} vtype=boolean value=true
+  become: yes
+  with_items:
+  - iptables-persistent/autosave_v4
+  - iptables-persistent/autosave_v6
+
+- debug:
+    var: installer_args
+    verbosity: 2
+
+- name: Install and configure OSM (It may require 10 min or more!)
+  shell:
+    cmd: ./install_osm.sh -y {{installer_args}} 2>&1 | tee osm_install_log.txt
+  args:
+    executable: /bin/bash
+  register: osm_installation_results
+  environment:
+    PATH: "/snap/bin:{{ ansible_env.PATH }}"
+  when: curl_result is failed
+
+- debug:
+    var: osm_installation_results
+    verbosity: 2
+
+- name: Check OSM reachability
+  uri:
+    url: http://localhost
+  register: curl_result
diff --git a/installers/openstack/roles/setup_openstack/tasks/main.yml b/installers/openstack/roles/setup_openstack/tasks/main.yml
new file mode 100644
index 0000000..8c729df
--- /dev/null
+++ b/installers/openstack/roles/setup_openstack/tasks/main.yml
@@ -0,0 +1,139 @@
+---
+#   Copyright 2020 British Telecommunications plc
+#
+#   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.
+#   Author: Antonio Marsico (antonio.marsico@bt.com)
+
+- name: Looking for the OpenStack external network
+  os_networks_info:
+    cloud: "{{ cloud_name }}"
+    filters:
+      name: "{{ external_network_name }}"
+  register: openstack_external_networks
+
+- name: Show OpenStack networks
+  debug:
+    msg: "{{ openstack_external_networks.openstack_networks }}"
+    verbosity: 2
+
+- name: Gather information about previously created subnets
+  os_subnets_info:
+    cloud: "{{ cloud_name }}"
+    name: "{{ openstack_external_networks.openstack_networks[0].subnets[0] }}"
+  register: subnet_info
+
+- name: Show openstack subnets
+  debug:
+    msg: "{{ subnet_info.openstack_subnets[0] }}"
+    verbosity: 2
+
+- set_fact:
+    cidr: "{{ subnet_info.openstack_subnets[0].cidr }}"
+
+- name: Creating a new openstack flavor
+  os_nova_flavor:
+    cloud: "{{ cloud_name }}"
+    state: present
+    name: "{{os_flavor.name}}"
+    ram: "{{os_flavor.ram}}"
+    vcpus: "{{os_flavor.cpu}}"
+    disk: "{{os_flavor.disk}}"
+
+- name: Gather information about OpenStack images
+  os_image_info:
+    cloud: "{{ cloud_name }}"
+    image: "{{ item.image }}"
+  with_items: "{{ servers }}"
+  register: image_query
+
+- name: Show OpenStack image information
+  debug:
+    msg: "{{ item.openstack_image }}"
+    verbosity: 2
+  with_items: "{{ image_query.results }}"
+  when: item.openstack_image != none
+
+# Missing SHA256 check if we want to update an image
+
+- name: Downloading images
+  get_url:
+    url: "{{item.0.image_url}}"
+    dest: "/tmp/{{ item.0.image }}.img"
+    mode: '644'
+  loop: "{{ servers |product(image_query.results)|list }}"
+  when: item.1.openstack_image == none and item.0.image == item.1.item.image
+
+- name: Creating images
+  os_image:
+    cloud: "{{ cloud_name }}"
+    name: "{{ item.0.image }}"
+    container_format: bare
+    disk_format: qcow2
+    state: present
+    filename: "/tmp/{{ item.0.image }}.img"
+  loop: "{{ servers |product(image_query.results)|list }}"
+  when: item.1.openstack_image == none and item.0.image == item.1.item.image
+
+- name: Removing tmp image files
+  file:
+    path: "/tmp/{{ item.image }}.img"
+    state: absent
+  with_items: "{{ servers }}"
+
+- name: Creating a security group
+  os_security_group:
+    cloud: "{{ cloud_name }}"
+    state: present
+    name: "{{external_network_name}}_access"
+    description: Security group for LAN external access
+
+- name: Creating ICMP rule
+  os_security_group_rule:
+    cloud: "{{ cloud_name }}"
+    security_group: "{{external_network_name}}_access"
+    protocol: icmp
+    remote_ip_prefix: 0.0.0.0/0
+
+- name: Creating TCP access rule
+  os_security_group_rule:
+    cloud: "{{ cloud_name }}"
+    security_group: "{{external_network_name}}_access"
+    protocol: tcp
+    remote_ip_prefix: "{{ cidr }}"
+
+- name: SSH key check
+  os_keypair:
+    cloud: "{{ cloud_name }}"
+    state: present
+    name: "{{ os_key_name }}"
+  register: keypair
+
+- debug:
+    var: keypair
+    verbosity: 2
+
+- name: Creating the new ansible key
+  local_action:
+    module: copy
+    content: "{{ keypair.key.public_key }}"
+    dest: "~/.ssh/{{ keypair.key.name }}.pub"
+    mode: '600'
+  when: keypair.key.public_key is not none
+
+- name: Creating the new ansible private key
+  local_action:
+    module: copy
+    content: "{{ keypair.key.private_key }}"
+    dest: "~/.ssh/{{ keypair.key.name }}"
+    mode: '600'
+  when: keypair.key.private_key is not none
\ No newline at end of file
diff --git a/installers/openstack/site.yml b/installers/openstack/site.yml
new file mode 100644
index 0000000..c1cba50
--- /dev/null
+++ b/installers/openstack/site.yml
@@ -0,0 +1,29 @@
+---
+#   Copyright 2020 British Telecommunications plc
+#
+#   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.
+#   Author: Antonio Marsico (antonio.marsico@bt.com)
+
+- name: Setting up OpenStack and creating a VM for hosting OSM
+  hosts: localhost
+
+  roles:
+    - setup_openstack
+    - create_instances
+
+- name: Install OSM
+  hosts: appservers
+  gather_facts: no
+
+  roles:
+    - osm_installation