buildhosts 0.0.2 → 0.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03ec0ab7bc45e163d3c1f018b00ca2e557136c17
4
- data.tar.gz: 9da6efcacf54aa3940dfb4260b98e173ed1513c7
3
+ metadata.gz: cd39ee6d2cf4a7bda58a8b798a3b75909095fde6
4
+ data.tar.gz: f77559e91525d86add3987b6509034aac2b5a46f
5
5
  SHA512:
6
- metadata.gz: a32c8a1d188a32570219591ac1511df36a8023f4bd2f95534c02bf8670c97d6d6d28b369834f186bd9f04bc3bcc2547b862fd18a62cd57de3a404d727d4a6723
7
- data.tar.gz: 7ae6b0dc91ab805800ea12fe8575dfed806c91b74332e5f9a21b08b1c5beca7b0c385b0664b642585cfd02097b2838f52a18c3ecfc6b797b6bdd23e04641b7ec
6
+ metadata.gz: 07d66f06173d5a1e5b408c7fe80ea4a925765a635c049f72b94fd57b6b24b95b0a0142857bc1cd150fcd7430b640c1c3a273b1cf381b9125835725fb093532e5
7
+ data.tar.gz: 34ccdf1682c6b1b1fd97a8f7fe48001f98ca95ee2d3abdb37d88c361eb2f46128b9e3e5d18a1c80387a9eddee62e3b95368c97b92e8f06aa066561c06f01d7e6
data/bin/buildhosts CHANGED
@@ -1,44 +1,28 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'buildhosts'
4
+ require 'buildhosts/Manginx'
4
5
 
5
- $path = File.expand_path('../../', __FILE__)
6
-
7
- $files = Hash.new
8
-
9
- def find_files files
10
- files.each do |f|
11
- if (File.exist?(File.expand_path("~/.buildhosts/#{f}")))
12
- $files[f] = File.expand_path("~/.buildhosts/#{f}").chomp
13
- else
14
- $files[f] = "#{$path}/#{f}".chomp
15
- end
16
- end
17
- end
18
-
19
- find_files ['config', 'custom', 'header', 'newhosts', 'temp']
6
+ main = Buildhosts::Main.new
20
7
 
21
8
  case ARGV[0]
22
9
  when "-e"
23
- system("vim #{$files['config']}")
10
+ system("vim #{main.files['config']}")
24
11
  when "-c"
25
- system("vim #{$files['custom']}")
12
+ system("vim #{main.files['custom']}")
26
13
  when "-ec"
27
- system("vim #{$files['config']} ; vim #{$files['custom']}")
14
+ system("vim #{main.files['config']} ; vim #{main.files['custom']}")
28
15
  when "-ce"
29
- system("vim #{$files['custom']} ; vim #{$files['config']}")
16
+ system("vim #{main.files['custom']} ; vim #{main.files['config']}")
30
17
  when "-l"
31
18
  system("grep 127.0.0.1 /etc/hosts | grep -Ev 'xip.io|localhost' | awk '{print $2}'")
32
19
  exit
33
20
  when "--nginx", "-nginx"
34
- puts "TODO: run manginx..."
21
+ Buildhosts::Manginx.new(main.files['config']).run
35
22
  exit
36
23
  end
37
24
 
38
25
  # Do all the stuff
39
26
  puts "Building..."
40
- Buildhosts::Main.build $files['config'], $files['newhosts']
41
- system("cat #{$files['header']} #{$files['custom']} #{$files['newhosts']} > #{$files['temp']}")
42
- system("sudo cp #{$files['temp']} /etc/hosts")
43
- system("rm #{$files['newhosts']} #{$files['temp']}")
27
+ main.build
44
28
  puts "Done!"
data/lib/buildhosts.rb CHANGED
@@ -2,18 +2,32 @@ require "buildhosts/version"
2
2
  require "buildhosts/ConfigParser"
3
3
 
4
4
  module Buildhosts
5
- class Main
6
- def self.build config, newhosts
5
+ class Main
6
+ attr_reader :files
7
+ attr_reader :path
8
+
9
+ def initialize
10
+ @path = File.expand_path('../../', __FILE__)
11
+ @files = Hash.new
12
+ ['config', 'custom', 'header', 'newhosts', 'temp'].each do |f|
13
+ if (File.exist?(File.expand_path("~/.buildhosts/#{f}")))
14
+ @files[f] = File.expand_path("~/.buildhosts/#{f}").chomp
15
+ else
16
+ @files[f] = "#{@path}/#{f}".chomp
17
+ end
18
+ end
19
+ end
20
+
21
+ def build
7
22
  # Prefixy strings
8
23
  ipv6 = '::1 '
9
24
  home = '127.0.0.1 '
10
25
 
11
26
  # Grab that parser and parse
12
-
13
- conf = Buildhosts::ConfigParser.parse config
27
+ conf = Buildhosts::ConfigParser.parse @files['config']
14
28
 
15
29
  # Write the lines to a "newhosts" file
16
- out = File.open(newhosts, 'w+')
30
+ out = File.open(@files['newhosts'], 'w+')
17
31
  conf['hosts'].each do |host|
18
32
  out.write "#{ipv6}#{host}\n"
19
33
  out.write "#{home}#{host}\n"
@@ -23,6 +37,9 @@ module Buildhosts
23
37
  end
24
38
  end
25
39
  out.close
40
+ system("cat #{@files['header']} #{@files['custom']} #{@files['newhosts']} > #{@files['temp']}")
41
+ system("sudo cp #{@files['temp']} /etc/hosts")
42
+ system("rm #{@files['newhosts']} #{@files['temp']}")
26
43
  end
27
44
  end
28
45
  end
@@ -0,0 +1,67 @@
1
+ require 'buildhosts/ConfigParser'
2
+
3
+ module Buildhosts
4
+ class Manginx
5
+
6
+ def initialize config
7
+ # Set up teh parser and parse with it
8
+ @conf = Buildhosts::ConfigParser.parse config
9
+ end
10
+
11
+ # Helper methods to print common messages
12
+ def configuring what
13
+ puts "Configuring: #{what}..."
14
+ end
15
+ def done next_step=''
16
+ puts "Done! #{next_step}"
17
+ end
18
+
19
+ def run
20
+ # If you don't have any IP addresses in your config, what are you doing here?
21
+ if @conf['ips'].nil? || @conf['ips'].empty?
22
+ puts 'What\'s the point?'
23
+ exit
24
+ end
25
+ # Let's find out what nginx is expecting from us...
26
+ expected = Array.new
27
+ n = File.open('/usr/local/etc/nginx/nginx.conf', 'r')
28
+ n.each_line do |line|
29
+ # So basically, if the line goes "include xip/derp.local;", we want to
30
+ # set up a file with that name with the appropriate server_name directives.
31
+ match = line.match(/include\s+xip\/([\w\.]+);/)
32
+ if !match.nil? # We don't want to try to convert nil to an array...
33
+ expected << match.to_a[1..-1][0]
34
+ end
35
+ end
36
+
37
+ # This is where the files will be stored.
38
+ base_path = '/usr/local/etc/nginx/xip'
39
+ `rm #{base_path}/*`
40
+
41
+ # Le bread and buttere
42
+ expected.each do |file|
43
+ configuring file
44
+ out = File.open("#{base_path}/#{file}", 'w+')
45
+ @conf['ips'].each do |ip|
46
+ # Simple. Just put a line in there for each IP in our config file.
47
+ out.write "server_name #{file}.#{ip}.xip.io;\n"
48
+ end
49
+ out.close
50
+ end
51
+
52
+ # Now we just reload nginx and apologize if something goes rotten.
53
+ done 'Reloading nginx...'
54
+ if system('nginx -s reload')
55
+ done
56
+ else
57
+ puts "Oops!\nSomething went wrong... Sorry."
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+
64
+
65
+
66
+
67
+
@@ -1,3 +1,3 @@
1
1
  module Buildhosts
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buildhosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-01 00:00:00.000000000 Z
11
+ date: 2013-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,7 +60,7 @@ files:
60
60
  - header
61
61
  - lib/buildhosts.rb
62
62
  - lib/buildhosts/ConfigParser.rb
63
- - lib/buildhosts/manginx.rb
63
+ - lib/buildhosts/Manginx.rb
64
64
  - lib/buildhosts/version.rb
65
65
  - lib/old.buildhosts.rb
66
66
  - sample-config
@@ -1,56 +0,0 @@
1
- # Start by load the config parser
2
- require './ConfigParser.rb'
3
-
4
- # Helper methods to print common messages
5
- def configuring what
6
- puts "Configuring: #{what}..."
7
- end
8
-
9
- def done next_step=''
10
- puts "Done! #{next_step}"
11
- end
12
-
13
- # Set up teh parser and parse with it
14
- parser = ConfigParser.new
15
- conf = parser.parse 'config'
16
-
17
- # If you don't have any IP addresses in your config, what are you doing here?
18
- if conf['ips'].nil? || conf['ips'].empty?
19
- puts 'What\'s the point?'
20
- exit
21
- end
22
-
23
- # Let's find out what nginx is expecting from us...
24
- expected = Array.new
25
- n = File.open('/usr/local/etc/nginx/nginx.conf', 'r')
26
- n.each_line do |line|
27
- # So basically, if the line goes "include xip/derp.local;", we want to
28
- # set up a file with that name with the appropriate server_name directives.
29
- match = line.match(/include\s+xip\/([\w\.]+);/)
30
- if !match.nil? # We don't want to try to convert nil to an array...
31
- expected << match.to_a[1..-1][0]
32
- end
33
- end
34
-
35
- # This is where the files will be stored.
36
- base_path = '/usr/local/etc/nginx/xip'
37
- `rm #{base_path}/*`
38
-
39
- # Le bread and buttere
40
- expected.each do |file|
41
- configuring file
42
- out = File.open("#{base_path}/#{file}", 'w+')
43
- conf['ips'].each do |ip|
44
- # Simple. Just put a line in there for each IP in our config file.
45
- out.write "server_name #{file}.#{ip}.xip.io;\n"
46
- end
47
- out.close
48
- end
49
-
50
- # Now we just reload nginx and apologize if something goes rotten.
51
- done 'Reloading nginx...'
52
- if system('nginx -s reload')
53
- done
54
- else
55
- puts "Oops!\nSomething went wrong... Sorry."
56
- end