func Start() { if !config.AppConfig().Http.Enabled { return } addr := config.AppConfig().Http.Listen if addr == "" { return } server := &http.Server{ Addr: addr, Handler: initRouter(), } log.Infoln("http listening on ", addr) log.Fatalln(server.ListenAndServe()) }
func (c *Context) Destory(rw web.ResponseWriter, req *web.Request) { resp := &Response{} defer c.WriteResponse(resp) body, err := ioutil.ReadAll(req.Body) if err != nil { rw.WriteHeader(http.StatusBadRequest) resp.Message = err.Error() return } sshConfig := &sshlib.SSHConfig{} err = json.Unmarshal(body, sshConfig) if err != nil { rw.WriteHeader(http.StatusBadRequest) resp.Message = err.Error() return } log.Info("Destory...... ", sshConfig.Address) client, err := sshlib.NewSSHClient(sshConfig) if err != nil { rw.WriteHeader(http.StatusBadRequest) resp.Message = err.Error() return } defer client.Close() for _, comp := range config.AppConfig().Components { comp.InitAttrs(comp.TarballDir) out, err := sshlib.RunCommand(client, comp.DeployFilepath+" stop") if err != nil { log.Error(err) resp.FailedFiles = append(resp.FailedFiles, comp.TarballFilename) continue } log.Info("command stop output: ", out) out, err = sshlib.RunCommand(client, "rm -rf "+comp.Directory) if err != nil { log.Error(err) resp.FailedFiles = append(resp.FailedFiles, comp.TarballFilename) continue } log.Info("command 'rm -rf ' output: ", out) resp.SucceedFiles = append(resp.SucceedFiles, comp.TarballFilename) } resp.Message = "Clean up ok." }
func initRouter() *web.Router { router := web.New(Context{}) router.Middleware(LogMiddleware) router.Middleware(web.ShowErrorsMiddleware) router.Middleware(web.StaticMiddleware(config.AppConfig().LocalTarballDir)) // OR use 'router.Get("/", FileServer)' router.Middleware((*Context).OutputJson) // API router.Get("/v1/config/reload", (*Context).ConfigReload) router.Post("/v1/transfer", (*Context).Transfer) router.Get("/v1/status", (*Context).Status) router.Post("/v1/destory", (*Context).Destory) // Internal communicate. router.Post("/heartbeat", (*Context).Heartbeat) return router }
func (c *Context) ConfigReload(rw web.ResponseWriter, req *web.Request) { // if strings.HasPrefix(req.RemoteAddr, "127.0.0.1") { err := config.LoadConfig(config.ConfigFile) if err != nil { c.WriteResponse(map[string]string{"msg": err.Error()}) } else { // var buf bytes.Buffer // en := toml.NewEncoder(&buf) // err := en.Encode(config.AppConfig()) // if err != nil { // c.WriteResponse(map[string]string{"msg": err.Error()}) // } // fmt.Println(buf.String()) c.WriteResponse(config.AppConfig()) } // } else { // c.WriteResponse(map[string]string{"msg": "permission denied"}) // } }
func transfer(sshConfig *sshlib.SSHConfig) (succeedFiles, failedFiles []string, err error) { client, err := sshlib.NewSSHClient(sshConfig) if err != nil { return } defer client.Close() scp := sshlib.NewScp(client) localTarballDir := config.AppConfig().LocalTarballDir for _, comp := range config.AppConfig().Components { comp.InitAttrs(comp.TarballDir) _, err := sshlib.RunCommand(client, "ls "+comp.VersionDir) if err != nil { // fileDir not exists. // Create a new directory to save tarball file. err = sshlib.Mkdir(client, comp.VersionDir) if err != nil { log.Errorf("Mkdir %s error: %s", comp.VersionDir, err.Error()) failedFiles = append(failedFiles, comp.TarballFilename) continue } } localTarbalPath := path.Join(localTarballDir, comp.TarballFilename) remoteFilePath := comp.TarballFilepath _, err = sshlib.RunCommand(client, "ls "+remoteFilePath) if err != nil { // remoteFilePath not exists. err = scp.PushFile(localTarbalPath, remoteFilePath) if err != nil { log.Errorf("Push file: %s --> %s, error: %s", localTarbalPath, remoteFilePath, err.Error()) failedFiles = append(failedFiles, comp.TarballFilename) continue } log.Debug("push file ", localTarbalPath, " successfully.") succeedFiles = append(succeedFiles, comp.TarballFilename) // Unpack the tarball or zip file. _, err = sshlib.RunCommand(client, "tar -zxf "+remoteFilePath+" -C "+comp.VersionDir) if err != nil { log.Errorf("Unpack file %s error: %s", remoteFilePath, err.Error()) continue } // if component name is AGENT_NAME, then use ssh execute the cmd option. if comp.Name == model.AGENT_NAME { var output string switch comp.Cmd { case "start": startCmd := fmt.Sprintf("cd %s && ./deploy start", comp.VersionDir) output, err = sshlib.RunCommand(client, startCmd) if err != nil { log.Errorf("Command: %s, Error: %s", comp.Cmd, err.Error()) continue } if output != "" { log.Debugf("Command: %s, Output: %s", comp.Cmd, string(output)) } case "status": // TODO statusCmd := fmt.Sprintf("cd %s && ./deploy status", comp.VersionDir) output, err = sshlib.RunCommand(client, statusCmd) if err != nil { log.Errorf("Command: %s, Error: %s", comp.Cmd, err.Error()) continue } if output != "" { log.Debugf("Command: %s, Output: %s", comp.Cmd, string(output)) } } } } } return }