vhost_admin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vhost_admin.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Hitoshi Kurokawa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # VhostAdmin
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vhost_admin'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vhost_admin
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'vhost_admin/runner'
4
+ VhostAdmin::Runner.start
5
+
@@ -0,0 +1,3 @@
1
+ require "vhost_admin/version"
2
+ require "vhost_admin/base"
3
+
@@ -0,0 +1,244 @@
1
+ require 'vhost_admin/version'
2
+ require 'thor'
3
+ require 'date'
4
+ require 'yaml'
5
+
6
+ class VhostAdmin::Base
7
+ CONFIG_FILE = '.vhost_admin.conf'
8
+
9
+ def initialize(options={})
10
+ @config = load_config
11
+ @crypt = options[:crypt] || false
12
+ end
13
+ def load_config
14
+ unless File.exist?(config_file)
15
+ create_config
16
+ puts "configure file: #{config_file} was generated.\nPlease execute after edit it."
17
+ exit
18
+ end
19
+ open(config_file) do |f|
20
+ YAML.load(f.read)
21
+ end
22
+ end
23
+ def create_config
24
+ config = {
25
+ 'apache_conf_file' => '/etc/httpd/conf/virtual/virtualhosts.conf',
26
+ 'home_dir_base' => '/var/www/vhost',
27
+ 'mail_dir_base' => '/mail',
28
+ 'backup_dir' => '/root/deletetmp',
29
+ 'quota_user' => 'quota1gb',
30
+ 'encrypt' => 'cleartext'
31
+ }
32
+ open(config_file, 'w') do |f|
33
+ f.write config.to_yaml
34
+ end
35
+ File.chmod(0600, config_file)
36
+ end
37
+ def config_file
38
+ File.expand_path(CONFIG_FILE, ENV['HOME'])
39
+ end
40
+ def add_domain(domain, password, user=nil)
41
+ begin
42
+ unless File.exist?(@config['apache_conf_file'])
43
+ FileUtils.touch(@config['apache_conf_file'])
44
+ end
45
+ if include_apache_domain?(domain)
46
+ raise "#{domain} is already registered in the apache config file."
47
+ end
48
+
49
+ user = user || domain
50
+ add_unix_user(user, password)
51
+ add_apache_domain(domain, user)
52
+ add_mail_domain(domain, password)
53
+ rescue => error
54
+ exit_with_error(error)
55
+ end
56
+ end
57
+ def delete_domain(domain, user=nil)
58
+ begin
59
+ user = user || domain
60
+ backup_domain_data(domain, user)
61
+ delete_domain_data(domain, user)
62
+
63
+ res = apache_test
64
+ if res
65
+ puts "Apache test is OK!"
66
+ exec("apachectl graceful")
67
+ end
68
+ rescue => error
69
+ exit_with_error(error)
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def exit_with_error(error)
76
+ puts "Error: #{error.message}"
77
+ puts "Exit"
78
+ exit
79
+ end
80
+
81
+ def delete_domain_data(domain, user)
82
+ delete_unix_user(user)
83
+ delete_domain_http_conf(domain)
84
+ delete_mail_data(domain)
85
+ end
86
+ def delete_domain_http_conf(domain)
87
+ unless include_apache_domain?(domain)
88
+ raise "#{domain} is not registered in the apache config file."
89
+ end
90
+ backup_conf= backup_apache_conf
91
+
92
+ flag = false
93
+ File.open(@config['apache_conf_file'], "w"){|wf|
94
+ File.open(backup_conf){|f|
95
+ f.each{|line|
96
+ if line =~ /# #{domain} :/
97
+ flag = true
98
+ end
99
+ if flag && line =~ /<\/VirtualHost>/
100
+ flag = false
101
+ next
102
+ end
103
+ wf.write line unless flag
104
+ }
105
+ }
106
+ }
107
+ end
108
+ def backup_domain_data(domain, user)
109
+ backup_web_data(domain, user)
110
+ backup_mail_data(domain)
111
+ end
112
+ def backup_web_data(domain, user)
113
+ output_value("Home", home_dir(user))
114
+ output_value("Domain", domain)
115
+ output_value("User", user)
116
+ file = "#{domain}_web.tgz"
117
+ tar_backup(file, home_dir(user))
118
+ end
119
+ def backup_mail_data(domain)
120
+ backup_file = File.join(@config['backup_dir'], domain+'_mail.txt')
121
+ exec("postfix_admin show #{domain} > #{backup_file}")
122
+ file = "#{domain}_mail.tgz"
123
+ tar_backup(file, mail_dir(domain))
124
+ end
125
+ def home_dir(user)
126
+ File.join(@config['home_dir_base'], user)
127
+ end
128
+ def mail_dir(domain)
129
+ File.join(@config['mail_dir_base'], domain)
130
+ end
131
+ def tar_backup(file, dir)
132
+ unless File.exist?(dir)
133
+ print "Can not find dir: #{dir}\n"
134
+ return
135
+ end
136
+ basename = File.basename(dir)
137
+ dirname = File.dirname(dir)
138
+ tar_file = File.join(@config['backup_dir'], file)
139
+ res = exec("tar zcf #{tar_file} -C #{dirname} #{basename}")
140
+ unless res
141
+ raise("Tar command was failure")
142
+ end
143
+ end
144
+ def delete_dir(dir)
145
+ FileUtils.rm_rf(dir)
146
+ end
147
+ def add_unix_user(user, password)
148
+ exec("useradd #{user} -m -d #{home_dir(user)} -s /sbin/nologin")
149
+ exec("echo #{user}:#{password} | /usr/sbin/chpasswd")
150
+ if @config['quota_user']
151
+ exec("edquota -p #{@config['quota_user']} #{user}")
152
+ end
153
+ FileUtils.chmod(0711, home_dir(user))
154
+ end
155
+ def delete_unix_user(user)
156
+ exec("userdel -r #{user}")
157
+ end
158
+ def delete_mail_data(domain)
159
+ exec("postfix_admin delete_domain #{domain}")
160
+ delete_dir(mail_dir(domain))
161
+ end
162
+ def add_apache_domain(domain, user)
163
+ backup_apache_conf
164
+ open(@config['apache_conf_file'], 'a') do |f|
165
+ f.write(vhost_text(domain, user))
166
+ end
167
+
168
+ res = exec("apachectl -t")
169
+ if res
170
+ res_restart = exec("apachectl graceful")
171
+ unless res_restart
172
+ raise("Apache restart was failure.")
173
+ end
174
+ else
175
+ raise("Apachec config test was failure.")
176
+ end
177
+ end
178
+ def add_mail_domain(domain, password)
179
+ set_password =
180
+ if @crypt
181
+ md5_crypt(password)
182
+ else
183
+ password
184
+ end
185
+ exec("postfix_admin add_domain #{domain}")
186
+ exec("postfix_admin add_admin admin@#{domain} '#{set_password}'")
187
+ exec("postfix_admin add_admin_domain admin@#{domain} #{domain}")
188
+ end
189
+ def md5_crypt(str)
190
+ salt_set = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + ['.', '/']
191
+ salt = '$1$'+salt_set.sample(8).join+'$'
192
+ str.crypt(salt)
193
+ end
194
+ def vhost_text(domain, user)
195
+ <<"EOS"
196
+
197
+ # #{domain} : #{date_time_str} by vhost_admin
198
+ <VirtualHost *:80>
199
+ ServerAdmin postmaster@#{domain}
200
+ ServerName www.#{domain}
201
+ DocumentRoot #{home_dir(user)}/public_html
202
+ ServerAlias #{domain}
203
+ SuexecUserGroup #{user} #{user}
204
+ </VirtualHost>
205
+ EOS
206
+ end
207
+ # ServerName www.example.com
208
+ def include_apache_domain?(domain)
209
+ open(@config['apache_conf_file']) do |f|
210
+ f.grep(/ServerName\s+www\.#{domain}/).size != 0
211
+ end
212
+ end
213
+ def backup_apache_conf
214
+ backup_conf = backup_apache_conf_file
215
+ if File.exist?(@config['apache_conf_file'])
216
+ FileUtils.copy(@config['apache_conf_file'], backup_apache_conf_file)
217
+ end
218
+ backup_conf
219
+ end
220
+ def backup_apache_conf_file
221
+ @config['apache_conf_file'] + backup_suffix
222
+ end
223
+ def date_time_str
224
+ DateTime.now.strftime("%Y-%m-%d %H:%M:%S")
225
+ end
226
+ def backup_suffix
227
+ DateTime.now.strftime(".%Y%m%d%H%M%S")
228
+ end
229
+ def exec(cmd, debug=false)
230
+ if debug
231
+ output_value("Command(test run)",cmd)
232
+ true
233
+ else
234
+ output_value("Command",cmd)
235
+ system(cmd)
236
+ end
237
+ end
238
+ def output_value(key, value)
239
+ print "#{key}\t: #{value}\n"
240
+ end
241
+ def apache_test
242
+ exec("apachectl -t")
243
+ end
244
+ end
@@ -0,0 +1,16 @@
1
+ require 'vhost_admin/base'
2
+
3
+ class VhostAdmin::Runner < Thor
4
+ desc "add_domain", "add a domain"
5
+ method_options :crypt => :boolean
6
+ def add_domain(domain, password, user=nil)
7
+ admin = VhostAdmin::Base.new(options)
8
+ admin.add_domain(domain, password, user)
9
+ end
10
+
11
+ desc "delete_domain", "delete a domain"
12
+ def delete_domain(domain, user=nil)
13
+ admin = VhostAdmin::Base.new
14
+ admin.delete_domain(domain, user)
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ class VhostAdmin
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vhost_admin/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.add_dependency 'thor'
8
+ gem.add_dependency 'postfix_admin'
9
+
10
+ gem.name = "vhost_admin"
11
+ gem.version = VhostAdmin::VERSION
12
+ gem.authors = ["Hitoshi Kurokawa"]
13
+ gem.email = ["hitoshi@nextseed.jp"]
14
+ gem.description = %q{Command Line Tools to manage virtual hosts fo Apache and Postfix}
15
+ gem.summary = gem.description
16
+ gem.homepage = ""
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vhost_admin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hitoshi Kurokawa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: postfix_admin
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Command Line Tools to manage virtual hosts fo Apache and Postfix
47
+ email:
48
+ - hitoshi@nextseed.jp
49
+ executables:
50
+ - vhost_admin
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - bin/vhost_admin
60
+ - lib/vhost_admin.rb
61
+ - lib/vhost_admin/base.rb
62
+ - lib/vhost_admin/runner.rb
63
+ - lib/vhost_admin/version.rb
64
+ - vhost_admin.gemspec
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Command Line Tools to manage virtual hosts fo Apache and Postfix
89
+ test_files: []