Class: Vhost

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

Overview

Vhost - Alex Clink =

alexclink.com

Constant Summary collapse

VERSION =
"1.1.5"
CONF_NAME =
"vhosts.yml"
CONFIG_PATHS =
[
  'vhosts-conf',
  '../vhosts-conf',
  '../etc/vhosts-conf',
  '/usr/local/etc/vhosts-conf',
  '/etc/vhosts-conf'
]
DEFAULT_TEMPLATE =
<<-TEMPLATE
  <% site_root = File.join(conf['sites_folder'], vhost, "public") %>
  <% site_name = vhost %>
  server {
  	listen 80;
  	root <%= site_root %>;
  	server_name <%= site_name %>;
  	index index.html;
  }
TEMPLATE
DEFAULT_CONFIG =
{
  'server_conf'      => '/usr/local/etc/nginx',
  'sites_folder'     => '/var/www',
  'default_template' => nil,
  'restart_cmd'      => 'sudo nginx -s reload',
  'editor:'          => 'emacs'
}.to_yaml
@@available_path =
File.join(@@conf['server_conf'], 'sites-available')
@@enabled_path =
File.join(@@conf['server_conf'], 'sites-enabled')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Vhost

Instance Methods =



120
121
122
123
124
125
126
127
# File 'lib/vhost.rb', line 120

def initialize(name)
  @name = name
  @paths = {
    enabled: File.join(@@conf['server_conf'], 'sites-enabled', @name),
    available: File.join(@@conf['server_conf'], 'sites-available', @name),
    folder: File.join(@@conf['sites_folder'], basename)
  }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



130
131
132
# File 'lib/vhost.rb', line 130

def name
  @name
end

#pathsObject (readonly)

Returns the value of attribute paths.



129
130
131
# File 'lib/vhost.rb', line 129

def paths
  @paths
end

Class Method Details

.allObject



86
87
88
89
90
# File 'lib/vhost.rb', line 86

def self.all
  self.available.map do |path|
    Vhost.find File.basename(path)
  end
end

.availableObject



78
79
80
# File 'lib/vhost.rb', line 78

def self.available
  Dir[File.join(@@available_path, '*')].keep_if { |v| File.basename(v) =~ /\A[^\.].*[^\#\~]\Z/ }
end

.confObject



60
61
62
# File 'lib/vhost.rb', line 60

def self.conf
  @@conf
end

.conf_fileObject



64
65
66
# File 'lib/vhost.rb', line 64

def self.conf_file
  @@config_file
end

.conf_pathObject



68
69
70
# File 'lib/vhost.rb', line 68

def self.conf_path
  @@config_path
end

.create(name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vhost.rb', line 100

def self.create(name)
  return false if Vhost.find(name)
    
  File.open File.join(@@available_path, "#{name}.conf"), "w" do |f|
    begin
      template = Erubis::Eruby.new File.read(File.join(@@config_path, 'templates', @@conf['default_template']))
    rescue
      puts "Error: No template found, Using default template"
      template = Erubis::Eruby.new DEFAULT_TEMPLATE
    end
    f.write template.result(vhost: name, conf: @@conf)
  end
  
  Vhost.find(name)
end

.enabledObject



82
83
84
# File 'lib/vhost.rb', line 82

def self.enabled
  Dir[File.join(@@enabled_path, '*')].keep_if { |v| File.basename(v) =~ /\A[^\.].*[^\#\~]\Z/ }
end

.find(name) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/vhost.rb', line 92

def self.find(name)
  name = Dir[File.join(@@available_path, "#{name}*")].first
  return false if name.nil?
  
  name = File.basename(name)
  Vhost.new(name)
end

.load_conf(paths = CONFIG_PATHS) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vhost.rb', line 41

def self.load_conf(paths = CONFIG_PATHS)
  Dir.chdir File.expand_path(File.dirname(__FILE__))
  @@config_path = CONFIG_PATHS.select { |path| File.exist? path }.first
  puts "Can't find configuration!\nSearch locations:\n#{CONFIG_PATHS.join("\n")}" if @@config_path.nil?
  @@config_file = File.join(@@config_path, 'vhosts.yml')
  
  begin
    @@conf = YAML.load_file @@config_file
  rescue
    @@conf = YAML.parse DEFAULT_CONFIG
  end
  
  @@conf
end

.save_confObject



72
73
74
75
76
# File 'lib/vhost.rb', line 72

def self.save_conf
  File.open @@config_file, "w" do |file|
    file.write @@conf.to_yaml
  end
end

Instance Method Details

#<=>(other) ⇒ Object



167
168
169
170
171
172
# File 'lib/vhost.rb', line 167

def <=> (other)
  return -1 if self.enabled? and not other.enabled?
  return self.name <=> other.name if (self.enabled? and other.enabled?) or (not self.enabled? and not other.enabled?)
  return 1 if not self.enabled? and other.enabled?
  nil
end

#basenameObject



132
133
134
# File 'lib/vhost.rb', line 132

def basename
  File.basename(@name, ".conf")
end

#delete!Object



158
159
160
161
# File 'lib/vhost.rb', line 158

def delete!
  File.unlink @paths[:enabled] if enabled?
  File.unlink @paths[:available]
end

#disableObject



153
154
155
156
# File 'lib/vhost.rb', line 153

def disable
  return false unless enabled?
  File.unlink @paths[:enabled]
end

#enableObject



148
149
150
151
# File 'lib/vhost.rb', line 148

def enable
  return false if enabled?
  File.symlink @paths[:available], @paths[:enabled]
end

#enabled?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/vhost.rb', line 144

def enabled?
  File.exist? @paths[:enabled]
end

#folderObject



140
141
142
# File 'lib/vhost.rb', line 140

def folder
  @paths[:folder]
end

#pathObject



136
137
138
# File 'lib/vhost.rb', line 136

def path
  @paths[:available]
end

#to_sObject



163
164
165
# File 'lib/vhost.rb', line 163

def to_s
  basename
end