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: 'latest', dry_run: false, timeout:) ⇒ nil

Parameters:

  • force (Boolean) (defaults to: false)
  • tag (String) (defaults to: 'latest')
  • 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: 'latest', dry_run: false, timeout:)
  containers = load_containers(tag, dry_run: dry_run)
  scripts = @app.yaml.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  volumes = @app.yaml.fetch('volumes', {})
  scheduler = load_scheduler(@app.yaml['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)
  • dry_run (Boolean)
  • with (Array<String>, nil) (defaults to: nil)

Returns:



116
117
118
# File 'lib/hako/commander.rb', line 116

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

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

Parameters:

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

Returns:



126
127
128
# File 'lib/hako/commander.rb', line 126

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

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

Parameters:

  • yaml (Hash)
  • dry_run (Boolean)

Returns:



133
134
135
# File 'lib/hako/commander.rb', line 133

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

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

Parameters:

  • commands (Array<String>)
  • tag (String)
  • env (Hash<String, String>) (defaults to: {})
  • dry_run (Boolean) (defaults to: false)

Returns:

  • (nil)


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

def oneshot(commands, tag:, containers:, env: {}, dry_run: false)
  containers = load_containers(tag, dry_run: dry_run, with: containers)
  scripts = @app.yaml.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  volumes = @app.yaml.fetch('volumes', {})
  scheduler = load_scheduler(@app.yaml['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)
  end
  scripts.each { |script| script.oneshot_finished(containers) }
  exit exit_code
end

#remove(dry_run:) ⇒ nil

Parameters:

  • dry_run (Boolean)

Returns:

  • (nil)


70
71
72
73
74
# File 'lib/hako/commander.rb', line 70

def remove(dry_run:)
  scripts = @app.yaml.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  load_scheduler(@app.yaml['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.yaml.fetch('scripts', []).map { |config| load_script(config, dry_run: dry_run) }
  scheduler = load_scheduler(@app.yaml['scheduler'], scripts, dry_run: dry_run)

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

#statusnil

Returns:

  • (nil)


64
65
66
# File 'lib/hako/commander.rb', line 64

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

#stop(dry_run:) ⇒ Object



76
77
78
# File 'lib/hako/commander.rb', line 76

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

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

Parameters:

Yield Returns:

  • (Fixnum)


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

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