vhost 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9aad392914b962931527c78446853ca156414112
4
+ data.tar.gz: afb156d0697ef7868750e30f56a5927a0751c26b
5
+ SHA512:
6
+ metadata.gz: 441dc776b078d335b60ae523dfea81e1980893bf7da8fb0b042f74ab31c1f999655527a95a6db96ec6b187549e283db4654af16e2cabb262c602f0e2e9f8d875
7
+ data.tar.gz: c59361cf0ae4bd8f161fc6d334cad5d24bfe9966dc384d96f1cee43237240481c8a04f796572d96f16449ff21ca0c5f9c5e17beb9493be0797da8828ac607d07
data/bin/vhost ADDED
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'optparse'
5
+ require 'colorize'
6
+ require_relative '../lib/vhost.rb'
7
+
8
+ options = {}
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: vhost NAME [options]"
11
+
12
+ opts.on "-c", "--create", "Create vhost" do |o| options[:create] = o end
13
+ opts.on "-m", "--modify", "--edit", "Modify vhost" do |o| options[:modify] = o end
14
+ opts.on "-e", "--enable", "Enable vhost" do |o| options[:enable] = o end
15
+ opts.on "-d", "--disable", "Disable vhost" do |o| options[:disable] = o end
16
+ opts.on "--delete", "delete vhost" do |o| options[:delete] = o end
17
+ opts.on "-r", "--restart", "Run server restart command" do |o| options[:restart] = o end
18
+ opts.on "--create-folder", "create site folder structure" do |o| options[:folder] = o end
19
+ opts.on "-p", "--path", "Show a site folder's path" do |o| options[:path] = o end
20
+ opts.on "-l", "--list", "List sites" do |o| options[:list] = o end
21
+ opts.on "--list-available", "List sites available" do |o| options[:lista] = o end
22
+ opts.on "--list-enabled", "List sites enabled" do |o| options[:liste] = o end
23
+ opts.on "-i", "--info", "Show vhost info" do |o| options[:info] = o end
24
+ opts.on "-f", "--folder", "Show folder" do |o| options[:show] = o end
25
+
26
+ # ==========================
27
+ # = Configuration Options: =
28
+ # ==========================
29
+
30
+ opts.separator ""
31
+ opts.separator "Configuration options:"
32
+
33
+ opts.on "-q", "--quiet", "Surpress output" do |o| options[:quiet] = o end
34
+
35
+ opts.on "--edit-conf", "Edit vhosts.yml" do |o|
36
+ system Vhost.conf['editor'], Vhost.conf_file
37
+ exit
38
+ end
39
+
40
+ opts.on "--conf [KEY | KEY=VALUE]", "Change/show configuration" do |e|
41
+ if e =~ /\=/i
42
+ e = e.split("=")
43
+ Vhost.conf[e[0]] = e[1]
44
+ Vhost.save_conf
45
+ else
46
+ puts Vhost.conf[e]
47
+ end
48
+ exit
49
+ end
50
+
51
+ # ==================
52
+ # = Common Options =
53
+ # ==================
54
+
55
+ opts.separator ""
56
+ opts.separator "Common options:"
57
+
58
+ opts.on "-v", "--version", "Print version" do
59
+ puts "vhost - version #{Vhost::VERSION}", "https://github.com/SleepingInsomniac/vhost"
60
+ exit
61
+ end
62
+
63
+ opts.on "-h", "--help", "Prints this help" do
64
+ puts opts
65
+ exit
66
+ end
67
+
68
+ end.parse!
69
+
70
+ abort "Usage:\tvhost NAME [options]\n\tvhost -h for more information" if options.empty? and ARGV[0].nil?
71
+
72
+ @options = options
73
+ def puts(*args)
74
+ super(*args) unless @options[:quiet]
75
+ end
76
+
77
+ if options[:create]
78
+ vhost = Vhost.find(ARGV[0])
79
+ abort "#{vhost.basename} exists!" if Vhost.find(ARGV[0])
80
+ vhost = Vhost.create(ARGV[0])
81
+ puts "#{vhost.basename} created" if vhost
82
+ end
83
+
84
+ vhost = Vhost.find(ARGV[0])
85
+ abort "#{ARGV[0]} doesn't exist" unless vhost
86
+ abort "#{vhost.basename} exists" if options.empty? and not ARGV[0].nil?
87
+
88
+ if options[:delete]
89
+ print "Really delete #{vhost.basename}? [y/n]: "
90
+ confirm = STDIN.gets.chomp
91
+ if confirm =~ /y/i
92
+ vhost.delete!
93
+ puts "#{vhost.basename} deleted"
94
+ exit
95
+ end
96
+ end
97
+
98
+ if options[:modify]
99
+ system Vhost.conf['editor'], vhost.paths[:available]
100
+ end
101
+
102
+ if options[:folder]
103
+ structure = ['/', '/public', '/log']
104
+ structure.each do |folder|
105
+ folder = File.join(Vhost.conf['sites_folder'], vhost.basename, folder)
106
+ FileUtils.mkdir_p folder
107
+ puts "Created #{folder}"
108
+ end
109
+ end
110
+
111
+ if options[:path]
112
+ puts File.join(Vhost.conf['sites_folder'], vhost.basename)
113
+ end
114
+
115
+ if options[:enable]
116
+ abort "#{vhost.basename} already enabled" if vhost.enabled?
117
+ vhost.enable
118
+ puts "#{vhost.basename} Enabled"
119
+ end
120
+
121
+ if options[:disable]
122
+ abort "#{vhost.basename} not enabled" unless vhost.enabled?
123
+ vhost.disable
124
+ puts "#{vhost.basename} disabled"
125
+ end
126
+
127
+ if options[:list]
128
+ puts "Sites available:"
129
+ system "ls", File.join(Vhost.conf['server_conf'], 'sites-available')
130
+ puts "\nSites enabled:"
131
+ system "ls", File.join(Vhost.conf['server_conf'], 'sites-enabled')
132
+ end
133
+
134
+ if options[:lista]
135
+ system "ls", File.join(Vhost.conf['server_conf'], 'sites-available')
136
+ end
137
+
138
+ if options[:liste]
139
+ system "ls", File.join(Vhost.conf['server_conf'], 'sites-enabled')
140
+ end
141
+
142
+ if options[:info]
143
+ puts "Name: #{vhost.basename}"
144
+ puts "Config: #{vhost.paths[:available]}"
145
+ puts "Enabled: #{vhost.enabled?}"
146
+ puts "Folder: #{vhost.paths[:folder]}"
147
+ end
148
+
149
+ if options[:show]
150
+ puts vhost.paths[:folder]
151
+ end
152
+
153
+ puts `#{Vhost.conf['restart_cmd']}` if options[:restart]
data/lib/vhost.rb ADDED
@@ -0,0 +1,155 @@
1
+ # ======================
2
+ # = Vhost - Alex Clink =
3
+ # ======================
4
+ # http://alexclink.com
5
+
6
+ class Vhost
7
+
8
+ require 'fileutils'
9
+ require 'erubis'
10
+ require 'yaml'
11
+
12
+ VERSION = "1.1.3"
13
+ CONF_NAME = "vhosts.yml"
14
+ CONFIG_PATHS = [
15
+ 'vhosts-conf',
16
+ '../vhosts-conf',
17
+ '../etc/vhosts-conf',
18
+ '/usr/local/etc/vhosts-conf',
19
+ '/etc/vhosts-conf'
20
+ ]
21
+
22
+ DEFAULT_TEMPLATE = <<-TEMPLATE
23
+ <% site_root = File.join(conf['sites_folder'], vhost, "public") %>
24
+ <% site_name = vhost %>
25
+ server {
26
+ listen 80;
27
+ root <%= site_root %>;
28
+ server_name <%= site_name %>;
29
+ index index.html;
30
+ }
31
+ TEMPLATE
32
+
33
+ DEFAULT_CONFIG = {
34
+ 'server_conf' => '/usr/local/etc/nginx',
35
+ 'sites_folder' => '/var/www',
36
+ 'default_template' => nil,
37
+ 'restart_cmd' => 'sudo nginx -s reload',
38
+ 'editor:' => 'emacs'
39
+ }.to_yaml
40
+
41
+ def self.load_conf(paths = CONFIG_PATHS)
42
+ Dir.chdir File.expand_path(File.dirname(__FILE__))
43
+ @@config_path = CONFIG_PATHS.select { |path| File.exist? path }.first
44
+ puts "Can't find configuration!\nSearch locations:\n#{CONFIG_PATHS.join("\n")}" if @@config_path.nil?
45
+ @@config_file = File.join(@@config_path, 'vhosts.yml')
46
+
47
+ begin
48
+ @@conf = YAML.load_file @@config_file
49
+ rescue
50
+ @@conf = YAML.parse DEFAULT_CONFIG
51
+ end
52
+
53
+ @@conf
54
+ end
55
+
56
+ self.load_conf
57
+ @@available_path = File.join(@@conf['server_conf'], 'sites-available')
58
+ @@enabled_path = File.join(@@conf['server_conf'], 'sites-enabled')
59
+
60
+ def self.conf
61
+ @@conf
62
+ end
63
+
64
+ def self.conf_file
65
+ @@config_file
66
+ end
67
+
68
+ def self.save_conf
69
+ File.open @@config_file, "w" do |file|
70
+ file.write @@conf.to_yaml
71
+ end
72
+ end
73
+
74
+ def self.available
75
+ Dir[File.join(@@available_path, '*')]
76
+ end
77
+
78
+ def self.enabled
79
+ Dir[File.join(@@enabled_path, '*')]
80
+ end
81
+
82
+ def self.all
83
+ self.available.map do |path|
84
+ Vhost.find File.basename(path)
85
+ end
86
+ end
87
+
88
+ def self.find(name)
89
+ name = Dir[File.join(@@available_path, "#{name}*")].first
90
+ return false if name.nil?
91
+
92
+ name = File.basename(name)
93
+ Vhost.new(name)
94
+ end
95
+
96
+ def self.create(name)
97
+ return false if Vhost.find(name)
98
+
99
+ File.open File.join(@@available_path, "#{name}.conf"), "w" do |f|
100
+ begin
101
+ template = Erubis::Eruby.new File.read(File.join(@@config_path, 'templates', @@conf['default_template']))
102
+ rescue
103
+ puts "Error: No template found, Using default template"
104
+ template = Erubis::Eruby.new DEFAULT_TEMPLATE
105
+ end
106
+ f.write template.result(vhost: name, conf: @@conf)
107
+ end
108
+
109
+ Vhost.find(name)
110
+ end
111
+
112
+ # ====================
113
+ # = Instance Methods =
114
+ # ====================
115
+
116
+ def initialize(name)
117
+ @name = name
118
+ @paths = {
119
+ enabled: File.join(@@conf['server_conf'], 'sites-enabled', @name),
120
+ available: File.join(@@conf['server_conf'], 'sites-available', @name),
121
+ folder: File.join(@@conf['sites_folder'], basename)
122
+ }
123
+ end
124
+
125
+ attr_reader :paths
126
+ attr_reader :name
127
+
128
+ def basename
129
+ File.basename(@name, ".conf")
130
+ end
131
+
132
+ def enabled?
133
+ File.exist? @paths[:enabled]
134
+ end
135
+
136
+ def enable
137
+ return false if enabled?
138
+ File.symlink @paths[:available], @paths[:enabled]
139
+ end
140
+
141
+ def disable
142
+ return false unless enabled?
143
+ File.unlink @paths[:enabled]
144
+ end
145
+
146
+ def delete!
147
+ File.unlink @paths[:enabled] if enabled?
148
+ File.unlink @paths[:available]
149
+ end
150
+
151
+ def to_s
152
+ basename
153
+ end
154
+
155
+ end
@@ -0,0 +1,11 @@
1
+ <% site_root = File.join(conf['sites_folder'], vhost, "public") %>
2
+ <% site_name = vhost %>
3
+ <% error_log = File.join(conf['sites_folder'], vhost, "log", "apache-error.log") %>
4
+ <% access_log = File.join(conf['sites_folder'], vhost, "log", "apache-access.log") %>
5
+
6
+ <VirtualHost *:80>
7
+ ServerName <%= site_name %>
8
+ DocumentRoot "<%= site_root %>"
9
+ ErrorLog "<%= error_log %>"
10
+ CustomLog "<%= access_log %>"
11
+ </VirtualHost>
@@ -0,0 +1,22 @@
1
+ <% site_root = File.join(conf['sites_folder'], vhost, "public") %>
2
+ <% site_name = vhost %>
3
+ <% error_log = File.join(conf['sites_folder'], vhost, "log", "nginx-error.log") %>
4
+ <% access_log = File.join(conf['sites_folder'], vhost, "log", "nginx-access.log") %>
5
+
6
+ server {
7
+ listen 80;
8
+
9
+ root <%= site_root %>;
10
+ server_name <%= site_name %>;
11
+ error_log <%= error_log %>;
12
+ access_log <%= access_log %>;
13
+
14
+ index index.php index.html;
15
+
16
+ location / {
17
+ # include /usr/local/etc/nginx/conf.d/php-fpm; # enable php
18
+ # passenger_enabled on # enabled passenger for rack based apps
19
+ # autoindex on; # just for development
20
+ }
21
+
22
+ }
@@ -0,0 +1,6 @@
1
+ ---
2
+ server_conf: /usr/local/etc/nginx
3
+ sites_folder: /var/www
4
+ default_template: nginx.conf.erb
5
+ restart_cmd: sudo nginx -s reload
6
+ editor: emacs
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vhost
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Alex Clink
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: erubis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ description: Create, enabled, disable, and delete virtual hosts with ease.
42
+ email: code@alexclink.com
43
+ executables:
44
+ - vhost
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/vhost
49
+ - lib/vhost.rb
50
+ - vhosts-conf/templates/apache.conf.erb
51
+ - vhosts-conf/templates/nginx.conf.erb
52
+ - vhosts-conf/vhosts.yml
53
+ homepage: https://github.com/sleepinginsomniac/vhost
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - vhost-conf
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.4.5
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Take control of your virtual hosts.
77
+ test_files: []