Class: CellManager

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

Defined Under Namespace

Classes: Result

Constant Summary collapse

GDK_ROOT =
Pathname.new(__dir__).parent
CELL_ROOT =
"#{GDK.root}/gitlab-cells".freeze
PORT_BASE_OFFSET =
12000
PORT_OFFSET =
150

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.are_we_in_a_cell?(config:) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
# File 'lib/cell_manager.rb', line 12

def self.are_we_in_a_cell?(config:)
  cells = config[:cells]
  return false unless cells

  cells[:port_offset].positive?
end

Instance Method Details

#cell_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/cell_manager.rb', line 103

def cell_exist?(name)
  cells.any? { |cell| cell[:name] == name }
end

#enabled?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/cell_manager.rb', line 99

def enabled?
  GDK.config.cells.enabled
end

#restartObject



61
62
63
64
65
66
67
# File 'lib/cell_manager.rb', line 61

def restart
  return true unless enabled?

  cells.all? do |cell|
    run_in_cell(cell[:name], %w[restart]).success?
  end
end

#run_in_cell(name, args, quiet: false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cell_manager.rb', line 78

def run_in_cell(name, args, quiet: false)
  return Result.new(false) unless enabled?

  cell = cells.find { |cell| cell[:name] == name }
  unless cell
    GDK::Output.error("Cell #{name} not found. #{help_cell_list}")
    return Result.new(false)
  end

  dir = directory_for_cell(cell)
  unless Dir.exist?(dir)
    GDK::Output.error("Cell #{cell[:name]} doesn’t exist yet, run `gdk cells up` first.")
    return Result.new(false)
  end

  sh = Shellout.new("gdk #{args.join ' '};", chdir: dir)
  sh.execute(display_output: !quiet)

  sh
end

#startObject



45
46
47
48
49
50
51
# File 'lib/cell_manager.rb', line 45

def start
  return true unless enabled?

  cells.all? do |cell|
    run_in_cell(cell[:name], %w[start]).success?
  end
end

#statusObject



69
70
71
72
73
74
75
76
# File 'lib/cell_manager.rb', line 69

def status
  return true unless enabled?

  cells.all? do |cell|
    GDK::Output.info(cell[:name])
    run_in_cell(cell[:name], %w[status]).success?
  end
end

#stopObject



53
54
55
56
57
58
59
# File 'lib/cell_manager.rb', line 53

def stop
  return true unless enabled?

  cells.all? do |cell|
    run_in_cell(cell[:name], %w[stop]).success?
  end
end

#upObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cell_manager.rb', line 19

def up
  return true unless enabled?

  cells.each_with_index.all? do |cell, index|
    break unless gdk_build(cell, PORT_BASE_OFFSET + (index * PORT_OFFSET))

    sh = run_in_cell(cell[:name], %w[reconfigure])
    if sh.success?
      GDK::Output.success("Cell #{cell[:name]} is ready.")
    else
      GDK::Output.error("Cell #{cell[:name]} failed to reconfigure.")
    end

    sh.success?
  end
end

#updateObject



36
37
38
39
40
41
42
43
# File 'lib/cell_manager.rb', line 36

def update
  return true unless enabled?

  cells.all? do |cell|
    GDK::Output.info("Updating cell #{cell[:name]}")
    run_in_cell(cell[:name], %w[update]).success?
  end
end