Class: Mrsk::Commands::Accessory

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

Constant Summary

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

Constructor Details

#initialize(config, name:) ⇒ Accessory

Returns a new instance of Accessory.



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

def initialize(config, name:)
  super(config)
  @accessory_config = config.accessory(name)
end

Instance Attribute Details

#accessory_configObject (readonly)

Returns the value of attribute accessory_config.



2
3
4
# File 'lib/mrsk/commands/accessory.rb', line 2

def accessory_config
  @accessory_config
end

Instance Method Details

#ensure_local_file_present(local_file) ⇒ Object



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

def ensure_local_file_present(local_file)
  if !local_file.is_a?(StringIO) && !Pathname.new(local_file).exist?
    raise "Missing file: #{local_file}"
  end
end

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



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

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

#execute_in_existing_container_over_ssh(*command) ⇒ Object



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

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

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



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

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

#execute_in_new_container_over_ssh(*command) ⇒ Object



74
75
76
# File 'lib/mrsk/commands/accessory.rb', line 74

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

#follow_logs(grep: nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/mrsk/commands/accessory.rb', line 45

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

#infoObject



34
35
36
# File 'lib/mrsk/commands/accessory.rb', line 34

def info
  docker :ps, *service_filter
end

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



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

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

#make_directory(path) ⇒ Object



93
94
95
# File 'lib/mrsk/commands/accessory.rb', line 93

def make_directory(path)
  [ :mkdir, "-p", path ]
end

#make_directory_for(remote_file) ⇒ Object



89
90
91
# File 'lib/mrsk/commands/accessory.rb', line 89

def make_directory_for(remote_file)
  make_directory Pathname.new(remote_file).dirname.to_s
end

#remove_containerObject



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

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

#remove_imageObject



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

def remove_image
  docker :image, :rm, "--force", image
end

#remove_service_directoryObject



97
98
99
# File 'lib/mrsk/commands/accessory.rb', line 97

def remove_service_directory
  [ :rm, "-rf", service_name ]
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mrsk/commands/accessory.rb', line 11

def run
  docker :run,
    "--name", service_name,
    "--detach",
    "--restart", "unless-stopped",
    *config.logging_args,
    *publish_args,
    *env_args,
    *volume_args,
    *label_args,
    *option_args,
    image,
    cmd
end

#run_over_ssh(command) ⇒ Object



78
79
80
# File 'lib/mrsk/commands/accessory.rb', line 78

def run_over_ssh(command)
  super command, host: hosts.first
end

#startObject



26
27
28
# File 'lib/mrsk/commands/accessory.rb', line 26

def start
  docker :container, :start, service_name
end

#stopObject



30
31
32
# File 'lib/mrsk/commands/accessory.rb', line 30

def stop
  docker :container, :stop, service_name
end