#!/usr/bin/env ruby require "sinatra" require "open3" if ARGV[0] == "onboot" File.open("/etc/init/dokku-installer.conf", "w") do |f| f.puts "start on runlevel [2345]" f.puts "exec #{File.absolute_path(__FILE__)} selfdestruct" end File.open("/etc/nginx/conf.d/dokku-installer.conf", "w") do |f| f.puts "upstream dokku-installer { server 127.0.0.1:2000; }" f.puts "server {" f.puts " listen 80;" f.puts " location / {" f.puts " proxy_pass http://dokku-installer;" f.puts " }" f.puts "}" end `rm -f /etc/nginx/sites-enabled/*` puts "Installed Upstart service and default Nginx virtualhost for installer to run on boot." exit end version = "v0.3.20" dokku_root = ENV["DOKKU_ROOT"] || "/home/dokku" admin_key = `cat /root/.ssh/authorized_keys`.split("\n").first hostname = `bash -c '[[ $(dig +short $HOSTNAME) ]] && echo $HOSTNAME || curl icanhazip.com'`.strip template = DATA.read set :port, 2000 set :environment, :production disable :protection get "/" do ERB.new(template).result binding end post "/setup" do if params[:vhost] == "true" File.open("#{dokku_root}/VHOST", "w") {|f| f.write params[:hostname] } else `rm #{dokku_root}/VHOST` end File.open("#{dokku_root}/HOSTNAME", "w") {|f| f.write params[:hostname] } Open3.capture2("sshcommand acl-add dokku admin", :stdin_data => params[:key]) Thread.new { `rm /etc/nginx/conf.d/dokku-installer.conf && /etc/init.d/nginx stop && /etc/init.d/nginx start` `rm /etc/init/dokku-installer.conf && stop dokku-installer` }.run if ARGV[0] == "selfdestruct" end __END__