Class: Mrsk::Commands::App

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

Constant Summary collapse

ACTIVE_DOCKER_STATUSES =
[ :running, :restarting ]

Constants inherited from Base

Base::DOCKER_HEALTH_LOG_FORMAT, Base::DOCKER_HEALTH_STATUS_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#container_id_for, #run_over_ssh

Constructor Details

#initialize(config, role: nil) ⇒ App

Returns a new instance of App.



6
7
8
9
# File 'lib/mrsk/commands/app.rb', line 6

def initialize(config, role: nil)
  super(config)
  @role = role
end

Instance Attribute Details

#roleObject (readonly)

Returns the value of attribute role.



4
5
6
# File 'lib/mrsk/commands/app.rb', line 4

def role
  @role
end

Instance Method Details

#container_id_for_version(version, only_running: false) ⇒ Object



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

def container_id_for_version(version, only_running: false)
  container_id_for(container_name: container_name(version), only_running: only_running)
end

#current_running_container_idObject



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

def current_running_container_id
  docker :ps, "--quiet", *filter_args(statuses: ACTIVE_DOCKER_STATUSES), "--latest"
end

#current_running_versionObject



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

def current_running_version
  list_versions("--latest", statuses: ACTIVE_DOCKER_STATUSES)
end

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



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

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

#execute_in_existing_container_over_ssh(*command, host:) ⇒ Object



87
88
89
# File 'lib/mrsk/commands/app.rb', line 87

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



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

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



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

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



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

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

#infoObject



47
48
49
# File 'lib/mrsk/commands/app.rb', line 47

def info
  docker :ps, *filter_args
end

#list_container_namesObject



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

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

#list_containersObject



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

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

#list_imagesObject



137
138
139
# File 'lib/mrsk/commands/app.rb', line 137

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

#list_versions(*docker_args, statuses: nil) ⇒ Object



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

def list_versions(*docker_args, statuses: nil)
  pipe \
    docker(:ps, *filter_args(statuses: statuses), *docker_args, "--format", '"{{.Names}}"'),
    %(grep -oE "\\-[^-]+$"), # Extract SHA from "service-role-dest-SHA"
    %(cut -c 2-)
end

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



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

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

#remove_container(version:) ⇒ Object



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

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

#remove_containersObject



133
134
135
# File 'lib/mrsk/commands/app.rb', line 133

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

#remove_imagesObject



141
142
143
# File 'lib/mrsk/commands/app.rb', line 141

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

#rename_container(version:, new_version:) ⇒ Object



129
130
131
# File 'lib/mrsk/commands/app.rb', line 129

def rename_container(version:, new_version:)
  docker :rename, container_name(version), container_name(new_version)
end

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mrsk/commands/app.rb', line 15

def run
  role = config.role(self.role)

  docker :run,
    "--detach",
    "--restart unless-stopped",
    "--name", container_name,
    "-e", "MRSK_CONTAINER_NAME=\"#{container_name}\"",
    *role.env_args,
    *role.health_check_args,
    *config.logging_args,
    *config.volume_args,
    *role.label_args,
    *role.option_args,
    config.absolute_image,
    role.cmd
end

#startObject



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

def start
  docker :start, container_name
end

#start_or_runObject



11
12
13
# File 'lib/mrsk/commands/app.rb', line 11

def start_or_run
  combine start, run, by: "||"
end

#status(version:) ⇒ Object



37
38
39
# File 'lib/mrsk/commands/app.rb', line 37

def status(version:)
  pipe container_id_for_version(version), xargs(docker(:inspect, "--format", DOCKER_HEALTH_STATUS_FORMAT))
end

#stop(version: nil) ⇒ Object



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

def stop(version: nil)
  pipe \
    version ? container_id_for_version(version) : current_running_container_id,
    xargs(config.stop_wait_time ? docker(:stop, "-t", config.stop_wait_time) : docker(:stop))
end

#tag_current_as_latestObject



145
146
147
# File 'lib/mrsk/commands/app.rb', line 145

def tag_current_as_latest
  docker :tag, config.absolute_image, config.latest_image
end