Top Level Namespace

Defined Under Namespace

Modules: Murakumo

Constant Summary collapse

Version =
Murakumo::VERSION

Instance Method Summary collapse

Instance Method Details

#mrkmctl_parse_argsObject



7
8
9
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
# File 'lib/cli/mrkmctl_options.rb', line 7

def mrkmctl_parse_args
  optopus do
    desc 'displays a list of a record'
    option :list, '-L', '--list [NAME]'

    desc 'adds or updates a record: <hostname>[,<TTL>[,{master|secondary|backup}]]'
    option :add, '-A', '--add 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

      # hostname
      /\A[0-9a-z\.\-]+\Z/i =~ hostname or invalid_argument

      # TTL
      unless ttl.nil? or (/\A\d+\Z/ =~ ttl and ttl.to_i > 0)
        invalid_argument
      end

      # Priority
      priority.nil? or /\A(master|secondary|backup)\Z/i =~ priority or invalid_argument
    end

    desc 'deletes a record'
    option :delete, '-D', '--delete NAME', :multiple => true do |value|
      /\A[0-9a-z\.\-]+\Z/i =~ value or invalid_argument
    end

    desc 'adds a node'
    option :add_node, '-a', '--add-node HOST', :multiple => true do |value|
      unless [/\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/, /\A[0-9a-z\.\-]+\Z/i].any? {|i| i =~ value }
        invalid_argument
      end
    end

    desc 'deletes a node'
    option :delete_node, '-d', '--delete-node HOST', :multiple => true do |value|
      unless [/\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/, /\A[0-9a-z\.\-]+\Z/i].any? {|i| i =~ value }
        invalid_argument
      end
    end

    desc "gets an attribute: #{Murakumo::ATTRIBUTES.keys.join(',')}"
    option :get, '-g', '--get ATTR' do |value|
      Murakumo::ATTRIBUTES.keys.include?(value.to_sym) or invalid_argument
    end

    desc "sets an attribute (name=value): #{Murakumo::ATTRIBUTES.keys.join(',')}"
    option :set, '-s', '--set ATTR' do |value|
      /\A.+=.+\Z/ =~ value or invalid_argument
      name, val = value.split('=', 2)
      Murakumo::ATTRIBUTES.keys.include?(name.to_sym) or invalid_argument

      if name == 'log_level'
        %w(debug info warn error fatal).include?(val) or invalid_argument
      end
    end

    desc ' configuration file is outputted by yaml'
    option :yaml, '-y', '--yaml'

    desc 'path of a socket file'
    option :socket, '-S', '--socket PATH', :default => '/var/tmp/murakumo.sock'

    after do |options|
      # add
      if options[:add]
        options[:add] = options[:add].map do |r|
          r = r.map {|i| i ? i.to_s.strip : i }
          [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], # name
            r[1].to_i, # TTL
            priority,
          ]
        end
      end

      # 一応、uniq
      [:delete, :add_node, :delete_node].each do |key|
        if options[key]
          options[key] = options[key].uniq
        end
      end

      if options[:get]
        options[:get] = options[:get].to_sym
      end

      if options[:set]
        options[:set] = options[:set].split('=', 2)
        options[:set][0] = options[:set][0].to_sym
      end

      # command
      commands = [:list, :add, :delete, :add_node, :delete_node, :get, :set, :yaml].map {|k|
        [k, options[k]]
      }.select {|i| not i[1].nil? }

      opt_keys = %w(-L -A -D --add-node --delete-node --get --set --yaml)

      if commands.length < 1
        parse_error('command is not specified', *opt_keys)
      elsif commands.length > 1
        parse_error('cannot use together', *opt_keys)
      end

      options[:command] = commands.first
    end

    error do |e|
      abort(e.message)
    end
  end
end

#murakumo_parse_argsObject



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

      # ip address
      /\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/ =~ ip_addr or invalid_argument

      # hostname
      /\A[0-9a-z\.\-]+\Z/i =~ hostname or invalid_argument

      # TTL
      unless ttl.nil? or (/\A\d+\Z/ =~ ttl and ttl.to_i > 0)
        invalid_argument
      end
    end # :host

    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

      # hostname
      /\A[0-9a-z\.\-]+\Z/ =~ hostname or invalid_argument

      # TTL
      unless ttl.nil? or (/\A\d+\Z/ =~ ttl and ttl.to_i > 0)
        invalid_argument
      end

      # Priority
      priority.nil? or /\A(master|secondary|backup)\Z/i =~ priority or invalid_argument
    end # :aliases

    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|
      # resolver
      if options[:resolver]
        options[:resolver] = options[:resolver].map {|i| i.strip }
        options[:resolver] = Resolv::DNS.new(:nameserver => options[:resolver])
      end

      # initial nodes
      if options[:initial_nodes]
        options[:initial_nodes] = options[:initial_nodes].map {|i| i.strip }
      end

      # host
      options[:host] = options[:host].map {|i| i.strip }
      options[:host][1] ||= Socket.gethostname
      options[:host][2] = (options[:host][2] || 60).to_i # TTL

      # aliases
      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], # name
          r[1].to_i, # TTL
          priority,
        ]
      end

      # logger
      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