-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formatting and fix compatibility with Chef 16
Make sure provides/resource_name are set Signed-off-by: Tim Smith <[email protected]>
- Loading branch information
Showing
5 changed files
with
135 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |