2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-02-20 08:40:36 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-03-04 13:47:03 +08:00
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
"github.com/codegangsta/cli"
|
2014-02-20 08:40:36 +08:00
|
|
|
)
|
|
|
|
|
2014-08-09 02:16:56 +08:00
|
|
|
var (
|
|
|
|
logPath = os.Getenv("log")
|
2014-08-13 02:43:12 +08:00
|
|
|
argvs = make(map[string]*rFunc)
|
2014-08-09 02:16:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-08-13 02:43:12 +08:00
|
|
|
argvs["nsenter-exec"] = &rFunc{
|
|
|
|
Usage: "execute a process inside an existing container",
|
|
|
|
Action: nsenterExec,
|
|
|
|
}
|
|
|
|
|
|
|
|
argvs["nsenter-mknod"] = &rFunc{
|
|
|
|
Usage: "mknod a device inside an existing container",
|
|
|
|
Action: nsenterMknod,
|
|
|
|
}
|
2014-08-09 02:16:56 +08:00
|
|
|
}
|
2014-02-20 12:35:04 +08:00
|
|
|
|
2014-06-06 05:28:09 +08:00
|
|
|
func preload(context *cli.Context) error {
|
2014-06-05 07:46:30 +08:00
|
|
|
if logPath != "" {
|
2014-06-06 05:28:09 +08:00
|
|
|
if err := openLog(logPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-24 09:06:14 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
return nil
|
2014-02-20 12:35:04 +08:00
|
|
|
}
|
2014-05-14 16:56:55 +08:00
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-08-13 01:46:43 +08:00
|
|
|
func main() {
|
2014-08-09 02:16:56 +08:00
|
|
|
// we need to check our argv 0 for any registred functions to run instead of the
|
|
|
|
// normal cli code path
|
2014-08-13 02:43:12 +08:00
|
|
|
f, exists := argvs[os.Args[0]]
|
2014-08-09 02:16:56 +08:00
|
|
|
if exists {
|
2014-08-13 02:43:12 +08:00
|
|
|
runFunc(f)
|
2014-08-09 02:16:56 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
app := cli.NewApp()
|
2014-08-09 02:16:56 +08:00
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
app.Name = "nsinit"
|
|
|
|
app.Version = "0.1"
|
|
|
|
app.Author = "libcontainer maintainers"
|
2014-08-05 06:05:50 +08:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{Name: "nspid"},
|
2014-08-07 09:44:41 +08:00
|
|
|
cli.StringFlag{Name: "console"},
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
app.Before = preload
|
2014-08-07 09:44:41 +08:00
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
app.Commands = []cli.Command{
|
|
|
|
execCommand,
|
|
|
|
initCommand,
|
|
|
|
statsCommand,
|
2014-06-27 07:24:16 +08:00
|
|
|
configCommand,
|
2014-06-27 07:44:43 +08:00
|
|
|
pauseCommand,
|
|
|
|
unpauseCommand,
|
2014-08-13 02:43:12 +08:00
|
|
|
execFuncCommand,
|
2014-05-30 08:23:18 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 07:46:30 +08:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
log.Fatal(err)
|
2014-05-30 08:23:18 +08:00
|
|
|
}
|
|
|
|
}
|