module Meroku module Infrastructure class Node attr_accessor :instance, :tunnel, :ec2_client def initialize(ec2_client) @ec2_client = ec2_client result = ec2_client.try( :run_instances, { image_id: 'ami-841f46ff', #was xenial 'ami-cd0f5cb6', min_count: 1, max_count: 1, key_name: 'meroku.id_rsa', instance_type: 't2.micro', tag_specifications: [ { resource_type: "instance", tags: [ { key: "Name", value: "node", }, ], }, ] } ) @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 @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 tweak_configuration @tunnel.run 'sudo adduser --disabled-password --gecos "" git' @tunnel.run 'sudo -u git mkdir /home/git/.ssh/' @tunnel.run 'sudo -u git touch /home/git/.ssh/authorized_keys' 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