Skip to content

Commit

Permalink
[daemon] moving generate_destination_name and clone_spec to private f…
Browse files Browse the repository at this point in the history
…unctions.
  • Loading branch information
georgeliao committed Jul 11, 2024
1 parent 6b082c3 commit f732f75
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 66 deletions.
133 changes: 67 additions & 66 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2697,41 +2697,7 @@ void mp::Daemon::clone(const CloneRequest* request,
require_operative_instances_reaction);
if (status.ok())
{
auto generate_destination_name = [this](const CloneRequest& request) -> std::string {

if (request.has_destination_name())
{
if (!mp::utils::valid_hostname(request.destination_name()))
{
throw std::runtime_error("Invalid destination virtual machine instance name: " +
request.destination_name());
}

if (is_instance_name_already_used(request.destination_name()))
{
throw std::runtime_error(request.destination_name() +
" already exists, please choose a new name. ");
}

return request.destination_name();
}
else
{
const std::string& source_name = request.source_name();
const std::string destination_name =
generate_next_clone_name(vm_instance_specs[source_name].clone_count, source_name);

if (is_instance_name_already_used(destination_name))
{
throw std::runtime_error("auto-generated name " + destination_name +
" already exists, please specify a new name manually. ");
}

return destination_name;
}
};

const std::string destination_name = generate_destination_name(*request);
const std::string destination_name = generate_destination_instance_name_for_clone(*request);

auto rollback_clean_up_all_resource_of_dest_instance = sg::make_scope_guard(
[this, destination_name]() noexcept -> void { release_resources(destination_name); });
Expand All @@ -2743,37 +2709,6 @@ void mp::Daemon::clone(const CloneRequest* request,
throw std::runtime_error("Please stop instance " + source_name + " before you clone it.");
}

auto clone_spec = [this](const mp::VMSpecs& src_vm_spec,
const std::string& src_name,
const std::string& dest_name) -> mp::VMSpecs {
mp::VMSpecs dest_vm_spec{src_vm_spec};
dest_vm_spec.clone_count = 0;

// update default mac addr and extra_interface mac addr
dest_vm_spec.default_mac_address = generate_unused_mac_address(allocated_mac_addrs);
for (auto& extra_interface : dest_vm_spec.extra_interfaces)
{
if (!extra_interface.mac_address.empty())
{
extra_interface.mac_address = generate_unused_mac_address(allocated_mac_addrs);
}
}

// non qemu snapshot files do not have metadata
if (!dest_vm_spec.metadata.isEmpty())
{
dest_vm_spec.metadata =
MP_JSONUTILS
.update_unique_identifiers_of_metadata(QJsonValue{dest_vm_spec.metadata},
src_vm_spec,
dest_vm_spec,
src_name,
dest_name)
.toObject();
}
return dest_vm_spec;
};

auto& src_spec = vm_instance_specs[source_name];
auto dest_spec = clone_spec(src_spec, source_name, destination_name);

Expand Down Expand Up @@ -3703,6 +3638,72 @@ bool mp::Daemon::is_instance_name_already_used(const std::string& instance_name)
delayed_shutdown_instances.find(instance_name) != delayed_shutdown_instances.end();
}

std::string mp::Daemon::generate_destination_instance_name_for_clone(const CloneRequest& request)
{

if (request.has_destination_name())
{
if (!mp::utils::valid_hostname(request.destination_name()))

Check warning on line 3646 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3646

Added line #L3646 was not covered by tests
{
throw std::runtime_error("Invalid destination virtual machine instance name: " +
request.destination_name());

Check warning on line 3649 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3648-L3649

Added lines #L3648 - L3649 were not covered by tests
}

if (is_instance_name_already_used(request.destination_name()))

Check warning on line 3652 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3652

Added line #L3652 was not covered by tests
{
throw std::runtime_error(request.destination_name() +
" already exists, please choose a new name. ");

Check warning on line 3655 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3654-L3655

Added lines #L3654 - L3655 were not covered by tests
}

return request.destination_name();

Check warning on line 3658 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3658

Added line #L3658 was not covered by tests
}
else
{
const std::string& source_name = request.source_name();
const std::string destination_name =
generate_next_clone_name(vm_instance_specs[source_name].clone_count, source_name);

if (is_instance_name_already_used(destination_name))
{
throw std::runtime_error("auto-generated name " + destination_name +
" already exists, please specify a new name manually. ");

Check warning on line 3669 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3668-L3669

Added lines #L3668 - L3669 were not covered by tests
}

return destination_name;
}
};

mp::VMSpecs mp::Daemon::clone_spec(const VMSpecs& src_vm_spec,
const std::string& src_name,
const std::string& dest_name)
{
mp::VMSpecs dest_vm_spec{src_vm_spec};
dest_vm_spec.clone_count = 0;

// update default mac addr and extra_interface mac addr
dest_vm_spec.default_mac_address = generate_unused_mac_address(allocated_mac_addrs);
for (auto& extra_interface : dest_vm_spec.extra_interfaces)
{
if (!extra_interface.mac_address.empty())

Check warning on line 3687 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3687

Added line #L3687 was not covered by tests
{
extra_interface.mac_address = generate_unused_mac_address(allocated_mac_addrs);

Check warning on line 3689 in src/daemon/daemon.cpp

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L3689

Added line #L3689 was not covered by tests
}
}

// non qemu snapshot files do not have metadata
if (!dest_vm_spec.metadata.isEmpty())
{
dest_vm_spec.metadata = MP_JSONUTILS
.update_unique_identifiers_of_metadata(QJsonValue{dest_vm_spec.metadata},
src_vm_spec,
dest_vm_spec,
src_name,
dest_name)
.toObject();
}
return dest_vm_spec;
}

bool mp::Daemon::is_bridged(const std::string& instance_name) const
{
return is_bridged_impl(vm_instance_specs.at(instance_name),
Expand Down
2 changes: 2 additions & 0 deletions src/daemon/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public slots:
populate_instance_info(VirtualMachine& vm, InfoReply& response, bool runtime_info, bool deleted, bool& have_mounts);

bool is_instance_name_already_used(const std::string& instance_name);
std::string generate_destination_instance_name_for_clone(const CloneRequest& request);
VMSpecs clone_spec(const VMSpecs& src_vm_spec, const std::string& src_name, const std::string& dest_name);

std::unique_ptr<const DaemonConfig> config;

Expand Down

0 comments on commit f732f75

Please sign in to comment.