Skip to content

Instantly share code, notes, and snippets.

View crizstian's full-sized avatar

Cristian Ramirez crizstian

View GitHub Profile
#!/bin/bash
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
function createDockerVolume {
cmd=$(docker volume ls -q | grep $1)
if [[ "$cmd" == $1 ]];
then
echo 'volume available'
else
echo '·· Pre-Start executed >>>> ··'
bash /var/mongo/pre-start.sh
sleep 20
echo '·· Pre-Start done >>>> ··'
echo '·· Starting DB Service >>>> ··'
bash /var/mongo/start.sh
sleep 20
admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "{{ env "DB_ADMIN_USER" }}",
pwd: "{{ env "DB_ADMIN_PASS" }}",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
db.getSiblingDB("admin").auth("{{ env "DB_ADMIN_USER" }}", "{{ env "DB_ADMIN_PASS" }}" )
consul-template -template "/var/mongo/admin.js.ctmpl:/var/mongo/admin.js" -once
consul-template -template "/var/mongo/grantRole.js.ctmpl:/var/mongo/grantRole.js" -once
consul-template -template "/var/mongo/replica.js.ctmpl:/var/mongo/replica.js" -once
cat > /var/mongo/hosts.sh <<EOF
. /etc/environment
DBS=`aws ec2 describe-instances --filters "Name=tag:Name,Values=*Mongo*" --query 'Reservations[].Instances[].PrivateIpAddress' --region $AWS_DEFAULT_REGION`
DB1=`echo $DBS | jq .[0] | sed -e 's/"//g'`
DB2=`echo $DBS | jq .[1] | sed -e 's/"//g'`
DB3=`echo $DBS | jq .[2] | sed -e 's/"//g'`
DB_HOST=`echo ${hostname} | sed -e 's/ip-//'`
IP=`ip -f inet addr show eth0 | grep -Po 'inet \K[\d.]+'`
DB_PORT=27017
BOOKSTORE_DBUSER=bookstore
BOOKSTORE_DBPASS=bookstorepass1
CINEMA_DBUSER=cinemas
CINEMA_DBPASS=cinemaspass1
#!/bin/bash
# This script is meant to be run in the User Data of each EC2 Instance while it's booting.
set -e
# Send the log output from this script to user-data.log, syslog, and the console
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
echo "export DB_ADMIN_USER=${dbAdminUser}" >> /etc/environment
echo "export DB_ADMIN_PASS=${dbAdminUserPass}" >> /etc/environment
data "aws_availability_zones" "available_zones" {}
data "aws_ami" "db_image" {
most_recent = true
owners = ["${var.aws_account}"]
filter {
name = "name"
values = ["mongo*"]
}
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
terraform {
required_version = ">= 0.11.9"
}
variable "ami_id" {}
variable "num_of_instances" {}
variable "instance_type" {}
variable "subnet_id" {}
variable "ssh_key" {}
variable "volume_size" {}
variable "user_data" {}
variable "cluster_name" {}
variable "instance_name" {}
variable "associate_public_ip_address" {}