diff --git a/attributes/default.rb b/attributes/default.rb index 2c7358b..69d0c29 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -13,7 +13,7 @@ # Packed in the following format: # # [{ -# :network_id => "", +# :network_id => "", # :auth_token => "key", # :central_url => "http://my.zerotier.com" // optional. Defaults to https://my.zerotier.com # }, diff --git a/libraries/zerotier_network.rb b/libraries/zerotier_network.rb index 9723bd1..fbf0a72 100644 --- a/libraries/zerotier_network.rb +++ b/libraries/zerotier_network.rb @@ -1,74 +1,75 @@ -require "chef/log" -require "uri" -require "mixlib/shellout" -require "net/http" -require "net/https" -require "json" +require 'chef/log' +require 'uri' +require 'mixlib/shellout' +require 'net/http' +require 'net/https' +require 'json' module ChefZerotierCookbook - class ZerotierNetwork < Chef::Resource - resource_name :zerotier_network + class ZerotierNetwork < Chef::Resource + resource_name :zerotier_network + provides :zerotier_network - default_action :join + default_action :join - # Set the properties for the resource - property :network_id, String, name_property: true, required: true - property :node_name, String, required: true - property :auth_token, String, required: true - property :central_url, String, default: "https://my.zerotier.com" + # Set the properties for the resource + property :network_id, String, name_property: true, required: true + property :node_name, String, required: true + property :auth_token, String, required: true + property :central_url, String, default: 'https://my.zerotier.com' - action :join do - if ::File.exists?(format("/var/lib/zerotier-one/networks.d/%s.conf", network_id)) - Chef::Log.info(format("Network %s already joined. Skipping.", network_id)) - else - join = Mixlib::ShellOut.new(format("/usr/sbin/zerotier-cli join %s", network_id)) - join.run_command - raise format("Error joining network %s", network_id) if join.error? + action :join do + if ::File.exist?(format('/var/lib/zerotier-one/networks.d/%s.conf', network_id)) + Chef::Log.info(format('Network %s already joined. Skipping.', network_id)) + else + join = Mixlib::ShellOut.new(format('/usr/sbin/zerotier-cli join %s', network_id)) + join.run_command + raise format('Error joining network %s', network_id) if join.error? - if auth_token - url = URI.parse(format("%s/api/network/%s/member/%s/", central_url, network_id, node['zerotier']['node_id'])) - - netinfo = { - networkId: network_id, - nodeId: node["zerotier"]["node_id"], - name: node_name, - config: { - nwid: network_id, - authorized: true - } - } + if auth_token + url = URI.parse(format('%s/api/network/%s/member/%s/', central_url, network_id, node['zerotier']['node_id'])) - response = Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == "https") do |http| - post = Net::HTTP::Post.new(url, "Content-Type" => "application/json") - post.add_field("Authorization", format("Bearer %s", auth_token)) - post.body = netinfo.to_json - http.request(post) - end + netinfo = { + networkId: network_id, + nodeId: node['zerotier']['node_id'], + name: node_name, + config: { + nwid: network_id, + authorized: true, + }, + } - case response - when Net::HTTPSuccess - # do nothing - else - leave = Mixlib::ShellOut.new(format("/usr/sbin/zerotier-cli leave %s", network_id)) - leave.run_command - error = JSON.parse(response.body) - raise format("Error %s authorizing network: %s: %s", response.code. error["type"], error["message"]) - end - end - - end + response = Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http| + post = Net::HTTP::Post.new(url, 'Content-Type' => 'application/json') + post.add_field('Authorization', format('Bearer %s', auth_token)) + post.body = netinfo.to_json + http.request(post) + end + + case response + when Net::HTTPSuccess + # do nothing + else + leave = Mixlib::ShellOut.new(format('/usr/sbin/zerotier-cli leave %s', network_id)) + leave.run_command + error = JSON.parse(response.body) + raise format('Error %s authorizing network: %s: %s', response.code. error['type'], error['message']) + end end - action :leave do - if ::File.exists?(format("/var/lib/zerotier-one/networks.d/%s.conf", network_id)) - converge_by(format("Leaving network %s", network_id)) do - leave = Mixlib::ShellOut.new(format("/usr/sbin/zerotier-cli leave %s", network_id)) - leave.run_command - raise format("Error leaving network %s", network_id) if leave.error? - end - else - Chef::Log.warn(format("Network %s is not joined. Skipping", network_id)) - end + end + end + + action :leave do + if ::File.exist?(format('/var/lib/zerotier-one/networks.d/%s.conf', network_id)) + converge_by(format('Leaving network %s', network_id)) do + leave = Mixlib::ShellOut.new(format('/usr/sbin/zerotier-cli leave %s', network_id)) + leave.run_command + raise format('Error leaving network %s', network_id) if leave.error? end + else + Chef::Log.warn(format('Network %s is not joined. Skipping', network_id)) + end end + end end \ No newline at end of file diff --git a/recipes/join_networks.rb b/recipes/join_networks.rb index 061bd7d..4a6566f 100644 --- a/recipes/join_networks.rb +++ b/recipes/join_networks.rb @@ -7,17 +7,17 @@ include_recipe 'zerotier::ohai_plugin' node['zerotier']['public_autojoin'].each do |nwid| - zerotier_network nwid do - action :join - end + zerotier_network nwid do + action :join + end end node['zerotier']['private_autojoin'].each do |network| - zerotier_network network['network_id'] do - only_if { network.key?("auth_token") } - action :join - auth_token network['auth_token'] - central_url network.key?("central_url") ? network[:central_url] : "https://my.zerotier.com" - node_name node['fqdn'] - end + zerotier_network network['network_id'] do + only_if { network.key?('auth_token') } + action :join + auth_token network['auth_token'] + central_url network.key?('central_url') ? network[:central_url] : 'https://my.zerotier.com' + node_name node['fqdn'] + end end \ No newline at end of file diff --git a/recipes/ohai_plugin.rb b/recipes/ohai_plugin.rb index 63eb07a..9fcef34 100644 --- a/recipes/ohai_plugin.rb +++ b/recipes/ohai_plugin.rb @@ -5,11 +5,11 @@ # Copyright:: 2017, ZeroTier, Inc., All Rights Reserved. ohai_plugin 'zerotier_ohai' do - compile_time false - resource :template - variables ({ - :zerotier_binary => node['zerotier']['binary'], - :control_port => node['zerotier']['control_port'], - :data_dir => node['zerotier']['data_dir'] - }) + compile_time false + resource :template + variables({ + zerotier_binary: node['zerotier']['binary'], + control_port: node['zerotier']['control_port'], + data_dir: node['zerotier']['data_dir'], + }) end diff --git a/templates/default/zerotier_ohai.rb b/templates/default/zerotier_ohai.rb index d59179f..839cbe1 100644 --- a/templates/default/zerotier_ohai.rb +++ b/templates/default/zerotier_ohai.rb @@ -1,71 +1,71 @@ Ohai.plugin(:ZeroTier) do - provides 'zerotier' + provides 'zerotier' - def zerotier_command (subcommand) - return "<%= @zerotier_binary %> <% unless @zerotier_binary.end_with?('-cli') %>-q<% end %> #{subcommand} -p<%= @control_port %> -D<%= @data_dir %>" - end + def zerotier_command(subcommand) + "<%= @zerotier_binary %> <% unless @zerotier_binary.end_with?('-cli') %>-q<% end %> #{subcommand} -p<%= @control_port %> -D<%= @data_dir %>" + end - def linux_get_networks - networks = Mash.new - cmd = zerotier_command('listnetworks') - Ohai::Log.info(cmd) - so = shell_out(cmd) - first_line = true - so.stdout.lines do |line| - if first_line - # skip the header line - first_line = false - next - end + def linux_get_networks + networks = Mash.new + cmd = zerotier_command('listnetworks') + Ohai::Log.info(cmd) + so = shell_out(cmd) + first_line = true + so.stdout.lines do |line| + if first_line + # skip the header line + first_line = false + next + end - data = line.strip.split(/\s+/) + data = line.strip.split(/\s+/) - cur_network = Mash.new - cur_network[:network_name] = data[3] - cur_network[:mac] = data[4] - cur_network[:status] = data[5] - cur_network[:type] = data[6] - cur_network[:interface] = data[7] - cur_network[:addresses] = [] + cur_network = Mash.new + cur_network[:network_name] = data[3] + cur_network[:mac] = data[4] + cur_network[:status] = data[5] + cur_network[:type] = data[6] + cur_network[:interface] = data[7] + cur_network[:addresses] = [] - data[8].split(',').each do |addr| - cur_network[:addresses].push(addr) - end + data[8].split(',').each do |addr| + cur_network[:addresses].push(addr) + end - networks[data[2]] = cur_network - end - return networks + networks[data[2]] = cur_network end + networks + end - def linux_get_node_id - node_id = '' - - if ::File.exists?("<%= @data_dir %>/identity.public") - node_id = ::File.read("<%= @data_dir %>/identity.public") - node_id = node_id[0..9] - Ohai - else - Ohai::Log.warn("<%= @data_dir %>/identity.public' does not exist") - end + def linux_get_node_id + node_id = '' - return node_id unless node_id.empty? + if ::File.exist?('<%= @data_dir %>/identity.public') + node_id = ::File.read('<%= @data_dir %>/identity.public') + node_id = node_id[0..9] + Ohai + else + Ohai::Log.warn("<%= @data_dir %>/identity.public' does not exist") end - def get_version - so = shell_out("<%= @zerotier_binary %> -v 2>&1") - version = so.stdout.strip - return version unless version.empty? - end + return node_id unless node_id.empty? + end + + def get_version + so = shell_out('<%= @zerotier_binary %> -v 2>&1') + version = so.stdout.strip + return version unless version.empty? + end - collect_data(:linux) do - if ::File.exists?("<%= @zerotier_binary %>") - zerotier Mash.new - zerotier[:version] = get_version - zerotier[:node_id] = linux_get_node_id - zerotier[:networks] = linux_get_networks - else - Ohai::Log.warn("Cannot find zerotier binary") - end + collect_data(:linux) do + if ::File.exist?('<%= @zerotier_binary %>') + zerotier Mash.new + zerotier[:version] = get_version + zerotier[:node_id] = linux_get_node_id + zerotier[:networks] = linux_get_networks + else + Ohai::Log.warn('Cannot find zerotier binary') end + end end