10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/cli/murakumo_options.rb', line 10
def murakumo_parse_args
optopus do
desc 'key for authentication (required)'
option :auth_key, '-K', '--auth-key STRING', :required => true
desc 'ip address to bind'
option :dns_address, '-a', '--address IP', :default => '0.0.0.0' do |value|
/\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/ =~ value or invalid_argument
end
desc 'port number of a name service'
option :dns_port, '-p', '--port NUM', :type => Integer, :default => 53
desc 'initial node list of gossip protocols'
option :initial_nodes, '-i', '--initial-nodes IP_LIST', :type => Array, :default => [] do |value|
value = value.map {|i| i.strip }
value.all? {|i| /\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/ =~ i } or invalid_argument
end
desc "host's resource record : <ip_addr>[,<hostname>[,<TTL>]] (required)"
option :host, '-H', '--host RECORD', :type => Array, :required => true do |value|
(1 <= value.length and value.length <= 3) or invalid_argument
value = value.map {|i| i.strip }
ip_addr, hostname, ttl = value
/\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/ =~ ip_addr or invalid_argument
/\A[0-9a-z\.\-]+\Z/i =~ hostname or invalid_argument
unless ttl.nil? or (/\A\d+\Z/ =~ ttl and ttl.to_i > 0)
invalid_argument
end
end
desc 'resource record of an alias: <hostname>[,<TTL>[,{master|secondary|backup}]]'
option :aliases, '-A', '--alias RECORD', :type => Array, :multiple => true do |value|
(1 <= value.length and value.length <= 3) or invalid_argument
value = value.map {|i| i.strip }
hostname, ttl, priority = value
/\A[0-9a-z\.\-]+\Z/ =~ hostname or invalid_argument
unless ttl.nil? or (/\A\d+\Z/ =~ ttl and ttl.to_i > 0)
invalid_argument
end
priority.nil? or /\A(master|secondary|backup)\Z/i =~ priority or invalid_argument
end
desc 'ip address of a default resolver'
option :resolver, '-r', '--resolver IP_LIST', :type => Array do |value|
value = value.map {|i| i.strip }
unless value.all? {|i| /\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/ =~ i }
invalid_argument
end
end
desc 'path of a socket file'
option :socket, '-S', '--socket PATH', :default => '/var/tmp/murakumo.sock'
desc 'maximum number of the IP address returned as a response'
option :max_ip_num, '-n', '--max-ip-num NUM', :type => Integer, :default => 8 do |value|
invalid_argument if value < 1
end
desc 'command of daemonize: {start|stop|restart|status}'
option :daemon, '-d', '--daemon CMD', :type => [:start, :stop, :restart, :status]
desc 'directory of a pid file'
option :pid_dir, '-f', '--pid-dir PATH'
desc 'output path of a log'
option :log_path, '-l', '--log-path PATH'
desc 'output level of a log'
option :log_level, '-L', '--log-level LEVEL', :type => [:debug, :info, :warn, :error, :fatal], :default => :info
desc 'path of a configuration file'
config_file '-c', '--config PATH'
desc 'port number of a gossip service'
option :gossip_port, '-P', '--gossip-port NUM', :type => Integer, :default => 10870
desc 'lifetime of the node of a gossip protocol'
option :gossip_node_lifetime, '-T', '--gossip-node-lifetime NUM', :type => Integer, :default => 10
desc 'transmitting interval of a gossip protocol'
option :gossip_send_interval, '-I', '--gossip-send-interval NUM', :type => Float, :default => 0.3
desc 'reception timeout of a gossip protocol'
option :gossip_receive_timeout, '-O', '--gossip-receive-timeout NUM', :type => Integer, :default => 3
after do |options|
if options[:resolver]
options[:resolver] = options[:resolver].map {|i| i.strip }
options[:resolver] = Resolv::DNS.new(:nameserver => options[:resolver])
end
if options[:initial_nodes]
options[:initial_nodes] = options[:initial_nodes].map {|i| i.strip }
end
options[:host] = options[:host].map {|i| i.strip }
options[:host][1] ||= Socket.gethostname
options[:host][2] = (options[:host][2] || 60).to_i
config_file_aliases = options.config_file ? options.config_file['alias'] : nil
if config_file_aliases
if config_file_aliases.kind_of?(Array)
options[:aliases] = config_file_aliases.map {|i| i.split(',') }
else
options[:aliases] = [options[:aliases]]
end
end
options[:aliases] = (options[:aliases] || []).map do |r|
r = r.map {|i| i.to_s.strip }
[nil, 60, 'master'].each_with_index {|v, i| r[i] ||= v }
priority = case r[2].to_s
when /master/i
Murakumo::MASTER
when /secondary/i
Murakumo::SECONDARY
else
Murakumo::BACKUP
end
[
r[0], r[1].to_i, priority,
]
end
if not options[:log_path] and options[:daemon]
options[:log_path] = '/var/log/murakumo.log'
end
options[:logger] = Logger.new(options[:log_path] || $stderr)
options[:logger].level = Logger.const_get(options[:log_level].to_s.upcase)
end
error do |e|
abort(e.message)
end
end
end
|