labnotes

Playing with SSHKit

Deployment testing

tags
sshkit
ruby
1
2
  bundle init
  bundle add sshkit dotenv

Which should yield something like this:

1
2
3
4
source "https://rubygems.org"

gem "sshkit", "~> 1.21"
gem "dotenv", "~> 2.8"

Set the REMOTE_SERVER in your .env file, in my case:

1
REMOTE_SERVER=root@registry.willschenk.com

Server connect test

connect_test.rb:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  require 'dotenv/load'
  require 'sshkit'
  require 'sshkit/dsl'
  include SSHKit::DSL

  host = ENV['REMOTE_SERVER']

  on( host ) do
    puts capture( 'hostname' )
    puts capture( 'pwd' )
  end

Install docker

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  require 'dotenv/load'
  require 'sshkit'
  require 'sshkit/dsl'

  SSHKit.config.output_verbosity = Logger::DEBUG

  class Docker
    include SSHKit::DSL

    def affirm
      on( ENV['REMOTE_SERVER'] ) do |host|
        puts "Affirming docker is installed on #{host}"
        unless test( "docker -v" )
          execute( "curl -fsSL https://get.docker.com | sh" )
        end
      end
    end
  end

  if __FILE__ == $0
    docker_installed = Docker.new.affirm

    puts "Docker Installed: #{docker_installed}"
  end

Packages

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  require 'dotenv/load'
  require 'sshkit'
  require 'sshkit/dsl'

  #SSHKit.config.output_verbosity = Logger::DEBUG

  class AptPackages
    include SSHKit::DSL

    def affirm( *package_list )
      on( ENV['REMOTE_SERVER'] ) do |host|
        install_list = []
        package_list.each do |p|
          puts "Affirming #{p} is installed on #{host}"
          unless test( "which #{p}" )
            puts "#{p} not found"
            install_list << p
          end
        end

        if install_list.length > 0
          execute( "apt-get update" )
          execute( "apt-get install -y #{install_list.join( " " )}")
        end
      end
    end
  end

  if __FILE__ == $0
    AptPackages.new.affirm( "jq", "jo", "zsh", "git" )
  end

Creating a user

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  require 'dotenv/load'
  require 'sshkit'
  require 'sshkit/dsl'

  #SSHKit.config.output_verbosity = Logger::DEBUG

  class SystemAccount
    include SSHKit::DSL

    def affirm( user, shell )
      on( ENV['REMOTE_SERVER'] ) do |host|
        puts capture( "which #{shell}" )
      end
    end
  end

  if __FILE__ == $0
    SystemAccount.new.affirm( ENV['USER'], 'bash' )
    SystemAccount.new.affirm( 'git', 'git-shell' )
  end

Copying files over

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  require 'dotenv/load'
  require 'sshkit'
  require 'sshkit/dsl'

  #SSHKit.config.output_verbosity = Logger::DEBUG

  class CopyFiles
    include SSHKit::DSL

    def affirm( *file_list )
      on( ENV['REMOTE_SERVER'] ) do |host|
        file_list.each do |f|
          upload! f, f
        end
      end
    end
  end

  if __FILE__ == $0
    CopyFiles.new.affirm( "caddy-compose.yml", "whoami-compose.yml" )
  end
1
2
3
  docker network create caddy
  docker compose -f caddy-compose.yml up -d
  docker compose -f whoami-compose.yml up -d

Previously

howto

Setting up emacs re-re-dux

What is more fun that configuring emacs?

tags
emacs

Next

labnotes

Controlling Hetzner with CLI

Simple wrapper scripts

tags
hetzner
cloud
cli