Class: Renuo::Cli::Commands::Debug

Inherits:
Object
  • Object
show all
Defined in:
lib/renuo/cli/commands/debug.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

Cache =
Renuo::Cli::Services::Cache
Deploio =
Renuo::Cli::Services::Deploio
Heroku =
Renuo::Cli::Services::Heroku
Hetzner =
Renuo::Cli::Services::Hetzner

Instance Method Summary collapse

Instance Method Details

#run(args, options) ⇒ Object

rubocop:todo Metrics/PerceivedComplexity rubocop:todo Metrics/MethodLength rubocop:todo Metrics/AbcSize



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/renuo/cli/commands/debug.rb', line 36

def run(args, options) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
  query = args[0]
  abort(">> Please provide an app name or domain.") if query.blank?

  cloud_unspecified = !options.heroku && !options.deploio && !options.hetzner
  should_scan_heroku = options.heroku || cloud_unspecified
  should_scan_deploio = options.deploio || cloud_unspecified
  should_scan_hetzner = options.hetzner || cloud_unspecified

  choose do |menu| # rubocop:todo Metrics/BlockLength
    deploio_cache_info = "Deploio: #{Cache.stored_at("deploio_apps") || "never"}"
    heroku_cache_info = "Heroku: #{Cache.stored_at("heroku_apps") || "never"}"
    hetzner_cache_info = "Hetzner: #{Cache.stored_at("hetzner_vms") || "never"}"

    menu.choice "Update caches (#{deploio_cache_info}, #{heroku_cache_info}, #{hetzner_cache_info})" do
      if should_scan_deploio
        say "Updating Deploio cache"
        Cache.store("deploio_apps", Deploio.fetch_apps)
        Cache.store("deploio_vms", Deploio.fetch_vms)
      end
      if should_scan_heroku
        say "Updating Heroku cache..."
        Cache.store("heroku_apps", Heroku.fetch_apps)
      end
      if should_scan_hetzner
        say "Updating Hetzner cache..."
        Cache.store("hetzner_vms", Hetzner.fetch_vms)
      end
      say "Caches updated"
    end

    open_cmds = []

    if should_scan_deploio && Cache.stored_at("deploio_apps")
      select_deploio_targets(query).each do |app|
        display_cmd, exec_cmd = nctl_command(app, "exec", "bash")
        menu.choice(display_cmd) { exec exec_cmd.squish }

        display_cmd, exec_cmd = nctl_command(app, "exec", "bundle exec rails console")
        menu.choice(display_cmd) { exec exec_cmd.squish }

        display_cmd, exec_cmd = nctl_command(app, "logs", "-f")
        menu.choice(display_cmd) { exec exec_cmd.squish }

        app[:hosts].each { |host| open_cmds << "open https://#{host}" }
      end
    end

    if should_scan_deploio && Cache.stored_at("deploio_vms")
      select_deploio_vm_targets(query).each do |vm|
        bash_cmd = "ssh root@#{vm[:fqdn]} docker ps"
        menu.choice(bash_cmd) { exec bash_cmd.squish }
      end
    end

    if should_scan_heroku && Cache.stored_at("heroku_apps")
      select_heroku_targets(query).each do |app|
        exec_cmd = "heroku #{bold("run")} bash --app #{bold(app[:name])}"
        menu.choice(exec_cmd) { exec exec_cmd.squish }

        rails_console_cmd = "heroku #{bold("run")} bundle exec rails console --app #{bold(app[:name])}"
        menu.choice(rails_console_cmd) { exec rails_console_cmd.squish }

        log_cmd = "heroku #{bold("logs")} -t --app #{bold(app[:name])}"
        menu.choice(log_cmd)  { exec log_cmd.squish }

        app[:domains].each do |host|
          open_cmds << "open https://#{host}"
        end
      end
    end

    if should_scan_hetzner && Cache.stored_at("hetzner_vms")
      select_hetzner_vm_targets(query).each do |vm|
        bash_cmd = "ssh root@#{vm[:fqdn] || vm[:public_ip]} docker ps"
        menu.choice(bash_cmd) { exec bash_cmd.squish }
        open_cmds << "open https://console.hetzner.com/projects/#{vm[:project_id]}/servers/#{vm[:id]}"
      end
    end

    open_cmds.uniq.each do |cmd|
      menu.choice(cmd) { exec cmd.squish }
    end
  end
end