Module: Mimas::Deployment::RubyApp

Included in:
Mimas::Deployment
Defined in:
lib/mimas/deployment/ruby_app.rb

Instance Method Summary collapse

Instance Method Details

#deploy_ruby_appObject



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mimas/deployment/ruby_app.rb', line 4

def deploy_ruby_app
  say "Starting deployment for #{site.domain}"

  @archive_path = site.archive(filename: archive_name)

  ssh(server) do |session|
    validate_server_ruby_version(session)

    create_directory_skeleton(session)

    expire_old_releases(session)

    # Check `current` symlink to get currently live version
    remote_current_path = session.run("readlink #{current_path}", capture: true, silent: true).chomp
    remote_current_version = Pathname.new(remote_current_path).basename.to_s.chomp

    say "Current live version is #{remote_current_version}" if remote_current_version.size > 0

    refreshing_caddy(session) do
      # Upload the mimas.env and touch an `.env` file in the site's root
      session.run "cp #{mimas_path.join("mimas.env")} #{mimas_path.join("mimas.env.backup")}"
      session.upload StringIO.new(mimas_env), mimas_path.join("mimas.env")
      session.run "touch #{site_root.join(".env")}"

      say "Uploading version: #{version}"
      session.upload archive_path, "#{remote_destination}/"

      say "Extracting version: #{version}"
      session.run "cd #{remote_destination} && tar -xf #{archive_name} && rm #{archive_name}", capture: true

      say "Running deploy script"
      session.upload StringIO.new(site.deploy), deploy_script_path
      session.run "cd #{remote_destination} && /usr/bin/env bash -l #{deploy_script}"
      session.run "rm #{deploy_script_path}"

      say "Setting current symlink"
      session.run "ln -sfn #{remote_destination} #{current_path}"
      say "Current symlink set"

      site.services.each do |_, service|
        say "Installing service: #{service[:name]}"
        systemd_service_name = systemd_service_name(service)
        system_contents = systemd(service)

        session.upload StringIO.new(system_contents), server.user_systemd_path.join("#{systemd_service_name}.service")
        session.run "systemctl --user enable --now #{systemd_service_name}", capture: true
        say "Started #{service[:name]} service using as #{systemd_service_name}"

        begin
          say "Health checking new service"
          session.run "curl --max-time 1 --silent --fail --unix-socket #{unix_socket} http://localhost#{site.healthcheck_path}", capture: true
          say "Health check succeeded"
        rescue SSH::Session::NonZeroExitCode => e
          say "Health check failed".red
          say e.to_s.red.bold
          raise e
        end
      end
    rescue Exception => e
      say "Error raised during deploy: #{e}".red.bold

      revert_files_after_failed_deployment(session, remote_current_path: remote_current_path)
      exit 1
    end


    cleanup_successful_deployment(session, remote_current_version: remote_current_version)
    say "\n"
    say "Deployment complete! #{version} is now live.".bold.white
  end
end