Creating an Autonomous VM
The goal of this topic is to present one of the solutions to deploy self-configured virtual machines (VMs) on the OUTSCALE Cloud.
This solution is a proper alternative to tools like Ansible or Salt, since you do not need to add inbound rules in the security groups of your VM. However, do not forget to open outbound flows if you are in a Net, and create routes and an Internet gateway.
Step 1: Create a VM Image Executing User Data
-
Create a script that is executed only when necessary:
To prevent from any further execution, we will end the execution by creating a file, which existence will be checked before re-executing itself.
-
To create the script, run the following command:
$ vim /opt/bootscript.sh
-
Insert the following content for the script in the text editor:
#!/usr/bin/bash if [ ! -f /var/setup_vm ]; then curl 169.254.169.254/latest/user-data | bash fi
-
-
Ensure that this script will be executed as a service using the following command:
-
To create the boot setup script, run the following command:
$ vim /usr/lib/systemd/system/setup_boot.service
-
Insert the following content for the script in the text editor:
[Unit] Description=Execute user-datas to setup the machine After=network-online.target [Service] Type=oneshot RemainAfterExit=no ExecStart=/usr/bin/bash /opt/bootscript.sh [Install] WantedBy=multi-user.target
-
-
Enable the service using the following command:
command$ systemctl enable setup_boot.service
-
Stop the VM and create an OMI from it. For more information, see Creating an OMI from a VM.
Step 2: Create a VM for a Small Python Webserver
With the OUTSCALE Python SDK, create a VM using the previously created OMI:
import base64
import time
from osc_sdk_python import Gateway
gw = Gateway(**{"profile": "default"})
userdata = """#!/usr/bin/bash
yum install python supervisor -y
wget https://oos.eu-west-2.outscale.com/BUCKET/package.tar.gz
tar -xvf package.tar.gz
mv package/* /root/
pip install -r /root/requirements.txt
mv /root/supervisord.conf /etc/supervisord.conf
systemctl start supervisord
touch /var/setup_vm
"""
userdata_base64_string = base64.b64encode(userdata.encode("utf-8")).decode("ascii")
create_result = gw.CreateVms(
ImageId="ami-a3ca408c",
KeypairName="testubuntu",
VmType="t2.medium",
UserData=userdata_base64_string,
)
vm_id = create_result["Vms"][0]["VmId"]
while True:
read_result = gw.ReadVms(Filters={"VmIds": [vm_id]})
if "PublicIp" not in read_result["Vms"][0]:
print("Waiting for the VM's public IP...")
time.sleep(10)
else:
print(read_result["Vms"][0]["PublicIp"])
break
You can now connect to your VM and enjoy your brand-new webserver.
Related Pages