vagrant-dnsdock-hostupdater 0.0.10

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.
@@ -0,0 +1,141 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Hosts.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require_relative '../hosts'
21
+
22
+ module Aef
23
+ module Hosts
24
+
25
+ # This represents a section as element of a hosts file. It consists of a
26
+ # header, futher included elements and a footer
27
+ class Section < Element
28
+
29
+ # Title of the section
30
+ #
31
+ # @return [String]
32
+ attr_reader :name
33
+
34
+ # Elements inside the section
35
+ #
36
+ # @note If the Array is modified in place, you need to manually
37
+ # invalidate the cache with option :only_section => true.
38
+ # @see #invalidate_cache!
39
+ # @return [Array<Aef::Host::Element>]
40
+ attr_reader :elements
41
+
42
+ # Initializes a section
43
+ #
44
+ # @param [String] name title of the section
45
+ # @param [Hash] options
46
+ # @option options [String] :cache sets a cached String representation
47
+ # @option options [Array<Aef::Hosts::Element>] :elements a list of
48
+ # elements in the section
49
+ def initialize(name, options = {})
50
+ validate_options(options, :cache, :elements)
51
+
52
+ raise ArgumentError, 'Name cannot be empty' unless name
53
+
54
+ @name = name.to_s
55
+ @elements = options[:elements] || []
56
+ @cache = options[:cache] || {:header => nil, :footer => nil}
57
+ end
58
+
59
+ # Deletes the cached String representation
60
+ #
61
+ # @param [Hash] options
62
+ # @option [true, false] :only_section if set to true, the invalidation
63
+ # will not cascade onto the elements. Default is false.
64
+ # @return [Aef::Hosts::Section] a self reference
65
+ def invalidate_cache!(options = {})
66
+ @cache = {:header => nil, :footer => nil}
67
+
68
+ unless options[:only_section]
69
+ elements.each do |element|
70
+ element.invalidate_cache!
71
+ end
72
+ end
73
+ end
74
+
75
+ # Tells if a String representation is cached or not
76
+ #
77
+ # @return [true, false] true if cache is not empty
78
+ def cache_filled?
79
+ !!@cache[:header] && !!@cache[:footer]
80
+ end
81
+
82
+ # Sets the title of the section
83
+ def name=(name)
84
+ set_if_changed(:name, name.to_s) do
85
+ invalidate_cache!(:only_section => true)
86
+ end
87
+ end
88
+
89
+ # Sets the elements inside the section
90
+ def elements=(elements)
91
+ set_if_changed(:elements, [*elements]) do
92
+ invalidate_cache!(:only_section => true)
93
+ end
94
+ end
95
+
96
+ # A String representation for debugging purposes
97
+ #
98
+ # @return [String]
99
+ def inspect
100
+ generate_inspect(self, :name, :cache, :elements)
101
+ end
102
+
103
+ protected
104
+
105
+ # Defines the algorithm to generate a String representation from scratch.
106
+ #
107
+ # @return [String] a generated String representation
108
+ def generate_string(options = {})
109
+ string = ''
110
+
111
+ string << "# -----BEGIN SECTION #{name}-----\n"
112
+
113
+ @elements.each do |element|
114
+ string << element.to_s(options)
115
+ end
116
+
117
+ string << "# -----END SECTION #{name}-----\n"
118
+
119
+ string
120
+ end
121
+
122
+ # Defines the algorithm to construct the String representation from cache
123
+ #
124
+ # @return [String] the cached String representation
125
+ def cache_string(options = {})
126
+ string = ''
127
+
128
+ string << @cache[:header]
129
+
130
+ @elements.each do |element|
131
+ string << element.to_s(options)
132
+ end
133
+
134
+ string << @cache[:footer]
135
+
136
+ string
137
+ end
138
+
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Hosts.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ module Aef
21
+ module Hosts
22
+
23
+ # The currently loaded library version
24
+ #
25
+ # Using Semantic Versioning (2.0.0-rc.1) rules
26
+ # @see http://semver.org/
27
+ VERSION = '0.1.1'.freeze
28
+ end
29
+ end
@@ -0,0 +1,148 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2009-2013
4
+
5
+ This file is part of Linebreak.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'set'
21
+
22
+ # Namespace for projects of Alexander E. Fischer <aef@raxys.net>.
23
+ #
24
+ # If you want to be able to simply type Example instead of Aef::Example to
25
+ # address classes in this namespace simply write the following before using the
26
+ # classes.
27
+ #
28
+ # @example Including the namespace
29
+ # include Aef
30
+ # @author Alexander E. Fischer
31
+ module Aef
32
+
33
+ # Namespace for the linebreak library
34
+ module Linebreak
35
+
36
+ # Mapping table from symbol to actual linebreak sequence
37
+ #
38
+ # @private
39
+ BREAK_BY_SYSTEM = {
40
+ :unix => "\n",
41
+ :windows => "\r\n",
42
+ :mac => "\r"
43
+ }
44
+
45
+ # Mapping table from actual linebreak sequence to symbol
46
+ #
47
+ # @private
48
+ SYSTEM_BY_BREAK = BREAK_BY_SYSTEM.invert
49
+
50
+ # Regular expression for linebreak detection and extraction
51
+ #
52
+ # @private
53
+ BREAK_REGEXP = /(\r\n|[\r\n])/
54
+
55
+ # Detects encoding systems of a string.
56
+ #
57
+ # @param [String] input a String to be analysed
58
+ # @return [Set<Symbol>] the encoding systems present in the given String
59
+ def self.encodings(input)
60
+ if input.respond_to?(:to_s) then input = input.to_s
61
+ else raise ArgumentError, 'Input needs to be a string or must support to_s' end
62
+
63
+ occurences = Set.new
64
+
65
+ input.scan(BREAK_REGEXP).each do |linebreak|
66
+ occurences << SYSTEM_BY_BREAK[linebreak.first]
67
+ end
68
+
69
+ occurences
70
+ end
71
+
72
+ # Checks whether a string includes linebreaks of all the given encoding
73
+ # systems.
74
+ #
75
+ # @param [String] input a String to be analysed
76
+ # @param [Array<Symbol>] encodings one or more encoding systems
77
+ # @return [true, false] true if all of the given linebreak systems are
78
+ # present in the given String
79
+ def self.encoding?(input, *encodings)
80
+ systems = BREAK_BY_SYSTEM.keys
81
+
82
+ encodings.flatten!
83
+ encodings.each do |encoding|
84
+ unless systems.include?(encoding)
85
+ raise ArgumentError,
86
+ %{Invalid encoding system. Available systems: #{systems.join(', ')}. Arguments are expected as symbols or an array of symbols.}
87
+ end
88
+ end
89
+
90
+ Aef::Linebreak.encodings(input) == Set.new(encodings)
91
+ end
92
+
93
+ # Create a copy of a string with all the string's linebreaks replaced by
94
+ # linebreaks of a specific system or a given replacement.
95
+ #
96
+ # @overload encode(input, system)
97
+ # @param [String] input a String as conversion template
98
+ # @param [:unix, :windows, :mac] system a target linebreak system
99
+ #
100
+ # @overload encode(input, replacement)
101
+ # @param [String] input a String as conversion template
102
+ # @param [String] replacement a String to be the replacement for all
103
+ # linebreaks in the template
104
+ def self.encode(input, system_or_replacement = :unix)
105
+ if input.respond_to?(:to_s) then input = input.to_s
106
+ else raise ArgumentError, 'Input needs to be a string or must support to_s' end
107
+
108
+ input.gsub(BREAK_REGEXP,
109
+ BREAK_BY_SYSTEM[system_or_replacement] || system_or_replacement)
110
+ end
111
+
112
+ # Detects encoding systems of a string.
113
+ #
114
+ # This method is supposed to be used as a method of String.
115
+ #
116
+ # @return [Set<Symbol>] the encoding systems present in the String
117
+ def linebreak_encodings
118
+ Aef::Linebreak.encodings(self)
119
+ end
120
+
121
+ # Checks whether a string includes linebreaks of all the given encoding
122
+ # systems.
123
+ #
124
+ # This method is supposed to be used as a method of String.
125
+ #
126
+ # @param [Array<Symbol>] encodings one or more encoding systems
127
+ # @return [true, false] true if all of the given linebreak systems are
128
+ # present in the given String
129
+ def linebreak_encoding?(*encodings)
130
+ Aef::Linebreak.encoding?(self, encodings)
131
+ end
132
+
133
+ # Create a copy of a string with all the string's linebreaks replaced by
134
+ # linebreaks of a specific system or a given replacement.
135
+ #
136
+ # This method is supposed to be used as a method of String.
137
+ #
138
+ # @overload encode(system)
139
+ # @param [:unix, :windows, :mac] system a target linebreak system
140
+ #
141
+ # @overload encode(replacement)
142
+ # @param [String] replacement a String to be the replacement for all
143
+ # linebreaks in the template
144
+ def linebreak_encode(system_or_replacement = :unix)
145
+ Aef::Linebreak.encode(self, system_or_replacement)
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Hosts.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ # Helper file to allow loading by gem name. Creates an alias for Aef::Hosts
21
+ # named simply Hosts if this name isn't used otherwise.
22
+
23
+ require_relative 'aef/hosts'
24
+
25
+ ::Hosts = Aef::Hosts unless defined?(::Hosts)
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@raxys.net>, 2012
4
+
5
+ This file is part of Hosts.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ # Require this file if you don't want an alias for Aef::Hosts named simply
21
+ # Hosts.
22
+
23
+ require_relative '../hosts'
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+ # vi: set ft=ruby :
4
+ require 'docker'
5
+ require 'yaml'
6
+
7
+
8
+ # build up hash of id => project name
9
+ id_project_hash = {}
10
+ Docker::Container.all(all: true, filters: { status: ["running"] }.to_json).each do |container|
11
+ # puts container.info['id']
12
+ # puts container.info['Labels']['com.docker.compose.project'].inspect
13
+
14
+ id_project_hash[container.info['id']] = container.info['Labels']['com.docker.compose.project']
15
+ # puts container.info['NetworkSettings']['IPAddress']
16
+ # puts container.info.to_yaml
17
+ end
18
+
19
+
20
+ module HostManager
21
+ def self.register(hostname, ip)
22
+ entry = "#{ip} #{hostname}"
23
+ puts entry
24
+ end
25
+ end
26
+
27
+ Docker::Network.all.each do |network|
28
+ # puts network.info['Options'].inspect
29
+ # puts network.inspect
30
+ network.info['Containers'].each do |pair|
31
+ id = pair[0]
32
+ container = pair[1]
33
+ ip = container['IPv4Address'][/[^\/]+/]
34
+ if id_project_hash.key?(id) and id_project_hash[id]
35
+ project_name = id_project_hash[id]
36
+ HostManager.register("#{container['Name']}.#{project_name}.local", ip)
37
+ elsif
38
+ HostManager.register("#{container['Name']}.local", ip)
39
+ end
40
+ end
41
+ end
42
+
43
+ # Docker::Event.stream { |event|
44
+ # # respond to all events
45
+ # puts event.Attributes.name;
46
+ # }
data/lib/server.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative 'host-manager'
2
+
3
+ HostManager::Server.create('0.0.0.0', 2991)
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+
3
+ begin
4
+ require 'vagrant'
5
+ rescue LoadError
6
+ Bundler.require(:default, :development)
7
+ end
8
+
9
+ require 'vagrant-dnsdock-hostupdater/plugin'
10
+ require 'vagrant-dnsdock-hostupdater/command'
@@ -0,0 +1,12 @@
1
+ module Vagrant
2
+ module DnsdockHostUpdater
3
+ class Command < Vagrant.plugin('2', :command)
4
+ def execute
5
+ puts 'command firing...'
6
+ exec('echo "Command fired!"')
7
+ 0
8
+ end
9
+ end
10
+ end
11
+
12
+ end