Class: Hako::Commander
- Inherits:
-
Object
- Object
- Hako::Commander
- Defined in:
- lib/hako/commander.rb
Defined Under Namespace
Classes: SignalTrapped
Constant Summary collapse
- TRAP_SIGNALS =
%i[INT TERM].freeze
Instance Method Summary collapse
- #deploy(tag:, timeout:, force: false, dry_run: false) ⇒ nil
-
#initialize(app) ⇒ Commander
constructor
A new instance of Commander.
- #load_containers(tag, dry_run:, with: nil) ⇒ Hash<String, Container> private
- #load_scheduler(scheduler_definition, scripts, dry_run:, volumes: {}, force: false, timeout: nil) ⇒ Scheduler private
- #load_script(script_definition, dry_run:) ⇒ Script private
- #oneshot(commands, tag:, containers:, env: {}, dry_run: false, no_wait: false, overrides: nil) ⇒ nil
- #remove(dry_run:) ⇒ nil
- #rollback(dry_run: false) ⇒ nil
- #status ⇒ nil
- #stop(dry_run:) ⇒ Object
- #with_oneshot_signal_handlers(scheduler, &block) ⇒ Object private
Constructor Details
#initialize(app) ⇒ Commander
Returns a new instance of Commander.
13 14 15 |
# File 'lib/hako/commander.rb', line 13 def initialize(app) @app = app end |
Instance Method Details
#deploy(tag:, timeout:, force: false, dry_run: false) ⇒ nil
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hako/commander.rb', line 21 def deploy(tag:, timeout:, force: false, dry_run: false) 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)
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, dry_run:, volumes: {}, force: false, timeout: nil) ⇒ Scheduler (private)
129 130 131 |
# File 'lib/hako/commander.rb', line 129 def load_scheduler(scheduler_definition, scripts, dry_run:, volumes: {}, force: false, 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)
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
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
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
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 |
#status ⇒ 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)
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 |