func postVmCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } job := eng.Job("vmCreate", r.Form.Get("cpu"), r.Form.Get("mem")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
// Install installs daemon capabilities to eng. func (daemon *Daemon) Install(eng *engine.Engine) error { // Now, we just install a command 'info' to set/get the information of the docker and Hyper daemon for name, method := range map[string]engine.Handler{ "info": daemon.CmdInfo, "version": daemon.CmdVersion, "create": daemon.CmdCreate, "pull": daemon.CmdPull, "podCreate": daemon.CmdPodCreate, "podStart": daemon.CmdPodStart, "podInfo": daemon.CmdPodInfo, "podRm": daemon.CmdPodRm, "podRun": daemon.CmdPodRun, "podStop": daemon.CmdPodStop, "vmCreate": daemon.CmdVmCreate, "vmKill": daemon.CmdVmKill, "list": daemon.CmdList, "exec": daemon.CmdExec, "attach": daemon.CmdAttach, "tty": daemon.CmdTty, "serveapi": apiserver.ServeApi, "acceptconnections": apiserver.AcceptConnections, } { glog.V(3).Infof("Engine Register: name= %s\n", name) if err := eng.Register(name, method); err != nil { return err } } return nil }
func postPodRemove(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } glog.V(1).Infof("Pod(%s) is process to be removed", r.Form.Get("podId")) job := eng.Job("podRm", r.Form.Get("podId")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
func postContainerCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } glog.V(1).Infof("Image name is %s\n", r.Form.Get("imageName")) job := eng.Job("create", r.Form.Get("imageName")) stdoutBuf := bytes.NewBuffer(nil) stderrBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) job.Stderr.Add(stderrBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ContainerID", dat["ContainerID"].(string)) return writeJSONEnv(w, http.StatusCreated, env) }
func postAttach(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } var ( job = eng.Job("attach", r.Form.Get("type"), r.Form.Get("value"), r.Form.Get("tag")) errOut io.Writer = os.Stderr errStream io.Writer ) // Setting up the streaming http interface. inStream, outStream, err := hijackServer(w) if err != nil { return err } defer closeStreams(inStream, outStream) fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n") errStream = outStream job.Stdin.Add(inStream) job.Stdout.Add(outStream) job.Stderr.Set(errStream) // Now run the user process in container. job.SetCloseIO(false) if err := job.Run(); err != nil { fmt.Fprintf(errOut, "Error starting attach to POD %s: %s\n", r.Form.Get("podname"), err.Error()) return err } w.WriteHeader(http.StatusNoContent) return nil }
func getPodInfo(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } job := eng.Job("podInfo", r.Form.Get("podName")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("hostname", dat["hostname"].(string)) return writeJSONEnv(w, http.StatusCreated, env) }
func postTtyResize(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } job := eng.Job("tty", r.Form.Get("id"), r.Form.Get("tag"), r.Form.Get("h"), r.Form.Get("w")) if err := job.Run(); err != nil { return err } return nil }
func postImageCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } glog.V(1).Infof("Image name is %s\n", r.Form.Get("imageName")) job := eng.Job("pull", r.Form.Get("imageName")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } return nil }
func getList(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } glog.V(1).Infof("List type is %s\n", r.Form.Get("item")) job := eng.Job("list", r.Form.Get("item")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } str := engine.Tail(stdoutBuf, 1) type listResponse struct { Item string `json:"item"` PodData []string `json:"podData"` VmData []string `json:"vmData"` CData []string `json:"cData"` } var res listResponse if err := json.Unmarshal([]byte(str), &res); err != nil { return err } var env engine.Env env.Set("Item", res.Item) if res.Item == "pod" { env.SetList("podData", res.PodData) } if res.Item == "vm" { env.SetList("vmData", res.VmData) } if res.Item == "container" { env.SetList("cData", res.CData) } return writeJSONEnv(w, http.StatusOK, env) }
func NewDaemonFromDirectory(eng *engine.Engine) (*Daemon, error) { // register portallocator release on shutdown eng.OnShutdown(func() { if err := portallocator.ReleaseAll(); err != nil { glog.Errorf("portallocator.ReleaseAll(): %s", err.Error()) } }) // Check that the system is supported and we have sufficient privileges if runtime.GOOS != "linux" { return nil, fmt.Errorf("The Docker daemon is only supported on linux") } if os.Geteuid() != 0 { return nil, fmt.Errorf("The Docker daemon needs to be run as root") } if err := checkKernel(); err != nil { return nil, err } cfg, err := goconfig.LoadConfigFile(eng.Config) if err != nil { glog.Errorf("Read config file (%s) failed, %s", eng.Config, err.Error()) return nil, err } kernel, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Kernel") initrd, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Initrd") glog.V(0).Infof("The config: kernel=%s, initrd=%s", kernel, initrd) biface, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Bridge") bridgeip, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "BridgeIP") glog.V(0).Infof("The config: bridge=%s, ip=%s", biface, bridgeip) bios, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Bios") cbfs, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Cbfs") glog.V(0).Infof("The config: bios=%s, cbfs=%s", bios, cbfs) host, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Host") var tempdir = "/var/run/hyper/" os.Setenv("TMPDIR", tempdir) if err := os.MkdirAll(tempdir, 0755); err != nil && !os.IsExist(err) { return nil, err } var realRoot = "/var/lib/hyper/" // Create the root directory if it doesn't exists if err := os.MkdirAll(realRoot, 0755); err != nil && !os.IsExist(err) { return nil, err } if err := network.InitNetwork(biface, bridgeip); err != nil { glog.Errorf("InitNetwork failed, %s\n", err.Error()) return nil, err } var ( proto = "unix" addr = "/var/run/docker.sock" db_file = fmt.Sprintf("%s/hyper.db", realRoot) ) db, err := leveldb.OpenFile(db_file, nil) if err != nil { glog.Errorf("open leveldb file failed, %s\n", err.Error()) return nil, err } dockerCli := docker.NewDockerCli("", proto, addr, nil) qemuchan := map[string]interface{}{} qemuclient := map[string]interface{}{} subQemuClient := map[string]interface{}{} cList := []*Container{} pList := map[string]*Pod{} vList := map[string]*Vm{} daemon := &Daemon{ ID: fmt.Sprintf("%d", os.Getpid()), db: db, eng: eng, kernel: kernel, initrd: initrd, bios: bios, cbfs: cbfs, dockerCli: dockerCli, containerList: cList, podList: pList, vmList: vList, qemuChan: qemuchan, qemuClientChan: qemuclient, subQemuClientChan: subQemuClient, Host: host, } stor := &Storage{} // Get the docker daemon info body, _, err := dockerCli.SendCmdInfo() if err != nil { return nil, err } outInfo := engine.NewOutput() remoteInfo, err := outInfo.AddEnv() if err != nil { return nil, err } if _, err := outInfo.Write(body); err != nil { return nil, fmt.Errorf("Error while reading remote info!\n") } outInfo.Close() storageDriver := remoteInfo.Get("Driver") stor.StorageType = storageDriver if storageDriver == "devicemapper" { if remoteInfo.Exists("DriverStatus") { var driverStatus [][2]string if err := remoteInfo.GetJson("DriverStatus", &driverStatus); err != nil { return nil, err } for _, pair := range driverStatus { if pair[0] == "Pool Name" { stor.PoolName = pair[1] } if pair[0] == "Backing Filesystem" { if strings.Contains(pair[1], "ext") { stor.Fstype = "ext4" } else if strings.Contains(pair[1], "xfs") { stor.Fstype = "xfs" } else { stor.Fstype = "dir" } break } } } } else { if remoteInfo.Exists("DriverStatus") { var driverStatus [][2]string if err := remoteInfo.GetJson("DriverStatus", &driverStatus); err != nil { return nil, err } for _, pair := range driverStatus { if pair[0] == "Root Dir" { stor.RootPath = pair[1] } if pair[0] == "Backing Filesystem" { stor.Fstype = "dir" break } } } } daemon.Storage = stor dmPool := dm.DeviceMapper{ Datafile: "/var/lib/hyper/data", Metadatafile: "/var/lib/hyper/metadata", DataLoopFile: "/dev/loop6", MetadataLoopFile: "/dev/loop7", PoolName: "hyper-volume-pool", Size: 20971520 * 512, } if storageDriver == "devicemapper" { daemon.Storage.DmPoolData = &dmPool // Prepare the DeviceMapper storage if err := dm.CreatePool(&dmPool); err != nil { return nil, err } } else { daemon.CleanVolume(0) } eng.OnShutdown(func() { if err := daemon.shutdown(); err != nil { glog.Errorf("Error during daemon.shutdown(): %v", err) } }) return daemon, nil }
func getInfo(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { w.Header().Set("Content-Type", "application/json") eng.ServeHTTP(w, r) return nil }