Dobby
3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
|
Wrapper around the runc command line app. More...
#include <DobbyRunC.h>
Classes | |
struct | ContainerListItem |
Public Types | |
enum class | ContainerStatus { Unknown , Created , Running , Pausing , Paused , Stopped } |
Public Member Functions | |
DobbyRunC (const std::shared_ptr< IDobbyUtils > &utils, const std::shared_ptr< const IDobbySettings > &settings) | |
std::pair< pid_t, pid_t > | create (const ContainerId &id, const std::shared_ptr< const DobbyBundle > &bundle, const std::shared_ptr< const IDobbyStream > &console, const std::list< int > &files=std::list< int >(), const std::string &customConfigPath="") const |
Creates the container but doesn't start the init process. More... | |
bool | destroy (const ContainerId &id, const std::shared_ptr< const IDobbyStream > &console, bool force=false) const |
Runs the runc command line tool with the 'delete' command. More... | |
bool | start (const ContainerId &id, const std::shared_ptr< const IDobbyStream > &console) const |
Starts a container created with create command. More... | |
bool | killCont (const ContainerId &id, int signal, bool all=false) const |
Runs the runc command line tool with the 'kill' command. More... | |
bool | pause (const ContainerId &id) const |
Runs the runc command line tool with the 'pause' command. More... | |
bool | resume (const ContainerId &id) const |
Runs the runc command line tool with the 'resume' command. More... | |
std::pair< pid_t, pid_t > | exec (const ContainerId &id, const std::string &options, const std::string &command) const |
Runs the crun command line tool with the 'exec' command. More... | |
ContainerStatus | state (const ContainerId &id) const |
Runs the runc command line tool with the 'state' command. More... | |
std::list< ContainerListItem > | list () const |
Runs the runc command line tool with the 'list' command. More... | |
pid_t | run (const ContainerId &id, const std::shared_ptr< const DobbyBundle > &bundle, const std::shared_ptr< const IDobbyStream > &console, const std::list< int > &files=std::list< int >()) const |
Runs the runc command line tool with the 'run' command. More... | |
const std::string | getWorkingDir () const |
Private Member Functions | |
pid_t | forkExecRunC (const std::vector< const char * > &args, const std::initializer_list< const char * > &envs, const std::list< int > &files=std::list< int >(), const std::shared_ptr< const IDobbyStream > &stdoutStream=nullptr, const std::shared_ptr< const IDobbyStream > &stderrStream=nullptr) const |
Performs a fork then exec of the runC binary with the supplied args. More... | |
pid_t | readPidFile (const std::string pidFilePath) const |
Reads file with pid of the container and converts it into pid_t type variable. More... | |
ContainerStatus | getContainerStatusFromJson (const Json::Value &state) const |
Gets the container status from the json object. More... | |
Private Attributes | |
const std::shared_ptr< IDobbyUtils > | mUtilities |
const std::string | mRuncPath |
const std::string | mWorkingDir |
const std::string | mLogDir |
const std::string | mLogFilePath |
const std::string | mConsoleSocket |
Wrapper around the runc command line app.
This class just formats the args and environment variables to give the runc command line app, which we launch with a fork/exec.
std::pair< pid_t, pid_t > DobbyRunC::create | ( | const ContainerId & | id, |
const std::shared_ptr< const DobbyBundle > & | bundle, | ||
const std::shared_ptr< const IDobbyStream > & | console, | ||
const std::list< int > & | files = std::list<int>() , |
||
const std::string & | customConfigPath = "" |
||
) | const |
Creates the container but doesn't start the init process.
This is equivalent to calling the following on the command line /usr/sbin/runc create –bundle <dir> <id>
The pid of the runc tool is returned, this will survive until the container is destroyed. A watch should be placed on the pid to determine when it quits and this is the point the container is destroyed.
[in] | id | The id / name of the container to create |
[in] | bundle | The bundle directory to pass to the runc tool |
[in] | console | The stream to attach to the stdout / stderr of the runc tool. |
[in] | files | A list of file descriptors to pass into the container. |
[in] | customConfigPath | Path to a different config.json file to use instead of the one in the bundle |
bool DobbyRunC::destroy | ( | const ContainerId & | id, |
const std::shared_ptr< const IDobbyStream > & | console, | ||
bool | force = false |
||
) | const |
Runs the runc command line tool with the 'delete' command.
This is equivalent to calling the following on the command line
/usr/sbin/runc delete <id>
This command will delete any cruft that the runc tool has left behind, things like cgroups, log and or pid files, etc.
[in] | id | The id / name of the container to create. |
std::pair< pid_t, pid_t > DobbyRunC::exec | ( | const ContainerId & | id, |
const std::string & | options, | ||
const std::string & | command | ||
) | const |
Runs the crun command line tool with the 'exec' command.
This is equivalent to calling the following on the command line
/usr/sbin/crun exec [options...] --detach <id> <command>
The exec is run with the –detach option enabled so we're not stuck waiting for the command to finish execution.
TODO:: Fix bug where Dobby does not correctly detect when container exits after running exec due to zombie process issue
[in] | id | The id / name of the container to execute the command in. |
[in] | options | The options to execute the command with. |
[in] | command | The command to execute. |
|
private |
Performs a fork then exec of the runC binary with the supplied args.
This function blocks until the runC process has finished, then it returns the integer result.
stdin is redirected to /dev/null before the exec. If a stdoutStream or stderrStream arguments are supplied then the stdout/stderr output will be written into the supplied streams, otherwise they'll also be redirected to /dev/null.
|
private |
Gets the container status from the json object.
[in] | state | Json object containing the container state. |
bool DobbyRunC::killCont | ( | const ContainerId & | id, |
int | signal, | ||
bool | all = false |
||
) | const |
Runs the runc command line tool with the 'kill' command.
This is equivalent to calling the following on the command line /usr/sbin/runc kill <id> <signal>
We only support the following signals; SIGTERM, SIGKILL, SIGUSR1, SIGUSR2 & SIGUSR2.
[in] | id | The id / name of the container to create. |
[in] | signal | The signal number to send. |
std::list< DobbyRunC::ContainerListItem > DobbyRunC::list | ( | ) | const |
Runs the runc command line tool with the 'list' command.
This is equivalent to calling the following on the command line
/usr/sbin/runc list
We use the json formatted response for easy parsing of the container data.
bool DobbyRunC::pause | ( | const ContainerId & | id | ) | const |
Runs the runc command line tool with the 'pause' command.
This is equivalent to calling the following on the command line:
/usr/sbin/runc pause <id>
[in] | id | The id / name of the container to pause. |
|
private |
Reads file with pid of the container and converts it into pid_t type variable.
bool DobbyRunC::resume | ( | const ContainerId & | id | ) | const |
Runs the runc command line tool with the 'resume' command.
This is equivalent to calling the following on the command line
/usr/sbin/runc resume <id>
[in] | id | The id / name of the container to resume. |
pid_t DobbyRunC::run | ( | const ContainerId & | id, |
const std::shared_ptr< const DobbyBundle > & | bundle, | ||
const std::shared_ptr< const IDobbyStream > & | console, | ||
const std::list< int > & | files = std::list<int>() |
||
) | const |
Runs the runc command line tool with the 'run' command.
This is equivalent to calling the following on the command line
/usr/sbin/runc run --bundle <dir> <id>
The pid of the runc tool is returned, this will survive until the container is destroyed. A watch should be placed on the pid to determine when it quits and this is the point the container is destroyed.
[in] | id | The id / name of the container to create |
[in] | bundle | The bundle directory to pass to the runc tool |
[in] | console | The stream to attach to the stdout / stderr of the runc tool. This in effect is the console out of the container. |
[in] | files | A list of file descriptors to pass into the container. |
bool DobbyRunC::start | ( | const ContainerId & | id, |
const std::shared_ptr< const IDobbyStream > & | console | ||
) | const |
Starts a container created with create command.
This is equivalent to calling the following on the command line /usr/sbin/runc start –bundle <dir> <id>
[in] | id | The id of the container created earlier |
[in] | console | The stream to attach to the stdout / stderr of the runc tool. |
DobbyRunC::ContainerStatus DobbyRunC::state | ( | const ContainerId & | id | ) | const |
Runs the runc command line tool with the 'state' command.
This is equivalent to calling the following on the command line
/usr/sbin/runc state <id>
The response is json formatted for easy parsing of the container data.
[in] | id | The id / name of the container to get the state for. |