From 5db403f0403abceb1c4c8da33f1bf6b929e027db Mon Sep 17 00:00:00 2001 From: arunodhayamsam <108027-arunodhayamsam@users.noreply.framagit.org> Date: Fri, 13 May 2022 10:38:08 +0530 Subject: [PATCH] Add lutim_startup.sh --- .provision/README.md | 2 +- .provision/terraform-aws-lutim/README.md | 74 +++++++++++++++++++ .../terraform-aws-lutim/lutim_startup.sh | 66 +++++++++++++++++ .provision/terraform-aws-lutim/main.tf | 38 +++------- .provision/terraform-aws-lutim/output.tf | 2 +- .provision/terraform-aws-lutim/provider.tf | 9 +++ .provision/terraform-aws-lutim/vars.tf | 24 ++++++ 7 files changed, 187 insertions(+), 28 deletions(-) create mode 100644 .provision/terraform-aws-lutim/lutim_startup.sh diff --git a/.provision/README.md b/.provision/README.md index e36c2ca..054a2a4 100644 --- a/.provision/README.md +++ b/.provision/README.md @@ -4,4 +4,4 @@ An ansible role deploy the application on host machine(Ubuntu 20.04) ## terraform-aws-lutim -A terraform plan creates necessary AWS infrastructure and deploy the lutim. This terraform plan uses the above ansible roles `ansible-role-lutim` to configure the application on AWS. +A terraform plan creates necessary AWS infrastructure and deploy the lutim. This terraform plan uses the `lutim_startup.sh` script to deploy lufi on AWS and also uses above ansible role `ansible-role-lutim` to configure the application on AWS. \ No newline at end of file diff --git a/.provision/terraform-aws-lutim/README.md b/.provision/terraform-aws-lutim/README.md index 93cb54d..818f7da 100644 --- a/.provision/terraform-aws-lutim/README.md +++ b/.provision/terraform-aws-lutim/README.md @@ -16,3 +16,77 @@ | `aws_access_key` | AWSACCESSKEY | Enter your aws access key | | `aws_secrete_key` | AWSSECRETEKEY | Enter your aws secrete key | | `instance_name` | lutim_app_instance | Set the name for instance | +| `app_dir` | /var/www/lutim | Set the application directory for the best practice | +| `lutim_owner` | www-data | Set the application user for the best practice | +| `lutim_group` | www-data | Set the application group for the best practice | +| `contact` | contact.example.com | Contact option (mandatory), where you have to put some way for the users to contact you. | +| `contact_user` | name | Name of the user | +| `secrets` | ffyg7kbkjba | Secrets option (mandotory), which is array of random string. Used by Mojolicious for encrypting session cookies | +| `app_dir` | /var/www/lutim | Set the application directory for the best practice | +| `lutim_owner` | www-data | Set the application user for the best practice | +| `lutim_group` | www-data | Set the application group for the best practice | +| `contact` | contact.example.com | Contact option (mandatory), where you have to put some way for the users to contact you. | +| `contact_user` | name | Name of the user | +| `secrets` | ffyg7kbkjba | Secrets option (mandotory), which is array of random string. Used by Mojolicious for encrypting session cookies | + +## Usage of terraform plan with lufi deploy script + +```sh +git clone https://framagit.org/fiat-tux/hat-softwares/lutim.git + +cd lutim/.provision/terraform-aws-lutim + +terraform init +terraform plan +terraform apply +``` +## Usage of terraform plan with ansible role + +- Comment out the below `locals` and `user_data` source in __main.tf__ file + +```hcl +locals { + user_data_vars = { + user = var.lutim_owner + group = var.lutim_group + directory = var.app_dir + contact_user = var.contact_user + contact_lutim = var.contact + secret_lutim = var.secret + } +} +``` + +```hcl +user_data = templatefile("${path.module}/lutim_startup.sh", local.user_data_vars) +``` + +- Add the below provisioner data in __main.tf__ file at the `aws_instance` resource + +```sh + connection { + agent = false + type = "ssh" + host = aws_instance.ec2_instance.public_dns + private_key = "${file(var.private_key)}" + user = "${var.user}" + } + + provisioner "remote-exec" { + inline = [ + "sudo apt update -y", + "sudo apt install python3.9 -y", + ] + } + + provisioner "local-exec" { + command = < hosts && \ + echo "[Lutim]" | tee -a hosts && \ + echo "${aws_instance.ec2_instance.public_ip} ansible_user=${var.user} ansible_ssh_private_key_file=${var.private_key}" | tee -a hosts && \ + export ANSIBLE_HOST_KEY_CHECKING=False && \ + ansible-playbook -u ${var.user} --private-key ${var.private_key} -i hosts site.yml + EOT + } +``` diff --git a/.provision/terraform-aws-lutim/lutim_startup.sh b/.provision/terraform-aws-lutim/lutim_startup.sh new file mode 100644 index 0000000..abaa215 --- /dev/null +++ b/.provision/terraform-aws-lutim/lutim_startup.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +echo "**********************************************************************" +echo " *" +echo "Install dependencies *" +echo " *" +echo "**********************************************************************" + +SUDO=sudo +$SUDO apt update +$SUDO apt install jq -y +$SUDO apt install wget -y +$SUDO apt install unzip +$SUDO apt install carton -y +$SUDO apt install build-essential -y +$SUDO apt install nginx -y +$SUDO apt install libssl-dev -y +$SUDO apt install libio-socket-ssl-perl -y +$SUDO apt install liblwp-protocol-https-perl -y +$SUDO apt install zlib1g-dev -y +$SUDO apt install libmojo-sqlite-perl -y +$SUDO apt install libpq-dev -y + +echo "**********************************************************************" +echo " *" +echo "Configuring the Application *" +echo " *" +echo "**********************************************************************" + +sleep 10; +version=$(curl -s https://framagit.org/api/v4/projects/1/releases | jq '.[]' | jq -r '.name' | head -1) +echo $version +pushd ${directory} +$SUDO wget https://framagit.org/fiat-tux/hat-softwares/lutim/-/archive/$version/lutim-$version.zip +$SUDO unzip lutim-$version.zip +$SUDO chown ${user} lutim-$version +$SUDO chgrp ${group} lutim-$version +pushd lutim-$version + +echo "**********************************************************************" +echo " *" +echo "Install Carton Packages *" +echo " *" +echo "**********************************************************************" + +$SUDO carton install --deployment --without=test --without=sqlite --without=mysql + +sleep 10; + +$SUDO cp lutim.conf.template lutim.conf + +sed -i 's/127.0.0.1/0.0.0.0/' lutim.conf +sed -i 's/#contact/contact/g' lutim.conf +sed -i "s/John Doe/${contact_user}/g" lutim.conf +sed -i "s/admin[at]example.com/${contact_lutim}/g" lutim.conf +sed -i "s/fdjsofjoihrei/${secret_lutim}/g" lutim.conf +sed -i '153 , 158 s/#/ /g' lutim.conf + +echo "**********************************************************************" +echo " *" +echo "Run the Application *" +echo " *" +echo "**********************************************************************" + +$SUDO carton exec hypnotoad script/lutim + diff --git a/.provision/terraform-aws-lutim/main.tf b/.provision/terraform-aws-lutim/main.tf index 8f42a01..2e22cb8 100644 --- a/.provision/terraform-aws-lutim/main.tf +++ b/.provision/terraform-aws-lutim/main.tf @@ -1,3 +1,14 @@ +locals { + user_data_vars = { + user = var.lutim_owner + group = var.lutim_group + directory = var.app_dir + contact_user = var.contact_user + contact_lutim = var.contact + secret_lutim = var.secret + } +} + #Create the VPC resource "aws_vpc" "vpc" { cidr_block = "${var.vpc_cidr}" @@ -100,33 +111,8 @@ resource "aws_instance" "ec2_instance" { associate_public_ip_address = "true" subnet_id = "${aws_subnet.publicsubnet.id}" vpc_security_group_ids = ["${aws_security_group.security.id}"] + user_data = templatefile("${path.module}/lutim_startup.sh", local.user_data_vars) key_name = "lutim.webapp" - - connection { - agent = false - type = "ssh" - host = aws_instance.ec2_instance.public_dns - private_key = "${file(var.private_key)}" - user = "${var.user}" - } - - provisioner "remote-exec" { - inline = [ - "sudo apt update -y", - "sudo apt install python3.9 -y", - ] - } - - provisioner "local-exec" { - command = < hosts && \ - echo "[lutim]" | tee -a hosts && \ - echo "${aws_instance.ec2_instance.public_ip} ansible_user=${var.user} ansible_ssh_private_key_file=${var.private_key}" | tee -a hosts && \ - export ANSIBLE_HOST_KEY_CHECKING=False && \ - ansible-playbook -u ${var.user} --private-key ${var.private_key} -i hosts site.yml - EOT - } tags = { Name = "${var.instance_name}" diff --git a/.provision/terraform-aws-lutim/output.tf b/.provision/terraform-aws-lutim/output.tf index 52c6607..8c6f79e 100644 --- a/.provision/terraform-aws-lutim/output.tf +++ b/.provision/terraform-aws-lutim/output.tf @@ -3,5 +3,5 @@ output "public_ip" { } output "App_running_at" { - value = "http://${aws_instance.ec2_instance.public_ip}:8081" + value = "http://${aws_instance.ec2_instance.public_ip}:8080" } diff --git a/.provision/terraform-aws-lutim/provider.tf b/.provision/terraform-aws-lutim/provider.tf index 22f192f..d037bd2 100644 --- a/.provision/terraform-aws-lutim/provider.tf +++ b/.provision/terraform-aws-lutim/provider.tf @@ -1,3 +1,12 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 3.0" + } + } +} + provider "aws" { access_key = "${var.aws_access_key}" secret_key = "${var.aws_secret_key}" diff --git a/.provision/terraform-aws-lutim/vars.tf b/.provision/terraform-aws-lutim/vars.tf index f2bfde5..1b7e183 100644 --- a/.provision/terraform-aws-lutim/vars.tf +++ b/.provision/terraform-aws-lutim/vars.tf @@ -33,4 +33,28 @@ variable "instance_name" { default = "instance_name" } +variable "lutim_owner" { + default = "" +} + +variable "lutim_group" { + default = "" +} + +variable "app_dir" { + default = "" +} + +variable "contact_user" { + default = "" +} + +variable "contact" { + default = "" +} + +variable "secret" { + default = "" +} +