From b8f6c1eac4c10242ed3054c4bd40de7ea7bd1389 Mon Sep 17 00:00:00 2001
From: Primo <aaalsaye@stud.ntnu.no>
Date: Sat, 6 Nov 2021 08:18:09 +0100
Subject: [PATCH] HOT template and resources basic

---
 autoscale_env.yaml |  10 +++
 autoscaling.yaml   | 149 +++++++++++++++++++++++++++++++++++++++++++++
 db_setup.sh        |  12 ++++
 lb_server.yaml     |  53 ++++++++++++++++
 scaled_service.sh  |  14 +++++
 5 files changed, 238 insertions(+)
 create mode 100644 autoscale_env.yaml
 create mode 100644 autoscaling.yaml
 create mode 100644 db_setup.sh
 create mode 100644 lb_server.yaml
 create mode 100644 scaled_service.sh

diff --git a/autoscale_env.yaml b/autoscale_env.yaml
new file mode 100644
index 0000000..09cf52e
--- /dev/null
+++ b/autoscale_env.yaml
@@ -0,0 +1,10 @@
+parameters:
+  key: ''
+  image: ''
+  flavor: ''
+  database_flavor: ''
+  external_network_id: ''
+  network: ''
+  subnet_id: ''
+  database_name: ''
+  database_user: ''
diff --git a/autoscaling.yaml b/autoscaling.yaml
new file mode 100644
index 0000000..9c075fe
--- /dev/null
+++ b/autoscaling.yaml
@@ -0,0 +1,149 @@
+heat_template_version: 2016-10-14
+description: AutoScaling Wordpress
+parameters:
+  image:
+    type: string
+    description: Image used for servers
+  key:
+    type: string
+    description: SSH key to connect to the servers
+  flavor:
+    type: string
+    description: flavor used by the web servers
+  database_flavor:
+    type: string
+    description: flavor used by the db server
+  network:
+    type: string
+    description: Network used by the server
+  subnet_id:
+    type: string
+    description: subnet on which the load balancer will be located
+  database_name:
+    type: string
+    description: Name of the wordpress DB
+    default: wordpress
+  database_user:
+    type: string
+    description: Name of the wordpress user
+    default: wordpress
+  external_network_id:
+    type: string
+    description: UUID of a Neutron external network
+resources:
+  database_password:
+    type: OS::Heat::RandomString
+  database_root_password:
+    type: OS::Heat::RandomString
+  db:
+    type: OS::Nova::Server
+    properties:
+      flavor: {get_param: database_flavor}
+      image: {get_param: image}
+      key_name: {get_param: key}
+      networks: [{network: {get_param: network} }]
+      user_data_format: RAW
+      user_data:
+        str_replace:
+          template: {get_file: db_setup.sh}
+          params:
+            $db_rootpassword: {get_attr: [database_root_password, value]}
+            $db_name: {get_param: database_name}
+            $db_user: {get_param: database_user}
+            $db_password: {get_attr: [database_password, value]}
+  asg:
+    type: OS::Heat::AutoScalingGroup
+    properties:
+      min_size: 1
+      max_size: 3
+      resource:
+        type: lb_server.yaml
+        properties:
+          flavor: {get_param: flavor}
+          image: {get_param: image}
+          key_name: {get_param: key}
+          network: {get_param: network}
+          subnet: {get_param: subnet_id}
+          pool_id: {get_resource: pool}
+          metadata: {"metering.server_group": {get_param: "OS::stack_id"}}
+          user_data:
+            str_replace:
+              template: {get_file: scaled_service.sh}
+              params:
+                $db_name: {get_param: database_name}
+                $db_user: {get_param: database_user}
+                $db_password: {get_attr: [database_password, value]}
+                $db_host: {get_attr: [db, first_address]}
+  web_server_scaleup_policy:
+    type: OS::Heat::ScalingPolicy
+    properties:
+      adjustment_type: change_in_capacity
+      auto_scaling_group_id: {get_resource: asg}
+      cooldown: 60
+      scaling_adjustment: 1
+  web_server_scaledown_policy:
+    type: OS::Heat::ScalingPolicy
+    properties:
+      adjustment_type: change_in_capacity
+      auto_scaling_group_id: {get_resource: asg}
+      cooldown: 60
+      scaling_adjustment: -1
+  lb:
+    type: OS::Octavia::LoadBalancer
+    properties:
+      vip_subnet: {get_param: subnet_id}
+  listener:
+    type: OS::Octavia::Listener
+    properties:
+      loadbalancer: {get_resource: lb}
+      protocol: HTTP
+      protocol_port: 80
+  pool:
+    type: OS::Octavia::Pool
+    properties:
+      listener: {get_resource: listener}
+      lb_algorithm: ROUND_ROBIN
+      protocol: HTTP
+      session_persistence:
+        type: SOURCE_IP
+  lb_monitor:
+    type: OS::Octavia::HealthMonitor
+    properties:
+      pool: { get_resource: pool }
+      type: TCP
+      delay: 5
+      max_retries: 5
+      timeout: 5
+  # assign a floating ip address to the load balancer
+  # pool.
+  lb_floating:
+    type: OS::Neutron::FloatingIP
+    properties:
+      floating_network_id: {get_param: external_network_id}
+      port_id: {get_attr: [lb, vip_port_id]}
+
+outputs:
+  scale_up_url:
+    description: >
+      This URL is the webhook to scale up the autoscaling group.  You
+      can invoke the scale-up operation by doing an HTTP POST to this
+      URL; no body nor extra headers are needed.
+    value: {get_attr: [web_server_scaleup_policy, alarm_url]}
+  scale_dn_url:
+    description: >
+      This URL is the webhook to scale down the autoscaling group.
+      You can invoke the scale-down operation by doing an HTTP POST to
+      this URL; no body nor extra headers are needed.
+    value: {get_attr: [web_server_scaledown_policy, alarm_url]}
+  pool_ip_address:
+    value: {get_attr: [lb, vip_address]}
+    description: The IP address of the load balancing pool
+  website_url:
+    value:
+      str_replace:
+        template: http://host/wordpress/
+        params:
+          host: { get_attr: [lb_floating, floating_ip_address] }
+    description: >
+      This URL is the "external" URL that can be used to access the
+      Wordpress site.
diff --git a/db_setup.sh b/db_setup.sh
new file mode 100644
index 0000000..70fbb70
--- /dev/null
+++ b/db_setup.sh
@@ -0,0 +1,12 @@
+#!/bin/bash -v
+yum -y install mariadb mariadb-server
+systemctl enable mariadb.service
+systemctl start mariadb.service
+mysqladmin -u root password $db_rootpassword
+cat << EOF | mysql -u root --password=$db_rootpassword
+CREATE DATABASE $db_name;
+GRANT ALL PRIVILEGES ON $db_name.* TO "$db_user"@"%"
+IDENTIFIED BY "$db_password";
+FLUSH PRIVILEGES;
+EXIT
+EOF
\ No newline at end of file
diff --git a/lb_server.yaml b/lb_server.yaml
new file mode 100644
index 0000000..6d02e15
--- /dev/null
+++ b/lb_server.yaml
@@ -0,0 +1,53 @@
+heat_template_version: 2016-10-14
+description: A load-balancer server
+parameters:
+  image:
+    type: string
+    description: Image used for servers
+  key_name:
+    type: string
+    description: SSH key to connect to the servers
+  flavor:
+    type: string
+    description: flavor used by the servers
+  pool_id:
+    type: string
+    description: Pool to contact
+  user_data:
+    type: string
+    description: Server user_data
+  metadata:
+    type: json
+  network:
+    type: string
+    description: Network used by the server
+  subnet:
+    type: string
+    description: Subnet used by the server
+
+resources:
+  server:
+    type: OS::Nova::Server
+    properties:
+      flavor: {get_param: flavor}
+      image: {get_param: image}
+      key_name: {get_param: key_name}
+      metadata: {get_param: metadata}
+      user_data: {get_param: user_data}
+      user_data_format: RAW
+      networks: [{network: {get_param: network} }]
+  member:
+    type: OS::Octavia::PoolMember
+    properties:
+      pool: {get_param: pool_id}
+      address: {get_attr: [server, first_address]}
+      protocol_port: 80
+      subnet: {get_param: subnet}
+
+outputs:
+  server_ip:
+    description: IP Address of the load-balanced server.
+    value: { get_attr: [server, first_address] }
+  lb_member:
+    description: LB member details.
+    value: { get_attr: [member, show] }
diff --git a/scaled_service.sh b/scaled_service.sh
new file mode 100644
index 0000000..64e18d6
--- /dev/null
+++ b/scaled_service.sh
@@ -0,0 +1,14 @@
+#!/bin/bash -v
+yum -y install httpd wordpress
+systemctl enable httpd.service
+systemctl start httpd.service
+setsebool -P httpd_can_network_connect_db=1
+
+sed -i "/Deny from All/d" /etc/httpd/conf.d/wordpress.conf
+sed -i "s/Require local/Require all granted/" /etc/httpd/conf.d/wordpress.conf
+sed -i s/database_name_here/$db_name/ /etc/wordpress/wp-config.php
+sed -i s/username_here/$db_user/ /etc/wordpress/wp-config.php
+sed -i s/password_here/$db_password/ /etc/wordpress/wp-config.php
+sed -i s/localhost/$db_host/ /etc/wordpress/wp-config.php
+
+systemctl restart httpd.service
\ No newline at end of file
-- 
GitLab