From 1330244fef0593858cb0c7bd28fbcb16afbec1d8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Jul 2015 18:04:16 -0600 Subject: [PATCH] core: save the UID that created a machine --- lib/vagrant/machine.rb | 27 +++++++++++++++++++++++++++ test/unit/vagrant/machine_test.rb | 15 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index cebd1eea1..b01087be1 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -279,6 +279,13 @@ module Vagrant end end + if uid_file + # Write the user id that created this machine + uid_file.open("w+") do |f| + f.write(Process.uid.to_s) + end + end + # If we don't have a UUID, then create one if index_uuid.nil? # Create the index entry and save it @@ -311,6 +318,7 @@ module Vagrant else # Delete the file, since the machine is now destroyed id_file.delete if id_file && id_file.file? + uid_file.delete if uid_file && uid_file.file? # If we have a UUID associated with the index, remove it uuid = index_uuid @@ -495,6 +503,17 @@ module Vagrant result end + # Returns the user ID that created this machine. This is specific to + # the host machine that this was created on. + # + # @return [String] + def uid + path = uid_file + return nil if !path + return nil if !path.file? + return uid_file.read.chomp + end + # Temporarily changes the machine UI. This is useful if you want # to execute an {#action} with a different UI. def with_ui(ui) @@ -508,5 +527,13 @@ module Vagrant end end end + + protected + + # Returns the path to the file that stores the UID. + def uid_file + return nil if !@data_dir + @data_dir.join("creator_uid") + end end end diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index d146d932e..4064fa1d5 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -424,6 +424,21 @@ describe Vagrant::Machine do third = new_instance expect(third.id).to be_nil end + + it "should set the UID that created the machine" do + instance.id = "foo" + + second = new_instance + expect(second.uid).to eq(Process.uid.to_s) + end + + it "should delete the UID when the id is nil" do + instance.id = "foo" + instance.id = nil + + second = new_instance + expect(second.uid).to be_nil + end end describe "#index_uuid" do