Class: Mrsk::Cli::Accessory

Inherits:
Base
  • Object
show all
Defined in:
lib/mrsk/cli/accessory.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

#boot(name, login: true) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mrsk/cli/accessory.rb', line 3

def boot(name, login: true)
  mutating do
    if name == "all"
      MRSK.accessory_names.each { |accessory_name| boot(accessory_name) }
    else
      with_accessory(name) do |accessory|
        directories(name)
        upload(name)

        on(accessory.hosts) do
          execute *MRSK.registry. if 
          execute *MRSK.auditor.record("Booted #{name} accessory"), verbosity: :debug
          execute *accessory.run
        end
      end
    end
  end
end

#details(name) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/mrsk/cli/accessory.rb', line 102

def details(name)
  if name == "all"
    MRSK.accessory_names.each { |accessory_name| details(accessory_name) }
  else
    with_accessory(name) do |accessory|
      on(accessory.hosts) { puts capture_with_info(*accessory.info) }
    end
  end
end

#directories(name) ⇒ Object



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

def directories(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        accessory.directories.keys.each do |host_path|
          execute *accessory.make_directory(host_path)
        end
      end
    end
  end
end

#exec(name, cmd) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mrsk/cli/accessory.rb', line 115

def exec(name, cmd)
  with_accessory(name) do |accessory|
    case
    when options[:interactive] && options[:reuse]
      say "Launching interactive command with via SSH from existing container...", :magenta
      run_locally { exec accessory.execute_in_existing_container_over_ssh(cmd) }

    when options[:interactive]
      say "Launching interactive command via SSH from new container...", :magenta
      run_locally { exec accessory.execute_in_new_container_over_ssh(cmd) }

    when options[:reuse]
      say "Launching command from existing container...", :magenta
      on(accessory.hosts) do
        execute *MRSK.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug
        capture_with_info(*accessory.execute_in_existing_container(cmd))
      end

    else
      say "Launching command from new container...", :magenta
      on(accessory.hosts) do
        execute *MRSK.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug
        capture_with_info(*accessory.execute_in_new_container(cmd))
      end
    end
  end
end

#logs(name) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mrsk/cli/accessory.rb', line 148

def logs(name)
  with_accessory(name) do |accessory|
    grep = options[:grep]

    if options[:follow]
      run_locally do
        info "Following logs on #{accessory.hosts}..."
        info accessory.follow_logs(grep: grep)
        exec accessory.follow_logs(grep: grep)
      end
    else
      since = options[:since]
      lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set

      on(accessory.hosts) do
        puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep))
      end
    end
  end
end

#reboot(name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mrsk/cli/accessory.rb', line 53

def reboot(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        execute *MRSK.registry.
      end

      stop(name)
      remove_container(name)
      boot(name, login: false)
    end
  end
end

#remove(name) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mrsk/cli/accessory.rb', line 171

def remove(name)
  mutating do
    if name == "all"
      MRSK.accessory_names.each { |accessory_name| remove(accessory_name) }
    else
      if options[:confirmed] || ask("This will remove all containers, images and data directories for #{name}. Are you sure?", limited_to: %w( y N ), default: "N") == "y"
        with_accessory(name) do
          stop(name)
          remove_container(name)
          remove_image(name)
          remove_service_directory(name)
        end
      end
    end
  end
end

#remove_container(name) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/mrsk/cli/accessory.rb', line 189

def remove_container(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        execute *MRSK.auditor.record("Remove #{name} accessory container"), verbosity: :debug
        execute *accessory.remove_container
      end
    end
  end
end

#remove_image(name) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/mrsk/cli/accessory.rb', line 201

def remove_image(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        execute *MRSK.auditor.record("Removed #{name} accessory image"), verbosity: :debug
        execute *accessory.remove_image
      end
    end
  end
end

#remove_service_directory(name) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/mrsk/cli/accessory.rb', line 213

def remove_service_directory(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        execute *accessory.remove_service_directory
      end
    end
  end
end

#restart(name) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/mrsk/cli/accessory.rb', line 92

def restart(name)
  mutating do
    with_accessory(name) do
      stop(name)
      start(name)
    end
  end
end

#start(name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/mrsk/cli/accessory.rb', line 68

def start(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        execute *MRSK.auditor.record("Started #{name} accessory"), verbosity: :debug
        execute *accessory.start
      end
    end
  end
end

#stop(name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/mrsk/cli/accessory.rb', line 80

def stop(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        execute *MRSK.auditor.record("Stopped #{name} accessory"), verbosity: :debug
        execute *accessory.stop, raise_on_non_zero_exit: false
      end
    end
  end
end

#upload(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mrsk/cli/accessory.rb', line 23

def upload(name)
  mutating do
    with_accessory(name) do |accessory|
      on(accessory.hosts) do
        accessory.files.each do |(local, remote)|
          accessory.ensure_local_file_present(local)

          execute *accessory.make_directory_for(remote)
          upload! local, remote
          execute :chmod, "755", remote
        end
      end
    end
  end
end