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.0.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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/lxc/lxc.c', line 68

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.



140
141
142
143
144
145
146
147
148
149
150
# File 'ext/lxc/lxc.c', line 140

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.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/lxc/lxc.c', line 174

static VALUE
lxc_list_containers(int argc, VALUE *argv, VALUE self)
{
    int i, num_containers;
    int active, defined;
    char *config;
    char **names;
    VALUE rb_active, rb_defined, rb_config;
    VALUE rb_opts;
    VALUE rb_containers;

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

    if (NIL_P(rb_opts)) {
        active = 1;
        defined = 1;
        config = NULL;
    } else {
        Check_Type(rb_opts, T_HASH);
        rb_active = rb_hash_aref(rb_opts, SYMBOL("active"));
        active = (rb_active != Qnil) && (rb_active != Qfalse);
        rb_defined = rb_hash_aref(rb_opts, SYMBOL("defined"));
        defined = (rb_defined != Qnil) && (rb_defined != Qfalse);
        rb_config = rb_hash_aref(rb_opts, SYMBOL("config_path"));
        config = NIL_P(rb_config) ? NULL : StringValuePtr(rb_config);
    }

    num_containers = 0;
    if (active && defined)
        num_containers = list_all_containers(config, &names, NULL);
    else if (active)
        num_containers = list_active_containers(config, &names, NULL);
    else if (defined)
        num_containers = list_defined_containers(config, &names, NULL);
    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(names[i]));
        free(names[i]);
    }
    free(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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/lxc/lxc.c', line 94

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.



122
123
124
125
126
127
128
129
130
131
132
# File 'ext/lxc/lxc.c', line 122

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.



158
159
160
161
162
# File 'ext/lxc/lxc.c', line 158

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