2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-06-05 07:46:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2014-08-13 03:03:53 +08:00
|
|
|
"github.com/codegangsta/cli"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-06-05 07:46:30 +08:00
|
|
|
)
|
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
// rFunc is a function registration for calling after an execin
|
|
|
|
type rFunc struct {
|
|
|
|
Usage string
|
2014-12-17 17:12:23 +08:00
|
|
|
Action func(*configs.Config, []string)
|
2014-08-13 02:43:12 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 17:12:23 +08:00
|
|
|
func loadConfig() (*configs.Config, error) {
|
2014-06-05 07:46:30 +08:00
|
|
|
f, err := os.Open(filepath.Join(dataPath, "container.json"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-12-17 17:12:23 +08:00
|
|
|
var container *configs.Config
|
2014-06-05 07:46:30 +08:00
|
|
|
if err := json.NewDecoder(f).Decode(&container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openLog(name string) error {
|
|
|
|
f, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetOutput(f)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-06 05:28:09 +08:00
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
func findUserArgs() []string {
|
|
|
|
i := 0
|
|
|
|
for _, a := range os.Args {
|
|
|
|
i++
|
|
|
|
|
|
|
|
if a == "--" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Args[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadConfigFromFd loads a container's config from the sync pipe that is provided by
|
|
|
|
// fd 3 when running a process
|
2014-12-17 17:12:23 +08:00
|
|
|
func loadConfigFromFd() (*configs.Config, error) {
|
2014-11-04 10:18:55 +08:00
|
|
|
pipe := os.NewFile(3, "pipe")
|
|
|
|
defer pipe.Close()
|
2014-08-13 02:43:12 +08:00
|
|
|
|
2014-12-17 17:12:23 +08:00
|
|
|
var config *configs.Config
|
2014-11-04 10:18:55 +08:00
|
|
|
if err := json.NewDecoder(pipe).Decode(&config); err != nil {
|
2014-08-13 02:43:12 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
2014-08-13 03:03:53 +08:00
|
|
|
|
|
|
|
func preload(context *cli.Context) error {
|
|
|
|
if logPath != "" {
|
|
|
|
if err := openLog(logPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runFunc(f *rFunc) {
|
|
|
|
userArgs := findUserArgs()
|
|
|
|
|
|
|
|
config, err := loadConfigFromFd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to receive config from sync pipe: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Action(config, userArgs)
|
|
|
|
}
|