Allow Machine Index entries to have a remote machine reference
This commit is contained in:
parent
81913d1042
commit
6f54cf51a2
@ -61,10 +61,11 @@ func (t *TargetIndex) Includes(ref *vagrant_plugin_sdk.Ref_Target) (exists bool,
|
||||
}
|
||||
|
||||
func (t *TargetIndex) Set(entry core.Target) (updatedEntry core.Target, err error) {
|
||||
target := entry.(*Target)
|
||||
updatedTarget, err := t.client.UpsertTarget(
|
||||
t.ctx,
|
||||
&vagrant_server.UpsertTargetRequest{
|
||||
Target: &vagrant_server.Target{},
|
||||
Target: target.target,
|
||||
},
|
||||
)
|
||||
// TODO: check if this actually gets back a full target
|
||||
|
||||
@ -124,6 +124,10 @@ module Vagrant
|
||||
"<Vagrant::Machine:resource_id=#{client.resource_id}>"
|
||||
end
|
||||
|
||||
def save
|
||||
# TODO
|
||||
end
|
||||
|
||||
|
||||
### HACKS
|
||||
|
||||
@ -219,89 +223,9 @@ module Vagrant
|
||||
|
||||
def id=(value)
|
||||
@logger.info("New machine ID: #{value.inspect}")
|
||||
|
||||
client.set_id(value.to_s)
|
||||
|
||||
id_file = nil
|
||||
if @data_dir
|
||||
# The file that will store the id if we have one. This allows the
|
||||
# ID to persist across Vagrant runs. Also, store the UUID for the
|
||||
# machine index.
|
||||
id_file = @data_dir.join("id")
|
||||
end
|
||||
|
||||
if value
|
||||
if id_file
|
||||
# Write the "id" file with the id given.
|
||||
id_file.open("w+") do |f|
|
||||
f.write(value)
|
||||
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
|
||||
entry = MachineIndex::Entry.new
|
||||
entry.local_data_path = @env.local_data_path
|
||||
entry.name = @name.to_s
|
||||
entry.provider = @provider_name.to_s
|
||||
entry.state = "preparing"
|
||||
entry.vagrantfile_path = @env.root_path
|
||||
entry.vagrantfile_name = @env.vagrantfile_name
|
||||
|
||||
if @box
|
||||
entry.extra_data["box"] = {
|
||||
"name" => @box.name,
|
||||
"provider" => @box.provider.to_s,
|
||||
"version" => @box.version.to_s,
|
||||
}
|
||||
end
|
||||
|
||||
entry = @env.machine_index.set(entry)
|
||||
@env.machine_index.release(entry)
|
||||
|
||||
# Store our UUID so we can access it later
|
||||
if @index_uuid_file
|
||||
@index_uuid_file.open("w+") do |f|
|
||||
f.write(entry.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
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
|
||||
if uuid
|
||||
entry = @env.machine_index.get(uuid)
|
||||
@env.machine_index.delete(entry) if entry
|
||||
end
|
||||
|
||||
if @data_dir
|
||||
# Delete the entire data directory contents since all state
|
||||
# associated with the VM is now gone.
|
||||
@data_dir.children.each do |child|
|
||||
begin
|
||||
child.rmtree
|
||||
rescue Errno::EACCES
|
||||
@logger.info("EACCESS deleting file: #{child}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Store the ID locally
|
||||
@id = value.nil? ? nil : value.to_s
|
||||
|
||||
# Notify the provider that the ID changed in case it needs to do
|
||||
# any accounting from it.
|
||||
@provider.machine_id_changed
|
||||
@ -404,33 +328,7 @@ module Vagrant
|
||||
end
|
||||
|
||||
def recover_machine(state)
|
||||
entry = @env.machine_index.get(index_uuid)
|
||||
if entry
|
||||
@env.machine_index.release(entry)
|
||||
return entry
|
||||
end
|
||||
|
||||
entry = MachineIndex::Entry.new(id=index_uuid, {})
|
||||
entry.local_data_path = @env.local_data_path
|
||||
entry.name = @name.to_s
|
||||
entry.provider = @provider_name.to_s
|
||||
entry.state = state
|
||||
entry.vagrantfile_path = @env.root_path
|
||||
entry.vagrantfile_name = @env.vagrantfile_name
|
||||
|
||||
if @box
|
||||
entry.extra_data["box"] = {
|
||||
"name" => @box.name,
|
||||
"provider" => @box.provider.to_s,
|
||||
"version" => @box.version.to_s,
|
||||
}
|
||||
end
|
||||
|
||||
@state_mutex.synchronize do
|
||||
entry = @env.machine_index.recover(entry)
|
||||
@env.machine_index.release(entry)
|
||||
end
|
||||
return entry
|
||||
# no-op
|
||||
end
|
||||
|
||||
def uid
|
||||
|
||||
@ -402,6 +402,8 @@ module Vagrant
|
||||
# @return [Hash]
|
||||
attr_accessor :extra_data
|
||||
|
||||
attr_accessor :remote_machine
|
||||
|
||||
# Initializes an entry.
|
||||
#
|
||||
# The parameter given should be nil if this is being created
|
||||
@ -412,11 +414,10 @@ module Vagrant
|
||||
@logger.debug("got raw: #{raw}")
|
||||
|
||||
@extra_data = {}
|
||||
|
||||
@id = id
|
||||
# Do nothing if we aren't given a raw value. Otherwise, parse it.
|
||||
return if !raw
|
||||
|
||||
@id = id
|
||||
@local_data_path = raw["local_data_path"]
|
||||
@name = raw["name"]
|
||||
@provider = raw["provider"]
|
||||
@ -426,6 +427,7 @@ module Vagrant
|
||||
# TODO(mitchellh): parse into a proper datetime
|
||||
@updated_at = raw["updated_at"]
|
||||
@extra_data = raw["extra_data"] || {}
|
||||
@remote_machine = raw["remote_machine"]
|
||||
|
||||
# Be careful with the paths
|
||||
@local_data_path = nil if @local_data_path == ""
|
||||
|
||||
@ -26,7 +26,7 @@ module Vagrant
|
||||
# @param [Entry] entry The entry to delete.
|
||||
# @return [Boolean] true if delete is successful
|
||||
def delete(entry)
|
||||
machine = entry_to_machine(entry)
|
||||
machine = entry.remote_machine.client.ref
|
||||
@client.delete(machine)
|
||||
end
|
||||
|
||||
@ -67,13 +67,11 @@ module Vagrant
|
||||
# @param [Entry] entry
|
||||
# @return [Entry]
|
||||
def set(entry)
|
||||
machine_in = entry_to_machine(entry)
|
||||
machine_out = @client.set(machine_in)
|
||||
machine_to_entry(machine_out)
|
||||
entry.remote_machine.save
|
||||
end
|
||||
|
||||
def recover(entry)
|
||||
#TODO
|
||||
#no-op
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user