Skip to content

Commit df4bec0

Browse files
bors[bot]Chris Townsend
andcommitted
Merge #800
800: backends/daemon: Allow for backend to determine its data/cache dirs r=Saviq a=townsend2010 Fixes #774 Co-authored-by: Chris Townsend <[email protected]>
2 parents 0b998b8 + 013434f commit df4bec0

File tree

10 files changed

+54
-15
lines changed

10 files changed

+54
-15
lines changed

include/multipass/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ enum class TimeoutAction
5454
QDir base_dir(const QString& path);
5555
multipass::Path make_dir(const QDir& a_dir, const QString& name);
5656
bool is_dir(const std::string& path);
57+
QString backend_directory_path(const Path& path, const QString& subdirectory);
5758
std::string filename_for(const std::string& path);
5859
QString make_uuid();
5960
std::string contents_of(const multipass::Path& file_path);

include/multipass/virtual_machine_factory.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017 Canonical, Ltd.
2+
* Copyright (C) 2017-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -13,8 +13,6 @@
1313
* You should have received a copy of the GNU General Public License
1414
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
*
16-
* Authored by: Alberto Aguirre <[email protected]>
17-
*
1816
*/
1917

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

5756
protected:
5857
VirtualMachineFactory() = default;

src/daemon/daemon.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,9 @@ grpc::Status ssh_reboot(const std::string& hostname, int port, const std::string
546546

547547
mp::Daemon::Daemon(std::unique_ptr<const DaemonConfig> the_config)
548548
: config{std::move(the_config)},
549-
vm_instance_specs{load_db(config->data_directory, config->cache_directory)},
549+
vm_instance_specs{load_db(
550+
mp::utils::backend_directory_path(config->data_directory, config->factory->get_backend_directory_name()),
551+
mp::utils::backend_directory_path(config->cache_directory, config->factory->get_backend_directory_name()))},
550552
daemon_rpc{config->server_address, config->connection_type, *config->cert_provider, *config->client_cert_store},
551553
metrics_provider{"https://api.staging.jujucharms.com/omnibus/v4/multipass/metrics",
552554
get_unique_id(config->data_directory), config->data_directory},
@@ -1756,7 +1758,8 @@ void mp::Daemon::persist_instances()
17561758
auto key = QString::fromStdString(record.first);
17571759
instance_records_json.insert(key, vm_spec_to_json(record.second));
17581760
}
1759-
QDir data_dir{config->data_directory};
1761+
QDir data_dir{
1762+
mp::utils::backend_directory_path(config->data_directory, config->factory->get_backend_directory_name())};
17601763
mp::write_json(instance_records_json, data_dir.filePath(instance_db_name));
17611764
}
17621765

src/daemon/daemon_config.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ std::unique_ptr<const mp::DaemonConfig> mp::DaemonConfigBuilder::build()
9696
{
9797
hosts.push_back(image.get());
9898
}
99-
vault = std::make_unique<DefaultVMImageVault>(hosts, url_downloader.get(), cache_directory, data_directory,
100-
days_to_expire);
99+
vault = std::make_unique<DefaultVMImageVault>(
100+
hosts, url_downloader.get(),
101+
mp::utils::backend_directory_path(cache_directory, factory->get_backend_directory_name()),
102+
mp::utils::backend_directory_path(data_directory, factory->get_backend_directory_name()), days_to_expire);
101103
}
102104
if (name_generator == nullptr)
103105
name_generator = mp::make_default_name_generator();

src/platform/backends/libvirt/libvirt_virtual_machine_factory.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018 Canonical, Ltd.
2+
* Copyright (C) 2018-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -43,6 +43,10 @@ class LibVirtVirtualMachineFactory final : public VirtualMachineFactory
4343
void prepare_instance_image(const VMImage& instance_image, const VirtualMachineDescription& desc) override;
4444
void configure(const std::string& name, YAML::Node& meta_config, YAML::Node& user_config) override;
4545
void check_hypervisor_support() override;
46+
QString get_backend_directory_name() override
47+
{
48+
return {};
49+
};
4650

4751
private:
4852
const ProcessFactory* process_factory;

src/platform/backends/qemu/qemu_virtual_machine_factory.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2018 Canonical, Ltd.
2+
* Copyright (C) 2017-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -41,6 +41,10 @@ class QemuVirtualMachineFactory final : public VirtualMachineFactory
4141
void prepare_instance_image(const VMImage& instance_image, const VirtualMachineDescription& desc) override;
4242
void configure(const std::string& name, YAML::Node& meta_config, YAML::Node& user_config) override;
4343
void check_hypervisor_support() override;
44+
QString get_backend_directory_name() override
45+
{
46+
return {};
47+
};
4448

4549
private:
4650
const ProcessFactory* process_factory;

src/utils/utils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ mp::Path mp::utils::make_dir(const QDir& a_dir, const QString& name)
175175
return a_dir.filePath(name);
176176
}
177177

178+
QString mp::utils::backend_directory_path(const mp::Path& path, const QString& subdirectory)
179+
{
180+
if (subdirectory.isEmpty())
181+
return path;
182+
183+
return mp::Path("%1/%2").arg(path).arg(subdirectory);
184+
}
185+
178186
QString mp::utils::make_uuid()
179187
{
180188
auto uuid = QUuid::createUuid().toString();

tests/mock_virtual_machine_factory.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017 Canonical, Ltd.
2+
* Copyright (C) 2017-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -13,8 +13,6 @@
1313
* You should have received a copy of the GNU General Public License
1414
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
*
16-
* Authored by: Alberto Aguirre <[email protected]>
17-
*
1816
*/
1917

2018
#ifndef MULTIPASS_MOCK_VIRTUAL_MACHINE_FACTORY_H
@@ -40,6 +38,7 @@ struct MockVirtualMachineFactory : public VirtualMachineFactory
4038
MOCK_METHOD2(prepare_instance_image, void(const VMImage&, const VirtualMachineDescription&));
4139
MOCK_METHOD3(configure, void(const std::string&, YAML::Node&, YAML::Node&));
4240
MOCK_METHOD0(check_hypervisor_support, void());
41+
MOCK_METHOD0(get_backend_directory_name, QString());
4342
};
4443
}
4544
}

tests/stub_virtual_machine_factory.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017 Canonical, Ltd.
2+
* Copyright (C) 2017-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -13,8 +13,6 @@
1313
* You should have received a copy of the GNU General Public License
1414
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
*
16-
* Authored by: Alberto Aguirre <[email protected]>
17-
*
1816
*/
1917

2018
#ifndef MULTIPASS_STUB_VIRTUAL_MACHINE_FACTORY_H
@@ -62,6 +60,11 @@ struct StubVirtualMachineFactory : public multipass::VirtualMachineFactory
6260
void check_hypervisor_support() override
6361
{
6462
}
63+
64+
QString get_backend_directory_name() override
65+
{
66+
return {};
67+
}
6568
};
6669
}
6770
}

tests/test_utils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,22 @@ TEST(Utils, filename_only_is_returned)
334334
EXPECT_THAT(mp::utils::filename_for(full_path), Eq(file_name));
335335
}
336336

337+
TEST(Utils, no_subdirectory_returns_same_path)
338+
{
339+
mp::Path original_path{"/tmp/foo"};
340+
QString empty_subdir{};
341+
342+
EXPECT_THAT(mp::utils::backend_directory_path(original_path, empty_subdir), Eq(original_path));
343+
}
344+
345+
TEST(Utils, subdirectory_returns_new_path)
346+
{
347+
mp::Path original_path{"/tmp/foo"};
348+
QString subdir{"bar"};
349+
350+
EXPECT_THAT(mp::utils::backend_directory_path(original_path, subdir), Eq(mp::Path{"/tmp/foo/bar"}));
351+
}
352+
337353
TEST(Utils, vm_running_returns_true)
338354
{
339355
mp::VirtualMachine::State state = mp::VirtualMachine::State::running;

0 commit comments

Comments
 (0)