-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvirtualhost
executable file
·176 lines (154 loc) · 6.16 KB
/
virtualhost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
53
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby
require "rubygems" # ruby1.9 doesn't "require" it though
require "thor"
require "thor/actions"
module VH
def self.config_file
"/private/etc/apache2/extra/httpd-vhosts.conf"
end
def self.httpd_file
"/etc/apache2/httpd.conf"
end
def self.hosts_file
"/etc/hosts"
end
def self.scape_string string
chars = {"." => "_"}
chars.map { |k,c| string.gsub! k , c }
string.downcase
end
def self.sudo_error name
"You don't have permission to update /etc/hosts... Try using sudo:\n\tsudo -E virtualhost #{name}\nDone."
end
class VirtualHosts < Thor
include Thor::Actions
desc :new, "Create new vhost entry"
method_option :path, :aliases => "-p",
:desc => "Caminho absoluto da pasta root dos arquivos"
method_option :url, :aliases => "-u",
:desc => "URL adicionada para acessor direto pelo browser"
method_option :name, :aliases => "-n",
:desc => "Nome de configuracao usado para exclusao"
# method_option :servers, :aliases => "-s",
# :type => :array,
# :default => ["127.0.0.1"],
# :desc => "Enderecos de ip, que o servidor aceitara acessos"
def new
path = if options[:path] then options[:path] else ask "Entre com o caminho absoluto da pasta dos arquivos:" end
url = if options[:url] then options[:url] else ask "Entre com a url desejada:" end
port = if options[:port] then options[:port] else 80 end
name = if options[:name] then options[:name] else "#{url}" end
# servers = if options[:servers] then options[:servers] else ["127.0.0.1"] end
name = VH::scape_string name
entry_hosts = "\n##<virtualhost #{name}>\n127.0.0.1\t#{url}\n##</virtualhost #{name}>\n"
begin
append_to_file VH::hosts_file, entry_hosts
entry_vh = %`\n##<virtualhost #{name}>\n\n`
entry_vh << %`<VirtualHost *:#{port}>\n`
entry_vh << %`\tDocumentRoot "#{File.expand_path path}"\n`
entry_vh << %`\tServerName #{url}\n\n`
entry_vh << %`\t<Directory #{File.expand_path path}>\n`
entry_vh << %`\t\tAllowOverride All\n`
entry_vh << %`\t\tOrder allow,deny\n`
entry_vh << %`\t\tAllow from all\n`
entry_vh << %`\t</Directory>\n`
entry_vh << %`</VirtualHost>\n\n`
entry_vh << %`##</virtualhost #{name}>`
append_to_file VH::config_file, entry_vh
run "apachectl restart"
rescue
say VH::sudo_error("new"), :red
exit 1
end
say "-------------------------------------------------------------------------------------"
say "Conteudo inserido no arquivo: #{VH::hosts_file}", :green
say ""
puts entry_hosts
say "-------------------------------------------------------------------------------------"
say "Conteudo inserido no arquivo: #{VH::config_file}", :green
say ""
puts entry_vh
say "-------------------------------------------------------------------------------------"
say "Para manutencoes/remover, utilize o nome: #{name}"
say "Finish", :green
end
desc "clean NAME", "Limpa uma entrada dos arquivos de hosts de virtualhosts"
def clean name
entry = "/\n##\\<virtualhost #{name}\\>.*\\<\\/virtualhost #{name}\\>$/m"
begin
gsub_file VH::hosts_file, eval(entry), ''
gsub_file VH::config_file, eval(entry), ''
rescue
say VH::sudo_error("clean NAME"), :red
exit 1
end
say "Files cleaning, for name content: #{name}", :green
say "Finish.", :green
end
desc :clean_all, "Limpa o arquivo de virtual hosts"
def clean_all
begin
say "Limpando a partir do arquivo: #{VH::config_file}"
reg = /^##\<virtualhost (?<name>[\w]*)\>$/
begin
config_file = File.read VH::config_file
match = reg.match config_file
invoke clean match[:name] if match
$stdout.flush
end while match
say_status "Finish clean form: #{VH::config_file}"
say "Limpando a partir do arquivo: #{VH::hosts_file}"
begin
hosts_file = File.read VH::hosts_file
match = reg.match hosts_file
invoke clean match[:name] if match
$stdout.flush
end while match
say_status "Finish clean form: #{VH::hosts_file}"
say "Clean all files", :green
say "Finish.", :green
# rescue
# say VH::sudo_error("clean_all"), :red
# exit 1
end
end
desc :default, "Mostra as configuracoes padrao de uso"
def default
puts %`+-----------------------------------------------------------------------------------------------------`
puts %`| Arquivo de configuracao do apache.........: #{VH::httpd_file}`
puts %`| Arquivo de configuracao dos virtuais hosts: #{VH::config_file}`
puts %`| Arquivo de configuracao dos ips dos hosts.: #{VH::hosts_file}`
puts %`+-----------------------------------------------------------------------------------------------------`
end
desc :list, "Lista todas as contas criadas"
def list
begin
reg = /^##\<virtualhost (?<name>[\w]*)\>$/
config_file = File.read VH::hosts_file
names = config_file.scan reg
indice = 1
say "+----------------------------------------"
names.each do |name|
name = name[0] if name.class == Array
say "| #{indice}. #{name}"
indice += 1
end
say "+----------------------------------------"
rescue
say VH::sudo_error("list"), :red
exit 1
end
end
desc :doctor, "Descomenta a linha de configuracao ao arquivo do apache"
def doctor
begin
uncomment_lines VH::httpd_file, "Include #{VH::config_file}"
run "apachectl restart"
rescue
say VH::sudo_error("doctor"), :red
exit 1
end
end
end
end
VH::VirtualHosts.start