Created
March 6, 2025 19:58
-
-
Save bankair/5907c2ef40331c633665652b263a3e91 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
require 'sshkit' | |
require 'sshkit/dsl' | |
require 'sshkit/sudo' | |
include SSHKit::DSL | |
DOKKU_VERSION = 'v0.35.15' | |
HOST = ENV.fetch('DOKKU_HOST') | |
SUDO_PWD = ENV.fetch('SUDO_PWD', nil) | |
if SUDO_PWD.nil? | |
puts 'Missing SUDO_PWD env var.' | |
else | |
SSHKit::Command.prepend(Module.new do | |
def on_stdout(channel, data) | |
if data.include? '[sudo] password for' | |
channel.send_data("#{SUDO_PWD}\n") | |
else | |
super | |
end | |
end | |
end) | |
end | |
def log(...) | |
SSHKit.config.output.log(...) | |
end | |
def add_ssk_key(authorized_key, index) | |
name = "admin#{index}" | |
path = "authorized_keys_#{name}" | |
execute "echo '#{authorized_key}' > #{path}" | |
sudo :dokku, 'ssh-keys:remove', name | |
sudo :dokku, 'ssh-keys:add', name, path | |
ensure | |
execute :rm, '-f', path | |
end | |
def install(binary, test_cmd: "which #{binary}") | |
raise 'Missing block' unless block_given? | |
result = test(test_cmd) | |
if result | |
log "#{binary} is installed, skipping." | |
else | |
log "Installing #{binary}." | |
yield | |
end | |
end | |
on HOST, in: :sequence do |host| | |
log "Now executing on #{host}" | |
sudo :apt, 'update' | |
sudo :apt, 'upgrade', '-y' | |
install('wget') { sudo :apt, *%w[install wget -y] } | |
install 'dokku' do | |
execute :wget, *%W[-NP . https://dokku.com/install/#{DOKKU_VERSION}/bootstrap.sh] | |
with DOKKU_TAG: DOKKU_VERSION do | |
sudo :bash, 'bootstrap.sh' | |
end | |
end | |
authorized_keys = | |
capture(:cat, '~/.ssh/authorized_keys') | |
.strip | |
.split("\n") | |
authorized_keys.each_with_index do |authorized_key, index| | |
add_ssk_key(authorized_key, index) | |
end | |
global_domains_report = capture("echo #{SUDO_PWD} | sudo -S dokku domains:report --global") | |
if global_domains_report.include? HOST | |
log "Domain #{HOST} is already registered." | |
else | |
log "Registering domain #{HOST}." | |
execute("echo #{SUDO_PWD} | sudo -S dokku domains:set-global #{HOST}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment