module Meroku module Infrastructure class Node include Meroku::Aws attr_accessor :instance, :tunnel def initialize result = ec2_client.try(:run_instances,Meroku::Aws::EC2_PARAMS) @instance = result.instances.first if result @tunnel = Meroku::Tunnel.new( ip: "34.239.241.218", username: "ubuntu", keys: "~/crypto/meroku/meroku.id_rsa", verify_host_key: false, verbose: false ) self end def associate_address retries ||= 0 return self if !ec2_client ec2_client.associate_address( allocation_id: "eipalloc-139f7823", instance_id: @instance.try(:instance_id) ) self rescue ::Aws::EC2::Errors::InvalidInstanceID => e print STDERR.print "." sleep 2 retry if (retries += 1) < 15 end def install_packages @tunnel.run "sudo apt-add-repository ppa:brightbox/ruby-ng\;" @tunnel.run "curl -s -o /tmp/modified-cedar-14.sh https://raw.githubusercontent.com/oystersauce8/meroku/master/modified-cedar-14.sh" @tunnel.run "sudo chmod 755 /tmp/modified-cedar-14.sh" @tunnel.run "/bin/bash -lc 'sudo /tmp/modified-cedar-14.sh'" #@tunnel.run "sudo apt-get update\;" @tunnel.run "sudo apt-get install -y ruby2.4 ruby2.4-dev" @tunnel.run "sudo apt-get install -y nginx libsqlite3-dev nodejs" self end def install_frontend_app @tunnel.run 'mkdir /home/ubuntu/.meroku' @tunnel.run "cd ~\; git clone https://github.com/oystersauce8/meroku\;" @tunnel.run "sudo cp ~/meroku/frontend/etc_nginx_sites-available_default /etc/nginx/sites-available/default" @tunnel.run "curl -o /home/ubuntu/.meroku/letsencrypt_fullchain.pem http://www.sam-we.com/dropbox/meroku-#{ENV['SECRET']}/letsencrypt_fullchain.pem" @tunnel.run "curl -o /home/ubuntu/.meroku/letsencrypt_privkey.pem http://www.sam-we.com/dropbox/meroku-#{ENV['SECRET']}/letsencrypt_privkey.pem" @tunnel.run "cd ~/.meroku/\; curl -O http://www.sam-we.com/dropbox/meroku-#{ENV['SECRET']}/ssh_host_keys.tgz" #@tunnel.run "cd ~/.meroku/\; sudo tar xf ssh_host_keys.tgz -C /etc/ssh/ --overwrite" @tunnel.run "cd ~/meroku/frontend/\; sudo gem install bundler\; bundle\;" @tunnel.run "(cd ~/meroku/frontend && RAILS_ENV=production bundle exec rails assets:precompile)" @tunnel.run "(cd ~/meroku/frontend && RAILS_ENV=production bundle exec rake db:migrate)" @tunnel.run "cd ~/meroku/frontend/\; bundle exec puma -d" @tunnel.run "sudo service nginx restart" @tunnel.run "sudo service ssh restart" self end end end end