File: dockerruntime.cpp

package info (click to toggle)
kdevelop 4%3A5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 52,544 kB
  • sloc: cpp: 254,897; python: 3,380; sh: 1,271; ansic: 657; xml: 221; php: 95; makefile: 36; lisp: 13; sed: 12
file content (250 lines) | stat: -rw-r--r-- 10,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
   Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "dockerruntime.h"
#include "dockerpreferencessettings.h"
#include "debug_docker.h"

#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <project/projectmodel.h>
#include <project/interfaces/ibuildsystemmanager.h>

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>

#include <KLocalizedString>
#include <KProcess>
#include <KActionCollection>
#include <KShell>
#include <KUser>
#include <QProcess>
#include <QDir>
#include <outputview/outputexecutejob.h>

using namespace KDevelop;

DockerPreferencesSettings* DockerRuntime::s_settings = nullptr;

DockerRuntime::DockerRuntime(const QString &tag)
    : KDevelop::IRuntime()
    , m_tag(tag)
{
    setObjectName(tag);
}

void DockerRuntime::inspectContainer()
{
    QProcess* process = new QProcess(this);
    connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [process, this](int code, QProcess::ExitStatus status){
        process->deleteLater();
        qCDebug(DOCKER) << "inspect container" << code << status;
        if (code || status) {
            qCWarning(DOCKER) << "Could not figure out the container" << m_container;
            return;
        }
        const QJsonArray arr = QJsonDocument::fromJson(process->readAll()).array();
        const QJsonObject obj = arr.constBegin()->toObject();

        const QJsonObject objGraphDriverData = obj.value(QLatin1String("GraphDriver")).toObject().value(QLatin1String("Data")).toObject();
        m_mergedDir = Path(objGraphDriverData.value(QLatin1String("MergedDir")).toString());
        qCDebug(DOCKER) << "mergeddir:" << m_container << m_mergedDir;

        const auto& entries = obj[QLatin1String("Config")].toObject()[QLatin1String("Env")].toArray();
        for (const auto& entry : entries) {
            const auto content = entry.toString().split(QLatin1Char('='));
            if (content.count() != 2)
                continue;
            m_envs.insert(content[0].toLocal8Bit(), content[1].toLocal8Bit());
        }
        qCDebug(DOCKER) << "envs:" << m_container << m_envs;
    });
    process->start(QStringLiteral("docker"), {QStringLiteral("container"), QStringLiteral("inspect"), m_container});
    process->waitForFinished();
    qDebug() << "inspecting" << QStringList{QStringLiteral("container"), QStringLiteral("inspect"), m_container} << process->exitCode();
}

DockerRuntime::~DockerRuntime()
{
}

QByteArray DockerRuntime::getenv(const QByteArray& varname) const
{
    return m_envs.value(varname);
}

void DockerRuntime::setEnabled(bool enable)
{
    if (enable) {
        m_userMergedDir = KDevelop::Path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1String("/docker-") + QString(m_tag).replace(QLatin1Char('/'), QLatin1Char('_')));
        QDir().mkpath(m_userMergedDir.toLocalFile());

        QProcess pCreateContainer;
        pCreateContainer.start(QStringLiteral("docker"), {QStringLiteral("run"), QStringLiteral("-d"), m_tag, QStringLiteral("tail"), QStringLiteral("-f"), QStringLiteral("/dev/null")});
        pCreateContainer.waitForFinished();
        if (pCreateContainer.exitCode()) {
            qCWarning(DOCKER) << "could not create the container" << pCreateContainer.readAll();
        }
        m_container = QString::fromUtf8(pCreateContainer.readAll().trimmed());

        inspectContainer();

        const QStringList cmd = {QStringLiteral("pkexec"), QStringLiteral("bindfs"), QLatin1String("--map=root/")+KUser().loginName(), m_mergedDir.toLocalFile(), m_userMergedDir.toLocalFile() };
        QProcess p;
        p.start(cmd.first(), cmd.mid(1));
        p.waitForFinished();
        if (p.exitCode()) {
            qCDebug(DOCKER) << "bindfs returned" << cmd << p.exitCode() << p.readAll();
        }
    } else {
        int codeContainer = QProcess::execute(QStringLiteral("docker"), {QStringLiteral("kill"), m_container});
        qCDebug(DOCKER) << "docker kill returned" << codeContainer;

        int code = QProcess::execute(QStringLiteral("pkexec"), {QStringLiteral("umount"), m_userMergedDir.toLocalFile()});
        qCDebug(DOCKER) << "umount returned" << code;

        m_container.clear();
    }
}

static QString ensureEndsSlash(const QString &string)
{
    return string.endsWith(QLatin1Char('/')) ? string : (string + QLatin1Char('/'));
}

static QStringList projectVolumes()
{
    QStringList ret;
    const QString dir = ensureEndsSlash(DockerRuntime::s_settings->projectsVolume());
    const QString buildDir = ensureEndsSlash(DockerRuntime::s_settings->buildDirsVolume());

    const auto& projects = ICore::self()->projectController()->projects();
    for (IProject* project : projects) {
        const Path path = project->path();
        if (path.isLocalFile()) {
            ret << QStringLiteral("--volume") << QStringLiteral("%1:%2").arg(path.toLocalFile(), dir + project->name());
        }

        const auto ibsm = project->buildSystemManager();
        if (ibsm) {
            ret << QStringLiteral("--volume") << ibsm->buildDirectory(project->projectItem()).toLocalFile() + QLatin1Char(':') +  buildDir + project->name();
        }
    }
    return ret;
}

QStringList DockerRuntime::workingDirArgs(QProcess* process) const
{
    const auto wd = process->workingDirectory();
    return wd.isEmpty() ? QStringList{} : QStringList{QStringLiteral("-w"), pathInRuntime(KDevelop::Path(wd)).toLocalFile()};
}

void DockerRuntime::startProcess(QProcess* process) const
{
    auto program = process->program();
    if (program.contains(QLatin1Char('/')))
        program = pathInRuntime(Path(program)).toLocalFile();

    const QStringList args = QStringList{QStringLiteral("run"), QStringLiteral("--rm")} << workingDirArgs(process) << KShell::splitArgs(s_settings->extraArguments()) << projectVolumes() << m_tag << program << process->arguments();
    process->setProgram(QStringLiteral("docker"));
    process->setArguments(args);

    qCDebug(DOCKER) << "starting qprocess" << process->program() << process->arguments();
    process->start();
}

void DockerRuntime::startProcess(KProcess* process) const
{
    auto program = process->program();
    if (program[0].contains(QLatin1Char('/')))
        program[0] = pathInRuntime(Path(program[0])).toLocalFile();
    process->setProgram(QStringList{QStringLiteral("docker"), QStringLiteral("run"), QStringLiteral("--rm")} << workingDirArgs(process) << KShell::splitArgs(s_settings->extraArguments()) << projectVolumes() << m_tag << program);

    qCDebug(DOCKER) << "starting kprocess" << process->program().join(QLatin1Char(' '));
    process->start();
}

static Path projectRelPath(const KDevelop::Path & projectsDir, const KDevelop::Path& runtimePath, bool sourceDir)
{
    const auto relPath = projectsDir.relativePath(runtimePath);
    const int index = relPath.indexOf(QLatin1Char('/'));
    auto project = ICore::self()->projectController()->findProjectByName(relPath.left(index));

    if (!project) {
        qCWarning(DOCKER) << "No project for" << relPath;
    } else {
        const auto repPathProject = index < 0 ? QString() : relPath.mid(index+1);
        const auto rootPath = sourceDir ? project->path() : project->buildSystemManager()->buildDirectory(project->projectItem());
        return Path(rootPath, repPathProject);
    }
    return {};
}

KDevelop::Path DockerRuntime::pathInHost(const KDevelop::Path& runtimePath) const
{
    Path ret;
    const Path projectsDir(DockerRuntime::s_settings->projectsVolume());
    if (runtimePath==projectsDir || projectsDir.isParentOf(runtimePath)) {
        ret = projectRelPath(projectsDir, runtimePath, true);
    } else {
        const Path buildDirs(DockerRuntime::s_settings->buildDirsVolume());
        if (runtimePath==buildDirs || buildDirs.isParentOf(runtimePath)) {
            ret = projectRelPath(buildDirs, runtimePath, false);
        } else
            ret = KDevelop::Path(m_userMergedDir, KDevelop::Path(QStringLiteral("/")).relativePath(runtimePath));
    }
    qCDebug(DOCKER) << "pathInHost" << ret << runtimePath;
    return ret;
}

KDevelop::Path DockerRuntime::pathInRuntime(const KDevelop::Path& localPath) const
{
    if (m_userMergedDir==localPath || m_userMergedDir.isParentOf(localPath)) {
        KDevelop::Path ret(KDevelop::Path(QStringLiteral("/")), m_userMergedDir.relativePath(localPath));
        qCDebug(DOCKER) << "docker runtime pathInRuntime..." << ret << localPath;
        return ret;
    } else if (auto project = ICore::self()->projectController()->findProjectForUrl(localPath.toUrl())) {
        const Path projectsDir(DockerRuntime::s_settings->projectsVolume());
        const QString relpath = project->path().relativePath(localPath);
        const KDevelop::Path ret(projectsDir, project->name() + QLatin1Char('/') + relpath);
        qCDebug(DOCKER) << "docker user pathInRuntime..." << ret << localPath;
        return ret;
    } else {
        const auto projects = ICore::self()->projectController()->projects();
        for (auto project : projects) {
            auto ibsm = project->buildSystemManager();
            if (ibsm) {
                const auto builddir = ibsm->buildDirectory(project->projectItem());
                if (builddir != localPath && !builddir.isParentOf(localPath))
                    continue;

                const Path builddirs(DockerRuntime::s_settings->buildDirsVolume());
                const QString relpath = builddir.relativePath(localPath);
                const KDevelop::Path ret(builddirs, project->name() + QLatin1Char('/') + relpath);
                qCDebug(DOCKER) << "docker build pathInRuntime..." << ret << localPath;
                return ret;
            }
        }
        qCWarning(DOCKER) << "only project files are accessible on the docker runtime" << localPath;
    }
    qCDebug(DOCKER) << "bypass..." << localPath;
    return localPath;
}