func InsureDesiredAgentDirExists(da *model.DesiredAgent) error { err := file.InsureDir(da.AgentDir) if err != nil { log.Println("insure dir", da.AgentDir, "fail", err) return err } err = file.InsureDir(da.AgentVersionDir) if err != nil { log.Println("insure dir", da.AgentVersionDir, "fail", err) } return err }
func ioWorker() { var err error for { select { case task := <-io_task_chan: if task.method == IO_TASK_M_READ { if args, ok := task.args.(*readfile_t); ok { args.data, err = ioutil.ReadFile(args.filename) task.done <- err } } else if task.method == IO_TASK_M_WRITE { //filename must not exist if args, ok := task.args.(*g.File); ok { baseDir := file.Dir(args.Filename) if err = file.InsureDir(baseDir); err != nil { task.done <- err } task.done <- writeFile(args.Filename, args.Body, 0644) } } else if task.method == IO_TASK_M_FLUSH { if args, ok := task.args.(*flushfile_t); ok { task.done <- flushrrd(args.filename, args.items) } } else if task.method == IO_TASK_M_FETCH { if args, ok := task.args.(*fetch_t); ok { args.data, err = fetch(args.filename, args.cf, args.start, args.end, args.step) task.done <- err } } } } }
// flush to disk from memory // 最新的数据在列表的最后面 // TODO fix me, filename fmt from item[0], it's hard to keep consistent func Flush(filename string, items []*cmodel.GraphItem) error { if items == nil || len(items) == 0 { return errors.New("empty items") } lock := L.GetLock(filename) lock.Lock() defer lock.Unlock() if !g.IsRrdFileExist(filename) { baseDir := file.Dir(filename) err := file.InsureDir(baseDir) if err != nil { return err } err = create(filename, items[0]) if err != nil { return err } } return update(filename, items) }
func configPluginRoutes() { http.HandleFunc("/plugin/update", func(w http.ResponseWriter, r *http.Request) { if !g.Config().Plugin.Enabled { w.Write([]byte("plugin not enabled")) return } dir := g.Config().Plugin.Dir parentDir := file.Dir(dir) file.InsureDir(parentDir) if file.IsExist(dir) { // git pull cmd := exec.Command("git", "pull") cmd.Dir = dir err := cmd.Run() if err != nil { w.Write([]byte(fmt.Sprintf("git pull in dir:%s fail. error: %s", dir, err))) return } } else { // git clone cmd := exec.Command("git", "clone", g.Config().Plugin.Git, file.Basename(dir)) cmd.Dir = parentDir err := cmd.Run() if err != nil { w.Write([]byte(fmt.Sprintf("git clone in dir:%s fail. error: %s", parentDir, err))) return } } w.Write([]byte("success")) }) http.HandleFunc("/plugin/reset", func(w http.ResponseWriter, r *http.Request) { if !g.Config().Plugin.Enabled { w.Write([]byte("plugin not enabled")) return } dir := g.Config().Plugin.Dir if file.IsExist(dir) { cmd := exec.Command("git", "reset", "--hard") cmd.Dir = dir err := cmd.Run() if err != nil { w.Write([]byte(fmt.Sprintf("git reset --hard in dir:%s fail. error: %s", dir, err))) return } } w.Write([]byte("success")) }) http.HandleFunc("/plugins", func(w http.ResponseWriter, r *http.Request) { //TODO: not thread safe RenderDataJson(w, plugins.Plugins) }) }
// flush to disk from memory // 最新的数据在列表的最后面 // TODO fix me, filename fmt from item[0], it's hard to keep consistent func flushrrd(filename string, items []*cmodel.GraphItem) error { if items == nil || len(items) == 0 { return errors.New("empty items") } if !g.IsRrdFileExist(filename) { baseDir := file.Dir(filename) err := file.InsureDir(baseDir) if err != nil { return err } err = create(filename, items[0]) if err != nil { return err } } return update(filename, items) }
func (this *MainController) Build() { o := &models.Build{} o.App = this.GetString("app") o.Version = this.GetString("version") o.Resume = this.GetString("resume") o.Base = this.GetString("base") o.Tarball = this.GetString("tarball") defer func() { this.Data["Mappings"] = g.TplMapping this.Data["O"] = o this.Layout = "layout.html" this.TplNames = "index.html" }() if o.App == "" { this.Data["Msg"] = "app名称不能为空" return } // app should be a-zA-Z0-9_- var appPattern = regexp.MustCompile(`^[a-zA-Z_]+[a-zA-Z0-9\-\_]*$`) if !appPattern.MatchString(o.App) { this.Data["Msg"] = "app名称应该符合正则:/^[a-zA-Z_]+[a-zA-Z0-9\\-\\_]*$/" return } if o.Version == "" { this.Data["Msg"] = "版本不能为空" return } // version should be digit and . var versionPattern = regexp.MustCompile(`^[0-9]+[0-9\.]*$`) if !versionPattern.MatchString(o.Version) { this.Data["Msg"] = "version应该符合正则:/^[0-9]+[0-9\\.]*$/" return } if o.Base == "" { this.Data["Msg"] = "Base Image不能为空" return } workDir := fmt.Sprintf("%s/%s", g.TmpDir, str_.RandSeq(6)) err := filetool.InsureDir(workDir) if err != nil { this.Data["Msg"] = fmt.Sprintf("create temp dir fail: %v", err) return } var fileName, filePath string if o.Tarball == "" { _, header, err := this.GetFile("file") if err != nil { this.Data["Msg"] = err.Error() return } // handle upload file fileName = header.Filename filePath = fmt.Sprintf("%s/%s", workDir, fileName) err = this.SaveToFile("file", filePath) if err != nil { this.Data["Msg"] = fmt.Sprintf("save file fail: %v", err) return } } else { if !strings.HasPrefix(o.Tarball, "http://") { this.Data["Msg"] = "tarball地址应该是一个http地址" return } if !(strings.HasSuffix(o.Tarball, ".tar.gz") || strings.HasSuffix(o.Tarball, ".war")) { this.Data["Msg"] = "tarball地址应该以.tar.gz或.war结尾" return } idx := strings.LastIndex(o.Tarball, "/") fileName = o.Tarball[idx+1:] filePath = fmt.Sprintf("%s/%s", workDir, fileName) err = filetool.Download(filePath, o.Tarball) if err != nil { this.Data["Msg"] = fmt.Sprintf("download tarball fail: %v", err) return } } o.UserId = this.CurrentUser.Id o.UserName = this.CurrentUser.Name o.Status = "saved meta in db" _, err = orm.NewOrm().Insert(o) if err != nil { this.Data["Msg"] = fmt.Sprintf("save meta to db fail: %v", err) return } err = o.GenDockerfile(workDir, fileName) if err != nil { this.Data["Msg"] = fmt.Sprintf("generate Dockerfile fail: %v", err) return } go o.DockerBuild(workDir) this.Redirect(fmt.Sprintf("/progress/%d", o.Id), 302) }
func ParseConfig() { runMode := beego.AppConfig.String("runmode") if runMode == "dev" { Debug = true } else { Debug = false } TmpDir = beego.AppConfig.String("tmpdir") if TmpDir == "" { log.Fatalln("configuration tmpdir is blank") } err := filetool.InsureDir(TmpDir) if err != nil { log.Fatalf("create dir: %s fail: %v", TmpDir, err) } TmpDir, err = filetool.RealPath(TmpDir) if err != nil { log.Fatalf("get real path of %s fail: %v", TmpDir, err) } LogDir = beego.AppConfig.String("logdir") if LogDir == "" { log.Fatalln("configuration logdir is blank") } err = filetool.InsureDir(LogDir) if err != nil { log.Fatalf("create dir: %s fail: %v", LogDir, err) } LogDir, err = filetool.RealPath(LogDir) if err != nil { log.Fatalf("get real path of %s fail: %v", LogDir, err) } Token = beego.AppConfig.String("token") UicInternal = beego.AppConfig.String("uicinternal") if UicInternal == "" { log.Fatalln("configuration uicinternal is blank") } UicExternal = beego.AppConfig.String("uicexternal") if UicExternal == "" { log.Fatalln("configuration uicexternal is blank") } BuildScript = beego.AppConfig.String("buildscript") if BuildScript == "" { log.Fatalln("configuration buildscript is blank") } Registry = beego.AppConfig.String("registry") if Registry == "" { log.Fatalln("configuration registry is blank") } _buildTimeout, err := beego.AppConfig.Int64("buildtimeout") if err != nil { log.Fatalf("parse configuration buildtimeout fail: %v", err) } BuildTimeout = time.Duration(_buildTimeout) * time.Minute // tpl mapping tpl_mapping := beego.AppConfig.String("tplmapping") tpl_mapping = strings.TrimSpace(tpl_mapping) mappings := strings.Split(tpl_mapping, ",") for i := 0; i < len(mappings); i++ { _mappings := strings.TrimSpace(mappings[i]) kv := strings.Split(_mappings, "=>") if len(kv) != 2 { log.Fatalf("split %s fail", _mappings) } TplMapping[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1]) } // cache Cache, err = cache.NewCache("memory", `{"interval":60}`) if err != nil { log.Fatalln("start cache fail :-(") } // db dbuser := beego.AppConfig.String("dbuser") dbpass := beego.AppConfig.String("dbpass") dbhost := beego.AppConfig.String("dbhost") dbport := beego.AppConfig.String("dbport") dbname := beego.AppConfig.String("dbname") dblink := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbuser, dbpass, dbhost, dbport, dbname) // dblink = "root:1234@/uic?charset=utf8&loc=Asia%2FChongqing" orm.RegisterDriver("mysql", orm.DR_MySQL) orm.RegisterDataBase("default", "mysql", dblink+"&loc=Asia%2FChongqing", 30, 200) // orm.DefaultTimeLoc = time.UTC if Debug { orm.Debug = true } }