maphosts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d39079162ebdbf9644a0a27ebbd45c5e88eccf7
4
+ data.tar.gz: a1a5c28e6db974fa6fe451280224af3bdd3ed151
5
+ SHA512:
6
+ metadata.gz: 2c5bb2f159cc9f3978bead0e5412a39fc38f76f2d20e1437f39a767af58c8494f42205de37bc2b50e60b28fc7417d9c6b17bee3dabe812e36ba9b131d751671f
7
+ data.tar.gz: a335998391852ad36d49c589087f29e939377e64f53760d14022ab7787b53a8c7db3ed712c08a1453411675897adc6f63c46537ea7698dab0d7166e77d7348bf
data/bin/maphosts ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+ require 'colorize'
3
+ require 'maphosts/host_manager'
4
+
5
+ hostsfile_path = '/etc/hosts'
6
+ ip = '127.0.0.1'
7
+ verbose = false
8
+
9
+ OptionParser.new do |options|
10
+ options.banner = "Usage: #{File.basename(__FILE__)} hosts..."
11
+
12
+ options.on "-t", "--to IP" do |value|
13
+ ip = value
14
+ end
15
+
16
+ options.on "-h", "--hostsfile FILE" do |path|
17
+ hostsfile_path = path
18
+ end
19
+
20
+ options.on "-v", "--verbose" do
21
+ verbose = true
22
+ end
23
+ end.parse!
24
+
25
+
26
+ if !File.exists?(hostsfile_path)
27
+ STDERR.puts "#{hostsfile_path}: no such file"
28
+ exit 1
29
+ end
30
+
31
+ hosts = ARGV
32
+
33
+ patch = HostManager.new(hostsfile_path, hosts, File.expand_path(Dir.pwd), ip, verbose).patch
34
+ if !patch
35
+ puts "No update of #{hostsfile_path} required"
36
+ exit
37
+ end
38
+
39
+ puts "Updating #{hostsfile_path}"
40
+ begin
41
+ patch.write
42
+ rescue
43
+ puts "Unable to write to #{hostsfile_path}. Trying again as root.".yellow
44
+ cmd = "echo #{patch.to_s.shellescape} > #{hostsfile_path.shellescape}"
45
+
46
+ begin
47
+ `sudo /bin/sh -c #{cmd.shellescape}`
48
+ rescue Interrupt
49
+ puts "\n" + patch.to_s
50
+ puts "\nYou can also apply the patch by coping the lines above to #{hostsfile_path} by hand.".red
51
+ end
52
+ end
@@ -0,0 +1,73 @@
1
+ require 'hosts'
2
+ require 'optparse'
3
+ require 'shellwords'
4
+
5
+ class HostManager
6
+ def initialize(hostsfile_path, hosts, section_name, ip, verbose = false)
7
+ @hostsfile = Hosts::File.read(hostsfile_path)
8
+ @hosts = hosts
9
+ @section = init_section("maphosts - #{section_name}")
10
+ @ip = ip
11
+ @dirty = false
12
+ @verbose = verbose
13
+ end
14
+
15
+ def patch
16
+ migrate_hosts(@section, @hostsfile, @hosts)
17
+
18
+ @hosts.each do |hostname|
19
+ host = @section.elements.find { |h| h.respond_to?(:name) && h.name == hostname }
20
+
21
+ if !host
22
+ log '+', hostname, "will be added"
23
+ @section.elements << Hosts::Entry.new(@ip, hostname)
24
+ @dirty = true
25
+ else
26
+ if host.address != @ip
27
+ log '*', hostname, "is pointing to #{host.address} instead of #{@ip}"
28
+ host.address = @ip
29
+ @dirty = true
30
+ else
31
+ log '*', hostname, "is already set up correctly"
32
+ end
33
+ end
34
+ end
35
+
36
+ if !@dirty
37
+ nil
38
+ else
39
+ @hostsfile.to_s.strip!
40
+ end
41
+ end
42
+
43
+ private
44
+ def log(symbol, hostname, status)
45
+ if @verbose
46
+ puts "#{symbol} #{hostname} #{status}"
47
+ end
48
+ end
49
+
50
+ def init_section(identifier)
51
+ @section = @hostsfile.elements.find {|e| e.respond_to?(:name) && e.name == identifier}
52
+
53
+ if !@section
54
+ @section = Hosts::Section.new(identifier)
55
+ @hostsfile.elements << @section
56
+ end
57
+
58
+ @section
59
+ end
60
+
61
+ def migrate_hosts(section, hostsfile, hosts)
62
+ hosts.each do |hostname|
63
+ host = hostsfile.elements.find { |h| h.respond_to?(:name) && h.name == hostname }
64
+
65
+ if host
66
+ hostsfile.elements.delete host
67
+ section.elements << host
68
+
69
+ log '*', hostname, "will be migrated"
70
+ end
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maphosts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marc Scholten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hosts
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Small command line application for keeping your project hostnames in
56
+ sync with /etc/hosts
57
+ email: marc@pedigital.de
58
+ executables:
59
+ - maphosts
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/maphosts/host_manager.rb
64
+ - bin/maphosts
65
+ homepage: https://github.com/mpscholten/maphosts
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.1.10
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Keeps your project hostnames in sync with /etc/hosts
89
+ test_files: []