Skip to content

Commit

Permalink
first public release
Browse files Browse the repository at this point in the history
  • Loading branch information
daveio committed Apr 11, 2019
1 parent b12c133 commit 3a96693
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 105 deletions.
141 changes: 81 additions & 60 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions lib/zt/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,37 @@ def pull
importer_results = Zt::Importers::Importer.new(rnetworks, rnodes).import
lnetworks = {}
ldomains = {}
lnodes = {}
importer_results.each do |importer_result|
importer_result.each_key do |network|
ldomains[network] = importer_result[network][:local][:dns_zone]
lnetworks[network] = importer_result[network]
if importer_result.key?(:networks)
importer_result[:networks].each_key do |netid|
ldomains[netid] =
importer_result[:networks][netid][:local][:dns_zone]
lnetworks[netid] = importer_result[:networks][netid]
end
end

next unless importer_result.key?(:nodes)

importer_result[:nodes].each_key do |nodeid|
lnodes[nodeid] = importer_result[:nodes][nodeid]
end
end
puts ' ≫ saving'
Zt::Conf.instance.conf.domains = ldomains
Zt::Conf.instance.conf.networks = lnetworks
Zt::Conf.instance.conf.nodes = lnodes
Zt::Conf.instance.save!
puts '≫ pull completed'
end

desc 'export',
'export the on-disk database to STDOUT in /etc/hosts format'
def export(format = :hosts)
output = Zt::Exporters::Exporter.new.export_one_format :hosts
puts output
end

desc 'auth [TOKEN]',
'Store an authentication token (unencrypted) in the config'
def auth(token)
Expand Down
8 changes: 6 additions & 2 deletions lib/zt/exporters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
module Zt
module Exporters
class Exporter
def initialize(data, *exporter_names)
def initialize(*exporter_names)
exporter_names = Zt::Constants::ALL_EXPORTERS if exporter_names.empty?

exporter_classes = exporter_names.map { |n| Zt::Exporters.const_get(n) }
@exporters = exporter_classes.map { |c| c.new(data) }
@exporters = exporter_classes.map(&:new)
end

# @return [Array[Hash]] an array of hashes from each exporter
Expand All @@ -23,6 +23,10 @@ def initialize(data, *exporter_names)
def export
@exporters.map(&:export)
end

def export_one_format(format)
Zt::Exporters::HostsFileExporter.new.export if format == :hosts
end
end
end
end
4 changes: 2 additions & 2 deletions lib/zt/exporters/_base_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Zt
module Exporters
class BaseExporter
attr_accessor :data
def initialize(data)
@data = data
def initialize
super
end

def export
Expand Down
18 changes: 18 additions & 0 deletions lib/zt/exporters/hosts_file_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
# frozen_string_literal: true

require 'zt/exporters/_base_exporter'
require 'zt/conf'

module Zt
module Exporters
class HostsFileExporter < BaseExporter
def export
# process the normalised Hash @data and return a String and a Block
# defining what to do with the String
conf = Zt::Conf.instance.conf
output = ''
conf.nodes.each_key do |node_id|
node = conf.nodes[node_id]
node_nets = node[:local][:networks]
node_nets.each_key do |net_id|
net_zone = conf.networks[net_id][:local][:dns_zone]
node_name = node[:remote][:node_name]
node_addrs = node[:local][:networks][net_id]
node_addrs.each do |node_addr|
output += "#{node_addr}\t\t#{node_name}.#{net_zone}\n"
end
end
end
"# BEGIN zt\n" + output.lines.sort_by do |a|
a.split[1]
end.join + "# END zt\n"
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/zt/importers/network_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def import
dns_zone: zone
}
end
output
{
networks: output
}
end
end
end
Expand Down
Loading

0 comments on commit 3a96693

Please sign in to comment.