Class: Mrsk::Commands::App

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

Constant Summary

Constants inherited from Base

Base::MAX_LOG_SIZE

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#container_id_for, #initialize, #run_over_ssh

Constructor Details

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

Instance Method Details

#all_versions_from_available_containersObject



94
95
96
97
98
# File 'lib/mrsk/commands/app.rb', line 94

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

#current_container_idObject



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

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

#current_running_versionObject



80
81
82
83
84
85
86
# File 'lib/mrsk/commands/app.rb', line 80

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



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

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



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

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

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



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

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

#execute_in_new_container_over_ssh(*command, host:) ⇒ Object



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

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

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



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

def follow_logs(host:, grep: nil)
  run_over_ssh \
    pipe(
      current_container_id,
      "xargs docker logs --timestamps --tail 10 --follow 2>&1",
      (%(grep "#{grep}") if grep)
    ),
    host: host
end

#infoObject



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

def info
  docker :ps, *service_filter
end

#list_container_namesObject



105
106
107
# File 'lib/mrsk/commands/app.rb', line 105

def list_container_names
  [ *list_containers, "--format", "'{{ .Names }}'" ]
end

#list_containersObject



101
102
103
# File 'lib/mrsk/commands/app.rb', line 101

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

#list_imagesObject



119
120
121
# File 'lib/mrsk/commands/app.rb', line 119

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}#{" --tail #{lines}" if lines} 2>&1",
    ("grep '#{grep}'" if grep)
end

#most_recent_version_from_available_imagesObject



88
89
90
91
92
# File 'lib/mrsk/commands/app.rb', line 88

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

#remove_container(version:) ⇒ Object



109
110
111
112
113
# File 'lib/mrsk/commands/app.rb', line 109

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

#remove_containersObject



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

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

#remove_imagesObject



123
124
125
# File 'lib/mrsk/commands/app.rb', line 123

def remove_images
  docker :image, :prune, "--all", "--force", *service_filter
end

#run(role: :web) ⇒ Object



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

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

  docker :run,
    "--detach",
    "--restart unless-stopped",
    "--log-opt", "max-size=#{MAX_LOG_SIZE}",
    "--name", service_with_version,
    *role.env_args,
    *config.volume_args,
    *role.label_args,
    config.absolute_image,
    role.cmd
end

#startObject



17
18
19
# File 'lib/mrsk/commands/app.rb', line 17

def start
  docker :start, service_with_version
end

#stop(version: nil) ⇒ Object



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

def stop(version: nil)
  pipe \
    version ? container_id_for_version(version) : current_container_id,
    xargs(docker(:stop))
end