Class: Superhosting::Controller::Mux

Inherits:
Base
  • Object
show all
Defined in:
lib/superhosting/controller/mux.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #lib

Instance Method Summary collapse

Methods inherited from Base

#get_base_controller_options, #get_controller

Methods included from Helpers

#instance_variables_to_hash

Methods included from Helper::Config

#_config, #_config_options, #_save_registry!, #apply, #configure, #configure_with_apply, #reconfig, #unapply, #unconfigure, #unconfigure_with_unapply

Methods included from Helper::Cmd

#_command, #_command_without_debug, #command, #command!

Methods included from Helper::File

#chmod!, #chown!, #chown_r!, #safe_link!, #safe_unlink!

Methods included from Helper::Logger

#__debug, #__dry_run, #__dry_run=, #__logger, #__logger=, #debug, #debug_block, #debug_operation, #indent, #indent=, #indent_reset, #indent_step, #indent_step_back, #info, #storage, #t, #with_dry_run, #with_indent, #with_logger

Constructor Details

#initialize(**kwargs) ⇒ Mux

Returns a new instance of Mux.



6
7
8
9
# File 'lib/superhosting/controller/mux.rb', line 6

def initialize(**kwargs)
  super
  @container_controller = self.get_controller(Container)
end

Instance Attribute Details

#indexObject



55
56
57
# File 'lib/superhosting/controller/mux.rb', line 55

def index
  @@index ||= self.reindex
end

Instance Method Details

#add(name:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/superhosting/controller/mux.rb', line 11

def add(name:)
  if (resp = self.adding_validation(name: name))
    mux_name = name[/(?<=mux-).*/]
    mapper = MapperInheritance::Mux.new(@config.muxs.f(mux_name)).set_inheritors

    # docker
    mapper.erb_options = { mux: mapper }
    if (resp = @container_controller._collect_docker_options(mapper: mapper, model_or_mux: mux_name)).net_status_ok?
      docker_options = resp[:data]
      @container_controller._safe_run_docker(*docker_options, name: name ).net_status_ok!
    end
  else
    resp
  end
end

#adding_validation(name:) ⇒ Object



36
37
38
39
40
41
# File 'lib/superhosting/controller/mux.rb', line 36

def adding_validation(name:)
  if (resp = @container_controller.base_validation(name: name)).net_status_ok?
    resp = self.not_running_validation(name: name)
  end
  resp
end

#existing_validation(name:) ⇒ Object



43
44
45
# File 'lib/superhosting/controller/mux.rb', line 43

def existing_validation(name:)
  self.index.include?(name) ? {} : { error: :logical_error, code: :mux_does_not_exists, data: { name: name } }
end

#index_pop(mux_name, container_name) ⇒ Object



75
76
77
78
79
80
# File 'lib/superhosting/controller/mux.rb', line 75

def index_pop(mux_name, container_name)
  if self.index.key? mux_name
    self.index[mux_name].delete(container_name)
    self.index.delete(mux_name) if self.index[mux_name].empty?
  end
end

#index_push(mux_name, container_name) ⇒ Object



82
83
84
85
# File 'lib/superhosting/controller/mux.rb', line 82

def index_push(mux_name, container_name)
  self.index[mux_name] ||= []
  self.index[mux_name] << container_name unless self.index[mux_name].include? container_name
end

#not_running_validation(name:) ⇒ Object



47
48
49
# File 'lib/superhosting/controller/mux.rb', line 47

def not_running_validation(name:)
  @container_controller.not_running_validation(name: name).net_status_ok? ? {} : { error: :logical_error, code: :mux_is_running, data: { name: name } }
end

#reconfigure(name:) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/superhosting/controller/mux.rb', line 27

def reconfigure(name:)
  if (resp = self.existing_validation(name: name)).net_status_ok?
    self.index[name].each do |container_name|
      break unless (resp = @container_controller.reconfigure(name: container_name)).net_status_ok?
    end
  end
  resp
end

#reindexObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/superhosting/controller/mux.rb', line 59

def reindex
  @@index ||= {}
  @container_controller._list.map do |container_name, data|
    container_mapper = @container_controller.index[container_name][:mapper]
    if (mux_mapper = container_mapper.mux).file?
      mux_name = "mux-#{mux_mapper.value}"
      if @container_controller.running_validation(name: container_name).net_status_ok?
        self.index_push(mux_name, container_name)
      else
        self.index_pop(mux_name, container_name)
      end
    end
  end
  @@index
end

#running_validation(name:) ⇒ Object



51
52
53
# File 'lib/superhosting/controller/mux.rb', line 51

def running_validation(name:)
  @container_controller.running_validation(name: name).net_status_ok? ? {} : { error: :logical_error, code: :mux_is_not_running, data: { name: name } }
end