예제 #1
0
파일: push.go 프로젝트: nyodas/cnt
func (u Uploader) initiateUpload(initurl string) (*initiateDetails, error) {
	if u.Debug {
		stderr("initiating upload")
	}
	resp, err := u.performRequest("POST", initurl, nil, false, "")
	if err != nil {
		return nil, errs.WithE(err, "Failed To perform push request")
	}
	defer resp.Close()

	respblob, err := ioutil.ReadAll(resp)
	if err != nil {
		return nil, errs.WithE(err, "Failed To read response")
	}

	deets := &initiateDetails{}
	err = json.Unmarshal(respblob, deets)

	if u.Debug {
		stderr("upload initiated")
		stderr(" - manifest endpoint: %s", deets.ManifestURL)
		stderr(" - signature endpoint: %s", deets.SignatureURL)
		stderr(" - aci endpoint: %s", deets.ACIURL)
	}

	if err != nil {
		return nil, errs.WithE(err, "Failed to unmarshal response from upload")
	}

	return deets, err
}
예제 #2
0
func RouterFromJson(content []byte, s *Synapse) (Router, error) {
	t := &RouterCommon{}
	if err := json.Unmarshal([]byte(content), t); err != nil {
		return nil, errs.WithE(err, "Failed to unmarshall check type")
	}

	fields := data.WithField("type", t.Type)
	var typedRouter Router
	switch t.Type {
	case "console":
		typedRouter = NewRouterConsole()
	case "haproxy":
		typedRouter = NewRouterHaProxy()
	case "template":
		typedRouter = NewRouterTemplate()
	default:
		return nil, errs.WithF(fields, "Unsupported router type")
	}

	if err := json.Unmarshal([]byte(content), &typedRouter); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to unmarshall router")
	}

	if err := typedRouter.Init(s); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to init router")
	}
	return typedRouter, nil
}
예제 #3
0
func ReporterFromJson(data []byte, s *Service) (Reporter, error) {
	t := &ReporterCommon{}
	if err := json.Unmarshal([]byte(data), t); err != nil {
		return nil, errs.WithE(err, "Failed to unmarshall reporter type")
	}

	fields := s.fields.WithField("type", t.Type)
	var typedReporter Reporter
	switch t.Type {
	case "file":
		typedReporter = NewReporterFile()
	case "console":
		typedReporter = NewReporterConsole()
	case "zookeeper":
		typedReporter = NewReporterZookeeper()
	default:
		return nil, errs.WithF(fields, "Unsupported reporter type")
	}

	if err := json.Unmarshal([]byte(data), &typedReporter); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to unmarshall reporter")
	}

	if err := typedReporter.getCommon().Init(s); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to init common reporter")
	}

	if err := typedReporter.Init(s); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to init reporter")
	}
	return typedReporter, nil
}
예제 #4
0
func CheckerFromJson(data []byte, s *Service) (Checker, error) {
	t := &CheckCommon{}
	if err := json.Unmarshal([]byte(data), t); err != nil {
		return nil, errs.WithE(err, "Failed to unmarshall check type")
	}

	fields := s.fields.WithField("type", t.Type)
	var typedCheck Checker
	switch t.Type {
	case "http":
		typedCheck = NewCheckHttp()
	case "proxyhttp":
		typedCheck = NewCheckProxyHttp()
	case "tcp":
		typedCheck = NewCheckTcp()
	case "sql":
		typedCheck = NewCheckSql()
	case "amqp":
		typedCheck = NewCheckAmqp()
	case "exec":
		typedCheck = NewCheckExec()
	default:
		return nil, errs.WithF(fields, "Unsupported check type")
	}

	if err := json.Unmarshal([]byte(data), &typedCheck); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to unmarshall check")
	}

	if err := typedCheck.Init(s); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to init check")
	}
	return typedCheck, nil
}
예제 #5
0
파일: aci.go 프로젝트: HimanshPal/cnt
func readAciManifest(manifestPath string) (*spec.AciManifest, error) {
	manifest := spec.AciManifest{Aci: spec.AciDefinition{}}

	source, err := ioutil.ReadFile(manifestPath)
	if err != nil {
		return nil, err
	}
	err = yaml.Unmarshal([]byte(source), &manifest)
	if err != nil {
		return nil, errs.WithE(err, "Cannot unmarshall manifest")
	}

	return &manifest, nil
}
예제 #6
0
func renderServerOptionsTemplate(report Report, serverOptions HapServerOptionsTemplate) (string, error) {
	if serverOptions.Template == nil {
		return "", nil
	}
	var buff bytes.Buffer
	if err := serverOptions.Execute(&buff, struct {
		Name string
	}{
		Name: report.Name,
	}); err != nil {
		return "", errs.WithE(err, "Failed to template serverOptions")
	}
	res := buff.String()
	if strings.Contains(res, "<no value>") {
		return "", errs.WithF(data.WithField("content", res), "serverOption templating has <no value>")
	}
	return res, nil
}
예제 #7
0
func (r *ReporterZookeeper) sendReportToZk() error {
	r.reportMutex.Lock()
	defer r.reportMutex.Unlock()

	exists, _, err := r.connection.Conn.Exists(r.currentNode)
	if *r.report.Available || r.ExposeOnUnavailable {
		content, err := r.report.toJson()
		if err != nil {
			return errs.WithEF(err, r.fields, "Failed to prepare report")
		}

		if !exists {
			logs.WithF(r.fields).Debug("Does not exists")
			acl := zk.WorldACL(zk.PermAll)
			if err = r.mkdirStaticPath(acl); err != nil {
				return errs.WithEF(err, r.fields, "Cannot create static path")
			}

			if r.currentNode, err = r.connection.CreateEphemeral(r.fullPath, content, acl); err != nil {
				return errs.WithEF(err, r.fields.WithField("fullpath", r.fullPath), "Cannot create path")
			}
		} else {
			logs.WithF(r.fields).Debug("writting data")
			if _, err := r.connection.Conn.Set(r.currentNode, content, -1); err != nil {
				return errs.WithE(err, "Failed to write status")
			}
		}
	} else if exists {
		logs.WithF(r.fields).Debug("delete")
		err = r.connection.Conn.Delete(r.currentNode, -1)
		if err != nil {
			return errs.WithEF(err, r.fields.WithField("fullpath", r.fullPath), "Cannot delete node")
		}
	}
	return nil
}
예제 #8
0
func WatcherFromJson(content []byte, service *Service) (Watcher, error) {
	t := &WatcherCommon{}
	if err := json.Unmarshal([]byte(content), t); err != nil {
		return nil, errs.WithE(err, "Failed to unmarshall watcher type")
	}

	fields := data.WithField("type", t.Type)
	var typedWatcher Watcher
	switch t.Type {
	case "zookeeper":
		typedWatcher = NewWatcherZookeeper()
	default:
		return nil, errs.WithF(fields, "Unsupported watcher type")
	}

	if err := json.Unmarshal([]byte(content), &typedWatcher); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to unmarshall watcher")
	}

	if err := typedWatcher.Init(service); err != nil {
		return nil, errs.WithEF(err, fields, "Failed to init watcher")
	}
	return typedWatcher, nil
}
예제 #9
0
파일: push.go 프로젝트: nyodas/cnt
// Upload performs the upload of the ACI and signature specified in the
// Uploader struct.
func (u Uploader) Upload() error {
	acifile, err := os.Open(u.Acipath)
	if err != nil {
		return errs.WithEF(err, data.WithField("file", u.Acipath), "Failed to open aci file")
	}
	defer acifile.Close()

	var ascfile *os.File
	if _, err := os.Stat(u.Ascpath); err == nil {
		ascfile, err = os.Open(u.Ascpath)
		if err != nil {
			return errs.WithEF(err, data.WithField("file", u.Ascpath), "Failed to open asc file")
		}
		defer ascfile.Close()
	}

	manifest, err := aci.ManifestFromImage(acifile)
	if err != nil {
		return errs.WithEF(err, data.WithField("file", u.Ascpath), "Failed to extract manifest from aci")
	}
	app, err := discovery.NewAppFromString(u.Uri)
	if err != nil {
		return errs.WithEF(err, data.WithField("uri", u.Uri), "Failed to prepare app")
	}

	if _, ok := app.Labels[archLabelName]; !ok {
		arch, ok := manifest.Labels.Get(archLabelName)
		if !ok {
			return fmt.Errorf("manifest is missing label: %q", archLabelName)
		}
		app.Labels[archLabelName] = arch
	}

	if _, ok := app.Labels[osLabelName]; !ok {
		os, ok := manifest.Labels.Get(osLabelName)
		if !ok {
			return fmt.Errorf("manifest is missing label: %q", osLabelName)
		}
		app.Labels[osLabelName] = os
	}

	if _, ok := app.Labels[extLabelName]; !ok {
		app.Labels[extLabelName] = strings.Trim(schema.ACIExtension, ".")
	}

	// Just to make sure that we start reading from the front of the file in
	// case aci.ManifestFromImage changed the cursor into the file.
	_, err = acifile.Seek(0, 0)
	if err != nil {
		return errs.WithE(err, "Failed to seek to beginning of file")
	}

	manblob, err := manifest.MarshalJSON()
	if err != nil {
		return errs.WithE(err, "Failed to marshall manifest")
	}

	initurl, err := u.getInitiationURL(app)
	if err != nil {
		return errs.WithEF(err, data.WithField("uri", u.Uri), "Failed to initate url")
	}

	initDeets, err := u.initiateUpload(initurl)
	if err != nil {
		return errs.WithE(err, "Failed to initiate upload")
	}

	type partToUpload struct {
		label string
		url   string
		r     io.Reader
		draw  bool
	}

	parts := []partToUpload{
		{"manifest", initDeets.ManifestURL, bytes.NewReader(manblob), false},
		{"ACI", initDeets.ACIURL, acifile, true}}
	if ascfile != nil {
		parts = append(parts, partToUpload{"signature", initDeets.SignatureURL, ascfile, true})
	}

	for _, part := range parts {
		err = u.uploadPart(part.url, part.r, part.draw, part.label)
		if err != nil {
			reason := fmt.Errorf("error uploading %s: %v", part.label, err)
			reportErr := u.reportFailure(initDeets.CompletedURL, reason.Error())
			if reportErr != nil {
				return fmt.Errorf("error uploading %s and error reporting failure: %v, %v", part.label, err, reportErr)
			}
			return reason
		}
	}

	err = u.reportSuccess(initDeets.CompletedURL)
	if err != nil {
		return errs.WithE(err, "Remote server report upload failure")
	}

	return nil
}
예제 #10
0
func (s *Synapse) Init(version string, buildTime string, logLevelIsSet bool) error {
	s.synapseBuildTime = buildTime
	s.synapseVersion = version

	if s.ApiPort == 0 {
		s.ApiPort = 3455
	}

	if !logLevelIsSet && s.LogLevel != nil {
		logs.SetLevel(*s.LogLevel)
	}

	s.routerUpdateFailures = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "synapse",
			Name:      "router_update_failure",
			Help:      "router update failures",
		}, []string{"type"})

	s.serviceAvailableCount = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "synapse",
			Name:      "service_available_count",
			Help:      "service available status",
		}, []string{"service"})

	s.serviceUnavailableCount = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "synapse",
			Name:      "service_unavailable_count",
			Help:      "service unavailable status",
		}, []string{"service"})

	s.watcherFailures = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "synapse",
			Name:      "watcher_failure",
			Help:      "watcher failure",
		}, []string{"service", "type"})

	if err := prometheus.Register(s.watcherFailures); err != nil {
		return errs.WithEF(err, s.fields, "Failed to register prometheus watcher_failure")
	}

	if err := prometheus.Register(s.serviceAvailableCount); err != nil {
		return errs.WithEF(err, s.fields, "Failed to register prometheus service_available_count")
	}

	if err := prometheus.Register(s.serviceUnavailableCount); err != nil {
		return errs.WithEF(err, s.fields, "Failed to register prometheus service_unavailable_count")
	}

	if err := prometheus.Register(s.routerUpdateFailures); err != nil {
		return errs.WithEF(err, s.fields, "Failed to register prometheus router_update_failure")
	}

	for _, data := range s.Routers {
		router, err := RouterFromJson(data, s)
		if err != nil {
			return errs.WithE(err, "Failed to init router")
		}
		s.typedRouters = append(s.typedRouters, router)
	}

	return nil
}
예제 #11
0
func (n *Nerve) Init(version string, buildTime string, logLevelIsSet bool) error {
	n.nerveVersion = version
	n.nerveBuildTime = buildTime
	if n.ApiPort == 0 {
		n.ApiPort = 3454
	}

	if !logLevelIsSet && n.LogLevel != nil {
		logs.SetLevel(*n.LogLevel)
	}

	n.checkerFailureCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "nerve",
			Name:      "checker_failure_total",
			Help:      "Counter of failed check",
		}, []string{"name", "ip", "port", "type"})

	n.execFailureCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "nerve",
			Name:      "exec_failure_total",
			Help:      "Counter of failed exec",
		}, []string{"name", "ip", "port", "type"})

	n.reporterFailureCount = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "nerve",
			Name:      "reporter_failure_total",
			Help:      "Counter of report failure",
		}, []string{"name", "ip", "port", "type"})

	n.availableGauge = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Namespace: "nerve",
			Name:      "service_available",
			Help:      "service available status",
		}, []string{"name", "ip", "port"})

	if err := prometheus.Register(n.execFailureCount); err != nil {
		return errs.WithEF(err, n.fields, "Failed to register prometheus exec_failure_total")
	}
	if err := prometheus.Register(n.checkerFailureCount); err != nil {
		return errs.WithEF(err, n.fields, "Failed to register prometheus check_failure_total")
	}
	if err := prometheus.Register(n.reporterFailureCount); err != nil {
		return errs.WithEF(err, n.fields, "Failed to register prometheus reporterFailureCount")
	}
	if err := prometheus.Register(n.availableGauge); err != nil {
		return errs.WithEF(err, n.fields, "Failed to register prometheus service_available")
	}

	n.serviceStopper = make(chan struct{})
	for _, service := range n.Services {
		if err := service.Init(n); err != nil {
			return errs.WithE(err, "Failed to init service")
		}
	}

	m := make(map[string]*Service)
	for i, service := range n.Services {
		m[service.Name] = service
		if len(m) != i+1 {
			return errs.WithF(n.fields.WithField("name", service.Name), "Duplicate service name")
		}
	}

	return nil
}