forked from caglar10ur/go-lxc-deprecated
/
lxc.go
92 lines (79 loc) · 2.13 KB
/
lxc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* lxc.go: Go bindings for lxc
*
* Copyright © 2013, S.Çağlar Onur
*
* Authors:
* S.Çağlar Onur <caglar@10ur.org>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//Go (golang) Bindings for LXC (Linux Containers)
//
//This package implements Go bindings for the LXC C API.
package lxc
// #cgo linux LDFLAGS: -llxc -lutil
// #include <lxc/lxc.h>
// #include <lxc/lxccontainer.h>
// #include "lxc.h"
import "C"
import (
"os"
"path/filepath"
"unsafe"
)
const (
// Timeout
WAIT_FOREVER int = iota - 1
DONT_WAIT
LXC_NETWORK_KEY = "lxc.network"
)
func init() {
if os.Geteuid() != 0 {
panic("Running as non-root.")
}
}
func NewContainer(name string) *Container {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return &Container{container: C.lxc_container_new(cname, nil)}
}
// Returns LXC version
func Version() string {
return C.GoString(C.lxc_get_version())
}
// Returns default config path
func DefaultConfigPath() string {
return C.GoString(C.lxc_get_default_config_path())
}
// Returns the names of containers on the system.
func ContainerNames() []string {
// FIXME: Support custom config paths
matches, err := filepath.Glob(filepath.Join(DefaultConfigPath(), "/*/config"))
if err != nil {
return nil
}
for i, v := range matches {
matches[i] = filepath.Base(filepath.Dir(v))
}
return matches
}
// Returns the containers on the system.
func Containers() []Container {
var containers []Container
for _, v := range ContainerNames() {
containers = append(containers, *NewContainer(v))
}
return containers
}