diff --git a/misc/nginx-openstack/README.md b/misc/nginx-openstack/README.md index a3b622d..2757a85 100644 --- a/misc/nginx-openstack/README.md +++ b/misc/nginx-openstack/README.md @@ -3,18 +3,23 @@ An example of OpenStack and nginx deployment. ## Table of Contents - [Description](#description) - - [Prerequisites](#prerequisites) + - [Prerequisites](#prerequisites) - [Running with xOpera](#running-with-xopera) -# Description -This example shows how to deploy and set up an OpenStack VM and the an nginx site on top of it. +## Description +This example shows how to deploy and set up an OpenStack VM and an nginx site on top of it. + +After the deployment the sample HTML website will be available on `:80`, so make sure that you unlock the +(ingress) port `80` within the specified OpenStack security group. ## Prerequisites To run this example we need some prerequisites such as running OpenStack. Because using OpenStack modules from Ansible playbooks is quite common, we can install `opera` with all required OpenStack libraries by running: - (.venv) $ pip install -U opera[openstack] +```console +(.venv) $ pip install -U opera[openstack] +``` Before we can actually use the OpenStack functionality, we also need to obtain the OpenStack credentials. If we log into OpenStack and navigate @@ -24,7 +29,9 @@ file with all required information. At the start of each session (e.g., when we open a new command line console), we must source the rc file by running: - (venv) $ . openstack.rc +```console +(venv) $ . openstack.rc +``` After we enter the password, we are ready to start using the OpenStack modules in playbooks that implement life cycle operations. @@ -34,5 +41,30 @@ We can run this example as follows: ```console (venv) $ cd misc/nginx-openstack -(venv) misc/nginx-openstack$ opera deploy service.yaml +(venv) misc/nginx-openstack$ opera deploy -i inputs.yaml service.yaml +[Worker_0] Deploying vm_0 +[Worker_0] Executing create on vm_0 +[Worker_0] Deployment of vm_0 complete +[Worker_0] Deploying nginx_0 +[Worker_0] Executing create on nginx_0 +[Worker_0] Executing post_configure_target on site_0--nginx_0 +[Worker_0] Deployment of nginx_0 complete +[Worker_0] Deploying site_0 +[Worker_0] Executing create on site_0 +[Worker_0] Deployment of site_0 complete +``` + +You can undeploy the solution with: + +```console +(venv) misc/nginx-openstack$ opera undeploy +[Worker_0] Undeploying site_0 +[Worker_0] Executing delete on site_0 +[Worker_0] Undeployment of site_0 complete +[Worker_0] Undeploying nginx_0 +[Worker_0] Executing delete on nginx_0 +[Worker_0] Undeployment of nginx_0 complete +[Worker_0] Undeploying vm_0 +[Worker_0] Executing delete on vm_0 +[Worker_0] Undeployment of vm_0 complete ``` diff --git a/misc/nginx-openstack/inputs.yaml b/misc/nginx-openstack/inputs.yaml new file mode 100644 index 0000000..6289f46 --- /dev/null +++ b/misc/nginx-openstack/inputs.yaml @@ -0,0 +1,20 @@ +# This yaml file contains inputs for service.yaml. + +vm_name: +vm_image: +vm_flavor: +vm_network: +vm_security_groups: +vm_key_name: +ssh_user: +ssh_key_file: + +### Example: +# vm_name: nginx-host +# vm_image: ubuntu-20.04.3 +# vm_flavor: small +# vm_network: ostack2 +# vm_security_groups: default +# vm_key_name: user1 +# ssh_user: ubuntu +# ssh_key_file: /home/user1/.ssh/openstack.key diff --git a/misc/nginx-openstack/library/nginx/server/playbooks/install.yaml b/misc/nginx-openstack/library/nginx/server/playbooks/install.yaml index 4944108..5f15218 100644 --- a/misc/nginx-openstack/library/nginx/server/playbooks/install.yaml +++ b/misc/nginx-openstack/library/nginx/server/playbooks/install.yaml @@ -1,10 +1,14 @@ --- - hosts: all + gather_facts: no become: yes + vars: + ansible_ssh_private_key_file: "{{ ssh_key_file }}" + ansible_ssh_user: "{{ ssh_user }}" tasks: - - name: Install EPEL repo - package: - name: epel-release + - name: Update repositories + apt: + update_cache: yes - name: Install nginx package: diff --git a/misc/nginx-openstack/library/nginx/server/playbooks/reload.yaml b/misc/nginx-openstack/library/nginx/server/playbooks/reload.yaml index e4d0dfe..cca4f73 100644 --- a/misc/nginx-openstack/library/nginx/server/playbooks/reload.yaml +++ b/misc/nginx-openstack/library/nginx/server/playbooks/reload.yaml @@ -1,6 +1,10 @@ --- - hosts: all + gather_facts: no become: yes + vars: + ansible_ssh_private_key_file: "{{ ssh_key_file }}" + ansible_ssh_user: "{{ ssh_user }}" tasks: - name: Restart nginx service: diff --git a/misc/nginx-openstack/library/nginx/server/playbooks/uninstall.yaml b/misc/nginx-openstack/library/nginx/server/playbooks/uninstall.yaml index 6e729fd..3d56ce8 100644 --- a/misc/nginx-openstack/library/nginx/server/playbooks/uninstall.yaml +++ b/misc/nginx-openstack/library/nginx/server/playbooks/uninstall.yaml @@ -1,6 +1,10 @@ --- - hosts: all + gather_facts: no become: yes + vars: + ansible_ssh_private_key_file: "{{ ssh_key_file }}" + ansible_ssh_user: "{{ ssh_user }}" tasks: - name: Stop nginx service: diff --git a/misc/nginx-openstack/library/nginx/server/types.yaml b/misc/nginx-openstack/library/nginx/server/types.yaml index edafa99..b252ebb 100644 --- a/misc/nginx-openstack/library/nginx/server/types.yaml +++ b/misc/nginx-openstack/library/nginx/server/types.yaml @@ -10,6 +10,13 @@ node_types: site_config_dir: type: string description: Location of nginx site configs + properties: + ssh_user: + type: string + description: The username that will be used to connect to the created VM + ssh_key_file: + type: string + description: Path to the local SSH private key file that will be used for the ssh connection to the VM capabilities: host: type: tosca.capabilities.Compute @@ -17,6 +24,9 @@ node_types: interfaces: Standard: type: tosca.interfaces.node.lifecycle.Standard + inputs: + ssh_user: { value: { get_property: [ SELF, ssh_user ] }, type: string } + ssh_key_file: { value: { get_property: [ SELF, ssh_key_file ] }, type: string } operations: create: implementation: @@ -33,8 +43,8 @@ relationship_types: operations: post_configure_target: inputs: - marker: - value: { get_attribute: [ TARGET, host, id ] } - type: string + marker: { value: { get_attribute: [ TARGET, host, id ] }, type: string } + ssh_user: { value: { get_property: [ SOURCE, ssh_user ] }, type: string } + ssh_key_file: { value: { get_property: [ SOURCE, ssh_key_file ] }, type: string } implementation: primary: playbooks/reload.yaml diff --git a/misc/nginx-openstack/library/nginx/site/playbooks/create.yaml b/misc/nginx-openstack/library/nginx/site/playbooks/create.yaml index cc5ca90..22809f9 100644 --- a/misc/nginx-openstack/library/nginx/site/playbooks/create.yaml +++ b/misc/nginx-openstack/library/nginx/site/playbooks/create.yaml @@ -1,11 +1,14 @@ --- - hosts: all + gather_facts: no become: yes - + vars: + ansible_ssh_private_key_file: "{{ ssh_key_file }}" + ansible_ssh_user: "{{ ssh_user }}" tasks: - name: Install sample site copy: - dest: /usr/share/nginx/html/index.html + dest: "{{ item }}" content: | @@ -17,3 +20,6 @@

With little content ;)

+ with_items: + - /var/www/html/index.html + - /usr/share/nginx/html/index.html diff --git a/misc/nginx-openstack/library/nginx/site/playbooks/delete.yaml b/misc/nginx-openstack/library/nginx/site/playbooks/delete.yaml index f6f3836..7848b4c 100644 --- a/misc/nginx-openstack/library/nginx/site/playbooks/delete.yaml +++ b/misc/nginx-openstack/library/nginx/site/playbooks/delete.yaml @@ -1,9 +1,12 @@ --- - hosts: all + gather_facts: no become: yes - + vars: + ansible_ssh_private_key_file: "{{ ssh_key_file }}" + ansible_ssh_user: "{{ ssh_user }}" tasks: - - name: Install sample site + - name: Uninstall sample site file: path: /usr/share/nginx/html/index.html state: absent diff --git a/misc/nginx-openstack/library/nginx/site/types.yaml b/misc/nginx-openstack/library/nginx/site/types.yaml index b9db82f..beb6b55 100644 --- a/misc/nginx-openstack/library/nginx/site/types.yaml +++ b/misc/nginx-openstack/library/nginx/site/types.yaml @@ -6,6 +6,13 @@ imports: node_types: nginx.Site: derived_from: tosca.nodes.Root + properties: + ssh_user: + type: string + description: The username that will be used to connect to the created VM + ssh_key_file: + type: string + description: Path to the local SSH private key file that will be used for the ssh connection to the VM requirements: - host: capability: tosca.capabilities.Compute @@ -13,9 +20,9 @@ node_types: interfaces: Standard: inputs: - site_config_dir: - value: { get_attribute: [ SELF, host, site_config_dir ] } - type: string + site_config_dir: { value: { get_attribute: [ SELF, host, site_config_dir ] }, type: string } + ssh_user: { value: { get_property: [ SELF, ssh_user ] }, type: string } + ssh_key_file: { value: { get_property: [ SELF, ssh_key_file ] }, type: string } operations: create: playbooks/create.yaml delete: playbooks/delete.yaml diff --git a/misc/nginx-openstack/library/openstack/vm/playbooks/create.yaml b/misc/nginx-openstack/library/openstack/vm/playbooks/create.yaml index 339135e..e73796e 100644 --- a/misc/nginx-openstack/library/openstack/vm/playbooks/create.yaml +++ b/misc/nginx-openstack/library/openstack/vm/playbooks/create.yaml @@ -20,12 +20,13 @@ public_address: "{{ server_info.server.public_v4 }}" id: "{{ server_info.server.id }}" - - name: Create temporary invetory for ssh wait + - name: Create temporary inventory for ssh wait add_host: name: server groups: vms ansible_host: "{{ server_info.server.public_v4 }}" - ansible_user: centos + ansible_user: "{{ ssh_user }}" + ansible_ssh_private_key_file: "{{ ssh_key_file }}" ansible_ssh_common_args: > -o IdentitiesOnly=yes -o BatchMode=yes @@ -37,3 +38,6 @@ tasks: - name: Wait for ssh to wake up wait_for_connection: + delay: 10 + sleep: 5 + timeout: 120 diff --git a/misc/nginx-openstack/library/openstack/vm/playbooks/delete.yaml b/misc/nginx-openstack/library/openstack/vm/playbooks/delete.yaml index b825bcc..bc63c06 100644 --- a/misc/nginx-openstack/library/openstack/vm/playbooks/delete.yaml +++ b/misc/nginx-openstack/library/openstack/vm/playbooks/delete.yaml @@ -1,7 +1,6 @@ --- - hosts: all gather_facts: no - tasks: - name: Delete VM os_server: diff --git a/misc/nginx-openstack/library/openstack/vm/types.yaml b/misc/nginx-openstack/library/openstack/vm/types.yaml index 7571023..4852c53 100644 --- a/misc/nginx-openstack/library/openstack/vm/types.yaml +++ b/misc/nginx-openstack/library/openstack/vm/types.yaml @@ -18,12 +18,17 @@ node_types: description: OpenStack network id (network names are not accepted) security_groups: type: string - description: > - OpenStack names of the security groups to which the instance should - be added. Comma separated string. + description: + OpenStack names of the security groups to which the instance should be added (comma separated string) key_name: type: string description: OpenStack SSH key name that should be placed on the VM + ssh_user: + type: string + description: The username that will be used to connect to the created VM + ssh_key_file: + type: string + description: Path to the local SSH private key file that will be used for the ssh connection to the VM attributes: id: type: string @@ -34,14 +39,14 @@ node_types: operations: create: inputs: - vm_name: { value: { get_property: [ SELF, name ] }, type: string } - image: { value: { get_property: [ SELF, image ] }, type: string } - flavor: { value: { get_property: [ SELF, flavor ] }, type: string } - network: { value: { get_property: [ SELF, network ] }, type: string } + vm_name: { value: { get_property: [ SELF, name ] }, type: string } + image: { value: { get_property: [ SELF, image ] }, type: string } + flavor: { value: { get_property: [ SELF, flavor ] }, type: string } + network: { value: { get_property: [ SELF, network ] }, type: string } key_name: { value: { get_property: [ SELF, key_name ] }, type: string } - security_groups: - value: { get_property: [ SELF, security_groups ] } - type: string + security_groups: { value: { get_property: [ SELF, security_groups ] }, type: string } + ssh_user: { value: { get_property: [ SELF, ssh_user ] }, type: string } + ssh_key_file: { value: { get_property: [ SELF, ssh_key_file ] }, type: string } implementation: playbooks/create.yaml delete: inputs: diff --git a/misc/nginx-openstack/service.yaml b/misc/nginx-openstack/service.yaml index cf68d42..19f8835 100644 --- a/misc/nginx-openstack/service.yaml +++ b/misc/nginx-openstack/service.yaml @@ -10,23 +10,49 @@ imports: - library/openstack/main.yaml topology_template: + inputs: + vm_name: + type: string + vm_image: + type: string + vm_flavor: + type: string + vm_network: + type: string + vm_security_groups: + type: string + vm_key_name: + type: string + ssh_user: + type: string + ssh_key_file: + type: string + node_templates: + vm: + type: openstack.VM + properties: + name: { get_input: vm_name } + image: { get_input: vm_image } + flavor: { get_input: vm_flavor } + network: { get_input: vm_network } + security_groups: { get_input: vm_security_groups } + key_name: { get_input: vm_key_name } + ssh_user: { get_input: ssh_user } + ssh_key_file: { get_input: ssh_key_file } + nginx: type: nginx.Server + properties: + ssh_user: { get_input: ssh_user } + ssh_key_file: { get_input: ssh_key_file } requirements: - host: vm - vm: - type: openstack.VM - properties: - name: nginx_host - image: 9ea4856a-32b2-4553-b408-cfa4cb1bb40b - flavor: d3046a41-245a-4042-862e-59568e81f8fa - network: 753940e0-c2a7-4c9d-992e-4d5bd71f85aa - security_groups: default - key_name: tadej_borovsak - site: type: nginx.Site + properties: + ssh_user: { get_input: ssh_user } + ssh_key_file: { get_input: ssh_key_file } requirements: - host: nginx diff --git a/validate.sh b/validate.sh index a3113e0..62d9562 100755 --- a/validate.sh +++ b/validate.sh @@ -72,7 +72,7 @@ validate_example "misc/concurrency" "service.yaml" "" # test an example from misc/hello-world validate_example "misc/hello-world" "service.yaml" "" # test an example from misc/nginx-openstack -validate_example "misc/nginx-openstack" "service.yaml" "" +validate_example "misc/nginx-openstack" "service.yaml" "inputs.yaml" # test an example from misc/scaling validate_example "misc/scaling" "service.yaml" "" # test an example from misc/server-client