Skip to content

Commit

Permalink
Formatting and fix compatibility with Chef 16
Browse files Browse the repository at this point in the history
Make sure provides/resource_name are set

Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
tas50 committed Sep 17, 2020
1 parent 9d227ec commit 0ed7434
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 134 deletions.
2 changes: 1 addition & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
# },
Expand Down
123 changes: 62 additions & 61 deletions libraries/zerotier_network.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions recipes/join_networks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions recipes/ohai_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
110 changes: 55 additions & 55 deletions templates/default/zerotier_ohai.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0ed7434

Please sign in to comment.