コード例 #1
0
ファイル: db.go プロジェクト: xamarin/benchmarker
func ensureConfigExists(database *pgx.Tx, config Config) *requestError {
	dbConfig, reqErr := fetchConfig(database, config.Name)
	if reqErr != nil {
		return reqErr
	}

	if dbConfig != nil {
		if !config.isSameAs(dbConfig) {
			return badRequestError("Config does not match the one in the database")
		}
		return nil
	}

	envVars, err := json.Marshal(config.MonoEnvironmentVariables)
	if err != nil {
		return internalServerError("Could not marshal mono environment variables to JSON")
	}

	_, err = database.Exec("insertConfig", config.Name, config.MonoExecutable, string(envVars), config.MonoOptions)
	if err != nil {
		fmt.Printf("config insert error: %s\n", err.Error())
		return internalServerError("Could not insert config")
	}

	return nil
}
コード例 #2
0
ファイル: data_store.go プロジェクト: ably-forks/flynn
func (d *pgDataStore) addCertWithTx(tx *pgx.Tx, c *router.Certificate) error {
	c.Cert = strings.Trim(c.Cert, " \n")
	c.Key = strings.Trim(c.Key, " \n")

	if _, err := tls.X509KeyPair([]byte(c.Cert), []byte(c.Key)); err != nil {
		return httphelper.JSONError{
			Code:    httphelper.ValidationErrorCode,
			Message: "Certificate invalid: " + err.Error(),
		}
	}

	tlsCertSHA256 := sha256.Sum256([]byte(c.Cert))
	if err := tx.QueryRow("select_certificate_by_sha", tlsCertSHA256[:]).Scan(&c.ID, &c.CreatedAt, &c.UpdatedAt); err != nil {
		if err := tx.QueryRow("insert_certificate", c.Cert, c.Key, tlsCertSHA256[:]).Scan(&c.ID, &c.CreatedAt, &c.UpdatedAt); err != nil {
			return err
		}
	}
	for _, rid := range c.Routes {
		if _, err := tx.Exec("delete_route_certificate_by_route_id", rid); err != nil {
			return err
		}
		if _, err := tx.Exec("insert_route_certificate", rid, c.ID); err != nil {
			return err
		}
	}
	return nil
}
コード例 #3
0
ファイル: db.go プロジェクト: xamarin/benchmarker
func updateRunSet(database *pgx.Tx, runSetID int32, rs *RunSet) *requestError {
	_, err := database.Exec("updateRunSet", runSetID, rs.LogURLs, rs.TimedOutBenchmarks, rs.CrashedBenchmarks)
	if err != nil {
		fmt.Printf("update run set error: %s\n", err)
		return internalServerError("Could not update run set")
	}
	return nil
}
コード例 #4
0
ファイル: db.go プロジェクト: xamarin/benchmarker
func (r *Run) ensureBenchmarksAndMetricsExist(database *pgx.Tx, benchmarks map[string]bool) *requestError {
	if benchmarks != nil && !benchmarks[r.Benchmark] {
		_, err := database.Exec("insertBenchmark", r.Benchmark)
		if err != nil {
			return badRequestError("Couldn't insert Benchmark: " + r.Benchmark)
		}
		benchmarks[r.Benchmark] = true
	}
	for m, v := range r.Results {
		if !metricIsAllowed(m, v) {
			return badRequestError("Metric not supported or results of wrong type: " + m)
		}
	}
	return nil
}
コード例 #5
0
ファイル: db.go プロジェクト: xamarin/benchmarker
func ensureProductExists(database *pgx.Tx, product Product) (string, *requestError) {
	var commitDate time.Time
	// FIXME: check and update mergeBaseHash
	err := database.QueryRow("queryCommitWithProduct", product.Commit, product.Name).Scan(&commitDate)
	if err == nil {
		return product.Commit, nil
	}

	if err == pgx.ErrNoRows {
		var sha string
		fmt.Printf("Commit %s for product %s not found in DB.\n", product.Commit, product.Name)

		owner, repo := githubRepoForProduct(product.Name)
		if owner == "" || repo == "" {
			return "", badRequestError("Unknown product")
		}

		commit, _, err := githubClient.Git.GetCommit(owner, repo, product.Commit)
		if err == nil {
			sha = *commit.SHA
			commitDate = *commit.Committer.Date
		} else {
			// try to fall back to provided commitDate
			if product.CommitDate != nil {
				sha = product.Commit
				commitDate = *product.CommitDate
			} else {
				return "", badRequestError("Could not get date for provided commit " + product.Commit)
			}
		}

		_, err = database.Exec("insertCommit", sha, commitDate, product.Name, product.MergeBaseHash)
		if err != nil {
			return "", internalServerError("Couldn't insert commit")
		}

		return sha, nil
	}

	return "", internalServerError("Database query error")
}
コード例 #6
0
ファイル: db.go プロジェクト: xamarin/benchmarker
func ensureMachineExists(database *pgx.Tx, machine Machine) *requestError {
	var architecture string
	err := database.QueryRow("queryMachine", machine.Name).Scan(&architecture)
	if err == nil {
		if machine.Architecture != architecture {
			return badRequestError("Machine has a different architecture than the one in the database")
		}
		return nil
	}

	if err == pgx.ErrNoRows {
		_, err = database.Exec("insertMachine", machine.Name, machine.Architecture)
		if err != nil {
			return internalServerError("Could not insert machine")
		}

		return nil
	}

	return internalServerError("Database query error")
}
コード例 #7
0
ファイル: db.go プロジェクト: xamarin/benchmarker
func insertResults(database *pgx.Tx, runID int32, results Results, update bool) *requestError {
	idByMetric := make(map[string]int32)
	if update {
		rows, err := database.Query("queryRunMetricsForRun", runID)
		if err != nil {
			return internalServerError("Could not query run metrics")
		}
		for rows.Next() {
			var runMetricID int32
			var metric string
			err = rows.Scan(&runMetricID, &metric)
			if err != nil {
				return internalServerError("Could not scan run metric")
			}
			idByMetric[metric] = runMetricID
		}
	}

	for m, v := range results {
		var err error
		runMetricID, exists := idByMetric[m]
		if metricIsArray(m) {
			var arr []float64
			for _, x := range v.([]interface{}) {
				arr = append(arr, x.(float64))
			}
			if exists {
				_, err = database.Exec("updateRunMetricArray", runMetricID, arr)
			} else {
				_, err = database.Exec("insertRunMetricArray", runID, m, arr)
			}
		} else {
			if exists {
				_, err = database.Exec("updateRunMetricNumber", runMetricID, v)
			} else {
				_, err = database.Exec("insertRunMetricNumber", runID, m, v)
			}
		}
		if err != nil {
			fmt.Printf("run metric insert error: %s\n", err)
			return internalServerError("Could not insert run metric")
		}
	}
	return nil
}
コード例 #8
0
ファイル: db.go プロジェクト: xamarin/benchmarker
func deleteRunSet(database *pgx.Tx, runSetID int32) (int64, int64, *requestError) {
	res, err := database.Exec("deleteRunMetricByRunSetId", runSetID)
	if err != nil {
		fmt.Printf("delete runmetric by runsetid: %s\n", err)
		return 0, 0, internalServerError("Could not delete runset")
	}
	numMetrics := res.RowsAffected()

	res, err = database.Exec("deleteRunByRunSetId", runSetID)
	if err != nil {
		fmt.Printf("delete run by runsetid: %s\n", err)
		return 0, 0, internalServerError("Could not delete runset")
	}
	numRuns := res.RowsAffected()

	_, err = database.Exec("deleteRunSet", runSetID)
	if err != nil {
		fmt.Printf("delete runset: %s\n", err)
		return 0, 0, internalServerError("Could not delete runset")
	}

	return numMetrics, numRuns, nil
}
コード例 #9
0
ファイル: process.go プロジェクト: imjorge/flynn
func (p *Process) assumePrimary(downstream *discoverd.Instance) (err error) {
	log := p.log.New("fn", "assumePrimary")
	if downstream != nil {
		log = log.New("downstream", downstream.Addr)
	}

	if p.running() && p.config().Role == state.RoleSync {
		log.Info("promoting to primary")

		if err := ioutil.WriteFile(p.triggerPath(), nil, 0655); err != nil {
			log.Error("error creating trigger file", "path", p.triggerPath(), "err", err)
			return err
		}

		p.waitForSync(downstream, true)

		return nil
	}

	log.Info("starting as primary")

	if p.running() {
		panic(fmt.Sprintf("unexpected state running role=%s", p.config().Role))
	}

	if err := p.initDB(); err != nil {
		return err
	}

	if err := os.Remove(p.recoveryConfPath()); err != nil && !os.IsNotExist(err) {
		log.Error("error removing recovery.conf", "path", p.recoveryConfPath(), "err", err)
		return err
	}

	if err := p.writeConfig(configData{ReadOnly: downstream != nil}); err != nil {
		log.Error("error writing postgres.conf", "path", p.configPath(), "err", err)
		return err
	}

	if err := p.start(); err != nil {
		return err
	}

	var tx *pgx.Tx
	defer func() {
		if err != nil {
			if tx != nil {
				tx.Rollback()
			}
			p.db.Close()
			if err := p.stop(); err != nil {
				log.Debug("ignoring error stopping postgres", "err", err)
			}
		}
	}()

	tx, err = p.db.Begin()
	if err != nil {
		log.Error("error acquiring connection", "err", err)
		return err
	}
	if _, err := tx.Exec("SET TRANSACTION READ WRITE"); err != nil {
		log.Error("error setting transaction read-write", "err", err)
		return err
	}
	if _, err := tx.Exec(fmt.Sprintf(`
		DO
		$body$
		BEGIN
		   IF NOT EXISTS (
			  SELECT * FROM pg_catalog.pg_user
			  WHERE	usename = 'flynn')
		   THEN
			  CREATE USER flynn WITH SUPERUSER CREATEDB CREATEROLE REPLICATION PASSWORD '%s';
		   END IF;
		END
		$body$;
	`, p.password)); err != nil {
		log.Error("error creating superuser", "err", err)
		return err
	}
	if err := tx.Commit(); err != nil {
		log.Error("error committing transaction", "err", err)
		return err
	}

	if downstream != nil {
		p.waitForSync(downstream, true)
	}

	return nil
}