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"
|
2015-04-11 12:28:07 +08:00
|
|
|
log "github.com/Sirupsen/logrus"
|
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"
|
2015-04-11 12:28:07 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups/systemd"
|
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) {
|
2015-03-12 02:46:11 +08:00
|
|
|
if path := context.String("config"); path != "" {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var config *configs.Config
|
|
|
|
if err := json.NewDecoder(f).Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-12 09:42:58 +08:00
|
|
|
return config, nil
|
|
|
|
}
|
2015-03-12 02:46:11 +08:00
|
|
|
config := getTemplate()
|
|
|
|
modify(config, context)
|
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) {
|
2015-04-11 12:28:07 +08:00
|
|
|
cgm := libcontainer.Cgroupfs
|
|
|
|
if context.Bool("systemd") {
|
|
|
|
if systemd.UseSystemd() {
|
|
|
|
cgm = libcontainer.SystemdCgroups
|
|
|
|
} else {
|
|
|
|
log.Warn("systemd cgroup flag passed, but systemd support for managing cgroups is not available.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return libcontainer.New(context.GlobalString("root"), cgm)
|
2015-02-01 12:51:12 +08:00
|
|
|
}
|
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)
|
2015-02-25 06:43:41 +08:00
|
|
|
os.Exit(1)
|
2015-02-01 12:51:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func fatalf(t string, v ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, t, v...)
|
|
|
|
os.Exit(1)
|
2014-08-13 03:03:53 +08:00
|
|
|
}
|