From 3f813101097983548e429b15e43080fc1d43e91f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Mon, 20 Mar 2017 19:38:12 +0000 Subject: [PATCH] Add a resource to handle config changes --- site-cookbooks/ipfs/README.md | 16 +++++++++++++- site-cookbooks/ipfs/recipes/default.rb | 15 ++----------- site-cookbooks/ipfs/resources/config.rb | 29 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 site-cookbooks/ipfs/resources/config.rb diff --git a/site-cookbooks/ipfs/README.md b/site-cookbooks/ipfs/README.md index 44797d5..778d89c 100644 --- a/site-cookbooks/ipfs/README.md +++ b/site-cookbooks/ipfs/README.md @@ -11,7 +11,9 @@ It currently only supports 64bit platforms ### Chef -- Chef 12.0 or later +- Chef 12.5 or later (we are providing a + [https://docs.chef.io/custom_resources.html](Custom Resource) to configure + IPFS ### Cookbook dependencies @@ -41,6 +43,18 @@ site (64bit) connect to. This will stop platforms like Hetzner to block your server (https://github.com/ipfs/go-ipfs/issues/1226) +## Resources + +`ipfs_config` sets the config. Supports hashes, arrays, booleans and strings. +Does not change anything if the config already has that value, and restarts +the server automatically + +```ruby +ipfs_config "Gateway.Writable" do + true + end +``` + ## License and Authors Authors: Kosmos diff --git a/site-cookbooks/ipfs/recipes/default.rb b/site-cookbooks/ipfs/recipes/default.rb index 134265b..70fb43b 100644 --- a/site-cookbooks/ipfs/recipes/default.rb +++ b/site-cookbooks/ipfs/recipes/default.rb @@ -73,17 +73,6 @@ else end # Configure ipfs to not contact local network addresses -execute "ipfs config --json Swarm.AddrFilters '#{node['ipfs']['config']['swarm']['addr_filter'].to_json}'" do - environment "IPFS_PATH" => "/home/ipfs/.ipfs" - user "ipfs" - not_if do - require 'json' - swarm_filter_config = `su - ipfs -c "ipfs config Swarm.AddrFilters"` - begin - JSON.parse(swarm_filter_config) == node['ipfs']['config']['swarm']['addr_filter'] - rescue JSON::ParserError - false - end - end - notifies :restart, "service[ipfs]", :delayed +ipfs_config "Swarm.AddrFilters" do + value node['ipfs']['config']['swarm']['addr_filter'] end diff --git a/site-cookbooks/ipfs/resources/config.rb b/site-cookbooks/ipfs/resources/config.rb new file mode 100644 index 0000000..67df68a --- /dev/null +++ b/site-cookbooks/ipfs/resources/config.rb @@ -0,0 +1,29 @@ +property :key, String, name_property: true +property :value, [String, Hash, Array, TrueClass, FalseClass], default: nil, required: true + +load_current_value do + # some Ruby +end + +action :create do + include_recipe "ipfs" + + json_value = JSON.generate(value) + execute "ipfs config --json #{key} #{json_value}" do + environment "IPFS_PATH" => "/home/ipfs/.ipfs" + user "ipfs" + not_if do + require 'json' + require 'mixlib/shellout' + cmd = Mixlib::ShellOut.new("ipfs", "config", key, user: 'ipfs', + env: {"IPFS_PATH" => "/home/ipfs/.ipfs"}) + cmd.run_command + begin + JSON.parse(cmd.stdout) == value + rescue JSON::ParserError + cmd.stdout.include?(value) + end + end + notifies :restart, "service[ipfs]", :delayed + end +end