Esempio n. 1
0
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."
}
Esempio n. 2
0
File: config.go Progetto: 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()

	// Clear the components map.
	cache.Components.Clear()

	for _, comp := range c.Components {
		component := &model.Component{
			Name:    comp.Name,
			Version: comp.Version,
			Md5:     comp.Md5,
			Cmd:     comp.Cmd,
		}
		cache.Components.Put(comp.Name, component)
	}

	appConfig = &c
	log.Info("Load config file: ", cfgFile, " successfully.")
	return nil
}
Esempio n. 3
0
File: config.go Progetto: 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
	}

	configLock.Lock()
	defer configLock.Unlock()

	appConfig = &c
	log.Info("Load config file: ", cfgFile, " successfully.")
	return nil
}
Esempio n. 4
0
File: engine.go Progetto: 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
}
Esempio n. 5
0
File: consul.go Progetto: ahjdzx/dis
func CreateConsulAPIClient(config_json_file string, port int) (err error) {
	log.Info("config_json_file = ", config_json_file)

	jsonFile, err := utils.ReadFile(config_json_file)
	if err != nil {
		return err
	}
	agentConfig, err := agent.DecodeConfig(bytes.NewBuffer(jsonFile))
	if err != nil {
		return err
	}

	var address string
	if agentConfig.ClientAddr != "" {
		address = agentConfig.ClientAddr
	} else if agentConfig.Addresses.HTTP != "" {
		address = agentConfig.Addresses.HTTP
	} else if agentConfig.Addresses.HTTPS != "" {
		address = agentConfig.Addresses.HTTPS
	} else {
		return errors.New("Can not get consul ip address.")
	}

	if port == 0 {
		port = DefaultConsulPort
	}

	consulAddr := fmt.Sprintf("%s:%d", address, port)
	consulConfig := &consulapi.Config{
		Address:    consulAddr,
		Scheme:     "http",
		HttpClient: http.DefaultClient,
	}
	ConsulAPIClient, err = consulapi.NewClient(consulConfig)
	return
}