Module: LXC

Defined in:
ext/lxc/lxc.c,
lib/lxc/version.rb,
ext/lxc/lxc.c

Overview

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.

Defined Under Namespace

Classes: Container, Error

Constant Summary collapse

VERSION =
'1.1.1'

Class Method Summary collapse

Class Method Details

.arch_to_personality(arch) ⇒ Object

Converts an architecture string (x86, i686, x86_64 or amd64) to a “personality”, either :linux32 or :linux, for the 32-bit and 64-bit architectures, respectively.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/lxc/lxc.c', line 115

static VALUE
lxc_arch_to_personality(VALUE self, VALUE rb_arch)
{
    int ret;
    char *arch;

    arch = StringValuePtr(rb_arch);
    ret = lxc_config_parse_arch(arch);

    switch (ret) {
    case PER_LINUX32:
        return SYMBOL("linux32");
    case PER_LINUX:
        return SYMBOL("linux");
    default:
        rb_raise(Error, "unknown personality");
    }
}

.global_config_item(key) ⇒ Object

Returns value for the given global config key.



187
188
189
190
191
192
193
194
195
196
197
# File 'ext/lxc/lxc.c', line 187

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]) ⇒ Object

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.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'ext/lxc/lxc.c', line 247

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) ⇒ Object

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/lxc/lxc.c', line 141

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_shellObject

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



169
170
171
172
173
174
175
176
177
178
179
# File 'ext/lxc/lxc.c', line 169

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;
}

.versionObject

Returns the liblxc version.



205
206
207
208
209
# File 'ext/lxc/lxc.c', line 205

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