Skip to content

Commit a2c443b

Browse files
authored
Create a python openstack webserver example (#1004)
* 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
1 parent 095de7c commit a2c443b

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ $ git pull origin master
5151
- [Python](#python-3)
5252
- [Go](#go-3)
5353
- [C#](#c-3)
54+
- [Openstack](#openstack)
5455
- [Cloud](#cloud)
5556
- [DigitalOcean](#digitalocean)
5657
- [Multicloud](#multicloud)
@@ -292,6 +293,12 @@ Example | Description |
292293
--------- | --------- |
293294
[Guestbook](kubernetes-go-guestbook) | Build and deploy a simple, multi-tier web application using Kubernetes and Docker.
294295

296+
## Openstack
297+
298+
### Python
299+
300+
[Web Server](openstack-py-webserver) | Deploy an Openstack instance and open port 8000.
301+
295302
## Cloud
296303

297304
### TypeScript

openstack-py-webserver/Pulumi.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: pulum
2+
runtime:
3+
name: python
4+
options:
5+
virtualenv: venv
6+
description: A minimal OpenStack Python Pulumi program

openstack-py-webserver/README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Web Server Using Openstack
4+
5+
6+
## Prerequisites
7+
8+
1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
9+
1. [Configure Pulumi for Openstack](https://www.pulumi.com/docs/intro/cloud-providers/openstack/setup/)
10+
1. [Configure Pulumi for Python](https://www.pulumi.com/docs/intro/languages/python/)
11+
12+
## Deploying and running the program
13+
14+
1. Create a new stack:
15+
16+
```bash
17+
$ pulumi stack init
18+
```
19+
20+
2. Modify `__main__.py` to include your keypair and image
21+
22+
3. Run `pulumi up` to preview and deploy changes:
23+
24+
```bash
25+
$ pulumi up
26+
Previewing update (dev):
27+
Type Name Plan
28+
pulumi:pulumi:Stack pulum-dev
29+
+ ├─ openstack:images:Image fedora create
30+
+ ├─ openstack:compute:Keypair default create
31+
+ ├─ openstack:networking:SecGroupRule secgroupRule1 create
32+
+ ├─ openstack:networking:SecGroupRule secgroupRule2 create
33+
+ ├─ openstack:networking:SecGroupRule secgroupRule3 create
34+
+ └─ openstack:compute:Instance test_fedora create
35+
36+
Outputs:
37+
~ instance_ip: "192.168.0.243" => output<string>
38+
39+
Resources:
40+
+ 6 to create
41+
1 unchanged
42+
Resources:
43+
+ 6 created
44+
1 unchanged
45+
46+
Duration: 38s
47+
48+
```
49+
50+
4. View the host name and IP address of the instance via `stack output`:
51+
52+
```bash
53+
$ pulumi stack output
54+
Current stack outputs (1):
55+
OUTPUT VALUE
56+
instance_ip 192.168.0.243
57+
```
58+
59+
5. Verify that the Openstack instance exists, by either using the Horizon dashboard or running `openstack server list`.
60+
```bash
61+
$ openstack server list
62+
+--------------------------------------+-------------+--------+-------------------------------------+--------------------------+----------+
63+
| ID | Name | Status | Networks | Image | Flavor |
64+
+--------------------------------------+-------------+--------+-------------------------------------+--------------------------+----------+
65+
| 8bdf8a6d-ac53-4448-ae09-e2a08ad554a0 | test_fedora | ACTIVE | public=192.168.0.243, 2001:db8::36b | fedora | m1.small |
66+
+--------------------------------------+-------------+--------+-------------------------------------+--------------------------+----------+
67+
```
68+
69+
## Clean up
70+
71+
To clean up resources, run `pulumi destroy` and answer the confirmation question at the prompt.

openstack-py-webserver/__main__.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""An OpenStack Python Pulumi program"""
2+
3+
import pulumi
4+
import pulumi_openstack as openstack
5+
6+
7+
fedora_image = openstack.images.Image(
8+
"fedora",
9+
name="fedora",
10+
container_format="bare",
11+
disk_format="qcow2",
12+
image_source_url="https://ftp.plusline.net/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2",
13+
visibility="public",
14+
)
15+
16+
default_keypair = openstack.compute.keypair.Keypair(
17+
"default",
18+
name="default",
19+
public_key="ssh-ed25519 <public key>",
20+
)
21+
22+
secgroup_default = openstack.networking.get_sec_group(name="default")
23+
24+
secgroup_rule_ssh = openstack.networking.SecGroupRule(
25+
"secgroupRule1",
26+
direction="ingress",
27+
ethertype="IPv4",
28+
port_range_max=22,
29+
port_range_min=22,
30+
protocol="tcp",
31+
remote_ip_prefix="0.0.0.0/0",
32+
description="Allow ssh",
33+
security_group_id=secgroup_default.id,
34+
)
35+
36+
secgroup_rule_python_server = openstack.networking.SecGroupRule(
37+
"secgroupRule2",
38+
direction="ingress",
39+
ethertype="IPv4",
40+
port_range_max=8000,
41+
port_range_min=8000,
42+
protocol="tcp",
43+
remote_ip_prefix="0.0.0.0/0",
44+
description="Allow python http server",
45+
security_group_id=secgroup_default.id,
46+
)
47+
48+
secgroup_rule_icmp = openstack.networking.SecGroupRule(
49+
"secgroupRule3",
50+
direction="ingress",
51+
ethertype="IPv4",
52+
protocol="icmp",
53+
remote_ip_prefix="0.0.0.0/0",
54+
description="Allow ping",
55+
security_group_id=secgroup_default.id,
56+
)
57+
58+
network_public = openstack.networking.get_network(name="public")
59+
60+
user_data = """
61+
#!/bin/bash
62+
echo "Hello, World!" > index.html
63+
nohup python3 -m http.server &
64+
"""
65+
66+
fedora = openstack.compute.Instance(
67+
"test_fedora",
68+
name="test_fedora",
69+
flavor_name="m1.small",
70+
image_id=fedora_image.id,
71+
key_pair=default_keypair.id,
72+
security_groups=["default"],
73+
networks=[
74+
openstack.compute.InstanceNetworkArgs(uuid=network_public.id),
75+
],
76+
user_data=user_data,
77+
)
78+
79+
# Export the IP of the instance
80+
pulumi.export("instance_ip", fedora.access_ip_v4)
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pulumi>=3.0.0,<4.0.0
2+
pulumi-openstack>=3.0.0,<4.0.0

0 commit comments

Comments
 (0)