Skip to content

Commit

Permalink
Merge pull request #3161 from canonical/populate-snapshot-details
Browse files Browse the repository at this point in the history
[snapshots] populate snapshot details
  • Loading branch information
ricab authored Aug 25, 2023
2 parents 2a71f74 + 729afb2 commit f2ad7a6
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 126 deletions.
2 changes: 1 addition & 1 deletion include/multipass/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Snapshot : private DisabledCopyMove

virtual std::string get_name() const = 0;
virtual std::string get_comment() const = 0;
virtual std::string get_parent_name() const = 0;
virtual std::string get_parents_name() const = 0;
virtual QDateTime get_creation_timestamp() const = 0;
virtual std::shared_ptr<const Snapshot> get_parent() const = 0;
virtual std::shared_ptr<Snapshot> get_parent() = 0;
Expand Down
1 change: 1 addition & 0 deletions include/multipass/virtual_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class VirtualMachine : private DisabledCopyMove
virtual void delete_snapshot(const QDir& snapshot_dir, const std::string& name) = 0;
virtual void restore_snapshot(const QDir& snapshot_dir, const std::string& name, VMSpecs& specs) = 0;
virtual void load_snapshots(const QDir& snapshot_dir) = 0;
virtual std::vector<std::string> get_childrens_names(const Snapshot* parent) const = 0;

VirtualMachine::State state;
const std::string vm_name;
Expand Down
294 changes: 175 additions & 119 deletions src/daemon/daemon.cpp

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/daemon/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ public slots:
template <typename Reply, typename Request>
void reply_msg(grpc::ServerReaderWriterInterface<Reply, Request>* server, std::string&& msg, bool sticky = false);

void populate_instance_info(VirtualMachine& vm, DetailedInfoItem* info, bool runtime_info, bool deleted,
bool& have_mounts);

std::unique_ptr<const DaemonConfig> config;
std::unordered_map<std::string, VMSpecs> vm_instance_specs;
InstanceTable operative_instances;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/backends/shared/base_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ QJsonObject multipass::BaseSnapshot::serialize() const
snapshot.insert("disk_space", QString::number(disk_space.in_bytes()));
snapshot.insert("state", static_cast<int>(state));
snapshot.insert("metadata", metadata);
snapshot.insert("parent", QString::fromStdString(get_parent_name()));
snapshot.insert("parent", QString::fromStdString(get_parents_name()));

// Extract mount serialization
QJsonArray json_mounts;
Expand Down
4 changes: 2 additions & 2 deletions src/platform/backends/shared/base_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BaseSnapshot : public Snapshot
std::string get_name() const override;
std::string get_comment() const override;
QDateTime get_creation_timestamp() const override;
std::string get_parent_name() const override;
std::string get_parents_name() const override;
std::shared_ptr<const Snapshot> get_parent() const override;
std::shared_ptr<Snapshot> get_parent() override;

Expand Down Expand Up @@ -115,7 +115,7 @@ inline QDateTime multipass::BaseSnapshot::get_creation_timestamp() const
return creation_timestamp;
}

inline std::string multipass::BaseSnapshot::get_parent_name() const
inline std::string multipass::BaseSnapshot::get_parents_name() const
{
std::unique_lock lock{mutex};
auto par = parent;
Expand Down
15 changes: 13 additions & 2 deletions src/platform/backends/shared/base_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,17 @@ void BaseVirtualMachine::load_snapshots(const QDir& snapshot_dir)
load_generic_snapshot_info(snapshot_dir);
}

std::vector<std::string> BaseVirtualMachine::get_childrens_names(const Snapshot* parent) const
{
std::vector<std::string> children;

for (const auto& snapshot : view_snapshots())
if (snapshot->get_parent().get() == parent)
children.push_back(snapshot->get_name());

return children;
}

void BaseVirtualMachine::load_generic_snapshot_info(const QDir& snapshot_dir)
{
try
Expand All @@ -376,7 +387,7 @@ template <typename LockT>
void BaseVirtualMachine::log_latest_snapshot(LockT lock) const
{
auto num_snapshots = static_cast<int>(snapshots.size());
auto parent_name = head_snapshot->get_parent_name();
auto parent_name = head_snapshot->get_parents_name();

assert(num_snapshots <= snapshot_count && "can't have more snapshots than were ever taken");

Expand Down Expand Up @@ -424,7 +435,7 @@ void BaseVirtualMachine::load_snapshot(const QJsonObject& json)

auto BaseVirtualMachine::make_head_file_rollback(const Path& head_path, QFile& head_file) const
{
return sg::make_scope_guard([this, &head_path, &head_file, old_head = head_snapshot->get_parent_name(),
return sg::make_scope_guard([this, &head_path, &head_file, old_head = head_snapshot->get_parents_name(),
existed = head_file.exists()]() noexcept {
head_file_rollback_helper(head_path, head_file, old_head, existed);
});
Expand Down
1 change: 1 addition & 0 deletions src/platform/backends/shared/base_virtual_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class BaseVirtualMachine : public VirtualMachine
void delete_snapshot(const QDir& snapshot_dir, const std::string& name) override;
void restore_snapshot(const QDir& snapshot_dir, const std::string& name, VMSpecs& specs) override;
void load_snapshots(const QDir& snapshot_dir) override;
std::vector<std::string> get_childrens_names(const Snapshot* parent) const override;

protected:
virtual std::shared_ptr<Snapshot> make_specific_snapshot(const QJsonObject& json) = 0;
Expand Down
1 change: 1 addition & 0 deletions tests/mock_virtual_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct MockVirtualMachineT : public T
MOCK_METHOD(void, delete_snapshot, (const QDir& snapshot_dir, const std::string& name), (override));
MOCK_METHOD(void, restore_snapshot, (const QDir& snapshot_dir, const std::string&, VMSpecs&), (override));
MOCK_METHOD(void, load_snapshots, (const QDir&), (override));
MOCK_METHOD(std::vector<std::string>, get_childrens_names, (const Snapshot*), (const, override));
};

using MockVirtualMachine = MockVirtualMachineT<>;
Expand Down
2 changes: 1 addition & 1 deletion tests/stub_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct StubSnapshot : public Snapshot
return QDateTime{};
}

std::string get_parent_name() const override
std::string get_parents_name() const override
{
return {};
}
Expand Down
5 changes: 5 additions & 0 deletions tests/stub_virtual_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ struct StubVirtualMachine final : public multipass::VirtualMachine
{
}

std::vector<std::string> get_childrens_names(const Snapshot*) const override
{
return {};
}

StubSnapshot snapshot;
};
} // namespace test
Expand Down

0 comments on commit f2ad7a6

Please sign in to comment.