Last active
January 20, 2018 21:42
-
-
Save pclose/9f6470f77529681d2f50325cc0c427a0 to your computer and use it in GitHub Desktop.
This script echos out a bunch of commands to set up ggtracker on a Debian box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # This script echos out a bunch of commands to set up ggtracker on a Debian box | |
| # probably have to run this from a linux box of some kind :-/ -pete 2017-11-12 | |
| host=${1:-"localhost"} | |
| key=${2:-"key"} # AWS key | |
| sec=${3:-"sec"} # AWS secret | |
| bucket_prefix=${4:-"local"} | |
| user=${5:-"gguser"} # User on Debian box | |
| pass=${6:-$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)} | |
| recaptcha_secret=${7:-"AAAA"} | |
| replays_bucket="${bucket_prefix}-gg-replays" | |
| matchblobs_bucket="${bucket_prefix}-gg-matchblobs" | |
| minimaps_bucket="${bucket_prefix}-gg-minimaps" | |
| s2gs_bucket="${bucket_prefix}-gg-s2gs" | |
| port="3000" | |
| proto="http://" | |
| origin=${proto}${host} | |
| aws_ssh_key="your-key" | |
| email_from="gg@$host" | |
| smtp_addr="smtp.postmarkapp.com" | |
| smtp_domain="$host" | |
| #:${port} | |
| if [[ $2 != "print-ec2-json" ]]; then | |
| cat - <<END | |
| # If you want to run it on AWS run this to create your box first | |
| #aws cloudformation create-stack --stack-name glhftracker \ | |
| --parameters '[{"ParameterKey":"KeyName","ParameterValue":"$aws_ssh_key"}]' \ | |
| --template-body '\$($0 "" print-ec2-json)' | |
| #aws ec2 describe-instances | grep PublicIpAdd | |
| # SSH key | |
| cat ~/.ssh/id_rsa.pub | ssh $user@$host 'mkdir -p ~/.ssh && cat - > .ssh/authorized_keys && chmod 0600 .ssh/authorized_keys' | |
| # Create user | |
| useradd -m $user -s /bin/bash | |
| passwd gguser << EOF | |
| $pass | |
| $pass | |
| EOF | |
| # Install dependencies | |
| apt-get install -y build-essential mysql-server redis-server imagemagick memcached python-pip \ | |
| libcurl3 libcurl3-gnutls libcurl4-openssl-dev libxslt-dev libxml2-dev libmemcached-dev \ | |
| libmysql++-dev apache2 | |
| # Might be dependencies if your running from the Debian AWS instance (wtf?) | |
| apt-get install curl screen less git | |
| # Setup node | |
| curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - | |
| apt-get update | |
| apt-get install -y nodejs | |
| # Setup rvm and Ruby | |
| gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB | |
| curl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer | |
| curl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc | |
| gpg --verify rvm-installer.asc && bash rvm-installer stable | |
| echo "source /etc/profile.d/rvm.sh" >> /etc/bash.bashrc | |
| source /etc/profile.d/rvm.sh | |
| rvm install "ruby-1.9.3-p551" | |
| gem install bundle | |
| # Config mysql | |
| sed -i s/utf8mb4/utf8/ /etc/mysql/mariadb.conf.d/* | |
| /etc/init.d/mysql restart | |
| printf "create user '$user'@'localhost';" | mysql | |
| printf "grant all on *.* to '$user'@'localhost';" | mysql | |
| # Configure Apache | |
| cat - <<HERE > /etc/apache2/sites-available/ggtracker.conf | |
| <VirtualHost *:80> | |
| <Location "/esdb"> | |
| ProxyPass "balancer://juggernaut/" | |
| </Location> | |
| <Location "/socket"> | |
| ProxyPass "balancer://juggernaut/" | |
| </Location> | |
| <LocationMatch "^.*"> | |
| ProxyPass "balancer://ggtracker/" | |
| </LocationMatch> | |
| <Proxy "balancer://ggtracker"> | |
| BalancerMember "http://localhost:3000/" | |
| </Proxy> | |
| <Proxy "balancer://juggernaut"> | |
| BalancerMember "http://localhost:5300/" | |
| </Proxy> | |
| <Proxy "balancer://esdb"> | |
| BalancerMember "http://localhost:9292/" | |
| </Proxy> | |
| </VirtualHost> | |
| HERE | |
| a2dissite 000-default.conf | |
| a2ensite ggtracker.conf | |
| a2enmod proxy proxy_http proxy_balancer lbmethod_byrequests proxy_wstunnel lbmethod_bytraffic | |
| systemctl reload apache2.service | |
| # Switch to non-root user | |
| ssh $user@$host | |
| # Setup nvm and node | |
| curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | bash | |
| export NVM_DIR="\$HOME/.nvm" | |
| [ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh" # This loads nvm | |
| nvm install 6.0 | |
| # Pull down ggtracker and esdb | |
| git clone https://github.com/dsjoerg/ggtracker | |
| git clone https://github.com/dsjoerg/esdb | |
| # Run these as root, or add sudo access to $user beforehand | |
| cd /home/$user/ggtracker && bundle | |
| cd /home/$user/esdb && bundle | |
| # Config ggtracker | |
| cd ~/ggtracker | |
| sed 's|socket: /tmp/mysql.sock|socket: /var/run/mysqld/mysqld.sock|' config/database.yml.example > config/database.yml | |
| sed -i 's|username: root|username: $user|' config/database.yml | |
| sed 's|host: 127.0.0.1:9292|host: $host:9292|' config/secrets.yml.example > config/secrets.yml | |
| sed -i '/wcs_cobrand/i\ \ recaptcha_secret: $recaptcha_secret' secrets.yml | |
| sed 's|access_key_id: YOUR_ACCESS_KEY|access_key_id: $key|' config/s3.yml.example > config/s3.yml | |
| sed -i 's|secret_access_key: YOUR_SECRET_ACCESS_KEY|secret_access_key: $sec|' config/s3.yml | |
| sed -i 's|gg2-replays.*|$replays_bucket|' config/s3.yml | |
| sed -i 's|gg2-matchblobs.*|$matchblobs_bucket|' config/s3.yml | |
| sed -i 's|gg2-minimaps.*|$minimaps_bucket|' config/s3.yml | |
| sed -i 's|gg2-s2gs.*|$s2gs_bucket|' config/s3.yml | |
| sed -i 's/dsjoerg@ggtracker.com/$email_from/' app/mailers/* | |
| sed -i 's|smtp.postmarkapp.com|$smtp_addr|' config/application.rb | |
| sed -i 's|ggtracker.com|$smtp_addr|' config/application.rb | |
| rake db:create | |
| bundle exec rake db:schema:load | |
| # Config esdb | |
| cd ~/esdb | |
| printf "create database esdb_development; create database esdb_test;" | mysql -u$user | |
| sed 's|socket: /tmp/mysql.sock|socket: /var/run/msudo ysqld/mysqld.sock|' config/database.yml.example > config/database.yml | |
| sed -i 's|username: root|username: $user|' config/database.yml | |
| sed 's|access_key_id: YOUR_AWS_ACCESS_KEY_ID|access_key_id: $key|' config/s3.yml.example > config/s3.yml | |
| sed -i 's|secret_access_key: YOUR_AWS_SECRET_ACCESS_KEY|secret_access_key: $sec|' config/s3.yml | |
| sed -i 's|gg2-replays.*|$replays_bucket|' config/s3.yml | |
| sed -i 's|gg2-matchblobs.*|$matchblobs_bucket|' config/s3.yml | |
| sed -i 's|gg2-minimaps.*|$minimaps_bucket|' config/s3.yml | |
| sed -i 's|gg2-s2gs.*|$s2gs_bucket|' config/s3.yml | |
| sed 's|access_key_id: YOUR_AWS_KEY|access_key_id: $key|' config/fog.rb.example > config/fog.rb | |
| sed -i 's|secret_access_key: YOUR_AWS_SECRET_ACCESS_KEY|secret_access_key: $sec|' config/fog.rb | |
| cp config/redis.yml.example config/redis.yml | |
| cp config/esdb.yml.example config/esdb.yml | |
| cp config/tokens.yml.example config/tokens.yml | |
| bundle exec sequel -m db/migrations -e development config/database.yml | |
| cat db/league_tables.sql | mysql -u$user -Desdb_development | |
| cat db/ggtracker_provider.sql | mysql -u$user -Desdb_development | |
| # Run as root | |
| cd /home/$user/esdb/ && rake py:init | |
| npm install -g juggernaut | |
| sed -i s/path.existsSync/fs.existsSync/g /usr/lib/node_modules/juggernaut/lib/juggernaut/server.js | |
| # Create S3 buckets | |
| aws s3api create-bucket --acl public-read --bucket $replays_bucket | |
| aws s3api create-bucket --acl public-read --bucket $matchblobs_bucket | |
| aws s3api create-bucket --acl public-read --bucket $minimaps_bucket | |
| aws s3api create-bucket --acl public-read --bucket $s2gs_bucket | |
| # Configure S3 buckets | |
| aws s3api put-bucket-policy --bucket $replays_bucket --policy '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::$replays_bucket/*"}]}' | |
| aws s3api put-bucket-policy --bucket $matchblobs_bucket --policy '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::$matchblobs_bucket/*"}]}' | |
| aws s3api put-bucket-policy --bucket $minimaps_bucket --policy '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::$minimaps_bucket/*"}]}' | |
| aws s3api put-bucket-policy --bucket $s2gs_bucket --policy '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::$s2gs_bucket/*"}]}' | |
| aws s3api put-bucket-cors --bucket $replays_bucket --cors-configuration '{"CORSRules":[{"AllowedHeaders":["Authorization"],"MaxAgeSeconds":3000,"AllowedMethods":["GET"],"AllowedOrigins":["$origin"]},{"AllowedHeaders":["Authorization"],"MaxAgeSeconds":3000,"AllowedMethods":["POST"],"AllowedOrigins":["$origin"]},{"AllowedHeaders":["Authorization"],"MaxAgeSeconds":3000,"AllowedMethods":["PUT"],"AllowedOrigins":["$origin"]}]}' | |
| # Remove the buckets | |
| #aws s3 rb s3://$replays_bucket --force | |
| #aws s3 rb s3://$matchblobs_bucket --force | |
| #aws s3 rb s3://$minimaps_bucket --force | |
| #aws s3 rb s3://$s2gs_bucket --force | |
| # Open up the site | |
| $origin | |
| END | |
| else | |
| # This prints out the cloudformation template for a Debian box in the us-east-1 region | |
| # https://wiki.debian.org/Cloud/AmazonEC2Image/Stretch | |
| cat - <<END | |
| { | |
| "AWSTemplateFormatVersion" : "2010-09-09", | |
| "Description" : "Cloud formation template that deploys a debian ec2 instance", | |
| "Parameters" : { | |
| "KeyName": { | |
| "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances", | |
| "Type": "String", | |
| "MinLength": "1", | |
| "MaxLength": "255", | |
| "AllowedPattern" : "[\\x20-\\x7E]*", | |
| "ConstraintDescription" : "can contain only ASCII characters." | |
| } | |
| }, | |
| "Resources" : { | |
| "Ggtracker": { | |
| "Type": "AWS::EC2::Instance", | |
| "Properties": { | |
| "ImageId" : "ami-add0abba", | |
| "InstanceType" : "t2.small", | |
| "SecurityGroups" : [{"Ref":"ggtracker"}], | |
| "KeyName" : {"Ref":"KeyName" } | |
| } | |
| }, | |
| "ggtracker" : { | |
| "Type" : "AWS::EC2::SecurityGroup", | |
| "Properties" : { | |
| "GroupDescription" : "GG tracker ports", | |
| "SecurityGroupIngress" : [ | |
| {"IpProtocol" : "tcp", "FromPort" : "3000", "ToPort" : "3000", "CidrIp" : "0.0.0.0/0"}, | |
| {"IpProtocol" : "tcp", "FromPort" : "5300", "ToPort" : "5300", "CidrIp" : "0.0.0.0/0"}, | |
| {"IpProtocol" : "tcp", "FromPort" : "9292", "ToPort" : "9292", "CidrIp" : "0.0.0.0/0"}, | |
| {"IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0"} | |
| ] | |
| } | |
| } | |
| } | |
| } | |
| END | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment