diff options
author | Tom Duffield <tom@chef.io> | 2016-02-17 17:30:56 -0600 |
---|---|---|
committer | Tom Duffield <tom@chef.io> | 2016-02-18 11:13:22 -0600 |
commit | ea3262bc62297f7d4632f9f2f0f5e9f1bc8d59eb (patch) | |
tree | 86fbafff109d5955a411dae37faa28d1b84d4120 | |
parent | 83bab84325ce969d03d6f6373497fa8b21e80b53 (diff) | |
download | chef-IPO-44/td-mc/add-node-uuid.tar.gz |
Add node UUID generatorIPO-44/td-mc/add-node-uuid
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 3 | ||||
-rw-r--r-- | lib/chef/node.rb | 25 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 32 |
3 files changed, 60 insertions, 0 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index ac55853bc7..a7dc38d3a4 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -264,6 +264,9 @@ module ChefConfig # '/tmp/chef-client-running.pid' default(:lockfile) { PathHelper.join(file_cache_path, "chef-client-running.pid") } + # The file where the UUID for the node is stored + default(:node_uuid_path) { platform_specific_path("/etc/chef/node-uuid") } + ## Daemonization Settings ## # What user should Chef run as? default :user, nil diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 0471fe1171..c57e452f19 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -604,6 +604,31 @@ class Chef from_hash(Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("nodes/#{name}")) end + # Node UUID + # Read from disk, or generate & save + # TODO: validate that the contents of the file are valid UUIDv4. If not, regenerate + def self.uuid + if ::File.exist?(Chef::Config[:node_uuid_path]) + # read_uuid_from_file + ::File.read(Chef::Config[:node_uuid_path]) + else + # generate_and_save_uuid + uuid = SecureRandom.uuid + ::File.write(Chef::Config[:node_uuid_path], uuid) + uuid + end + end + + Node.uuid + + # def self.uuid + # begin + # read_uuid_from_file + # rescue + # generate_and_save_uuid + # end + # end + # Remove this node via the REST API def destroy chef_server_rest.delete("nodes/#{name}") diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 5304ac9f92..12fd02b918 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -1562,4 +1562,36 @@ describe Chef::Node do end end + describe "uuid" do + let(:node_uuid_string) { "bb322253-b674-4efd-9090-2d38681cc3fb" } + let(:node_uuid_file_path) { "/etc/chef/node-uuid" } + + before do + Chef::Config[:node_uuid_path] = node_uuid_file_path + end + + context "when an UUID file does not exist" do + before do + allow(::File).to receive(:exist?).with(node_uuid_file_path).and_return(false) + end + + it "creates a new one" do + allow(SecureRandom).to receive(:uuid).and_return(node_uuid_string) + expect(::File).to receive(:write).with(node_uuid_file_path, node_uuid_string) + expect(Chef::Node.uuid).to eq(node_uuid_string) + end + end + + context "when an UUID file does exist" do + before do + allow(::File).to receive(:exist?).with(node_uuid_file_path).and_return(true) + end + + it "reads value from file" do + expect(::File).to receive(:read).with(node_uuid_file_path).and_return(node_uuid_string) + expect(Chef::Node.uuid).to eq(node_uuid_string) + end + end + end + end |