Class: Mrsk::Commands::App

Inherits:
Base
  • Object
show all
Defined in:
lib/mrsk/commands/app.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize, #run_over_ssh

Constructor Details

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

Instance Method Details

#container_id_for(container_name:) ⇒ Object



79
80
81
# File 'lib/mrsk/commands/app.rb', line 79

def container_id_for(container_name:)
  docker :container, :ls, "-a", "-f", "name=#{container_name}", "-q"
end

#current_container_idObject



75
76
77
# File 'lib/mrsk/commands/app.rb', line 75

def current_container_id
  docker :ps, "-q", *service_filter
end

#current_running_versionObject



83
84
85
86
87
88
89
# File 'lib/mrsk/commands/app.rb', line 83

def current_running_version
  # FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail!
  pipe \
    docker(:ps, "--filter", "label=service=#{config.service}", "--format", '"{{.Names}}"'),
    %(sed 's/-/\\n/g'),
    "tail -n 1"
end

#execute_in_existing_container(*command, interactive: false) ⇒ Object



48
49
50
51
52
53
# File 'lib/mrsk/commands/app.rb', line 48

def execute_in_existing_container(*command, interactive: false)
  docker :exec,
    ("-it" if interactive),
    config.service_with_version,
    *command
end

#execute_in_existing_container_over_ssh(*command, host:) ⇒ Object



66
67
68
# File 'lib/mrsk/commands/app.rb', line 66

def execute_in_existing_container_over_ssh(*command, host:)
  run_over_ssh execute_in_existing_container(*command, interactive: true).join(" "), host: host
end

#execute_in_new_container(*command, interactive: false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/mrsk/commands/app.rb', line 55

def execute_in_new_container(*command, interactive: false)
  docker :run,
    ("-it" if interactive),
    "--rm",
    *rails_master_key_arg,
    *config.env_args,
    *config.volume_args,
    config.absolute_image,
    *command
end

#execute_in_new_container_over_ssh(*command, host:) ⇒ Object



70
71
72
# File 'lib/mrsk/commands/app.rb', line 70

def execute_in_new_container_over_ssh(*command, host:)
  run_over_ssh execute_in_new_container(*command, interactive: true).join(" "), host: host
end

#follow_logs(host:, grep: nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/mrsk/commands/app.rb', line 39

def follow_logs(host:, grep: nil)
  run_over_ssh pipe(
    current_container_id,
    "xargs docker logs -t -n 10 -f 2>&1",
    (%(grep "#{grep}") if grep)
  ).join(" "), host: host
end

#infoObject



27
28
29
# File 'lib/mrsk/commands/app.rb', line 27

def info
  docker :ps, *service_filter
end

#list_containersObject



98
99
100
# File 'lib/mrsk/commands/app.rb', line 98

def list_containers
  docker :container, :ls, "-a", *service_filter
end

#list_imagesObject



112
113
114
# File 'lib/mrsk/commands/app.rb', line 112

def list_images
  docker :image, :ls, config.repository
end

#logs(since: nil, lines: nil, grep: nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/mrsk/commands/app.rb', line 32

def logs(since: nil, lines: nil, grep: nil)
  pipe \
    current_container_id,
    "xargs docker logs#{" --since #{since}" if since}#{" -n #{lines}" if lines} 2>&1",
    ("grep '#{grep}'" if grep)
end

#most_recent_version_from_available_imagesObject



91
92
93
94
95
# File 'lib/mrsk/commands/app.rb', line 91

def most_recent_version_from_available_images
  pipe \
    docker(:image, :ls, "--format", '"{{.Tag}}"', config.repository),
    "head -n 1"
end

#remove_container(version:) ⇒ Object



102
103
104
105
106
# File 'lib/mrsk/commands/app.rb', line 102

def remove_container(version:)
  pipe \
    container_id_for(container_name: service_with_version(version)),
    xargs(docker(:container, :rm))
end

#remove_containersObject



108
109
110
# File 'lib/mrsk/commands/app.rb', line 108

def remove_containers
  docker :container, :prune, "-f", *service_filter
end

#remove_imagesObject



116
117
118
# File 'lib/mrsk/commands/app.rb', line 116

def remove_images
  docker :image, :prune, "-a", "-f", *service_filter
end

#run(role: :web) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mrsk/commands/app.rb', line 4

def run(role: :web)
  role = config.role(role)

  docker :run,
    "-d",
    "--restart unless-stopped",
    "--name", service_with_version,
    *rails_master_key_arg,
    *role.env_args,
    *config.volume_args,
    *role.label_args,
    config.absolute_image,
    role.cmd
end

#startObject



19
20
21
# File 'lib/mrsk/commands/app.rb', line 19

def start
  docker :start, service_with_version
end

#stopObject



23
24
25
# File 'lib/mrsk/commands/app.rb', line 23

def stop
  pipe current_container_id, xargs(docker(:stop))
end