Class: Superhosting::Controller::Site

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

Defined Under Namespace

Classes: Alias

Constant Summary collapse

DOMAIN_NAME_FORMAT =
/^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}$/

Instance Attribute Summary collapse

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 Attribute Details

#site_indexObject (readonly)

Returns the value of attribute site_index.



4
5
6
# File 'lib/superhosting/controller/site.rb', line 4

def site_index
  @site_index
end

Instance Method Details

#add(name:, container_name:) ⇒ Object



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
# File 'lib/superhosting/controller/site.rb', line 26

def add(name:, container_name:)
  return { error: :input_error, message: "Invalid site name '#{name}' only '#{DOMAIN_NAME_FORMAT}'" } if name !~ DOMAIN_NAME_FORMAT
  return { error: :logical_error, message: 'Site already exists.' } if self.site_index.include? name
  return { error: :logical_error, message: "Container '#{container_name}' doesn\'t exists." } if (config_path_mapper = @config.containers.f(container_name)).nil?

  site_mapper = config_path_mapper.sites.f(name)
  FileUtils.mkdir_p site_mapper._path

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

  lib_sites_path = lib_path_mapper.registry.sites
  unless model_mapper.f('site.rb').nil?
    registry_path = lib_sites_path.f(name)._path
    FileUtils.mkdir_p lib_sites_path._path
    ex = ScriptExecutor::Site.new(
        site: site_mapper, site_name: name,
        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('site.rb'))
    ex.commands.each {|c| self.command c }
  end

  {}
end

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



101
102
103
# File 'lib/superhosting/controller/site.rb', line 101

def alias(name:, logger: @logger)
  Alias.new(name: name, logger: logger)
end

#delete(name:) ⇒ Object



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
# File 'lib/superhosting/controller/site.rb', line 54

def delete(name:)
  if site = self.site_index[name]
    FileUtils.rm_rf site[:site]._path
    container_sites = site[:container].sites
    FileUtils.rm_rf container_sites._path if container_sites.nil?

    config_path_mapper = site[:container]
    lib_path_mapper = PathMapper.new("#{@lib_path}/containers/#{config_path_mapper._name}")
    model = config_path_mapper.model(default: @config.default_model)
    model_mapper = @config.models.f(:"#{model}")

    lib_sites_path = lib_path_mapper.registry.sites

    unless (registry_site = lib_sites_path.f(name)).nil?
      FileUtils.rm registry_site.lines
      FileUtils.rm registry_site._path
    end

    unless model_mapper.f('site.rb').nil?
      registry_path = lib_sites_path.f(name)._path
      FileUtils.mkdir_p lib_sites_path._path
      ex = ScriptExecutor::Site.new(
          site: site[:site], site_name: name,
          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('site.rb'))
      ex.commands.each {|c| self.command c }
    end

    {}
  end
end

#generateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/superhosting/controller/site.rb', line 9

def generate
  @site_index = {}
  @config.containers._grep_dirs.each do |c|
    c.sites._grep_dirs.each do |s|
      names = []
      names << s._name
      s.aliases.lines {|n| names << n.strip } unless s.aliases.nil?
      raise NetStatus::Exception.new(error: :error, message: "Conflict between container_sites: '#{@site_index[s._name][:site]._path}' and '#{s._path}'") if @site_index.key? s._name
      names.each {|n| @site_index[n] = { container: c, site: s } }
    end
  end
  @site_index
end

#rename(name:, new_name:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/superhosting/controller/site.rb', line 89

def rename(name:, new_name:)
  return { error: :logical_error, message: "Site '#{name}' doesn't exists." } unless site = self.site_index[name]
  return { error: :logical_error, message: "Site '#{new_name}' already exists." } if self.site_index[new_name]

  FileUtils.mv site[:container].sites.f(name)._path, site[:container].sites.f(new_name)._path

  lib_path_mapper = PathMapper.new("#{@lib_path}/containers/#{site[:container]._name}")
  unless (lib_sites_path = lib_path_mapper.registry.sites).nil?
    FileUtils.mv lib_sites_path.f(name), lib_sites_path.f(new_name)
  end
end