Skip to content

Instantly share code, notes, and snippets.

@angryTit
Created February 20, 2019 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angryTit/6fca3b1826e12e335754277aac87adb1 to your computer and use it in GitHub Desktop.
Save angryTit/6fca3b1826e12e335754277aac87adb1 to your computer and use it in GitHub Desktop.
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash\n",
"#\n",
"# This script starts at the launch of a VM, and handles final cluster coordination.\n",
"LOGFILE=/home/ubuntu/setup.log\n",
"echo `date` 'Preparing Causal Cluster' | tee -a $LOGFILE\n",
"\n",
"/bin/systemctl stop neo4j.service 2>&1 | tee -a $LOGFILE\n",
"mkdir neo4jjars\n",
"aws s3 cp s3://prod-company-neo4j-jars/graphaware-server-community-all-3.5.2.jar neo4jjars/graphaware-server-community-all-3.5.2.jar\n",
"aws s3 cp s3://prod-company-neo4j-jars/relationships-1.0-SNAPSHOT.jar neo4jjars/relationships-1.0-SNAPSHOT.jar\n",
"cp neo4jjars/graphaware-server-community-all-3.5.2.jar /var/lib/neo4j/plugins/graphaware-server-community-all-3.5.2.jar\n",
"cp neo4jjars/relationships-1.0-SNAPSHOT.jar /var/lib/neo4j/plugins/relationships-1.0-SNAPSHOT.jar\n",
"chown neo4j:neo4j /var/lib/neo4j/plugins/graphaware-server-community-all-3.5.2.jar\n",
"chown neo4j:neo4j /var/lib/neo4j/plugins/relationships-1.0-SNAPSHOT.jar\n",
"echo dbms.unmanaged_extension_classes=com.graphaware.server=/graphaware >> /etc/neo4j/neo4j.template\n",
"export API=http://169.254.169.254/latest/\n",
"export EC2_AVAIL_ZONE=$(curl --silent $API/meta-data/placement/availability-zone)\n",
"export EC2_INSTANCE_ID=$(curl -s $API/meta-data/instance-id)\n",
"export EC2_REGION=$(curl -s $API/dynamic/instance-identity/document | jq -r .region)\n",
"export ROOT_DISK_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=${EC2_INSTANCE_ID} Name=attachment.device,Values=/dev/sda1 --query 'Volumes[*].[VolumeId]' --region=${EC2_REGION} --out text | cut -f 1)\n",
"export DATA_DISK_ID=$(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=${EC2_INSTANCE_ID} Name=attachment.device,Values=/dev/sdb --query 'Volumes[*].[VolumeId]' --region=${EC2_REGION} --out text | cut -f 1)\n",
"env | tee -a $LOGFILE\n",
"# Tag volumes, which CloudFormation does not allow\n",
"# Root volume: /dev/sda, data volume /dev/sdb\n",
"aws ec2 create-tags --resources $ROOT_DISK_ID --tags Key=Name,Value=\"Root Neo4j Vol for $EC2_INSTANCE_ID\" --region ${EC2_REGION} 2>&1 | tee -a $LOGFILE\n",
"aws ec2 create-tags --resources $DATA_DISK_ID --tags Key=Name,Value=\"Neo4j Data Vol for $EC2_INSTANCE_ID\" --region ${EC2_REGION} 2>&1 | tee -a $LOGFILE\n",
"# Format EBS storage, and mount it in Neo4j directory\n",
"echo `date` 'Preparing neo4j volume...' | tee -a $LOGFILE\n",
"mkfs -t ext4 /dev/xvdb 2>&1 | tee -a $LOGFILE\n",
"mkdir /tmpmount 2>&1 | tee -a $LOGFILE\n",
"mount /dev/xvdb /tmpmount/ 2>&1 | tee -a $LOGFILE\n",
"cp --preserve=all -r /var/lib/neo4j/* /tmpmount/ 2>&1 | tee -a $LOGFILE\n",
"umount /tmpmount 2>&1 | tee -a $LOGFILE\n",
"rm -rf /tmpmount 2>&1 | tee -a $LOGFILE\n",
"echo `date` 'Remounting new volume in place...' | tee -a $LOGFILE\n",
"mount /dev/xvdb /var/lib/neo4j 2>&1 | tee -a $LOGFILE\n",
"FSTAB_ENTRY='/dev/xvdb /var/lib/neo4j ext4 defaults,discard 0 2'\n",
"echo $FSTAB_ENTRY >> /etc/fstab\n",
"mount -a 2>&1 | tee -a $LOGFILE\n",
"echo `date` 'Preparing neo4j service...' | tee -a $LOGFILE\n",
"/bin/rm -rf /var/lib/neo4j/data/databases/graph.db/ 2>&1 | tee -a $LOGFILE\n",
"/bin/systemctl start neo4j.service 2>&1 | tee -a $LOGFILE\n",
"\n",
"sudo apt-get update\n",
"mkdir aws-cfn-bootstrap-latest\n",
"curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1\n",
"easy_install aws-cfn-bootstrap-latest\n",
"\n",
"echo Stack ID '",
{
"Ref": "AWS::StackId"
},
"' | tee -a $LOGFILE\n",
"export STACK_TOKEN=$(echo '",
{
"Ref": "AWS::StackId"
},
"' | base64 | tail -c 12)\n",
"# Loop waiting for neo4j service to start.\n",
"while true; do\n",
" if curl -s -I http://localhost:7474 | grep '200 OK'; then\n",
" echo `date` 'Neo4j is up; changing default password' 2>&1 | tee -a $LOGFILE\n",
"\n",
" curl -v -H 'Content-Type: application/json' \\n",
" -XPOST -d '{\"password\":\"",
{
"Ref": "Password"
},
"\"}' \\\n",
" -u neo4j:neo4j \\\n",
" http://localhost:7474/user/neo4j/password \\\n",
" 2>&1 | tee -a $LOGFILE\n",
" echo `date` 'Password reset; a graph user is you!' 2>&1 | tee -a $LOGFILE\n",
"\n",
" echo `date` 'Startup complete ' | tee -a $LOGFILE\n",
" break\n",
" fi\n",
"\n",
" echo `date` 'Waiting for neo4j to come up' 2>&1 | tee -a $LOGFILE\n",
" sleep 1\n",
"done\n",
"\n",
"echo Signaling stack success | tee -a $LOGFILE\n",
"/usr/local/bin/cfn-signal --stack ",
{
"Ref": "AWS::StackName"
},
" \\\n",
" --id $EC2_INSTANCE_ID \\\n",
" --region ",
{
"Ref": "AWS::Region"
},
" \\\n",
" --success true -d \"$STACK_TOKEN\" '",
{
"Ref": "StackTokenWaitHandle"
},
"' 2>&1 | tee -a $LOGFILE \n"
]
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment