Class: Pave::VirtualHost

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shell

#sh, #shell

Constructor Details

#initialize(host) ⇒ VirtualHost

Returns a new instance of VirtualHost.



10
11
12
# File 'lib/pave/virtual_host.rb', line 10

def initialize(host)
  @hostname = host
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

Instance Method Details

#backup_vhostObject



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

def backup_vhost
  File.delete(vhosts_conf_file_backup) if File.exist?(vhosts_conf_file_backup)
  FileUtils.cp vhosts_conf_file, vhosts_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

#create_vhostObject



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

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 && restart_apache && say("Created virtual host for #{hostname}.")
end

#remove_vhostObject



21
22
23
24
25
# File 'lib/pave/virtual_host.rb', line 21

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 && restart_apache && say("Removed virtual host for #{hostname}.")
end

#restart_apacheObject



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

def restart_apache
  code = sh "sudo apachectl restart"
  say "Apache restarted."
  true
end

#restore_vhostObject



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

def restore_vhost
  return say "Couldn't find vhosts backup." unless File.exist?(vhosts_conf_file_backup)
  File.delete(vhosts_conf_file)
  FileUtils.cp vhosts_conf_file_backup, vhosts_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