Skip to content

Commit

Permalink
Create a python openstack webserver example (pulumi#1004)
Browse files Browse the repository at this point in the history
* Create a python openstack webserver example

Shows how to create an openstack instance
starting with image upload, addition of
security group rules, creating a keypari for
ssh access and a custom user data
script to run on the instance

* Use openstack and horizon in step description
  • Loading branch information
kbaikov committed May 18, 2021
1 parent 095de7c commit a2c443b
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ $ git pull origin master
- [Python](#python-3)
- [Go](#go-3)
- [C#](#c-3)
- [Openstack](#openstack)
- [Cloud](#cloud)
- [DigitalOcean](#digitalocean)
- [Multicloud](#multicloud)
Expand Down Expand Up @@ -292,6 +293,12 @@ Example | Description |
--------- | --------- |
[Guestbook](kubernetes-go-guestbook) | Build and deploy a simple, multi-tier web application using Kubernetes and Docker.

## Openstack

### Python

[Web Server](openstack-py-webserver) | Deploy an Openstack instance and open port 8000.

## Cloud

### TypeScript
Expand Down
6 changes: 6 additions & 0 deletions openstack-py-webserver/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: pulum
runtime:
name: python
options:
virtualenv: venv
description: A minimal OpenStack Python Pulumi program
71 changes: 71 additions & 0 deletions openstack-py-webserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Web Server Using Openstack


## Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
1. [Configure Pulumi for Openstack](https://www.pulumi.com/docs/intro/cloud-providers/openstack/setup/)
1. [Configure Pulumi for Python](https://www.pulumi.com/docs/intro/languages/python/)

## Deploying and running the program

1. Create a new stack:

```bash
$ pulumi stack init
```

2. Modify `__main__.py` to include your keypair and image

3. Run `pulumi up` to preview and deploy changes:

```bash
$ pulumi up
Previewing update (dev):
Type Name Plan
pulumi:pulumi:Stack pulum-dev
+ ├─ openstack:images:Image fedora create
+ ├─ openstack:compute:Keypair default create
+ ├─ openstack:networking:SecGroupRule secgroupRule1 create
+ ├─ openstack:networking:SecGroupRule secgroupRule2 create
+ ├─ openstack:networking:SecGroupRule secgroupRule3 create
+ └─ openstack:compute:Instance test_fedora create

Outputs:
~ instance_ip: "192.168.0.243" => output<string>

Resources:
+ 6 to create
1 unchanged
Resources:
+ 6 created
1 unchanged

Duration: 38s

```

4. View the host name and IP address of the instance via `stack output`:

```bash
$ pulumi stack output
Current stack outputs (1):
OUTPUT VALUE
instance_ip 192.168.0.243
```

5. Verify that the Openstack instance exists, by either using the Horizon dashboard or running `openstack server list`.
```bash
$ openstack server list
+--------------------------------------+-------------+--------+-------------------------------------+--------------------------+----------+
| ID | Name | Status | Networks | Image | Flavor |
+--------------------------------------+-------------+--------+-------------------------------------+--------------------------+----------+
| 8bdf8a6d-ac53-4448-ae09-e2a08ad554a0 | test_fedora | ACTIVE | public=192.168.0.243, 2001:db8::36b | fedora | m1.small |
+--------------------------------------+-------------+--------+-------------------------------------+--------------------------+----------+
```

## Clean up

To clean up resources, run `pulumi destroy` and answer the confirmation question at the prompt.
80 changes: 80 additions & 0 deletions openstack-py-webserver/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""An OpenStack Python Pulumi program"""

import pulumi
import pulumi_openstack as openstack


fedora_image = openstack.images.Image(
"fedora",
name="fedora",
container_format="bare",
disk_format="qcow2",
image_source_url="https://ftp.plusline.net/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2",
visibility="public",
)

default_keypair = openstack.compute.keypair.Keypair(
"default",
name="default",
public_key="ssh-ed25519 <public key>",
)

secgroup_default = openstack.networking.get_sec_group(name="default")

secgroup_rule_ssh = openstack.networking.SecGroupRule(
"secgroupRule1",
direction="ingress",
ethertype="IPv4",
port_range_max=22,
port_range_min=22,
protocol="tcp",
remote_ip_prefix="0.0.0.0/0",
description="Allow ssh",
security_group_id=secgroup_default.id,
)

secgroup_rule_python_server = openstack.networking.SecGroupRule(
"secgroupRule2",
direction="ingress",
ethertype="IPv4",
port_range_max=8000,
port_range_min=8000,
protocol="tcp",
remote_ip_prefix="0.0.0.0/0",
description="Allow python http server",
security_group_id=secgroup_default.id,
)

secgroup_rule_icmp = openstack.networking.SecGroupRule(
"secgroupRule3",
direction="ingress",
ethertype="IPv4",
protocol="icmp",
remote_ip_prefix="0.0.0.0/0",
description="Allow ping",
security_group_id=secgroup_default.id,
)

network_public = openstack.networking.get_network(name="public")

user_data = """
#!/bin/bash
echo "Hello, World!" > index.html
nohup python3 -m http.server &
"""

fedora = openstack.compute.Instance(
"test_fedora",
name="test_fedora",
flavor_name="m1.small",
image_id=fedora_image.id,
key_pair=default_keypair.id,
security_groups=["default"],
networks=[
openstack.compute.InstanceNetworkArgs(uuid=network_public.id),
],
user_data=user_data,
)

# Export the IP of the instance
pulumi.export("instance_ip", fedora.access_ip_v4)
2 changes: 2 additions & 0 deletions openstack-py-webserver/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pulumi>=3.0.0,<4.0.0
pulumi-openstack>=3.0.0,<4.0.0

0 comments on commit a2c443b

Please sign in to comment.