Class: VhostAdmin::Base

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

Constant Summary collapse

CONFIG_FILE =
'.vhost_admin.conf'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
# File 'lib/vhost_admin/base.rb', line 9

def initialize(options={})
  @config = load_config
  @crypt = options[:crypt] || false
end

Instance Method Details

#add_domain(domain, password, user = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vhost_admin/base.rb', line 40

def add_domain(domain, password, user=nil)
  begin
    unless File.exist?(@config['apache_conf_file'])
      FileUtils.touch(@config['apache_conf_file'])
    end
    if include_apache_domain?(domain)
      raise "#{domain} is already registered in the apache config file."
    end

    user = user || domain
    add_unix_user(user, password)
    add_apache_domain(domain, user)
    add_mail_domain(domain, password)
  rescue => error
    exit_with_error(error)
  end
end

#config_fileObject



37
38
39
# File 'lib/vhost_admin/base.rb', line 37

def config_file
  File.expand_path(CONFIG_FILE, ENV['HOME'])
end

#create_configObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vhost_admin/base.rb', line 23

def create_config
  config = {
    'apache_conf_file'  => '/etc/httpd/conf/virtual/virtualhosts.conf',
    'home_dir_base'   => '/var/www/vhost',
    'mail_dir_base' => '/mail',
    'backup_dir' => '/root/deletetmp',
    'quota_user'  => 'quota1gb',
    'encrypt' => 'cleartext'
  }
  open(config_file, 'w') do |f|
    f.write config.to_yaml
  end
  File.chmod(0600, config_file)
end

#delete_domain(domain, user = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vhost_admin/base.rb', line 57

def delete_domain(domain, user=nil)
  begin
    user = user || domain
    backup_domain_data(domain, user)
    delete_domain_data(domain, user)

    res = apache_test
    if res
      puts "Apache test is OK!"
      exec("apachectl graceful")
    end
  rescue => error
    exit_with_error(error)
  end
end

#load_configObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/vhost_admin/base.rb', line 13

def load_config
  unless File.exist?(config_file)
    create_config
    puts "configure file: #{config_file} was generated.\nPlease execute after edit it."
    exit
  end
    open(config_file) do |f|
    YAML.load(f.read)
  end
end