Ejemplo n.º 1
0
Archivo: engine.go Proyecto: ahjdzx/dis
func LocalComponents() (remoteComponents map[string]*model.Component) {
	subDirs, err := utils.SubDirs(stackDir)
	if err != nil {
		log.Errorln(err)
		return
	}

	remoteComponents = make(map[string]*model.Component)
	for _, subDir := range subDirs {
		log.Infoln("subDir: ", subDir)

		// If .version file not found in current directory, so we considered that not a remote component existed.
		versionFile := path.Join(stackDir, subDir, ".version")
		if !utils.IsExist(versionFile) {
			log.Errorf("Can't found .version file: %s", versionFile)
			continue
		}

		versionBytes, err := utils.ReadFile(versionFile)
		if err != nil {
			log.Errorf("Failed to read %s/.version : %v", subDir, err)
			continue
		}

		version := strings.TrimSpace(string(versionBytes))
		cmd := exec.Command("./deploy", "status")
		cmd.Dir = path.Join(stackDir, subDir, version)
		statusOutput, err := cmd.CombinedOutput()
		if err != nil {
			log.Errorf("Failed to run command: %s ,error: %v", "./deploy status", err)
			continue
		}
		status := strings.TrimSpace(string(statusOutput))
		log.Infof("local componet: %s, stauts: %s", subDir, status)
		if strings.Contains(status, "stoped") {
			os.Remove(versionFile)
			continue
		}

		remoteComp := &model.Component{
			Name:      subDir,
			Version:   version,
			Status:    status,
			Timestamp: time.Now(),
		}
		remoteComp.InitAttrs(stackDir)
		remoteComponents[remoteComp.Name] = remoteComp
	}
	return
}
Ejemplo n.º 2
0
Archivo: config.go Proyecto: ahjdzx/dis
func LoadConfig(cfgFile string) error {
	if cfgFile == "" {
		return errors.New("Use -c to specify configuration file.")
	}
	ConfigFile = cfgFile
	var c Config
	_, err := toml.DecodeFile(cfgFile, &c)
	if err != nil {
		return err
	}

	lock.Lock()
	defer lock.Unlock()

	appConfig = &c
	log.Infof("Load config file: %s successfully.", cfgFile)
	return nil
}
Ejemplo n.º 3
0
Archivo: api.go Proyecto: ahjdzx/dis
// LogMiddleware is generic middleware that will log requests to Logger.
func LogMiddleware(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
	startTime := time.Now()

	next(rw, req)

	duration := time.Since(startTime).Nanoseconds()
	var durationUnits string
	switch {
	case duration > 2000000:
		durationUnits = "ms"
		duration /= 1000000
	case duration > 1000:
		durationUnits = "μs"
		duration /= 1000
	default:
		durationUnits = "ns"
	}

	log.Infof("[%d %s] %d '%s'", duration, durationUnits, rw.StatusCode(), req.URL.Path)
}
Ejemplo n.º 4
0
Archivo: engine.go Proyecto: ahjdzx/dis
func downloadFromURL(filePath, url string) (err error) {
	log.Info("Downloading ", url, " to ", filePath)

	// TODO: check file existence first with io.IsExist
	output, err := os.Create(filePath)
	if err != nil {
		return
	}
	defer output.Close()

	response, err := http.Get(url)
	if err != nil {
		return
	}
	defer response.Body.Close()

	n, err := io.Copy(output, response.Body)
	if err != nil {
		return
	}

	log.Infof("%d bytes downloaded.", n)
	return nil
}
Ejemplo n.º 5
0
Archivo: main.go Proyecto: ahjdzx/dis
func main() {
	flag.Parse()

	if *versionFlag {
		fmt.Println(VERSION)
		os.Exit(0)
	}

	if *logstashFlag {
		log.ChangeToLogstashFormater(APP_NAME)
	}

	log.SetLogFile(LOG_FILE)

	err := config.LoadConfig(*cfgFileFlag)
	if err != nil {
		log.Fatalln(err)
	}

	pid := os.Getpid() // This process's pid.
	// Save the pid into the pid file.
	err = utils.WriteFile(PID_FILE, []byte(strconv.Itoa(pid)), 0666)
	if err != nil {
		log.Fatalln(err)
	}
	defer os.Remove(PID_FILE)

	clientSet, err := initClientSet()
	if err != nil {
		log.Fatalln(err)
	}

	go engine.Boot()

	engine.Watcher, err = engine.NewWatch(clientSet)
	if err != nil {
		log.Fatalln(err)
	}
	go engine.Run()

	if engine.CurrentDir == "" {
		log.Fatalln("current directory is blank.")
	}

	err = utils.WriteFile(path.Join(path.Dir(engine.CurrentDir), ".version"), []byte(VERSION), 0644)
	if err != nil {
		log.Fatalln(err)
	}

	err = common.ConsulRegister(APP_NAME, path.Join(engine.CurrentDir, "deploy"))
	if err != nil {
		log.Fatalln(err)
	}

	// go api.Start()

	// catch some signal
	sigCh := make(chan os.Signal)
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, os.Kill)
	// Block until a signal is received.
	s := <-sigCh
	log.Infof("Caught Signal: %s, shutting down...", s)
}