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 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 --shell /usr/bin/git-shell --gecos "" git' #@tunnel.run 'sudo -u git mkdir /home/git/.ssh/' #@tunnel.run 'sudo -u git touch /home/git/.ssh/authorized_keys' @tunnel.run 'sudo mkdir /sharedro && sudo chmod 777 /sharedro' end def install_frontend_app @tunnel.run 'mkdir /home/ubuntu/.meroku' @tunnel.run "cd ~\; git clone https://github.com/oystersauce8/meroku\;" @tunnel.run "sudo rm -f /etc/nginx/sites-enabled/*" @tunnel.run "sudo cp ~/meroku/frontend/etc_nginx_sites-enabled_default /etc/nginx/sites-enabled/default" @tunnel.run 'sudo cp /home/ubuntu/meroku/frontend/etc_nginx_sites-enabled_template /sharedro' @tunnel.run 'sudo chmod a+r /sharedro/etc_nginx_sites-enabled_template' @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/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 -b unix:///tmp/meroku.sock" @tunnel.run "sudo service nginx restart" @tunnel.run %Q[sudo sh -c 'echo "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIPWJQU+TkDu9uyVzHQcVnKklRhQvIBdXHkA/7zAQG8XuoAoGCCqGSM49\nAwEHoUQDQgAEI1lm18nECH4jH+6p80jhn8WgZRDOC1ufVVtoPUnUgEvslfV3xzWl\nDXZKof765EiCOYyt2TZ7pKClMexhHWhMtA==\n-----END EC PRIVATE KEY-----" > /etc/ssh/ssh_host_ecdsa_key'] @tunnel.run %Q[sudo sh -c 'echo "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCNZZtfJxAh+Ix/uqfNI4Z/FoGUQzgtbn1VbaD1J1IBL7JX1d8c1pQ12SqH++uRIgjmMrdk2e6SgpTHsYR1oTLQ= root@ip-172-31-64-77" > /etc/ssh/ssh_host_ecdsa_key.pub'] #@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 && sudo service ssh restart" self rescue byebug end end end end