module LXC

This module provides a Ruby API allowing programmatic managing of “Linux Containers”.

The LXC module contains generic methods (which are not related to a specific container instance) and methods related to liblxc. The container-specific methods are contained in the LXC::Container class.

Constants

VERSION

Public Class Methods

global_config_item(key) click to toggle source

Returns value for the given global config key.

static VALUE
lxc_global_config_item(VALUE self, VALUE rb_key)
{
    char *key;
    const char *value;
    key = StringValuePtr(rb_key);
    value = lxc_get_global_config_item(key);
    if (value == NULL)
        rb_raise(Error, "invalid configuration key %s", key);
    return rb_str_new2(value);
}
list_containers([opts]) click to toggle source

Returns an array of containers. Which containers are returned depends on the options hash: by default, all containers are returned. One may list only active or defined containers by setting either the :active or :defined keys to true. The :config_path key allows an alternate configuration path to be scanned when building the list.

static VALUE
lxc_list_containers(int argc, VALUE *argv, VALUE self)
{
    int i, num_containers;
    VALUE rb_active, rb_defined, rb_config;
    VALUE rb_opts;
    VALUE rb_containers;
    struct list_containers_without_gvl_args args;

    rb_scan_args(argc, argv, "01", &rb_opts);

    args.active = 1;
    args.defined = 1;
    args.config = NULL;

    if (!NIL_P(rb_opts)) {
        Check_Type(rb_opts, T_HASH);

        rb_active = rb_hash_aref(rb_opts, SYMBOL("active"));
        if (!NIL_P(rb_active))
            args.active = rb_active != Qfalse;

        rb_defined = rb_hash_aref(rb_opts, SYMBOL("defined"));
        if (!NIL_P(rb_defined))
            args.defined = rb_defined != Qfalse;

        rb_config = rb_hash_aref(rb_opts, SYMBOL("config_path"));
        if (!NIL_P(rb_config))
            args.config = StringValuePtr(rb_config);
    }
    num_containers = RELEASING_GVL(list_containers_without_gvl, &args);
    if (num_containers < 0)
        rb_raise(Error, "failure to list containers");

    rb_containers = rb_ary_new2(num_containers);
    /*
     * The `names` array is not NULL-terminated, so free it manually,
     * ie, don't use free_c_string_array().
     */
    for (i = 0; i < num_containers; i++) {
        rb_ary_store(rb_containers, i, rb_str_new2(args.names[i]));
        free(args.names[i]);
    }
    free(args.names);

    return rb_containers;
}
run_command(command) click to toggle source

Runs the given command (given as a string or as an argv array) in an attached container. Useful in conjunction with +LXC::Container#attach+.

static VALUE
lxc_run_command(VALUE self, VALUE rb_command)
{
    int ret;
    lxc_attach_command_t cmd;
    VALUE rb_program;

    if (TYPE(rb_command) == T_STRING)
        rb_command = rb_str_split(rb_command, " ");

    rb_program = rb_ary_entry(rb_command, 0);
    cmd.program = StringValuePtr(rb_program);
    cmd.argv = ruby_to_c_string_array(rb_command);

    ret = lxc_attach_run_command(&cmd);
    if (ret == -1)
        rb_raise(Error, "unable to run command on attached container");
    /* NOTREACHED */
    return Qnil;
}
run_shell click to toggle source

Runs a shell in an attached container. Useful in conjunction with +LXC::Container#attach+.

static VALUE
lxc_run_shell(VALUE self)
{
    int ret;

    ret = lxc_attach_run_shell(NULL);
    if (ret == -1)
        rb_raise(Error, "unable to run shell on attached container");
    /* NOTREACHED */
    return Qnil;
}
version click to toggle source

Returns the liblxc version.

static VALUE
lxc_version(VALUE self)
{
    return rb_str_new2(lxc_get_version());
}