func (r *registry_response) Save() error {
	dump, err := json.Marshal(r)
	if err != nil {
		return logging.SError(err)
	}
	err = ioutil.WriteFile(config.REGISTRY_FILEPATH, dump, 0644)
	if err != nil {
		return logging.SError(err)
	}

	return nil
}
func new_reg_response_from_json(dump []byte) (*registry_response, error) {
	hr, err := new_hashed_reg_response_from_json(dump)
	if err != nil {
		return nil, logging.SError(err)
	}
	return new_reg_resp_from_hashed(*hr)
}
Exemple #3
0
func Start() error {

	// start api serve once.
	if !api_srv_running && config.CoreConf.Enable_http_api {
		go serve_api()
	}

	if Running() == true {
		return logging.SError("one core is already running. stop it first!")
	}
	logging.Info("Starting the core.")

	switch config.ValidStrategy(config.CoreConf.Config_strategy) {
	case config.ETCD:
		logging.Info("use etcd config strategy")
		if len(config.CoreConf.Etcd_machines) <= 0 {
			return logging.SCritical("EtcdMachines is empty!!")
		}
		if config.CoreConf.Etcd_path == "" {
			return logging.SCritical("EtcdPath is empty!!")
		}
		go new_core_from_etcd(config.CoreConf.Etcd_machines, config.CoreConf.Etcd_path, done)
	case config.REGISTRY:
		logging.Info("use registry config strategy")
		if len(config.CoreConf.Registry_urls) <= 0 {
			return logging.SCritical("RegistryURLS is empty!!")
		}
		go new_core_from_registry(done)
	default:
		logging.Info("[default] use file config strategy")
		_, err := new_core_from_file()
		if err != nil {
			return logging.SError(err)
		}
	}
	return nil
}
func new_hashed_reg_resp(r *registry_response) (*hashed_registry_response, error) {
	dump, err := json.Marshal(r)
	if err != nil {
		return nil, logging.SError(err)
	}

	h := md5.New()
	h.Write(dump)
	hash := hex.EncodeToString(h.Sum(nil))

	return &hashed_registry_response{
		Hash:        hash,
		ResponseStr: string(dump),
	}, nil
}
func do_registry(reg_url string) (*registry_response, error) {
	logging.Debug("do_registry started")
	req, err := new_reg_request()
	if err != nil {
		return nil, logging.SError(err)
	}
	hReq, err := new_hashed_reg_request(req)
	if err != nil {
		return nil, logging.SError(err)
	}

	hResp, err := goreq.Request{
		Method:      "POST",
		Uri:         reg_url,
		Body:        hReq,
		Accept:      "application/json",
		ContentType: "application/json",
		UserAgent:   "hickwall",
		Timeout:     10 * time.Second,
	}.Do()

	if serr, ok := err.(*goreq.Error); ok {
		if serr.Timeout() {
			return nil, logging.SError(err)
		}
		return nil, logging.SErrorf("registry failed: %d", hResp.StatusCode)
	}
	defer hResp.Body.Close()

	if hResp.StatusCode != 200 {
		return nil, logging.SErrorf("status code != 200: %d", hResp.StatusCode)
	}

	body, err := ioutil.ReadAll(hResp.Body)
	if err != nil {
		return nil, logging.SErrorf("failed to read body %v", err)
	}

	resp, err := new_reg_response_from_json(body)
	if err != nil {
		return nil, logging.SError(err)
	}

	if resp.ErrorCode != 0 {
		if resp.ErrorCode == 1001 {
			logging.Warn("this agent already registried.")
		} else {
			return nil, logging.SErrorf("registry server give this error: %d, msg: %s", resp.ErrorCode, resp.ErrorMsg)
		}

	}

	if len(resp.EtcdMachines) <= 0 {
		return nil, logging.SError("EtcdMachines is empty")
	}

	for _, m := range resp.EtcdMachines {
		machine, err := url.Parse(m)
		if err != nil {
			return nil, logging.SErrorf("invalid etcd machine url: %s", err)
		}
		if machine.Scheme != "http" {
			return nil, logging.SErrorf("etcd machine url only support http : %s", m)
		}
	}

	if resp.EtcdConfigPath == "" {
		return nil, logging.SError("config path is empty")
	}

	if resp.RequestHash != hReq.Hash {
		return nil, logging.SErrorf("request hash and response hash mismatch: %s != (response)%s", hReq.Hash, resp.RequestHash)
	}
	resp.Request = req

	logging.Debug("do_registry finished")
	resp.Save()
	return resp, nil
}