Class: Superhosting::Controller::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/superhosting/controller/container.rb,
lib/superhosting/controller/container/admin.rb

Defined Under Namespace

Classes: Admin

Constant Summary collapse

CONTAINER_NAME_FORMAT =
/[a-zA-Z0-9][a-zA-Z0-9_.-]+/

Instance Method Summary collapse

Methods inherited from Base

#command, #command!, #debug, #initialize

Methods included from Helpers

#create_conf, #erb, #instance_variables_to_hash, #remove_line_from_file, #write_if_not_exist

Constructor Details

This class inherits a constructor from Superhosting::Base

Instance Method Details

#add(name:, mail: 'model', admin_mail: nil, model: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/superhosting/controller/container.rb', line 14

def add(name:, mail: 'model', admin_mail: nil, model: nil)
  return { error: :input_error, message: "Invalid container name '#{name}' - only '#{CONTAINER_NAME_FORMAT}' are allowed" } if name !~ CONTAINER_NAME_FORMAT
  return { error: :logical_error, message: 'Container already running.' } if @docker_api.container_info(name)
  return { error: :input_error, message: 'No model given.' } unless (model_ = model || @config.default_model(default: nil))

  # config
  config_path_ = "#{@config_path}/containers/#{name}"
  config_path_mapper = PathMapper.new(config_path_)
  FileUtils.mkdir_p config_path_

  # model
  create_conf("#{config_path_}/model", model) unless model.nil?
  model_mapper = @config.models.f(:"#{model_}")

  # image
  return { error: :input_error, message: "No docker_image specified in model '#{model_}.'" } unless (image = model_mapper.docker_image(default: nil))

  # mail
  unless mail != 'no'
    if mail == 'yes'
      create_conf("#{config_path_}/mail", mail)
      admin_mail_ = admin_mail
      create_conf("#{config_path_}/admin_mail", admin_mail_) unless admin_mail_.nil?
      return { error: :input_error, message: 'Admin mail required.' } if admin_mail_.nil?
    elsif mail == 'model'
      if model_mapper.default_mail == 'yes'
        admin_mail_ = admin_mail || model_mapper.default_admin_mail(default: nil)
        return { error: :input_error, message: 'Admin mail required.' } if admin_mail_.nil?
      end
    end
  end

  # lib
  lib_path_ = "#{@lib_path}/containers/#{name}"
  lib_path_mapper = PathMapper.new(lib_path_)
  FileUtils.rm_rf "#{lib_path_}/configs"
  FileUtils.mkdir_p "#{lib_path_}/configs"
  FileUtils.mkdir_p "#{lib_path_}/web"

  # user/group
  self.command("groupadd #{name}")
  self.command("useradd #{name} -g #{name} -d #{lib_path_}/web/")

  user = Etc.getpwnam(name)

  write_if_not_exist("#{lib_path_}/configs/etc-group", "#{name}:x:#{user.gid}:")
  write_if_not_exist("#{lib_path_}/configs/etc-passwd", "#{name}:x:#{user.uid}:#{user.gid}::/web/:/usr/sbin/nologin")

  # system users
  users = [config_path_mapper.system_users, model_mapper.system_users].find {|f| f.is_a? PathMapper::FileNode }
  users.lines.each {|u| self.command("useradd #{name}_#{u.strip} -g #{name} -d #{lib_path_}/web/") } unless users.nil?

  # services
  cserv = @config.containers.f(name).services._grep(/.*\.erb/).map {|n| [n._name, n]}.to_h
  mserv = model_mapper.services._grep(/.*\.erb/).map {|n| [n._name, n]}.to_h
  services = mserv.merge!(cserv)

  supervisor_path = "#{lib_path_}/supervisor"
  FileUtils.mkdir_p supervisor_path
  services.each do |_name, node|
    text = erb(node, model: model_, container: config_path_mapper)
    create_conf("#{supervisor_path}/#{_name[/.*[^\.erb]/]}", text)
  end

  # container
  unless model_mapper.f('container.rb').nil?
    registry_path = lib_path_mapper.registry.f('container')._path
    ex = ScriptExecutor::Container.new(
        container: config_path_mapper, container_name: name, container_lib: lib_path_mapper, registry_path: registry_path,
        model: model_mapper, config: @config, lib: @lib
    )
    ex.execute(model_mapper.f('container.rb'))
    ex.commands.each {|c| self.command c }
  end

  # docker
  write_if_not_exist('/etc/security/docker.conf', "@#{name} #{name}")

  # run container
  self.command "docker run --detach --name #{name} -v #{lib_path_}/configs/:/.configs:ro
                -v #{lib_path_}/web:/web #{image} /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf".split

  { error: :error, message: 'Unable to run docker container.' } unless @docker_api.container_info(name)
end

#admin(name:, logger: @logger) ⇒ Object



169
170
171
# File 'lib/superhosting/controller/container.rb', line 169

def admin(name:, logger: @logger)
  Admin.new(name: name, logger: logger)
end

#change(name:, mail: 'model', admin_mail: nil, model: nil) ⇒ Object



149
150
151
# File 'lib/superhosting/controller/container.rb', line 149

def change(name:, mail: 'model', admin_mail: nil, model: nil)

end

#delete(name:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/superhosting/controller/container.rb', line 99

def delete(name:)
  config_path_mapper = @config.containers.f(name)
  lib_path_mapper = PathMapper.new("#{@lib_path}/containers/#{name}")
  model = config_path_mapper.model(default: @config.default_model)
  model_mapper = @config.models.f(:"#{model}")

  sites = config_path_mapper.sites._grep_dirs.map { |n| n._name }
  sites.each do |s|
    begin
      Site.new(instance_variables_to_hash(self)).delete(s)
    rescue NetStatus::Exception => e
      raise Error::Controller, e.net_status
    end
  end

  unless (container = lib_path_mapper.registry.f('container')).nil?
    FileUtils.rm container.lines
    FileUtils.rm container._path
  end

  unless model_mapper.f('container.rb').nil?
    registry_path = lib_path_mapper.registry.f('container')._path
    ex = ScriptExecutor::Container.new(
        container: config_path_mapper, container_name: name, container_lib: lib_path_mapper,
        registry_path: registry_path, on_reconfig_only: true,
        model: model_mapper, config: @config, lib: @lib
    )
    ex.execute(model_mapper.f('container.rb'))
    ex.commands.each {|c| self.command c }
  end

  @docker_api.container_kill(name)
  @docker_api.container_rm(name)
  remove_line_from_file('/etc/security/docker.conf', "@#{name} #{name}")

  begin
    gid = Etc.getgrnam(name).gid
    Etc.passwd do |user|
      self.command("userdel #{user.name}") if user.gid == gid
    end
    self.command("groupdel #{name}")
  rescue ArgumentError => e
    # repeated command execution: group name already does not exist
  end

  FileUtils.rm_rf "#{@lib_path}/containers/#{name}/configs"

  {}
end

#listObject



6
7
8
9
10
11
12
# File 'lib/superhosting/controller/container.rb', line 6

def list
  docker = @docker_api.container_list.map {|c| c['Names'].first.slice(1..-1) }.to_set
  sx = @config.containers._grep_dirs.map {|n| n._name }.to_set
  containers = (docker & sx)

  { data: containers.to_a }
end

#reconfig(name:) ⇒ Object



157
158
159
# File 'lib/superhosting/controller/container.rb', line 157

def reconfig(name:)

end

#restore(name:, from:, mail: 'model', admin_mail: nil, model: nil) ⇒ Object



165
166
167
# File 'lib/superhosting/controller/container.rb', line 165

def restore(name:, from:, mail: 'model', admin_mail: nil, model: nil)

end

#save(name:, to:) ⇒ Object



161
162
163
# File 'lib/superhosting/controller/container.rb', line 161

def save(name:, to:)

end

#update(name:) ⇒ Object



153
154
155
# File 'lib/superhosting/controller/container.rb', line 153

def update(name:)

end