Class: Mrsk::Cli::Main

Inherits:
Base
  • Object
show all
Defined in:
lib/mrsk/cli/main.rb

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?, #initialize

Constructor Details

This class inherits a constructor from Mrsk::Cli::Base

Instance Method Details

#auditObject



79
80
81
82
83
# File 'lib/mrsk/cli/main.rb', line 79

def audit
  on(MRSK.hosts) do |host|
    puts_by_host host, capture_with_info(*MRSK.auditor.reveal)
  end
end

#configObject



86
87
88
89
90
# File 'lib/mrsk/cli/main.rb', line 86

def config
  run_locally do
    puts MRSK.config.to_h.to_yaml
  end
end

#deployObject



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
# File 'lib/mrsk/cli/main.rb', line 12

def deploy
  runtime = print_runtime do
    say "Ensure Docker is installed...", :magenta
    invoke "mrsk:cli:server:bootstrap"

    say "Log into image registry...", :magenta
    invoke "mrsk:cli:registry:login"

    say "Build and push app image...", :magenta
    invoke "mrsk:cli:build:deliver"

    say "Ensure Traefik is running...", :magenta
    invoke "mrsk:cli:traefik:boot"

    say "Ensure app can pass healthcheck...", :magenta
    invoke "mrsk:cli:healthcheck:perform"

    invoke "mrsk:cli:app:boot"

    say "Prune old containers and images...", :magenta
    invoke "mrsk:cli:prune:all"
  end

  audit_broadcast "Deployed app in #{runtime.to_i} seconds"
end

#detailsObject



72
73
74
75
76
# File 'lib/mrsk/cli/main.rb', line 72

def details
  invoke "mrsk:cli:traefik:details"
  invoke "mrsk:cli:app:details"
  invoke "mrsk:cli:accessory:details", [ "all" ]
end

#envifyObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mrsk/cli/main.rb', line 123

def envify
  if destination = options[:destination]
    env_template_path = ".env.#{destination}.erb"
    env_path          = ".env.#{destination}"
  else
    env_template_path = ".env.erb"
    env_path          = ".env"
  end

  File.write(env_path, ERB.new(File.read(env_template_path)).result, perm: 0600)
end

#initObject



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/mrsk/cli/main.rb', line 94

def init
  require "fileutils"

  if (deploy_file = Pathname.new(File.expand_path("config/deploy.yml"))).exist?
    puts "Config file already exists in config/deploy.yml (remove first to create a new one)"
  else
    FileUtils.mkdir_p deploy_file.dirname
    FileUtils.cp_r Pathname.new(File.expand_path("templates/deploy.yml", __dir__)), deploy_file
    puts "Created configuration file in config/deploy.yml"
  end

  unless (deploy_file = Pathname.new(File.expand_path(".env"))).exist?
    FileUtils.cp_r Pathname.new(File.expand_path("templates/template.env", __dir__)), deploy_file
    puts "Created .env file"
  end

  if options[:bundle]
    if (binstub = Pathname.new(File.expand_path("bin/mrsk"))).exist?
      puts "Binstub already exists in bin/mrsk (remove first to create a new one)"
    else
      puts "Adding MRSK to Gemfile and bundle..."
      `bundle add mrsk`
      `bundle binstubs mrsk`
      puts "Created binstub file in bin/mrsk"
    end
  end
end

#redeployObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mrsk/cli/main.rb', line 39

def redeploy
  runtime = print_runtime do
    say "Build and push app image...", :magenta
    invoke "mrsk:cli:build:deliver"

    say "Ensure app can pass healthcheck...", :magenta
    invoke "mrsk:cli:healthcheck:perform"

    invoke "mrsk:cli:app:boot"
  end

  audit_broadcast "Redeployed app in #{runtime.to_i} seconds"
end

#removeObject



136
137
138
139
140
# File 'lib/mrsk/cli/main.rb', line 136

def remove
  invoke "mrsk:cli:traefik:remove"
  invoke "mrsk:cli:app:remove"
  invoke "mrsk:cli:registry:logout"
end

#rollback(version) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mrsk/cli/main.rb', line 54

def rollback(version)
  MRSK.version = version

  if container_name_available?(MRSK.config.service_with_version)
    say "Stop current version, then start version #{version}...", :magenta

    on(MRSK.hosts) do |host|
      execute *MRSK.app.stop, raise_on_non_zero_exit: false
      execute *MRSK.app.start
    end

    audit_broadcast "Rolled back app to version #{version}"
  else
    say "The app version '#{version}' is not available as a container (use 'mrsk app containers' for available versions)", :red
  end
end

#setupObject



3
4
5
6
7
8
9
# File 'lib/mrsk/cli/main.rb', line 3

def setup
  print_runtime do
    invoke "mrsk:cli:server:bootstrap"
    invoke "mrsk:cli:accessory:boot", [ "all" ]
    deploy
  end
end

#versionObject



143
144
145
# File 'lib/mrsk/cli/main.rb', line 143

def version
  puts Mrsk::VERSION
end