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(force: false, tag:, dry_run: false, timeout:) ⇒ 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, volumes: {}, force: false, dry_run:, timeout: nil) ⇒ Scheduler private
- #load_script(script_definition, dry_run:) ⇒ Script private
- #oneshot(commands, tag:, containers:, env: {}, dry_run: false, no_wait: false) ⇒ 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(force: false, tag:, dry_run: false, timeout:) ⇒ 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)
118 119 120 |
# File 'lib/hako/commander.rb', line 118 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)
128 129 130 |
# File 'lib/hako/commander.rb', line 128 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)
135 136 137 |
# File 'lib/hako/commander.rb', line 135 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) ⇒ nil
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/hako/commander.rb', line 50 def oneshot(commands, tag:, containers:, env: {}, dry_run: false, no_wait: false) 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) end scripts.each { |script| script.oneshot_finished(containers) } exit exit_code end |
#remove(dry_run:) ⇒ nil
71 72 73 74 75 76 |
# File 'lib/hako/commander.rb', line 71 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
65 66 67 |
# File 'lib/hako/commander.rb', line 65 def status load_scheduler(@app.definition['scheduler'], [], dry_run: false).status end |
#stop(dry_run:) ⇒ Object
78 79 80 |
# File 'lib/hako/commander.rb', line 78 def stop(dry_run:) load_scheduler(@app.definition['scheduler'], [], dry_run: dry_run).stop end |
#with_oneshot_signal_handlers(scheduler, &block) ⇒ Object (private)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/hako/commander.rb', line 89 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 |