Class: Pave::VirtualHost

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/pave/virtual_host.rb

Constant Summary collapse

VHOST_CONF_FILE =
"/etc/apache2/extra/httpd-vhosts.conf".freeze
VHOST_CONF_FILE_BACKUP =
"#{VHOST_CONF_FILE}.backup".freeze
HOSTS_FILE =
"/etc/hosts".freeze
HOSTS_FILE_BACKUP =
"#{HOSTS_FILE}.backup".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shell

#file_insert, included, #sh, #shell

Constructor Details

#initialize(host, dir) ⇒ VirtualHost

Returns a new instance of VirtualHost.



14
15
16
17
# File 'lib/pave/virtual_host.rb', line 14

def initialize(host, dir)
  @hostname = host
  @directory = dir
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



7
8
9
# File 'lib/pave/virtual_host.rb', line 7

def directory
  @directory
end

#hostnameObject

Returns the value of attribute hostname.



7
8
9
# File 'lib/pave/virtual_host.rb', line 7

def hostname
  @hostname
end

Class Method Details

.backup_vhostObject



25
26
27
28
29
30
31
# File 'lib/pave/virtual_host.rb', line 25

def self.backup_vhost
  File.delete(VHOST_CONF_FILE_BACKUP) if File.exist?(VHOST_CONF_FILE_BACKUP)
  FileUtils.cp VHOST_CONF_FILE, VHOST_CONF_FILE_BACKUP
  File.delete(HOSTS_FILE_BACKUP) if File.exist?(HOSTS_FILE_BACKUP)
  FileUtils.cp HOSTS_FILE, HOSTS_FILE_BACKUP
  say "Backed up vhosts conf and hosts file. Use `pave vh:restore` to restore them."
end

.restart_apacheObject



19
20
21
22
23
# File 'lib/pave/virtual_host.rb', line 19

def self.restart_apache
  `sudo apachectl restart`
  say "Apache restarted."
  true
end

.restore_vhostObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pave/virtual_host.rb', line 33

def self.restore_vhost
  return say "Couldn't find vhosts backup." unless File.exist?(VHOST_CONF_FILE_BACKUP)
  File.delete(VHOST_CONF_FILE)
  FileUtils.cp VHOST_CONF_FILE_BACKUP, VHOST_CONF_FILE

  return say "Couldn't find host file backup." unless File.exist?(HOSTS_FILE_BACKUP)
  File.delete(HOSTS_FILE)
  FileUtils.cp HOSTS_FILE_BACKUP, HOSTS_FILE

  restart_apache

  say "Restored vhosts conf and host file."
end

Instance Method Details

#create_vhostObject



47
48
49
50
51
52
# File 'lib/pave/virtual_host.rb', line 47

def create_vhost
  return say "No virtual host backup found. Run `pave vh:backup` before adding a virtual host." unless check_backup
  return say "No host name provided. Run `pave help` for more details." unless hostname.size > 0

  add_vhost_to_conf && add_hosts_entry && self.class.restart_apache && say("Created virtual host for #{hostname}.")
end

#remove_vhostObject



54
55
56
57
58
# File 'lib/pave/virtual_host.rb', line 54

def remove_vhost
  return say "No virtual host backup found. Run `pave vh:backup` before adding a virtual host." unless check_backup

  remove_vhost_from_conf && remove_hosts_entry && self.class.restart_apache && say("Removed virtual host for #{hostname}.")
end