Skip to content

Commit

Permalink
Update nginx-openstack example
Browse files Browse the repository at this point in the history
  • Loading branch information
anzoman committed Dec 13, 2021
1 parent e43cba6 commit 412d104
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 43 deletions.
44 changes: 38 additions & 6 deletions misc/nginx-openstack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<YOUR_VM_IP>: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
Expand All @@ -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.
Expand All @@ -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
```
20 changes: 20 additions & 0 deletions misc/nginx-openstack/inputs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This yaml file contains inputs for service.yaml.

vm_name: <openstack-vm-name>
vm_image: <openstack-vm-image-name>
vm_flavor: <openstack-vm-flavor-name>
vm_network: <openstack-vm-network-name>
vm_security_groups: <openstack-vm-security-groups>
vm_key_name: <openstack-vm-ssh-key-name>
ssh_user: <openstack-vm-ssh-user>
ssh_key_file: <path-to-local-ssh-private-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
10 changes: 7 additions & 3 deletions misc/nginx-openstack/library/nginx/server/playbooks/install.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
16 changes: 13 additions & 3 deletions misc/nginx-openstack/library/nginx/server/types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ 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
valid_source_types: [nginx.Site]
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:
Expand All @@ -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
10 changes: 8 additions & 2 deletions misc/nginx-openstack/library/nginx/site/playbooks/create.yaml
Original file line number Diff line number Diff line change
@@ -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: |
<!doctype html>
<html lang="en">
Expand All @@ -17,3 +20,6 @@
<p>With little content ;)</p>
</body>
</html>
with_items:
- /var/www/html/index.html
- /usr/share/nginx/html/index.html
7 changes: 5 additions & 2 deletions misc/nginx-openstack/library/nginx/site/playbooks/delete.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 10 additions & 3 deletions misc/nginx-openstack/library/nginx/site/types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ 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
relationship: my.relationships.NginxSiteHosting
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,3 +38,6 @@
tasks:
- name: Wait for ssh to wake up
wait_for_connection:
delay: 10
sleep: 5
timeout: 120
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
- hosts: all
gather_facts: no

tasks:
- name: Delete VM
os_server:
Expand Down
25 changes: 15 additions & 10 deletions misc/nginx-openstack/library/openstack/vm/types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
46 changes: 36 additions & 10 deletions misc/nginx-openstack/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 412d104

Please sign in to comment.