Skip to content

Commit

Permalink
Merge pull request #4 from Renater/add-cloud-init
Browse files Browse the repository at this point in the history
Add Cloud init
  • Loading branch information
just1not2 authored Jun 23, 2022
2 parents 9ffae04 + 2b01fe8 commit 9ff735f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Application version
VERSION=1.0.0
VERSION=1.1.0

# Webserver configuration
APP_HOST=0.0.0.0
Expand Down Expand Up @@ -41,3 +41,4 @@ OPENSTACK_NETWORK=Ext-Net
OPENSTACK_METADATA_KEY=group
OPENSTACK_METADATA_VALUE=scaled
OPENSTACK_KEYPAIR=
OPENSTACK_CLOUD_INIT_FILE=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
cloud-init.sh
env/
tests/mock.env
__pycache__/
5 changes: 4 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ endif
build:
docker build -t renater/simplescalevm:${PROVIDER}-test --build-arg provider=${PROVIDER} .

cloud-init.sh:
cp cloud-init.sh.example cloud-init.sh

env:
virtualenv env

Expand Down Expand Up @@ -43,7 +46,7 @@ start:
env/bin/python3 src/main.py

.PHONY: start-docker
start-docker: build
start-docker: build cloud-init.sh
docker-compose up -d

tests/mock.env:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following values should be overwritten to configure the connection to the Op
* `OPENSTACK_METADATA_KEY`: metadata key to identify the scaled server pool.
* `OPENSTACK_METADATA_VALUE`: metadata value to identify the scaled server pool.
* `OPENSTACK_KEYPAIR`: name of the keypair that is provisioned on the virtual machines.
* `OPENSTACK_CLOUD_INIT_FILE`: path to a cloud-init file to launch on virtual machines at creation.


## Installation
Expand Down
3 changes: 3 additions & 0 deletions cloud-init.sh.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

echo "Cloud-init executed with success" > /cloud-init.log
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ services:
ports:
- "${APP_PORT}:${APP_PORT}"
env_file: .env
volumes:
- ./cloud-init.sh:/${OPENSTACK_CLOUD_INIT_FILE}
27 changes: 19 additions & 8 deletions src/providers/openstack/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
OPENSTACK_METADATA_KEY,
OPENSTACK_METADATA_VALUE,
OPENSTACK_NETWORK,
OPENSTACK_CLOUD_INIT_FILE,
)
from providers.schema import BaseProviderService
from providers.replica import Replica, ReplicaStatus
Expand Down Expand Up @@ -63,14 +64,24 @@ def list(self) -> List[Replica]:
def create(self, count: int = 1):

def creation_function():
return self.connector.create_server(
name="gateway",
image=OPENSTACK_IMAGE,
flavor=OPENSTACK_FLAVOR,
key_name=OPENSTACK_KEYPAIR,
network=OPENSTACK_NETWORK,
meta={ OPENSTACK_METADATA_KEY: OPENSTACK_METADATA_VALUE },
)
server_configuration = {
"name": "server",
"image": OPENSTACK_IMAGE,
"flavor": OPENSTACK_FLAVOR,
"network": OPENSTACK_NETWORK,
"meta": { OPENSTACK_METADATA_KEY: OPENSTACK_METADATA_VALUE },
}

# Add optional key pair
if OPENSTACK_KEYPAIR:
server_configuration["key_name"] = OPENSTACK_KEYPAIR

# Add optional cloud-init
if OPENSTACK_CLOUD_INIT_FILE:
with open(OPENSTACK_CLOUD_INIT_FILE, encoding="utf-8") as cloud_init_file:
server_configuration["userdata"] = cloud_init_file.read()

return self.connector.create_server(**server_configuration)

for _ in range(count):
thread = Thread(target=creation_function)
Expand Down
2 changes: 2 additions & 0 deletions src/providers/openstack/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Configure the Openstack provider.
Variables:
OPENSTACK_CLOUD_INIT_FILE
OPENSTACK_FLAVOR
OPENSTACK_IMAGE
OPENSTACK_IP_VERSION
Expand All @@ -16,6 +17,7 @@


# Create Openstack environment parameters
OPENSTACK_CLOUD_INIT_FILE = os.getenv("OPENSTACK_CLOUD_INIT_FILE")
OPENSTACK_FLAVOR = os.getenv("OPENSTACK_FLAVOR", "d2-8")
OPENSTACK_IMAGE = os.getenv("OPENSTACK_IMAGE", "Debian 11")
OPENSTACK_IP_VERSION = int(os.getenv("OPENSTACK_IP_VERSION", "4"))
Expand Down

0 comments on commit 9ff735f

Please sign in to comment.