From 73a5223bf78a3dc96e7027b308f6d2fcbf52273b Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Thu, 20 Sep 2018 16:08:52 -0300 Subject: [PATCH 1/6] Adjust HWRPs to correct syntax errors on Chef 14 --- attributes/cluster.rb | 8 ++++---- libraries/bucket_provider.rb | 3 +++ libraries/bucket_resource.rb | 3 ++- libraries/cluster_provider.rb | 9 +++++---- libraries/cluster_resource.rb | 5 +++-- libraries/settings_provider.rb | 1 + libraries/settings_resource.rb | 1 + 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/attributes/cluster.rb b/attributes/cluster.rb index 382dd00..409ee11 100644 --- a/attributes/cluster.rb +++ b/attributes/cluster.rb @@ -28,9 +28,9 @@ default['couchbase']['cluster'] = Chef::DataBagItem.load('couchbase', 'cluster')[node.chef_environment] rescue {} -node.set['couchbase']['cluster']['name'] = "default" unless node['couchbase']['cluster']['name'] +default['couchbase']['cluster']['name'] = "default" unless node['couchbase']['cluster']['name'] # If this server is a member of a cluster, the cluster settings should override some server settings -node.set['couchbase']['server']['username'] = node['couchbase']['cluster']['username'] -node.set['couchbase']['server']['password'] = node['couchbase']['cluster']['password'] -node.set['couchbase']['server']['memory_quota_mb'] = node['couchbase']['cluster']['memory_quota_mb'] +override['couchbase']['server']['username'] = node['couchbase']['cluster']['username'] +override['couchbase']['server']['password'] = node['couchbase']['cluster']['password'] +override['couchbase']['server']['memory_quota_mb'] = node['couchbase']['cluster']['memory_quota_mb'] diff --git a/libraries/bucket_provider.rb b/libraries/bucket_provider.rb index 81bb912..d1d5fa3 100644 --- a/libraries/bucket_provider.rb +++ b/libraries/bucket_provider.rb @@ -7,6 +7,9 @@ class Provider class CouchbaseBucket < Provider include Couchbase::Client include Couchbase::ClusterData + provides :couchbase_bucket + + def load_current_resource @current_resource = Resource::CouchbaseBucket.new @new_resource.name diff --git a/libraries/bucket_resource.rb b/libraries/bucket_resource.rb index f89ccd4..d07fffb 100644 --- a/libraries/bucket_resource.rb +++ b/libraries/bucket_resource.rb @@ -5,6 +5,7 @@ class Chef class Resource class CouchbaseBucket < Resource include Couchbase::CredentialsAttributes + provides :couchbase_bucket def bucket(arg=nil) set_or_return(:bucket, arg, :kind_of => String, :name_attribute => true) @@ -17,7 +18,7 @@ def cluster(arg=nil) def exists(arg=nil) set_or_return(:exists, arg, :kind_of => [TrueClass, FalseClass], :required => true) end - + def proxyport(arg=nil) set_or_return(:proxyport, arg, :kind_of => Integer) end diff --git a/libraries/cluster_provider.rb b/libraries/cluster_provider.rb index 430a96b..f268812 100644 --- a/libraries/cluster_provider.rb +++ b/libraries/cluster_provider.rb @@ -1,12 +1,13 @@ -require "chef/provider" -require File.join(File.dirname(__FILE__), "client") -require File.join(File.dirname(__FILE__), "cluster_data") +require 'chef/provider' +require_relative 'client' +require_relative 'cluster_data' class Chef class Provider - class CouchbaseCluster < Provider + class CouchbaseCluster < Chef::Provider include Couchbase::Client include Couchbase::ClusterData + provides :couchbase_cluster def load_current_resource @current_resource = Resource::CouchbaseCluster.new @new_resource.name diff --git a/libraries/cluster_resource.rb b/libraries/cluster_resource.rb index 3db1708..8eddba7 100644 --- a/libraries/cluster_resource.rb +++ b/libraries/cluster_resource.rb @@ -1,10 +1,11 @@ -require "chef/resource" -require File.join(File.dirname(__FILE__), "credentials_attributes") +require 'chef/resource' +require_relative 'credentials_attributes' class Chef class Resource class CouchbaseCluster < Resource include Couchbase::CredentialsAttributes + provides :couchbase_cluster def cluster(arg=nil) set_or_return(:cluster, arg, :kind_of => String, :name_attribute => true) diff --git a/libraries/settings_provider.rb b/libraries/settings_provider.rb index af31f95..5f13a76 100644 --- a/libraries/settings_provider.rb +++ b/libraries/settings_provider.rb @@ -5,6 +5,7 @@ class Chef class Provider class CouchbaseSettings < Provider include Couchbase::Client + provides :couchbase_settings def load_current_resource @current_resource = Resource::CouchbaseSettings.new @new_resource.name diff --git a/libraries/settings_resource.rb b/libraries/settings_resource.rb index d660cae..a966256 100644 --- a/libraries/settings_resource.rb +++ b/libraries/settings_resource.rb @@ -5,6 +5,7 @@ class Chef class Resource class CouchbaseSettings < Resource include Couchbase::CredentialsAttributes + provides :couchbase_settings def group(arg=nil) set_or_return(:group, arg, :kind_of => String, :name_attribute => true) From 0472a825396a2c2f359e9fa32000204714200eee Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Thu, 20 Sep 2018 16:13:08 -0300 Subject: [PATCH 2/6] Remove incorrect include --- libraries/helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/helper.rb b/libraries/helper.rb index ad7b5c4..d9ea0d4 100644 --- a/libraries/helper.rb +++ b/libraries/helper.rb @@ -23,7 +23,6 @@ # Shamelessly stolen from Opscode's jenkins cookbook. Works for now. require 'chef/mixin/shell_out' -require 'chef/rest' module CouchbaseHelper extend Chef::Mixin::ShellOut From f5b6d66fe453b7255e355076ae9430d0a368f2c8 Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Thu, 20 Sep 2018 16:19:52 -0300 Subject: [PATCH 3/6] Add provides :coubase_node --- libraries/node_provider.rb | 10 ++++++---- libraries/node_resource.rb | 9 ++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/libraries/node_provider.rb b/libraries/node_provider.rb index 91a685f..de9f232 100644 --- a/libraries/node_provider.rb +++ b/libraries/node_provider.rb @@ -1,11 +1,13 @@ -require "chef/provider" -require "net/http" -require File.join(File.dirname(__FILE__), "client") +require 'chef/provider' +require 'net/http' +require_relative 'client' class Chef class Provider - class CouchbaseNode < Provider + class CouchbaseNode < Chef::Provider::LWRPBase include Couchbase::Client + provides :couchbase_node + def load_current_resource @current_resource = Chef::Resource::CouchbaseNode.new @new_resource.name diff --git a/libraries/node_resource.rb b/libraries/node_resource.rb index e65ad49..b873cf6 100644 --- a/libraries/node_resource.rb +++ b/libraries/node_resource.rb @@ -1,10 +1,13 @@ -require "chef/resource" -require File.join(File.dirname(__FILE__), "credentials_attributes") +require 'chef/resource' +require_relative 'credentials_attributes' +require_relative 'client' class Chef class Resource - class CouchbaseNode < Resource + class CouchbaseNode < Chef::Resource include Couchbase::CredentialsAttributes + include Couchbase::Client + provides :couchbase_node def id(arg=nil) set_or_return(:id, arg, :kind_of => String, :name_attribute => true) From 7cac0cc615ca09886982a11bf6dd6daa6a5e56ad Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Thu, 27 Sep 2018 19:30:27 -0300 Subject: [PATCH 4/6] Use correct correct attribute syntax when overriding attributes --- attributes/cluster.rb | 8 ++++---- recipes/server.rb | 44 +++++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/attributes/cluster.rb b/attributes/cluster.rb index 409ee11..773e3ec 100644 --- a/attributes/cluster.rb +++ b/attributes/cluster.rb @@ -28,9 +28,9 @@ default['couchbase']['cluster'] = Chef::DataBagItem.load('couchbase', 'cluster')[node.chef_environment] rescue {} -default['couchbase']['cluster']['name'] = "default" unless node['couchbase']['cluster']['name'] +default['couchbase']['cluster']['name'] = "default"# unless node['couchbase']['cluster']['name'] # If this server is a member of a cluster, the cluster settings should override some server settings -override['couchbase']['server']['username'] = node['couchbase']['cluster']['username'] -override['couchbase']['server']['password'] = node['couchbase']['cluster']['password'] -override['couchbase']['server']['memory_quota_mb'] = node['couchbase']['cluster']['memory_quota_mb'] +override['couchbase']['server']['username'] = node['couchbase']['cluster']['username'] if node['couchbase']['cluster']['username'] +override['couchbase']['server']['password'] = node['couchbase']['cluster']['password'] if node['couchbase']['cluster']['password'] +override['couchbase']['server']['memory_quota_mb'] = node['couchbase']['cluster']['memory_quota_mb'] if node['couchbase']['cluster']['memory_quota_mb'] diff --git a/recipes/server.rb b/recipes/server.rb index 9c8a782..59e11b0 100644 --- a/recipes/server.rb +++ b/recipes/server.rb @@ -41,7 +41,7 @@ end else # generate all passwords - (node.set['couchbase']['server']['password'] = secure_password && node.save) unless node['couchbase']['server']['password'] + (node.default['couchbase']['server']['password'] = secure_password) unless node['couchbase']['server']['password'] end remote_file File.join(Chef::Config[:file_cache_path], node['couchbase']['server']['package_file']) do @@ -74,23 +74,7 @@ end end -ruby_block "block_until_operational" do - block do - Chef::Log.info "Waiting until Couchbase is listening on port #{node['couchbase']['server']['port']}" - until CouchbaseHelper.service_listening?(node['couchbase']['server']['port']) do - sleep 1 - Chef::Log.debug(".") - end - Chef::Log.info "Waiting until the Couchbase admin API is responding" - test_url = URI.parse("http://localhost:#{node['couchbase']['server']['port']}") - until CouchbaseHelper.endpoint_responding?(test_url) do - sleep 1 - Chef::Log.debug(".") - end - end - action :nothing -end directory node['couchbase']['server']['log_dir'] do owner "couchbase" @@ -128,12 +112,32 @@ end service node['couchbase']['server']['service_name'] do - supports :restart => true, :status => true + supports( + restart: true, + status: true + ) action [:enable, :start] - notifies :create, "ruby_block[block_until_operational]", :immediately + notifies :run, 'ruby_block[block_until_operational]', :immediately end -couchbase_node "self" do +ruby_block 'block_until_operational' do + block do + Chef::Log.info "Waiting until Couchbase is listening on port #{node['couchbase']['server']['port']}" + until CouchbaseHelper.service_listening?(node['couchbase']['server']['port']) do + sleep 1 + Chef::Log.info('.') + end + + Chef::Log.info 'Waiting until the Couchbase admin API is responding' + test_url = URI.parse("http://localhost:#{node['couchbase']['server']['port']}") + until CouchbaseHelper.endpoint_responding?(test_url) do + sleep 1 + Chef::Log.info('.') + end + end + action :run +end +couchbase_node 'self' do database_path node['couchbase']['server']['database_path'] index_path node['couchbase']['server']['index_path'] From 3863309d738d79a49fd02a705d0a6a12538d05de Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Thu, 27 Sep 2018 19:33:53 -0300 Subject: [PATCH 5/6] Use Net::HTTP instead of deprecated Chef::REST --- libraries/helper.rb | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/libraries/helper.rb b/libraries/helper.rb index d9ea0d4..ec82037 100644 --- a/libraries/helper.rb +++ b/libraries/helper.rb @@ -37,20 +37,13 @@ def self.service_listening?(port) end def self.endpoint_responding?(url) - # XXX Should probably not use Chef::REST for this. Chef::REST only - # Accepts application/json; why not just use Net::HTTP directly? - begin - response = Chef::REST::RESTRequest.new(:GET, url, nil).call - rescue Errno::ECONNREFUSED, Errno::ENETUNREACH - return false - end - if response.kind_of?(Net::HTTPSuccess) || - response.kind_of?(Net::HTTPRedirection) || - response.kind_of?(Net::HTTPForbidden) + response = Net::HTTP.get_response(url, nil) + case response + when Net::HTTPSuccess, Net::HTTPRedirection, Net::HTTPForbidden Chef::Log.debug("GET to #{url} successful") return true else - Chef::Log.debug("GET to #{url} returned #{response.code} / #{response.class}") + Chef::Log.debug("GET to #{url} returned #{response.body if response.response_body_permitted?} / #{response}") return false end rescue EOFError, Errno::ECONNREFUSED From a2ed82c51a5efbaab66674c1c568c2498e0a99a1 Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Thu, 27 Sep 2018 19:34:18 -0300 Subject: [PATCH 6/6] Fix trivial change --- libraries/node_provider.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/node_provider.rb b/libraries/node_provider.rb index de9f232..901b964 100644 --- a/libraries/node_provider.rb +++ b/libraries/node_provider.rb @@ -4,7 +4,7 @@ class Chef class Provider - class CouchbaseNode < Chef::Provider::LWRPBase + class CouchbaseNode < Chef::Provider include Couchbase::Client provides :couchbase_node