2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-02-01 12:51:12 +08:00
|
|
|
"fmt"
|
2014-06-05 07:46:30 +08:00
|
|
|
"os"
|
|
|
|
|
2014-08-13 03:03:53 +08:00
|
|
|
"github.com/codegangsta/cli"
|
2015-02-01 11:56:27 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-06-05 07:46:30 +08:00
|
|
|
)
|
|
|
|
|
2015-02-01 12:51:12 +08:00
|
|
|
func loadConfig(context *cli.Context) (*configs.Config, error) {
|
|
|
|
f, err := os.Open(context.String("config"))
|
2014-06-05 07:46:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2015-02-01 12:51:12 +08:00
|
|
|
var config *configs.Config
|
|
|
|
if err := json.NewDecoder(f).Decode(&config); err != nil {
|
2014-06-05 07:46:30 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-01 12:51:12 +08:00
|
|
|
return config, nil
|
2014-06-05 07:46:30 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 12:51:12 +08:00
|
|
|
func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
|
|
|
|
return libcontainer.New(context.GlobalString("root"), []string{os.Args[0], "init", "--fd", "3", "--"})
|
|
|
|
}
|
2014-08-13 02:43:12 +08:00
|
|
|
|
2015-02-01 12:51:12 +08:00
|
|
|
func getContainer(context *cli.Context) (libcontainer.Container, error) {
|
|
|
|
factory, err := loadFactory(context)
|
|
|
|
if err != nil {
|
2014-08-13 02:43:12 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-01 12:51:12 +08:00
|
|
|
container, err := factory.Load(context.String("id"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return container, nil
|
2014-08-13 02:43:12 +08:00
|
|
|
}
|
2014-08-13 03:03:53 +08:00
|
|
|
|
2015-02-01 12:51:12 +08:00
|
|
|
func fatal(err error) {
|
|
|
|
if lerr, ok := err.(libcontainer.Error); ok {
|
|
|
|
lerr.Detail(os.Stderr)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fatalf(t string, v ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, t, v...)
|
|
|
|
os.Exit(1)
|
2014-08-13 03:03:53 +08:00
|
|
|
}
|