mirror of
https://github.com/lxc/go-lxc.git
synced 2026-02-05 06:46:38 +01:00
enable Execute method via os/exec for now...
This commit is contained in:
2
const.go
2
const.go
@@ -63,7 +63,7 @@ const (
|
||||
errSettingSwapLimitFailed = "setting swap limit for the container %q failed"
|
||||
errShutdownFailed = "shutting down the container %q failed"
|
||||
errStartFailed = "starting the container %q failed"
|
||||
errExecuteFailed = "executing args in temporary container %q failed"
|
||||
errExecuteFailed = "executing the command in a temporary container %q failed"
|
||||
errStopFailed = "stopping the container %q failed"
|
||||
errSwapLimit = "your kernel does not support cgroup swap controller"
|
||||
errUnfreezeFailed = "unfreezing the container %q failed"
|
||||
|
||||
29
container.go
29
container.go
@@ -16,6 +16,7 @@ import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -329,17 +330,27 @@ func (lxc *Container) Start() error {
|
||||
}
|
||||
|
||||
// Execute executes the given argument in a temporary container
|
||||
func (lxc *Container) Execute(args ...string) error {
|
||||
// FIXME: disable for now
|
||||
return fmt.Errorf("NOT SUPPORTED")
|
||||
func (lxc *Container) Execute(args ...string) ([]byte, error) {
|
||||
if lxc.Defined() {
|
||||
return nil, fmt.Errorf(errAlreadyDefined, C.GoString(lxc.container.name))
|
||||
}
|
||||
|
||||
cargs := []string{"lxc-execute", "-n", lxc.Name(), "-P", lxc.ConfigPath(), "--"}
|
||||
cargs = append(cargs, args...)
|
||||
|
||||
lxc.Lock()
|
||||
defer lxc.Unlock()
|
||||
|
||||
/*
|
||||
if lxc.Defined() || lxc.Running() {
|
||||
return fmt.Errorf(errAlreadyDefined, C.GoString(lxc.container.name))
|
||||
}
|
||||
|
||||
lxc.Lock()
|
||||
defer lxc.Unlock()
|
||||
* FIXME: Go runtime and src/lxc/start.c signal_handler are not playing nice together so use lxc-execute for now
|
||||
*/
|
||||
output, err := exec.Command(cargs[0], cargs[1:]...).CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(errExecuteFailed, C.GoString(lxc.container.name))
|
||||
}
|
||||
|
||||
return output, nil
|
||||
/*
|
||||
cargs := makeNullTerminatedArgs(args)
|
||||
defer freeNullTerminatedArgs(cargs, len(args))
|
||||
|
||||
|
||||
55
examples/execute.go
Normal file
55
examples/execute.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* execute.go
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"github.com/caglar10ur/lxc"
|
||||
)
|
||||
|
||||
var (
|
||||
lxcpath string
|
||||
name string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&lxcpath, "lxcpath", lxc.DefaultConfigPath(), "Use specified container path")
|
||||
flag.StringVar(&name, "name", "rubik", "Name of the container")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func main() {
|
||||
c, err := lxc.NewContainer(name, lxcpath)
|
||||
if err != nil {
|
||||
log.Fatalf("ERROR: %s\n", err.Error())
|
||||
}
|
||||
defer lxc.PutContainer(c)
|
||||
|
||||
c.LoadConfigFile(lxc.DefaultConfigPath())
|
||||
if output, err := c.Execute("uname", "-a"); err != nil {
|
||||
log.Fatalf("ERROR: %s\n", err.Error())
|
||||
} else {
|
||||
log.Printf("%s", output)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user