Created
August 22, 2018 18:01
-
-
Save yosukehasumi/b1e8c0825d970a4bdaf33469bf81920e to your computer and use it in GitHub Desktop.
Digital Ocean Proxy server generation for ngrok
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
| #!/usr/bin/env ruby | |
| require 'awesome_print' | |
| require 'json' | |
| PROXY_NAME = 'proxy' | |
| ACCESS_TOKEN = 'XXXXXXXX' | |
| # DESTROY | |
| def destroy_all_servers | |
| proxy_servers.each do |droplet| | |
| puts "destroying droplet: #{droplet['id']}" | |
| `doctl compute droplet delete #{droplet['id']} -f --access-token #{ACCESS_TOKEN}` | |
| end | |
| end | |
| # CREATE | |
| def create_proxy_server | |
| puts "creating proxy server: #{PROXY_NAME}" | |
| `doctl compute droplet create #{PROXY_NAME} --region tor1 --image 37438457 --size s-1vcpu-1gb --ssh-keys 1546173 --access-token #{ACCESS_TOKEN}` | |
| end | |
| def add_ssh_known_host | |
| `ssh-keyscan -H #{ip_address} >> ~/.ssh/known_hosts` | |
| end | |
| def install_doctl | |
| puts "installing doctl" | |
| `ssh webhook@#{ip_address} 'sudo snap install doctl'` | |
| end | |
| def setup_self_destruct | |
| puts "setting up self destruct for 24 hours" | |
| self_destruct_string = "doctl compute droplet delete #{proxy_server['id']} -f --access-token #{ACCESS_TOKEN}" | |
| `ssh webhook@#{ip_address} 'echo "#{self_destruct_string}" | sudo tee /etc/self_destruct'` | |
| `ssh webhook@#{ip_address} '(echo "0 3 * * * /etc/self_destruct") | sudo crontab -'` | |
| end | |
| # DNS | |
| def domain_records_list | |
| JSON.parse `doctl compute domain records list do.mediumraredev.com -o json --access-token #{ACCESS_TOKEN}` | |
| end | |
| def proxy_domain_records | |
| domain_records_list.select { |record| record['name'] == PROXY_NAME } | |
| end | |
| def remove_old_proxy_records | |
| proxy_domain_records.each do |record| | |
| puts "deleting dns record: #{record['id']}" | |
| `doctl compute domain records delete do.mediumraredev.com #{record['id']} -f --access-token #{ACCESS_TOKEN}` | |
| end | |
| end | |
| def point_dns | |
| puts "point the DNS to the proxy server: #{ip_address}" | |
| `doctl compute domain records create --record-type A --record-name #{PROXY_NAME} --record-data #{ip_address} do.mediumraredev.com --access-token #{ACCESS_TOKEN}` | |
| end | |
| # NGROK | |
| def ngrok_tunnels | |
| JSON.parse(`curl --silent --show-error http://127.0.0.1:4040/api/tunnels`) | |
| end | |
| def ngrok_hostname | |
| ngrok_tunnels['tunnels'].first['public_url'] | |
| end | |
| def nginx_config | |
| "server { listen 80 default_server; listen [::]:80 default_server; location / { proxy_pass #{ngrok_hostname}; } }" | |
| end | |
| def update_nginx_config | |
| puts "updating nginx_config" | |
| `ssh webhook@#{ip_address} \'echo \"#{nginx_config}\" | sudo tee /etc/nginx/sites-available/proxy\'` | |
| end | |
| def restart_nginx | |
| puts "restarting #{PROXY_NAME}.do.mediumraredev.com" | |
| `ssh webhook@#{ip_address} 'sudo service nginx restart'` | |
| end | |
| # GENERAL | |
| def droplets_list | |
| JSON.parse `doctl compute droplet list -o json --access-token #{ACCESS_TOKEN}` | |
| end | |
| def proxy_servers | |
| droplets_list.select { |droplet| droplet['name'] == PROXY_NAME } | |
| end | |
| def proxy_server | |
| proxy_servers.first | |
| end | |
| def ip_address | |
| proxy_server['networks']['v4'].first['ip_address'] | |
| end | |
| destroy_all_servers | |
| create_proxy_server | |
| loop do | |
| puts 'waiting for server' | |
| sleep(1) | |
| unless `curl -Is #{ip_address} 80 | head -1`.empty? | |
| puts 'server ready!' | |
| break | |
| end | |
| end | |
| add_ssh_known_host | |
| install_doctl | |
| setup_self_destruct | |
| remove_old_proxy_records | |
| point_dns | |
| update_nginx_config | |
| restart_nginx | |
| gets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment