Skip to content

Commit

Permalink
[FIO toup] docker-apps: Add support for getCurrent
Browse files Browse the repository at this point in the history
This will be needed for aktualizr-lite so that it can understand
what version of a docker app is deployed.

Signed-off-by: Andy Doan <andy@foundries.io>
  • Loading branch information
doanac committed Jul 15, 2019
1 parent aebd6f6 commit 40c51db
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/libaktualizr/package_manager/dockerappmanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ bool DockerAppManager::fetchTarget(const Uptane::Target &target, Uptane::Fetcher

data::InstallationResult DockerAppManager::install(const Uptane::Target &target) const {
auto res = OstreeManager::install(target);
if (config.docker_apps.empty()) {
return res;
}
Utils::writeFile(config.docker_apps_root / "target_apps", target.custom_data()["docker_apps"], true);
auto cb = [this](const std::string &app, const Uptane::Target &app_target) {
LOG_INFO << "Installing " << app << " -> " << app_target;
std::stringstream ss;
Expand All @@ -114,3 +118,17 @@ data::InstallationResult DockerAppManager::install(const Uptane::Target &target)
}
return res;
}

Uptane::Target DockerAppManager::getCurrent() const {
auto target = fakeGetCurrent ? Uptane::Target::Unknown() : OstreeManager::getCurrent();
Json::Value apps;

std::ifstream apps_stream((config.docker_apps_root / ("target_apps")).c_str());
if (!apps_stream.fail()) {
auto custom = target.custom_data();
apps_stream >> custom["docker_apps"];
target.updateCustom(custom);
}
return target;
}
bool DockerAppManager::fakeGetCurrent = false;
4 changes: 4 additions & 0 deletions src/libaktualizr/package_manager/dockerappmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class DockerAppManager : public OstreeManager {
FetcherProgressCb progress_cb, const api::FlowControlToken *token = nullptr) override;
data::InstallationResult install(const Uptane::Target &target) const override;
std::string name() const override { return "ostree+docker-app"; }
Uptane::Target getCurrent() const override;

static bool fakeGetCurrent;

private:
bool iterate_apps(const Uptane::Target &target, DockerAppCb cb) const;
Expand All @@ -25,4 +28,5 @@ class DockerAppManager : public OstreeManager {
// and we just need to construct a dummy one to make the compiler happy.
std::shared_ptr<Uptane::Fetcher> fake_fetcher_;
};

#endif // DOCKERAPPMGR_H_
14 changes: 13 additions & 1 deletion src/libaktualizr/package_manager/dockerappmanager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "config/config.h"
#include "http/httpclient.h"
#include "package_manager/dockerappmanager.h"
#include "package_manager/packagemanagerfactory.h"
#include "package_manager/packagemanagerinterface.h"
#include "primary/sotauptaneclient.h"
Expand Down Expand Up @@ -81,8 +82,12 @@ TEST(DockerAppManager, DockerApp_Fetch) {
KeyManager keys(storage, config.keymanagerConfig());
auto http = std::make_shared<HttpClient>();
auto client = newTestClient(config, storage, http, nullptr);
ASSERT_TRUE(client->updateImagesMeta());

// Make sure we can read an empty target
auto current = client->package_manager_->getCurrent();
ASSERT_EQ("", current.custom_data()["docker_apps"].asString());

ASSERT_TRUE(client->updateImagesMeta());
std::string targets = Utils::readFile(repo / "repo/image/targets.json");
LOG_INFO << "Repo targets " << targets;

Expand All @@ -100,6 +105,9 @@ TEST(DockerAppManager, DockerApp_Fetch) {
client->package_manager_->install(target);
std::string content = Utils::readFile(config.pacman.docker_apps_root / "app1/docker-compose.yml");
ASSERT_EQ("DOCKER-APP RENDER OUTPUT\nfake contents of a docker app\n", content);

current = client->package_manager_->getCurrent();
ASSERT_EQ("foo.dockerapp", current.custom_data()["docker_apps"]["app1"]["filename"].asString());
}

#ifndef __NO_MAIN__
Expand Down Expand Up @@ -127,6 +135,10 @@ int main(int argc, char** argv) {

TestUtils::waitForServer(treehub_server + "/");

// OstreeManager::getCurrent throws an exception because there's not
// a real booted deployment.
DockerAppManager::fakeGetCurrent = true;

return RUN_ALL_TESTS();
}
#endif
1 change: 1 addition & 0 deletions src/libaktualizr/uptane/tuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class Target {
std::vector<HardwareIdentifier> hardwareIds() const { return hwids_; };
std::string custom_version() const { return custom_["version"].asString(); }
Json::Value custom_data() const { return custom_; }
void updateCustom(Json::Value &custom) { custom_ = custom; };
std::string correlation_id() const { return correlation_id_; };
void setCorrelationId(std::string correlation_id) { correlation_id_ = std::move(correlation_id); };
uint64_t length() const { return length_; }
Expand Down

0 comments on commit 40c51db

Please sign in to comment.