Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
DobbyRunC Class Reference

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< ContainerListItemlist () 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< IDobbyUtilsmUtilities
 
const std::string mRuncPath
 
const std::string mWorkingDir
 
const std::string mLogDir
 
const std::string mLogFilePath
 
const std::string mConsoleSocket
 

Detailed Description

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.

Member Function Documentation

◆ create()

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.

Parameters
[in]idThe id / name of the container to create
[in]bundleThe bundle directory to pass to the runc tool
[in]consoleThe stream to attach to the stdout / stderr of the runc tool.
[in]filesA list of file descriptors to pass into the container.
[in]customConfigPathPath to a different config.json file to use instead of the one in the bundle
Returns
Pair of PIDs. First PID is the PID of the crun process, used to match the launched process to the logging connection. Second PID is the contents of the container pidfile and is the actual PID of the container

◆ destroy()

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.

Parameters
[in]idThe id / name of the container to create.
Returns
true or false based on the return code of the runc tool.

◆ exec()

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

Parameters
[in]idThe id / name of the container to execute the command in.
[in]optionsThe options to execute the command with.
[in]commandThe command to execute.
Returns
Pair of PIDs. First PID is the PID of the crun process, used to match the launched process to the logging connection. Second PID is the PID of the newly launched process

◆ forkExecRunC()

pid_t DobbyRunC::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
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.

◆ getContainerStatusFromJson()

DobbyRunC::ContainerStatus DobbyRunC::getContainerStatusFromJson ( const Json::Value &  state) const
private

Gets the container status from the json object.

Parameters
[in]stateJson object containing the container state.
Returns
the container state, or ContainerStatus::Unknown.

◆ killCont()

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.

Parameters
[in]idThe id / name of the container to create.
[in]signalThe signal number to send.
Returns
true or false based on the return code of the runc tool.

◆ list()

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.

Returns
A map of current container ids and their status.

◆ pause()

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>
Parameters
[in]idThe id / name of the container to pause.
Returns
true or false based on the return code of the runc tool.

◆ readPidFile()

pid_t DobbyRunC::readPidFile ( const std::string  pidFilePath) const
private

Reads file with pid of the container and converts it into pid_t type variable.

Returns
container pid, -1 if failed

◆ resume()

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>
Parameters
[in]idThe id / name of the container to resume.
Returns
true or false based on the return code of the runc tool.

◆ run()

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.

Parameters
[in]idThe id / name of the container to create
[in]bundleThe bundle directory to pass to the runc tool
[in]consoleThe stream to attach to the stdout / stderr of the runc tool. This in effect is the console out of the container.
[in]filesA list of file descriptors to pass into the container.
Returns
the pid of the runc process if successful, otherwise -1.

◆ start()

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>

Parameters
[in]idThe id of the container created earlier
[in]consoleThe stream to attach to the stdout / stderr of the runc tool.
Returns
true on success, false on failure.

◆ state()

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.

Parameters
[in]idThe id / name of the container to get the state for.
Returns
the container state, or ContainerStatus::Unknown if the container isn't running.

The documentation for this class was generated from the following files: