core: save the UID that created a machine

This commit is contained in:
Mitchell Hashimoto 2015-07-06 18:04:16 -06:00
parent 0de0f51e8a
commit 1330244fef
2 changed files with 42 additions and 0 deletions

View File

@ -279,6 +279,13 @@ module Vagrant
end end
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 we don't have a UUID, then create one
if index_uuid.nil? if index_uuid.nil?
# Create the index entry and save it # Create the index entry and save it
@ -311,6 +318,7 @@ module Vagrant
else else
# Delete the file, since the machine is now destroyed # Delete the file, since the machine is now destroyed
id_file.delete if id_file && id_file.file? 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 # If we have a UUID associated with the index, remove it
uuid = index_uuid uuid = index_uuid
@ -495,6 +503,17 @@ module Vagrant
result result
end 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 # Temporarily changes the machine UI. This is useful if you want
# to execute an {#action} with a different UI. # to execute an {#action} with a different UI.
def with_ui(ui) def with_ui(ui)
@ -508,5 +527,13 @@ module Vagrant
end end
end 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
end end

View File

@ -424,6 +424,21 @@ describe Vagrant::Machine do
third = new_instance third = new_instance
expect(third.id).to be_nil expect(third.id).to be_nil
end 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 end
describe "#index_uuid" do describe "#index_uuid" do