Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum class TimeoutAction
QDir base_dir(const QString& path);
multipass::Path make_dir(const QDir& a_dir, const QString& name);
bool is_dir(const std::string& path);
QString backend_directory_path(const Path& path, const QString& subdirectory);
std::string filename_for(const std::string& path);
QString make_uuid();
std::string contents_of(const multipass::Path& file_path);
Expand Down
5 changes: 2 additions & 3 deletions include/multipass/virtual_machine_factory.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Canonical, Ltd.
* Copyright (C) 2017-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -13,8 +13,6 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Alberto Aguirre <[email protected]>
*
*/

#ifndef MULTIPASS_VIRTUAL_MACHINE_FACTORY_H
Expand Down Expand Up @@ -53,6 +51,7 @@ class VirtualMachineFactory
virtual void prepare_instance_image(const VMImage& instance_image, const VirtualMachineDescription& desc) = 0;
virtual void configure(const std::string& name, YAML::Node& meta_config, YAML::Node& user_config) = 0;
virtual void check_hypervisor_support() = 0;
virtual QString get_backend_directory_name() = 0;

protected:
VirtualMachineFactory() = default;
Expand Down
7 changes: 5 additions & 2 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,9 @@ grpc::Status ssh_reboot(const std::string& hostname, int port, const std::string

mp::Daemon::Daemon(std::unique_ptr<const DaemonConfig> the_config)
: config{std::move(the_config)},
vm_instance_specs{load_db(config->data_directory, config->cache_directory)},
vm_instance_specs{load_db(
mp::utils::backend_directory_path(config->data_directory, config->factory->get_backend_directory_name()),
mp::utils::backend_directory_path(config->cache_directory, config->factory->get_backend_directory_name()))},
daemon_rpc{config->server_address, config->connection_type, *config->cert_provider, *config->client_cert_store},
metrics_provider{"https://api.staging.jujucharms.com/omnibus/v4/multipass/metrics",
get_unique_id(config->data_directory), config->data_directory},
Expand Down Expand Up @@ -1756,7 +1758,8 @@ void mp::Daemon::persist_instances()
auto key = QString::fromStdString(record.first);
instance_records_json.insert(key, vm_spec_to_json(record.second));
}
QDir data_dir{config->data_directory};
QDir data_dir{
mp::utils::backend_directory_path(config->data_directory, config->factory->get_backend_directory_name())};
mp::write_json(instance_records_json, data_dir.filePath(instance_db_name));
}

Expand Down
6 changes: 4 additions & 2 deletions src/daemon/daemon_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ std::unique_ptr<const mp::DaemonConfig> mp::DaemonConfigBuilder::build()
{
hosts.push_back(image.get());
}
vault = std::make_unique<DefaultVMImageVault>(hosts, url_downloader.get(), cache_directory, data_directory,
days_to_expire);
vault = std::make_unique<DefaultVMImageVault>(
hosts, url_downloader.get(),
mp::utils::backend_directory_path(cache_directory, factory->get_backend_directory_name()),
mp::utils::backend_directory_path(data_directory, factory->get_backend_directory_name()), days_to_expire);
}
if (name_generator == nullptr)
name_generator = mp::make_default_name_generator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Canonical, Ltd.
* Copyright (C) 2018-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -43,6 +43,10 @@ class LibVirtVirtualMachineFactory final : public VirtualMachineFactory
void prepare_instance_image(const VMImage& instance_image, const VirtualMachineDescription& desc) override;
void configure(const std::string& name, YAML::Node& meta_config, YAML::Node& user_config) override;
void check_hypervisor_support() override;
QString get_backend_directory_name() override
{
return {};
};

private:
const ProcessFactory* process_factory;
Expand Down
6 changes: 5 additions & 1 deletion src/platform/backends/qemu/qemu_virtual_machine_factory.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2018 Canonical, Ltd.
* Copyright (C) 2017-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -41,6 +41,10 @@ class QemuVirtualMachineFactory final : public VirtualMachineFactory
void prepare_instance_image(const VMImage& instance_image, const VirtualMachineDescription& desc) override;
void configure(const std::string& name, YAML::Node& meta_config, YAML::Node& user_config) override;
void check_hypervisor_support() override;
QString get_backend_directory_name() override
{
return {};
};

private:
const ProcessFactory* process_factory;
Expand Down
8 changes: 8 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ mp::Path mp::utils::make_dir(const QDir& a_dir, const QString& name)
return a_dir.filePath(name);
}

QString mp::utils::backend_directory_path(const mp::Path& path, const QString& subdirectory)
{
if (subdirectory.isEmpty())
return path;

return mp::Path("%1/%2").arg(path).arg(subdirectory);
}

QString mp::utils::make_uuid()
{
auto uuid = QUuid::createUuid().toString();
Expand Down
5 changes: 2 additions & 3 deletions tests/mock_virtual_machine_factory.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Canonical, Ltd.
* Copyright (C) 2017-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -13,8 +13,6 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Alberto Aguirre <[email protected]>
*
*/

#ifndef MULTIPASS_MOCK_VIRTUAL_MACHINE_FACTORY_H
Expand All @@ -40,6 +38,7 @@ struct MockVirtualMachineFactory : public VirtualMachineFactory
MOCK_METHOD2(prepare_instance_image, void(const VMImage&, const VirtualMachineDescription&));
MOCK_METHOD3(configure, void(const std::string&, YAML::Node&, YAML::Node&));
MOCK_METHOD0(check_hypervisor_support, void());
MOCK_METHOD0(get_backend_directory_name, QString());
};
}
}
Expand Down
9 changes: 6 additions & 3 deletions tests/stub_virtual_machine_factory.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Canonical, Ltd.
* Copyright (C) 2017-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -13,8 +13,6 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Alberto Aguirre <[email protected]>
*
*/

#ifndef MULTIPASS_STUB_VIRTUAL_MACHINE_FACTORY_H
Expand Down Expand Up @@ -62,6 +60,11 @@ struct StubVirtualMachineFactory : public multipass::VirtualMachineFactory
void check_hypervisor_support() override
{
}

QString get_backend_directory_name() override
{
return {};
}
};
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,22 @@ TEST(Utils, filename_only_is_returned)
EXPECT_THAT(mp::utils::filename_for(full_path), Eq(file_name));
}

TEST(Utils, no_subdirectory_returns_same_path)
{
mp::Path original_path{"/tmp/foo"};
QString empty_subdir{};

EXPECT_THAT(mp::utils::backend_directory_path(original_path, empty_subdir), Eq(original_path));
}

TEST(Utils, subdirectory_returns_new_path)
{
mp::Path original_path{"/tmp/foo"};
QString subdir{"bar"};

EXPECT_THAT(mp::utils::backend_directory_path(original_path, subdir), Eq(mp::Path{"/tmp/foo/bar"}));
}

TEST(Utils, vm_running_returns_true)
{
mp::VirtualMachine::State state = mp::VirtualMachine::State::running;
Expand Down