2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2022-04-05 11:43:56 +02:00
|
|
|
|
|
|
|
|
#include "dockerapi.h"
|
|
|
|
|
|
2022-07-13 10:43:31 +02:00
|
|
|
#include "dockertr.h"
|
|
|
|
|
|
2022-04-05 11:43:56 +02:00
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2023-05-03 15:05:47 +02:00
|
|
|
#include <utils/async.h>
|
2023-05-03 17:05:35 +02:00
|
|
|
#include <utils/process.h>
|
2022-05-06 11:59:17 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2022-04-05 11:43:56 +02:00
|
|
|
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
2022-08-02 10:14:40 +02:00
|
|
|
Q_LOGGING_CATEGORY(dockerApiLog, "qtc.docker.api", QtWarningMsg);
|
2022-04-05 11:43:56 +02:00
|
|
|
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
2022-07-13 10:43:31 +02:00
|
|
|
namespace Docker::Internal {
|
|
|
|
|
|
2022-04-05 11:43:56 +02:00
|
|
|
DockerApi *s_instance{nullptr};
|
|
|
|
|
|
2023-07-14 15:30:19 +02:00
|
|
|
DockerApi::DockerApi()
|
2022-04-05 11:43:56 +02:00
|
|
|
{
|
|
|
|
|
s_instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DockerApi *DockerApi::instance()
|
|
|
|
|
{
|
|
|
|
|
return s_instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DockerApi::canConnect()
|
|
|
|
|
{
|
2023-05-03 16:00:22 +02:00
|
|
|
Process process;
|
2022-07-04 11:09:11 +02:00
|
|
|
FilePath dockerExe = dockerClient();
|
2022-04-05 11:43:56 +02:00
|
|
|
if (dockerExe.isEmpty() || !dockerExe.isExecutableFile())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
process.setCommand(CommandLine(dockerExe, QStringList{"info"}));
|
2023-05-03 16:00:22 +02:00
|
|
|
connect(&process, &Process::done, [&process, &result] {
|
2022-04-05 11:43:56 +02:00
|
|
|
qCInfo(dockerApiLog) << "'docker info' result:\n" << qPrintable(process.allOutput());
|
|
|
|
|
if (process.result() == ProcessResult::FinishedWithSuccess)
|
|
|
|
|
result = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.start();
|
|
|
|
|
process.waitForFinished();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 14:04:20 +02:00
|
|
|
void DockerApi::checkCanConnect(bool async)
|
2022-04-05 11:43:56 +02:00
|
|
|
{
|
2022-04-07 14:04:20 +02:00
|
|
|
if (async) {
|
|
|
|
|
std::unique_lock lk(m_daemonCheckGuard, std::try_to_lock);
|
|
|
|
|
if (!lk.owns_lock())
|
|
|
|
|
return;
|
2022-04-05 11:43:56 +02:00
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
m_dockerDaemonAvailable = std::nullopt;
|
2022-07-04 11:09:11 +02:00
|
|
|
emit dockerDaemonAvailableChanged();
|
2022-04-05 11:43:56 +02:00
|
|
|
|
2023-03-09 17:03:28 +01:00
|
|
|
auto future = Utils::asyncRun([lk = std::move(lk), this] {
|
2022-04-07 14:04:20 +02:00
|
|
|
m_dockerDaemonAvailable = canConnect();
|
2022-07-04 11:09:11 +02:00
|
|
|
emit dockerDaemonAvailableChanged();
|
2022-04-07 14:04:20 +02:00
|
|
|
});
|
|
|
|
|
|
2022-07-13 10:43:31 +02:00
|
|
|
Core::ProgressManager::addTask(future, Tr::tr("Checking docker daemon"), "DockerPlugin");
|
2022-04-07 14:04:20 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_lock lk(m_daemonCheckGuard);
|
|
|
|
|
bool isAvailable = canConnect();
|
|
|
|
|
if (!m_dockerDaemonAvailable.has_value() || isAvailable != m_dockerDaemonAvailable) {
|
|
|
|
|
m_dockerDaemonAvailable = isAvailable;
|
2022-07-04 11:09:11 +02:00
|
|
|
emit dockerDaemonAvailableChanged();
|
2022-04-07 14:04:20 +02:00
|
|
|
}
|
2022-04-05 11:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DockerApi::recheckDockerDaemon()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(s_instance, return );
|
|
|
|
|
s_instance->checkCanConnect();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<bool> DockerApi::dockerDaemonAvailable(bool async)
|
2022-04-05 11:43:56 +02:00
|
|
|
{
|
|
|
|
|
if (!m_dockerDaemonAvailable.has_value())
|
2022-04-07 14:04:20 +02:00
|
|
|
checkCanConnect(async);
|
2022-04-05 11:43:56 +02:00
|
|
|
return m_dockerDaemonAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<bool> DockerApi::isDockerDaemonAvailable(bool async)
|
2022-04-05 11:43:56 +02:00
|
|
|
{
|
2022-08-26 10:30:00 +02:00
|
|
|
QTC_ASSERT(s_instance, return std::nullopt);
|
2022-04-07 14:04:20 +02:00
|
|
|
return s_instance->dockerDaemonAvailable(async);
|
2022-04-05 11:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-04 11:09:11 +02:00
|
|
|
FilePath DockerApi::dockerClient()
|
2022-04-05 11:43:56 +02:00
|
|
|
{
|
2023-07-14 15:30:19 +02:00
|
|
|
return settings().dockerBinaryPath();
|
2022-04-05 11:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 14:17:11 +02:00
|
|
|
QFuture<Utils::expected_str<QList<Network>>> DockerApi::networks()
|
|
|
|
|
{
|
|
|
|
|
return Utils::asyncRun([this]() -> Utils::expected_str<QList<Network>> {
|
|
|
|
|
QList<Network> result;
|
|
|
|
|
|
|
|
|
|
Process process;
|
|
|
|
|
FilePath dockerExe = dockerClient();
|
|
|
|
|
if (dockerExe.isEmpty() || !dockerExe.isExecutableFile())
|
|
|
|
|
return make_unexpected(Tr::tr("Docker executable not found"));
|
|
|
|
|
|
|
|
|
|
process.setCommand(
|
|
|
|
|
CommandLine(dockerExe, QStringList{"network", "ls", "--format", "{{json .}}"}));
|
|
|
|
|
process.runBlocking();
|
|
|
|
|
|
|
|
|
|
if (process.result() != ProcessResult::FinishedWithSuccess) {
|
|
|
|
|
return make_unexpected(
|
|
|
|
|
Tr::tr("Failed to retrieve docker networks. Exit code: %1. Error: %2")
|
|
|
|
|
.arg(process.exitCode())
|
|
|
|
|
.arg(process.allOutput()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const auto &line : process.readAllStandardOutput().split('\n')) {
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(line.toUtf8(), &error);
|
|
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
|
qCWarning(dockerApiLog)
|
|
|
|
|
<< "Failed to parse docker network info:" << error.errorString();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Network network;
|
|
|
|
|
network.id = doc["ID"].toString();
|
|
|
|
|
network.name = doc["Name"].toString();
|
|
|
|
|
network.driver = doc["Driver"].toString();
|
|
|
|
|
network.scope = doc["Scope"].toString();
|
|
|
|
|
network.internal = doc["Internal"].toString() == "true";
|
|
|
|
|
network.ipv6 = doc["IPv6"].toString() == "true";
|
|
|
|
|
network.createdAt = QDateTime::fromString(doc["CreatedAt"].toString(), Qt::ISODate);
|
|
|
|
|
network.labels = doc["Labels"].toString();
|
|
|
|
|
|
|
|
|
|
result.append(network);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Network::toString() const
|
|
|
|
|
{
|
|
|
|
|
return QString(R"(ID: "%1"
|
|
|
|
|
Name: "%2"
|
|
|
|
|
Driver: "%3"
|
|
|
|
|
Scope: "%4"
|
|
|
|
|
Internal: "%5"
|
|
|
|
|
IPv6: "%6"
|
|
|
|
|
CreatedAt: "%7"
|
|
|
|
|
Labels: "%8"
|
|
|
|
|
)")
|
|
|
|
|
.arg(id)
|
|
|
|
|
.arg(name)
|
|
|
|
|
.arg(driver)
|
|
|
|
|
.arg(scope)
|
|
|
|
|
.arg(internal)
|
|
|
|
|
.arg(ipv6)
|
|
|
|
|
.arg(createdAt.toString(Qt::ISODate))
|
|
|
|
|
.arg(labels);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 10:43:31 +02:00
|
|
|
} // Docker::Internal
|