func postContainerCommit(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("container ID is %s", r.Form.Get("container")) job := eng.Job("commit", r.Form.Get("container"), r.Form.Get("repo"), r.Form.Get("author"), r.Form.Get("change"), r.Form.Get("message"), r.Form.Get("pause")) 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", 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 getContainerLogs(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } var args []string = []string{r.Form.Get("container"), r.Form.Get("tail"), r.Form.Get("since")} boolVals := []string{"follow", "timestamps", "stdout", "stderr"} for _, k := range boolVals { if v := r.Form.Get(k); v == "yes" { args = append(args, k) } } glog.V(1).Infof("Log for %s", r.Form.Get("container")) job := eng.Job("containerLogs", args...) w.Header().Set("Content-Type", "plain/text") outStream := ioutils.NewWriteFlusher(w) job.Stdout.Add(outStream) output := ioutils.NewWriteFlusher(w) if err := job.Run(); err != nil { output.Write([]byte(err.Error())) return err } return nil }
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) }
func postStop(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } glog.V(1).Infof("Stop the POD name is %s", r.Form.Get("podName")) job := eng.Job("podStop", r.Form.Get("podId"), r.Form.Get("stopVm")) 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 postPodLabels(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } podID := r.Form.Get("podId") override := r.Form.Get("override") labels := r.Form.Get("labels") job := eng.Job("podLabels", podID, override, labels) 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 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 getImages(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("images", r.Form.Get("all")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } str := engine.Tail(stdoutBuf, 1) type response struct { Images []string `json:"imagesList"` } var res response if err := json.Unmarshal([]byte(str), &res); err != nil { return err } var env engine.Env env.SetList("imagesList", res.Images) return writeJSONEnv(w, http.StatusOK, 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"), r.Form.Get("remove")) 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 postImagePush(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if vars == nil { return fmt.Errorf("Missing parameter") } metaHeaders := map[string][]string{} for k, v := range r.Header { if strings.HasPrefix(k, "X-Meta-") { metaHeaders[k] = v } } if err := parseForm(r); err != nil { return err } authConfig := &cliconfig.AuthConfig{} output := ioutils.NewWriteFlusher(w) authEncoded := r.Header.Get("X-Registry-Auth") if authEncoded != "" { // the new format is to handle the authConfig as a header authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded)) if err := json.NewDecoder(authJson).Decode(authConfig); err != nil { // to increase compatibility to existing api it is defaulting to be empty authConfig = &cliconfig.AuthConfig{} } } else { // the old format is supported for compatibility if there was no authConfig header if err := json.NewDecoder(r.Body).Decode(authConfig); err != nil { err = fmt.Errorf("Bad parameters and missing X-Registry-Auth: %v", err) sf := streamformatter.NewJSONStreamFormatter() output.Write(sf.FormatError(err)) return nil } } imagePushConfig := &types.ImagePushConfig{ MetaHeaders: metaHeaders, AuthConfig: authConfig, Tag: r.Form.Get("tag"), } w.Header().Set("Content-Type", "application/json") job := eng.Job("push", r.Form.Get("remote")) job.Stdout.Add(output) if err := job.SetenvJson("ImagePushConfig", imagePushConfig); err != nil { sf := streamformatter.NewJSONStreamFormatter() output.Write(sf.FormatError(err)) return nil } if err := job.Run(); err != nil { sf := streamformatter.NewJSONStreamFormatter() output.Write(sf.FormatError(err)) } return nil }
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 postPodUnpause(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } job := eng.Job("unpause", r.Form.Get("podId")) if err := job.Run(); err != nil { return err } w.WriteHeader(http.StatusNoContent) return nil }
// 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{ "auth": daemon.CmdAuth, "info": daemon.CmdInfo, "version": daemon.CmdVersion, "create": daemon.CmdCreate, "pull": daemon.CmdPull, "build": daemon.CmdBuild, "commit": daemon.CmdCommit, "rename": daemon.CmdRename, "push": daemon.CmdPush, "podCreate": daemon.CmdPodCreate, "podStart": daemon.CmdPodStart, "podInfo": daemon.CmdPodInfo, "podStats": daemon.CmdPodStats, "podLabels": daemon.CmdPodLabels, "containerInfo": daemon.CmdContainerInfo, "containerLogs": daemon.CmdLogs, "podRm": daemon.CmdPodRm, "podStop": daemon.CmdPodStop, "vmCreate": daemon.CmdVmCreate, "vmKill": daemon.CmdVmKill, "list": daemon.CmdList, "exec": daemon.CmdExec, "exitcode": daemon.CmdExitCode, "attach": daemon.CmdAttach, "tty": daemon.CmdTty, "serviceAdd": daemon.AddService, "serviceList": daemon.GetServices, "serviceUpdate": daemon.UpdateService, "serviceDelete": daemon.DeleteService, "serveapi": apiserver.ServeApi, "acceptconnections": apiserver.AcceptConnections, "images": daemon.CmdImages, "imagesremove": daemon.CmdImagesRemove, } { glog.V(3).Infof("Engine Register: name= %s", name) if err := eng.Register(name, method); 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 := parseForm(r); err != nil { return err } item := r.Form.Get("item") auxiliary := r.Form.Get("auxiliary") pod := r.Form.Get("pod") vm := r.Form.Get("vm") glog.V(1).Infof("List type is %s, specified pod: [%s], specified vm: [%s], list auxiliary pod: %s", item, pod, vm, auxiliary) job := eng.Job("list", item, pod, vm, auxiliary) 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 postServiceDelete(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("serviceDelete", r.Form.Get("podId"), r.Form.Get("services")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var env engine.Env env.Set("Result", "success") return writeJSONEnv(w, http.StatusOK, env) }
func postImageBuild(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 } w.Header().Set("Content-Type", "application/json") glog.V(1).Infof("Image name is %s", r.Form.Get("name")) job := eng.Job("build", r.Form.Get("name")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) job.Stdin.Add(r.Body) output := ioutils.NewWriteFlusher(w) if err := job.Run(); err != nil { sf := streamformatter.NewJSONStreamFormatter() output.Write(sf.FormatError(err)) } return nil }
func postAuth(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { job := eng.Job("auth") job.Stdin.Add(r.Body) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } return writeJSON(w, http.StatusOK, &dockertypes.AuthResponse{ Status: dat["status"].(string), }) }
func postPodCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } if r.Body != nil && (r.ContentLength > 0 || r.ContentLength == -1) { if err := checkForJson(r); err != nil { return err } } else { return fmt.Errorf("no provided podfile data") } podArgs, _ := ioutil.ReadAll(r.Body) autoRemove := r.Form.Get("remove") glog.V(1).Infof("Args string is %s, %s", string(podArgs), autoRemove) job := eng.Job("podCreate", string(podArgs), autoRemove) 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 postImageCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } authEncoded := r.Header.Get("X-Registry-Auth") authConfig := &cliconfig.AuthConfig{} if authEncoded != "" { authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded)) if err := json.NewDecoder(authJson).Decode(authConfig); err != nil { // for a pull it is not an error if no auth was given // to increase compatibility with the existing api it is defaulting to be empty authConfig = &cliconfig.AuthConfig{} } } w.Header().Set("Content-Type", "application/json") glog.V(1).Infof("Image name is %s", r.Form.Get("imageName")) job := eng.Job("pull", r.Form.Get("imageName")) output := ioutils.NewWriteFlusher(w) metaHeaders := map[string][]string{} for k, v := range r.Header { if strings.HasPrefix(k, "X-Meta-") { metaHeaders[k] = v } } job.Stdout.Add(output) imagePullConfig := &types.ImagePullConfig{ MetaHeaders: metaHeaders, AuthConfig: authConfig, } job.SetenvJson("ImagePullConfig", imagePullConfig) if err := job.Run(); err != nil { sf := streamformatter.NewJSONStreamFormatter() output.Write(sf.FormatError(err)) } return nil }
func postImagesRemove(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(%s) is process to be removed", r.Form.Get("imageId")) var ( force string = "yes" noprune string = "yes" ) if r.Form.Get("force") != "" { force = r.Form.Get("force") } if r.Form.Get("noprune") != "" { noprune = r.Form.Get("noprune") } job := eng.Job("imagesremove", r.Form.Get("imageId"), force, noprune) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } str := engine.Tail(stdoutBuf, 1) type response struct { Images []string `json:"imagesList"` } var res response if err := json.Unmarshal([]byte(str), &res); err != nil { return err } var env engine.Env env.SetList("imagesList", res.Images) return writeJSONEnv(w, http.StatusOK, env) }
func getServices(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("serviceList", r.Form.Get("podId")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } return writeJSON(w, http.StatusCreated, dat["data"]) }
func getExitCode(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } job := eng.Job("exitcode", r.Form.Get("container"), r.Form.Get("tag")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } return writeJSON(w, http.StatusOK, dat["ExitCode"]) }
func postPodStart(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 } ttyTag := r.Form.Get("tag") job := eng.Job("podStart", r.Form.Get("podId"), r.Form.Get("vmId"), ttyTag) if ttyTag == "" { 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) } else { var ( 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 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()) } }) if os.Geteuid() != 0 { return nil, fmt.Errorf("The Hyper 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) vboxImage, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Vbox") glog.V(0).Infof("The config: vbox image=%s", vboxImage) 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 = path.Join(utils.HYPER_ROOT, "run") os.Setenv("TMPDIR", tempdir) if err := os.MkdirAll(tempdir, 0755); err != nil && !os.IsExist(err) { return nil, err } var realRoot = path.Join(utils.HYPER_ROOT, "lib") // Create the root directory if it doesn't exists if err := os.MkdirAll(realRoot, 0755); err != nil && !os.IsExist(err) { return nil, err } var ( 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", err.Error()) return nil, err } dockerCli, err1 := NewDocker() if err1 != nil { glog.Errorf(err1.Error()) return nil, err1 } vList := map[string]*hypervisor.Vm{} daemon := &Daemon{ ID: fmt.Sprintf("%d", os.Getpid()), db: db, eng: eng, Kernel: kernel, Initrd: initrd, Bios: bios, Cbfs: cbfs, VboxImage: vboxImage, DockerCli: dockerCli, PodList: NewPodList(), VmList: vList, Host: host, BridgeIP: bridgeip, BridgeIface: biface, } daemon.vmCache.daemon = daemon // Get the docker daemon info sysinfo, err := dockerCli.SendCmdInfo() if err != nil { return nil, err } stor, err := StorageFactory(sysinfo) if err != nil { return nil, err } daemon.Storage = stor daemon.Storage.Init() eng.OnShutdown(func() { if err := daemon.shutdown(); err != nil { glog.Errorf("Error during daemon.shutdown(): %v", err) } }) return daemon, nil }
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()) } }) if os.Geteuid() != 0 { return nil, fmt.Errorf("The Hyper 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) vboxImage, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Vbox") glog.V(0).Infof("The config: vbox image=%s", vboxImage) 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 = path.Join(utils.HYPER_ROOT, "run") os.Setenv("TMPDIR", tempdir) if err := os.MkdirAll(tempdir, 0755); err != nil && !os.IsExist(err) { return nil, err } var realRoot = path.Join(utils.HYPER_ROOT, "lib") // Create the root directory if it doesn't exists if err := os.MkdirAll(realRoot, 0755); err != nil && !os.IsExist(err) { return nil, err } var ( 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, err1 := NewDocker() if err1 != nil { glog.Errorf(err1.Error()) return nil, err1 } pList := map[string]*hypervisor.Pod{} vList := map[string]*hypervisor.Vm{} daemon := &Daemon{ ID: fmt.Sprintf("%d", os.Getpid()), db: db, eng: eng, Kernel: kernel, Initrd: initrd, Bios: bios, Cbfs: cbfs, VboxImage: vboxImage, DockerCli: dockerCli, PodList: pList, PodsMutex: new(sync.RWMutex), VmList: vList, Host: host, BridgeIP: bridgeip, BridgeIface: biface, } stor := &Storage{} // Get the docker daemon info sysinfo, err := dockerCli.SendCmdInfo() if err != nil { return nil, err } storageDriver := sysinfo.Driver stor.StorageType = storageDriver if storageDriver == "devicemapper" { for _, pair := range sysinfo.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 storageDriver == "aufs" { for _, pair := range sysinfo.DriverStatus { if pair[0] == "Root Dir" { stor.RootPath = pair[1] } if pair[0] == "Backing Filesystem" { stor.Fstype = "dir" break } } } else if storageDriver == "overlay" { for _, pair := range sysinfo.DriverStatus { if pair[0] == "Backing Filesystem" { stor.Fstype = "dir" break } } stor.RootPath = path.Join(utils.HYPER_ROOT, "overlay") } else if storageDriver == "vbox" { stor.Fstype = "ext4" stor.RootPath = path.Join(utils.HYPER_ROOT, "vbox") } else { return nil, fmt.Errorf("hyperd can not support docker's backing storage: %s", storageDriver) } daemon.Storage = stor dmPool := dm.DeviceMapper{ Datafile: path.Join(utils.HYPER_ROOT, "lib") + "/data", Metadatafile: path.Join(utils.HYPER_ROOT, "lib") + "/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 }