Class: Hako::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/hako/commander.rb

Defined Under Namespace

Classes: SignalTrapped

Constant Summary collapse

TRAP_SIGNALS =
%i[INT TERM].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Commander

Returns a new instance of Commander.

Parameters:



13
14
15
# File 'lib/hako/commander.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#deploy(force: false, tag:, dry_run: false, timeout:) ⇒ nil

Parameters:

  • force (Boolean) (defaults to: false)
  • tag (String, nil)
  • dry_run (Boolean) (defaults to: false)

Returns:

  • (nil)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hako/commander.rb', line 21

def deploy(force: false, tag:, dry_run: false, timeout:)
  containers = load_containers(tag, dry_run: dry_run)
  scripts = @app.definition.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  volumes = @app.definition.fetch('volumes', {})
  scheduler = load_scheduler(@app.definition['scheduler'], scripts, volumes: volumes, force: force, dry_run: dry_run, timeout: timeout)

  scripts.each { |script| script.deploy_starting(containers) }
  scheduler.deploy(containers)
  scripts.each { |script| script.deploy_finished(containers) }
  nil
end

#load_containers(tag, dry_run:, with: nil) ⇒ Hash<String, Container> (private)

Parameters:

  • tag (String, nil)
  • dry_run (Boolean)
  • with (Array<String>, nil) (defaults to: nil)

Returns:



119
120
121
# File 'lib/hako/commander.rb', line 119

def load_containers(tag, dry_run:, with: nil)
  DefinitionLoader.new(@app, dry_run: dry_run).load(tag: tag, with: with)
end

#load_scheduler(scheduler_definition, scripts, volumes: {}, force: false, dry_run:, timeout: nil) ⇒ Scheduler (private)

Parameters:

  • scheduler_definition (Hash)
  • volumes (Hash) (defaults to: {})
  • force (Boolean) (defaults to: false)
  • dry_run (Boolean)
  • timeout (Integer) (defaults to: nil)

Returns:



129
130
131
# File 'lib/hako/commander.rb', line 129

def load_scheduler(scheduler_definition, scripts, volumes: {}, force: false, dry_run:, timeout: nil)
  Loader.new(Hako::Schedulers, 'hako/schedulers').load(scheduler_definition.fetch('type')).new(@app.id, scheduler_definition, volumes: volumes, scripts: scripts, force: force, dry_run: dry_run, timeout: timeout)
end

#load_script(script_definition, dry_run:) ⇒ Script (private)

Parameters:

  • script_definition (Hash)
  • dry_run (Boolean)

Returns:



136
137
138
# File 'lib/hako/commander.rb', line 136

def load_script(script_definition, dry_run:)
  Loader.new(Hako::Scripts, 'hako/scripts').load(script_definition.fetch('type')).new(@app, script_definition, dry_run: dry_run)
end

#oneshot(commands, tag:, containers:, env: {}, dry_run: false, no_wait: false, overrides: nil) ⇒ nil

Parameters:

  • commands (Array<String>)
  • tag (String, nil)
  • env (Hash<String, String>) (defaults to: {})
  • dry_run (Boolean) (defaults to: false)
  • no_wait (Boolean) (defaults to: false)
  • overrides (Hako::CLI::Oneshot::Overrides, nil) (defaults to: nil)

Returns:

  • (nil)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hako/commander.rb', line 51

def oneshot(commands, tag:, containers:, env: {}, dry_run: false, no_wait: false, overrides: nil)
  containers = load_containers(tag, dry_run: dry_run, with: containers)
  scripts = @app.definition.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  volumes = @app.definition.fetch('volumes', {})
  scheduler = load_scheduler(@app.definition['scheduler'], scripts, volumes: volumes, dry_run: dry_run)

  scripts.each { |script| script.oneshot_starting(containers) }
  exit_code = with_oneshot_signal_handlers(scheduler) do
    scheduler.oneshot(containers, commands, env, no_wait: no_wait, overrides: overrides)
  end
  scripts.each { |script| script.oneshot_finished(containers) }
  exit exit_code
end

#remove(dry_run:) ⇒ nil

Parameters:

  • dry_run (Boolean)

Returns:

  • (nil)


72
73
74
75
76
77
# File 'lib/hako/commander.rb', line 72

def remove(dry_run:)
  scripts = @app.definition.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  scripts.each(&:remove_starting)
  load_scheduler(@app.definition['scheduler'], scripts, dry_run: dry_run).remove
  scripts.each(&:after_remove)
end

#rollback(dry_run: false) ⇒ nil

Parameters:

  • dry_run (Boolean) (defaults to: false)

Returns:

  • (nil)


35
36
37
38
39
40
41
42
# File 'lib/hako/commander.rb', line 35

def rollback(dry_run: false)
  scripts = @app.definition.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  scheduler = load_scheduler(@app.definition['scheduler'], scripts, dry_run: dry_run)

  scripts.each(&:rollback_starting)
  scheduler.rollback
  scripts.each(&:rollback_finished)
end

#statusnil

Returns:

  • (nil)


66
67
68
# File 'lib/hako/commander.rb', line 66

def status
  load_scheduler(@app.definition['scheduler'], [], dry_run: false).status
end

#stop(dry_run:) ⇒ Object



79
80
81
# File 'lib/hako/commander.rb', line 79

def stop(dry_run:)
  load_scheduler(@app.definition['scheduler'], [], dry_run: dry_run).stop
end

#with_oneshot_signal_handlers(scheduler, &block) ⇒ Object (private)

Parameters:

Yield Returns:

  • (Fixnum)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hako/commander.rb', line 90

def with_oneshot_signal_handlers(scheduler, &block)
  old_handlers = {}
  trapped = false
  exit_code = nil

  begin
    TRAP_SIGNALS.each do |sig|
      old_handlers[sig] = Signal.trap(sig) { raise SignalTrapped }
    end
    exit_code = block.call
  rescue SignalTrapped
    trapped = true
  ensure
    old_handlers.each do |sig, command|
      Signal.trap(sig, command)
    end
  end

  if trapped
    exit_code = scheduler.stop_oneshot
  end

  exit_code
end