This script will create a local SSH key pair and a Linux instance in the cPouta environment. It will then upgrade the Linux, install the Apache2 web server, add the SSH key pair, create HTTP and SSH security groups, add a public IP and enable SSH and HTTP access to the server. Finally, it will print out instructions on how to test the installation and connect the machine.
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
#!/bin/basharg="$1"arg="${arg:-default}"case"$arg"inubuntu|default)IMAGE="Ubuntu-24.04"cat<< EOF > /tmp/init.sh#!/bin/bashapt-get update && apt-get upgrade -yapt-get install apache2 -yEOF;;alma|centos)if["$arg"="alma"];thenIMAGE="AlmaLinux-9"elseIMAGE="CentOS-9-Stream"ficat<< EOF > /tmp/init.sh#!/bin/bashyum update -ydnf install httpd -ysystemctl start httpdsystemctl enable httpdEOF;;*)echo"Invalid option: $arg"echo"Valid options are: ubuntu, centos, alma, or leave empty for default"exit2;;esacINSTANCE="$(echo"${IMAGE%%-*}"|tr'[:upper:]''[:lower:]')-apache-webserver"# Check that we are authenticated with an OpenStack environmentopenstacktokenissue&>/dev/null
if[$?-ne0];thenecho"No valid authentication with cPouta found. Source OpenStack RC File to log in."exit1fi# Generate an SSH key pairssh-keygen-trsa-N""-f$INSTANCEopenstackkeypaircreate--public-key${INSTANCE}.pub${INSTANCE}-key
# Create the instanceopenstackservercreate--flavorstandard.tiny--image$IMAGE--key-name${INSTANCE}-key--user-data/tmp/init.sh$INSTANCE# Create and attach HTTP rule to the instanceopenstacksecuritygrouplist-fvalue-cName|grep-qw"^HTTP$"if[$?-eq0];thenecho"Security group HTTP exists."elseecho"Creating security group HTTP."openstacksecuritygroupcreateHTTP
openstacksecuritygrouprulecreate--prototcp--remote-ip0.0.0.0/0--dst-port80HTTP
fiopenstackserveraddsecuritygroup$INSTANCEHTTP
# Create and attach SSH rule to the instanceopenstacksecuritygrouplist-fvalue-cName|grep-qw"^SSH$"if[$?-eq0];thenecho"Security group SSH exists."elseecho"Creating security group SSH."openstacksecuritygroupcreateSSH
openstacksecuritygrouprulecreate--prototcp--remote-ip0.0.0.0/0--dst-port22SSH
fiopenstackserveraddsecuritygroup$INSTANCESSH
# Assign a public floating IPFLOATING_IP=$(openstackfloatingipcreatepublic--formatvalue--columnfloating_ip_address)openstackserveraddfloatingip$INSTANCE$FLOATING_IPecho"Finishing installation of $IMAGE may take some time, please wait"echo"You can connect the virtual machine with command: ssh -i $INSTANCE${INSTANCE%%-*}@$FLOATING_IP"echo"The apache installation can be tested with command: curl $FLOATING_IP"echo"Or you can check the logs and status of the servers at https://pouta.csc.fi"