File indexing completed on 2024-05-05 04:39:44

0001 /*
0002     SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef DOCKERRUNTIME_H
0008 #define DOCKERRUNTIME_H
0009 
0010 #include <interfaces/iruntime.h>
0011 #include <util/path.h>
0012 #include <QHash>
0013 #include <QByteArray>
0014 
0015 class KJob;
0016 class DockerPreferencesSettings;
0017 
0018 class DockerRuntime : public KDevelop::IRuntime
0019 {
0020     Q_OBJECT
0021 public:
0022     explicit DockerRuntime(const QString& tag);
0023     ~DockerRuntime() override;
0024 
0025     /**
0026      * @returns the docker tagname as a text identifier
0027      */
0028     QString name() const override { return m_tag; }
0029 
0030     /**
0031      * if @p enabled
0032      * Mounts the docker image's file system into a subdirectory the user can read.
0033      * if not @p enabled, it unmounts the image file system
0034      *
0035      * See GraphDriver.Data.UpperDir value in docker image inspect imagename
0036      *
0037      * Both require root privileges for now
0038      */
0039     void setEnabled(bool enabled) override;
0040 
0041     /**
0042      * Call processes using "docker run..." passing on the proper environment and volumes
0043      *
0044      * Volumes will include source and build directories that need to be exposed
0045      * into the container.
0046      */
0047     void startProcess(KProcess *process) const override;
0048     void startProcess(QProcess *process) const override;
0049 
0050     /**
0051      * Translates @p runtimePath from within the image into the host
0052      *
0053      * Takes into account the mounted upperDir and the different volumes set up
0054      */
0055     KDevelop::Path pathInHost(const KDevelop::Path & runtimePath) const override;
0056 
0057     /**
0058      * Translates @p localPath into a path that can be accessed by the runtime
0059      */
0060     KDevelop::Path pathInRuntime(const KDevelop::Path & localPath) const override;
0061 
0062     QString findExecutable(const QString& executableName) const override;
0063 
0064     /**
0065      * @returns the environment variable with @p varname set by the recipe (usually the Dockerfile)
0066      */
0067     QByteArray getenv(const QByteArray & varname) const override;
0068 
0069     KDevelop::Path buildPath() const override { return {}; }
0070 
0071     static DockerPreferencesSettings* s_settings;
0072 
0073 private:
0074     void inspectContainer();
0075     QStringList workingDirArgs(QProcess* process) const;
0076 
0077     const QString m_tag;
0078     QString m_container;
0079     QHash<QByteArray,QByteArray> m_envs;
0080     KDevelop::Path m_mergedDir;
0081     KDevelop::Path m_userMergedDir;
0082 };
0083 
0084 #endif