Ejemplo n.º 1
0
func (self *DBRcs) Init(config *util.MzConfig) (err error) {

	dsn := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=%s",
		config.Get("db.user", "user"),
		config.Get("db.password", "password"),
		config.Get("db.host", "localhost"),
		config.Get("db.db", "postgres"),
		config.Get("db.sslmode", "disable"))

	if self.Db, err = sql.Open("postgres", dsn); err != nil {
		return err
	}

	if _, err = self.Db.Exec("set search_path to public"); err != nil {
		return err
	}

	self.Db.SetMaxIdleConns(100)
	if err = self.Db.Ping(); err != nil {
		return err
	}

	if err = self.InitVersioning(); err != nil {
		return err
	}

	return err
}
Ejemplo n.º 2
0
func SendPush(devRec *storage.Device, config *util.MzConfig) error {
	// wow, so very tempted to make sure this matches the known push server.
	if config.GetFlag("push.disabled") {
		return nil
	}
	/* Must provide a version value for some push servers. This value
	   is meaningless to FMD. */
	bbody := []byte(fmt.Sprintf("version=%d", time.Now().UTC().Unix()))
	body := bytes.NewReader(bbody)
	/* If your server is not trustfully signed, the following will fail.
	   If partners are unable/unwilling to trustfully sign servers,
	   it is possible to skip validation by using
	    &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
	   however that is not advised as a general policy for damn good reasons.

	*/
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{
		//NameToCertificate: config["partnerCertPool"],
		//InsecureSkipVerify: true,
		},
	}

	req, err := http.NewRequest("PUT", devRec.PushUrl, body)
	if err != nil {
		return err
	}
	cli := http.Client{Transport: tr}
	resp, err := cli.Do(req)
	// Close the body, otherwise Memory leak!
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return errors.New("Push Server Error")
	}
	return nil
}
Ejemplo n.º 3
0
// Open the database.
func OpenPostgres(config *util.MzConfig, logger util.Logger, metrics util.Metrics) (storage Storage, err error) {

	dsn := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=%s",
		config.Get("db.user", "user"),
		config.Get("db.password", "password"),
		config.Get("db.host", "localhost"),
		config.Get("db.db", "wmf"),
		config.Get("db.sslmode", "disable"))
	logCat := "storage"
	// default expry is 5 days
	defExpry, err := strconv.ParseInt(config.Get("db.default_expry", "432000"), 0, 64)
	if err != nil {
		defExpry = 432000
	}

	if err = applyPostgresUpdates(config); err != nil {
		return nil, err
	}

	db, err := sql.Open("postgres", dsn)
	if err != nil {
		panic("Storage is unavailable: " + err.Error() + "\n")
	}
	db.SetMaxIdleConns(100)
	if err = db.Ping(); err != nil {
		return nil, err
	}
	maxDev, err := strconv.ParseInt(config.Get("db.max_devices_per_user", "1"), 0, 64)
	if err != nil {
		maxDev = 1
	}

	return &pgStore{
		config:   config,
		logger:   logger,
		logCat:   logCat,
		defExpry: defExpry,
		maxDev:   maxDev,
		metrics:  metrics,
		dsn:      dsn,
		db:       db}, nil
}
Ejemplo n.º 4
0
func applyPostgresUpdates(config *util.MzConfig) error {
	if config.Get("ddl.create", "") != "" ||
		config.GetFlag("ddl.upgrade") ||
		config.Get("ddl.downgrade", "") != "" ||
		config.GetFlag("ddl.log") {
		if config.Get("ddl.create", "") != "" && config.GetFlag("ddl.upgrade") {
			log.Fatalf("Invalid DDL options.  You can only specify one DDL command at a time.")
			return errors.New("Invalid option")
		}

		rcs := new(DBRcs)
		rcs.Init(config)
		if create := config.Get("ddl.create", ""); create != "" {
			if _, _, err := rcs.CreateNextRev("sql/patches", create); err != nil {
				log.Fatalf("Could not create a new revision: %s", err.Error())
			}
			return errors.New("Invalid revision")
		}

		if config.GetFlag("ddl.upgrade") {
			err := rcs.Upgrade("sql/patches", true)
			if err != nil {
				log.Fatalf("Could not upgrade database: %s", err.Error())
			}
			return errors.New("Invalid database upgrade")
		}

		if down := config.Get("ddl.downgrade", ""); down != "" {
			err := rcs.Downgrade("sql/patches", down)
			if err != nil {
				log.Fatalf("Could not downgrade database: %s", err.Error())
			}
			return err
		}

		if config.GetFlag("ddl.log") {
			err := rcs.Changelog("sql/patches")
			if err != nil {
				log.Fatalf("Could not get changelog: %s", err.Error())
			}
			return err
		}
	}
	return nil
}