Skip to content

Commit

Permalink
[daemon] refined the clone_spec_item function.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeliao committed Feb 15, 2024
1 parent b6c365a commit 563f9f3
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2786,10 +2786,12 @@ void mp::Daemon::clone(const CloneRequest* request,
throw std::runtime_error("Please stop instance " + source_name + " before you clone it.");

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2786

Added line #L2786 was not covered by tests
}

auto clone_spec_item = [this](const std::string& source_name, const std::string& destination_name) -> void {
const auto& src_vm_specs = vm_instance_specs[source_name];
auto& dest_vm_spec = vm_instance_specs[destination_name] = src_vm_specs;
auto clone_spec = [this](const mp::VMSpecs& src_vm_spec,

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2789

Added line #L2789 was not covered by tests
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;

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2791-L2793

Added lines #L2791 - L2793 were not covered by tests

// 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)

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2796-L2797

Added lines #L2796 - L2797 were not covered by tests
Expand All @@ -2798,10 +2800,11 @@ void mp::Daemon::clone(const CloneRequest* request,
}

dest_vm_spec.metadata = MP_JSONUTILS.update_unique_identifiers_of_metadata(dest_vm_spec.metadata,

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2802

Added line #L2802 was not covered by tests
src_vm_specs,
src_vm_spec,
dest_vm_spec,
source_name,
destination_name);
src_name,
dest_name);

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2806

Added line #L2806 was not covered by tests

auto update_extra_interfaces_mac_address_of_run_at_boot =
[](const std::vector<std::string>& run_at_boot,

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2809

Added line #L2809 was not covered by tests
const std::vector<NetworkInterface>& src_extra_interfaces,
Expand Down Expand Up @@ -2831,27 +2834,31 @@ void mp::Daemon::clone(const CloneRequest* request,

dest_vm_spec.run_at_boot =
update_extra_interfaces_mac_address_of_run_at_boot(dest_vm_spec.run_at_boot,
src_vm_specs.extra_interfaces,
src_vm_spec.extra_interfaces,
dest_vm_spec.extra_interfaces);
return dest_vm_spec;
};

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2836-L2840

Added lines #L2836 - L2840 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2842-L2843

Added lines #L2842 - L2843 were not covered by tests

config->vault->clone(source_name, destination_name);

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2845

Added line #L2845 was not covered by tests

const mp::VMImage dest_vm_image = fetch_image_for(destination_name, *config->factory, *config->vault);

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2847

Added line #L2847 was not covered by tests

// QemuVirtualMachine constructor depends on vm_instance_specs[destination_name], so the appending has to be
// done before that
vm_instance_specs.emplace(destination_name, dest_spec);
operative_instances[destination_name] =
config->factory->create_vm_and_instance_disk_data(config->data_directory,

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2851-L2853

Added lines #L2851 - L2853 were not covered by tests
vm_instance_specs[source_name],
vm_instance_specs[destination_name],
src_spec,
dest_spec,
source_name,
destination_name,
dest_vm_image,
*this);

++vm_instance_specs[source_name].clone_count;
++src_spec.clone_count;
persist_instances();

init_mounts(destination_name);

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2859-L2862

Added lines #L2859 - L2862 were not covered by tests

CloneReply rpc_response;
Expand All @@ -2861,6 +2868,7 @@ void mp::Daemon::clone(const CloneRequest* request,
}
catch (const std::exception& e)

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2869

Added line #L2869 was not covered by tests
{
// clean up the possible leftover RAM data and disk files
status_promise->set_value(grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, e.what(), ""));

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

View check run for this annotation

Codecov / codecov/patch

src/daemon/daemon.cpp#L2872

Added line #L2872 was not covered by tests
}
}
Expand Down

0 comments on commit 563f9f3

Please sign in to comment.