Esempio n. 1
0
func NewManager(addr string, database string, authKey string, version string, disableUsageInfo bool) (*Manager, error) {
	session, err := r.Connect(r.ConnectOpts{
		Address:     addr,
		Database:    database,
		AuthKey:     authKey,
		MaxIdle:     10,
		IdleTimeout: time.Second * 30,
	})
	if err != nil {
		return nil, err
	}
	logger.Info("checking database")
	r.DbCreate(database).Run(session)
	m := &Manager{
		address:          addr,
		database:         database,
		authKey:          authKey,
		session:          session,
		authenticator:    &shipyard.Authenticator{},
		store:            store,
		StoreKey:         storeKey,
		version:          version,
		disableUsageInfo: disableUsageInfo,
	}
	m.initdb()
	m.init()
	return m, nil
}
Esempio n. 2
0
func (suite *UserStoreSuite) SetupSuite() {
	store, err := newTestUserStore()
	if err != nil {
		panic(err)
	}
	suite.store = store

	// create necessary DB, table, indices
	_, err = r.DbCreate("test").Run(suite.store.conn)
	if err != nil {
		panic(err)
	}
	_, err = r.Db("test").TableCreate("users").Run(suite.store.conn)
	if err != nil {
		panic(err)
	}
	_, err = r.Db("test").Table("users").IndexCreate("email").Run(suite.store.conn)
	if err != nil {
		panic(err)
	}

	suite.user = NewUser("*****@*****.**", "password")
	_, err = r.Db("test").Table("users").Insert(suite.user).RunWrite(suite.store.conn)
	if err != nil {
		panic(err)
	}
}
Esempio n. 3
0
func InitDB() *rethink.Session {

	session, err := rethink.Connect(map[string]interface{}{
		"address":     "localhost:8080", //os.Getenv("RETHINKDB_URL"),
		"database":    "test",
		"maxIdle":     10,
		"idleTimeout": time.Second * 10,
	})

	if err != nil {
		log.Println(err)
	}

	err = rethink.DbCreate("test").Exec(session)
	if err != nil {
		log.Println(err)
	}

	_, err = rethink.Db("test").TableCreate("articles").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	return session
}
Esempio n. 4
0
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	flag.Parse()

	// Connect to RethinkDB
	var err error
	session, err = r.Connect(r.ConnectOpts{
		Address: *rethinkAddress,
	})
	if err != nil {
		log.Fatal(err)
	}

	// Create the database and tables
	r.DbCreate(*rethinkName).Exec(session)
	r.Db(*rethinkName).TableCreate("invites").Exec(session)
	r.Db(*rethinkName).Table("invites").IndexCreate("email").Exec(session)
	r.Db(*rethinkName).Table("invites").IndexCreate("name").Exec(session)

	// Add a CORS middleware
	goji.Use(cors.New(cors.Options{
		AllowCredentials: true,
	}).Handler)

	// Add routes to goji
	goji.Get("/", index)
	goji.Post("/check", check)
	goji.Post("/free", free)
	goji.Post("/create", create)

	// Start the server
	goji.Serve()
}
Esempio n. 5
0
func createDatabase(database string, debug bool) {
	// create database
	_, err := r.DbCreate(database).Run(session)
	if err != nil {
		if debug {
			log.Println(err.Error())
		}
	}
}
Esempio n. 6
0
func dbinit() *r.Session {
	session, err := r.Connect(r.ConnectOpts{
		Address:     "localhost:28015",
		Database:    "goShop",
		MaxIdle:     10,
		IdleTimeout: time.Second * 10,
	})

	if err != nil {
		log.Println(err)
	}

	err = r.DbCreate("goShop").Exec(session)
	if err != nil {
		log.Println(err)
	}

	_, err = r.Db("goShop").TableCreate("staff").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	newAdmin := adminRegister{
		Login:    "******",
		Password: "******",
	}
	_, err = r.Db("goShop").Table("staff").Insert(newAdmin).RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	_, err = r.Db("goShop").TableCreate("clients").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	_, err = r.Db("goShop").TableCreate("items").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	_, err = r.Db("goShop").TableCreate("orders").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	_, err = r.Db("goShop").TableCreate("options").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	return session

}
Esempio n. 7
0
func main() {
	flag.Parse()

	log = logrus.New()
	if *logFormatterType == "text" {
		log.Formatter = &logrus.TextFormatter{
			ForceColors: *logForceColors,
		}
	} else if *logFormatterType == "json" {
		log.Formatter = &logrus.JSONFormatter{}
	}
	log.Level = logrus.DebugLevel

	if *enableHub || *enableSender {
		var err error
		session, err = r.Connect(r.ConnectOpts{
			Address: *rethinkdbAddress,
		})
		if err != nil {
			log.WithField("error", err.Error()).Fatal("Unable to connect to RethinkDB")
		}

		r.DbCreate(*rethinkdbDatabase).Exec(session)
		r.Db(*rethinkdbDatabase).TableCreate("templates").Exec(session)
		r.Db(*rethinkdbDatabase).Table("templates").IndexCreate("name").Exec(session)
		r.Db(*rethinkdbDatabase).Table("templates").IndexCreate("version").Exec(session)
		r.Db(*rethinkdbDatabase).TableCreate("hub_state").Exec(session)
		r.Db(*rethinkdbDatabase).Table("hub_state").IndexCreate("time").Exec(session)
	}

	up := strings.Split(*usernames, ",")
	pp := strings.Split(*passwords, ",")

	if len(up) != len(pp) {
		log.Fatal("length of usernames and passwords is different")
	}

	if *enableSender {
		go initTemplates()

		for i, username := range up {
			go initSender(username, pp[i])
		}
	}

	if *enableHub {
		change := make(chan struct{})

		go initChurner(change)
		go initHub(change)
	}

	select {}
}
Esempio n. 8
0
func InitDatabase(s *r.Session, databaseName string) error {
	exists, err := ListContains(s, r.DbList(), databaseName)
	if err != nil {
		return err
	}
	if !exists {
		err := r.DbCreate(databaseName).Exec(s)
		if err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 9
0
func init() {
	var err error

	session, err = r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "harbor",
		MaxIdle:  40,
		MaxOpen:  40,
	})
	if err != nil {
		log.Fatalln(err.Error())
	}

	r.DbCreate("harbor").RunWrite(session)
	r.Db("harbor").TableCreate("users").Run(session)
}
Esempio n. 10
0
// Create/Setup all relevant tables in the database
func (db *RethinkDBAdapter) Setup() *CASServerError {

	// Setup the Database
	_, err := r.
		DbCreate(db.dbName).
		Run(db.session)
	if err != nil {
		casError := &FailedToSetupDatabaseError
		casError.err = &err
		return casError
	}

	// Setup tables
	db.SetupServicesTable()
	db.SetupTicketsTable()
	db.SetupUsersTable()
	db.SetupApiKeysTable()

	return nil
}
Esempio n. 11
0
File: db.go Progetto: sporto/kic
func initDb(dbSession *r.Session) error {
	// log.Println("initDb")

	_, database := getDbConf()

	log.Println("Database name ", database)

	_, err := r.DbCreate(database).Run(dbSession)
	if err != nil {
		// log.Println(err)
	}

	_, err = r.Db(database).TableCreate("accounts").Run(dbSession)
	if err != nil {
		// log.Println(err)
	}

	_, err = r.Db(database).TableCreate("transactions").Run(dbSession)
	if err != nil {
		// log.Println(err)
	}

	return nil
}
Esempio n. 12
0
func Setup() error {
	if err := r.DbCreate(TestDatabase).Exec(rethinkSession()); err != nil {
		return err
	}
	return r.Db(TestDatabase).TableCreate(TestTable).Exec(rethinkSession())
}
Esempio n. 13
0
func main() {
	flag.Parse()

	// Initialize a new logger
	log = logrus.New()
	if *logFormatterType == "text" {
		log.Formatter = &logrus.TextFormatter{
			ForceColors: *forceColors,
		}
	} else if *logFormatterType == "json" {
		log.Formatter = &logrus.JSONFormatter{}
	}
	log.Level = logrus.DebugLevel

	// Parse kiri addresses
	ka := strings.Split(*kiriAddresses, ",")

	// Set up kiri agent for discovery
	kd := kiri.New(ka)

	// Add stores to kd
	for i, store := range strings.Split(*kiriDiscoveryStores, ";") {
		parts := strings.Split(store, ",")
		if len(parts) != 2 {
			log.Fatalf("Invalid parts count in kiri_discovery_stores#%d", i)
		}

		var kind kiri.Format
		switch parts[0] {
		case "default":
			kind = kiri.Default
		case "puro":
			kind = kiri.Puro
		default:
			log.Fatalf("Invalid kind of store in kiri_discovery_stores#%d", i)
		}
		kd.Store(kind, parts[1])
	}

	// Connect to RethinkDB
	var session *r.Session
	err := kd.Discover(*kiriDiscoveryRethinkDB, nil, kiri.DiscoverFunc(func(service *kiri.Service) error {
		var err error
		session, err = r.Connect(r.ConnectOpts{
			Address: service.Address,
		})
		if err != nil {
			log.Print(err)
		}

		return err
	}))
	if err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to connect to RethinkDB")
	}

	// Prepare database's structure
	r.DbCreate(*rethinkdbDatabase).Exec(session)
	r.Db(*rethinkdbDatabase).TableCreate("jobs").Exec(session)

	// Connect to nsq
	var producer *nsq.Producer
	err = kd.Discover(*kiriDiscoveryNSQd, nil, kiri.DiscoverFunc(func(service *kiri.Service) error {
		producer, err = nsq.NewProducer(service.Address, nsq.NewConfig())
		if err != nil {
			log.Error(err)
			return err
		}

		err = producer.Ping()
		if err != nil {
			log.Error(err)
			return err
		}

		return nil
	}))
	if err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to connect to NSQd")
	}

	// Fetch the jobs
	cursor, err := r.Db(*rethinkdbDatabase).Table("jobs").Run(session)
	if err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to fetch jobs from RethinkDB")
	}
	var jobs []*Job
	if err := cursor.All(&jobs); err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to fetch jobs from RethinkDB")
	}

	// Create a new cron runner
	runner := cron.New()
	for _, job := range jobs {
		id, err := runner.AddJob(job.When, job)
		if err != nil {
			log.WithFields(logrus.Fields{
				"error": err.Error(),
			}).Fatal("Unable to queue a job")
		}

		mapping[job.ID] = id
	}

	log.Print("Starting the runner")

	runner.Start()

	log.Print("Starting the change watcher")

	// Watch for changes
	cursor, err = r.Db(*rethinkdbDatabase).Table("jobs").Changes().Run(session)

	var change struct {
		NewValue *Job `gorethink:"new_val"`
		OldValue *Job `gorethink:"old_val"`
	}
	for cursor.Next(&change) {
		if change.NewValue == nil {
			// Remove a job
			if id, ok := mapping[change.OldValue.ID]; ok {
				runner.Remove(id)
			}
		} else if change.OldValue == nil {
			// Create a new job
			id, err := runner.AddJob(change.NewValue.When, change.NewValue)
			if err != nil {
				log.WithFields(logrus.Fields{
					"error": err.Error(),
				}).Fatal("Unable to queue a job")
			}
			mapping[change.NewValue.ID] = id
		} else {
			// Recreate a job
			if id, ok := mapping[change.OldValue.ID]; ok {
				runner.Remove(id)
			}

			// Create a new job
			id, err := runner.AddJob(change.NewValue.When, change.NewValue)
			if err != nil {
				log.WithFields(logrus.Fields{
					"error": err.Error(),
				}).Fatal("Unable to queue a job")
			}
			mapping[change.NewValue.ID] = id
		}
	}
}
Esempio n. 14
0
func main() {
	// Parse the flags
	flag.Parse()

	// Initialize a new logger
	log := logrus.New()
	if *logFormatterType == "text" {
		log.Formatter = &logrus.TextFormatter{
			ForceColors: *forceColors,
		}
	} else if *logFormatterType == "json" {
		log.Formatter = &logrus.JSONFormatter{}
	}
	log.Level = logrus.DebugLevel

	// Parse kiri addresses
	ka := strings.Split(*kiriAddresses, ",")

	// Set up kiri agent for discovery
	kd := kiri.New(ka)

	// Add stores to kd
	for i, store := range strings.Split(*kiriDiscoveryStores, ";") {
		parts := strings.Split(store, ",")
		if len(parts) != 2 {
			log.Fatalf("Invalid parts count in kiri_discovery_stores#%d", i)
		}

		var kind kiri.Format
		switch parts[0] {
		case "default":
			kind = kiri.Default
		case "puro":
			kind = kiri.Puro
		default:
			log.Fatalf("Invalid kind of store in kiri_discovery_stores#%d", i)
		}
		kd.Store(kind, parts[1])
	}

	// Connect to RethinkDB
	var session *r.Session
	err := kd.Discover(*kiriDiscoveryRethinkDB, nil, kiri.DiscoverFunc(func(service *kiri.Service) error {
		var err error
		session, err = r.Connect(r.ConnectOpts{
			Address: service.Address,
		})
		if err != nil {
			log.Print(err)
		}

		return err
	}))
	if err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to connect to RethinkDB")
	}

	// Create the database and a scripts table
	r.DbCreate(*rethinkdbDatabase).Exec(session)
	r.Db(*rethinkdbDatabase).TableCreate("scripts").Exec(session)

	// Fetch the scripts
	cursor, err := r.Db(*rethinkdbDatabase).Table("scripts").Run(session)
	if err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to fetch scripts from RethinkDB")
	}
	var scripts []*shared.Script
	if err := cursor.All(&scripts); err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to fetch scripts from RethinkDB")
	}

	// Load them
	for _, script := range scripts {
		if script.Interpreter == "js" {
			tasks[script.ID] = &JSTask{
				ID:     script.ID,
				Source: script.Source,
			}
		} /* else if script.Interpreter == "lua" {
			tasks[script.ID] = &LuaTask{
				ID:     script.ID,
				Source: script.Source,
			}
		}*/
	}

	// Queue for jobs
	hostname, err := os.Hostname()
	if err != nil {
		log.WithFields(logrus.Fields{
			"error": err,
		}).Fatal("Unable to get the hostname")
	}
	// Create a new consumer
	consumer, err := nsq.NewConsumer("jobs", hostname, nsq.NewConfig())
	if err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to create a new consumer")
	}
	consumer.AddConcurrentHandlers(nsq.HandlerFunc(func(m *nsq.Message) error {
		// Decode the job
		var job *shared.Job
		if err := json.Unmarshal(m.Body, &job); err != nil {
			return err
		}

		// Ensure that we have such job
		if task, ok := tasks[job.Name]; ok {
			// Run it
			if err := task.Run(job); err != nil {
				m.Requeue(-1)
				return err
			}

			m.Finish()
			return nil
		}

		// We failed.
		m.Requeue(-1)
		return nil
	}), runtime.GOMAXPROCS(0))

	if err = kd.Discover(*kiriDiscoveryNSQLookupd, nil, kiri.DiscoverFunc(func(service *kiri.Service) error {
		return consumer.ConnectToNSQLookupd(service.Address)
	})); err != nil {
		log.WithFields(logrus.Fields{
			"error": err.Error(),
		}).Fatal("Unable to connect to the lookupd")
	}

	log.Print("Loaded all scripts.")
	log.Print("Starting the watcher.")

	// Watch for changes
	cursor, err = r.Db(*rethinkdbDatabase).Table("scripts").Changes().Run(session)
	var change struct {
		NewValue *shared.Script `gorethink:"new_val"`
		OldValue *shared.Script `gorethink:"old_val"`
	}
	for cursor.Next(&change) {
		if change.NewValue == nil {
			// Remove a job
			if _, ok := tasks[change.OldValue.ID]; ok {
				delete(tasks, change.OldValue.ID)
			}
		} else if change.OldValue == nil {
			if change.NewValue.Interpreter == "js" {
				tasks[change.NewValue.ID] = &JSTask{
					ID:     change.NewValue.ID,
					Source: change.NewValue.Source,
				}
			} /* else if change.NewValue.Interpreter == "lua" {
				tasks[change.NewValue.ID] = &LuaTask{
					ID:     change.NewValue.ID,
					Source: change.NewValue.Source,
				}
			}*/
		} else {
			if _, ok := tasks[change.OldValue.ID]; ok {
				delete(tasks, change.OldValue.ID)
			}

			if change.NewValue.Interpreter == "js" {
				tasks[change.NewValue.ID] = &JSTask{
					ID:     change.NewValue.ID,
					Source: change.NewValue.Source,
				}
			} /* else if change.NewValue.Interpreter == "lua" {
				tasks[change.NewValue.ID] = &LuaTask{
					ID:     change.NewValue.ID,
					Source: change.NewValue.Source,
				}
			}*/
		}
	}
}
Esempio n. 15
0
// Setup configures the RethinkDB server
func Setup(opts r.ConnectOpts) error {
	// Initialize a new setup connection
	ss, err := r.Connect(opts)
	if err != nil {
		return err
	}

	// Create databases
	for _, d := range databaseNames {
		r.DbCreate(d).Exec(ss)

		r.Db(d).TableCreate("accounts").Exec(ss)
		r.Db(d).Table("accounts").IndexCreate("name").Exec(ss)
		r.Db(d).Table("accounts").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("accounts").IndexCreate("date_modified").Exec(ss)
		r.Db(d).Table("accounts").IndexCreate("alt_email").Exec(ss)
		r.Db(d).Table("accounts").IndexCreate("type").Exec(ss)
		r.Db(d).Table("accounts").IndexCreate("status").Exec(ss)

		r.Db(d).TableCreate("addresses").Exec(ss)
		r.Db(d).Table("addresses").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("addresses").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("addresses").IndexCreate("date_modified").Exec(ss)

		r.Db(d).TableCreate("contacts").Exec(ss)
		r.Db(d).Table("contacts").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("contacts").IndexCreate("name").Exec(ss)
		r.Db(d).Table("contacts").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("contacts").IndexCreate("date_modified").Exec(ss)

		r.Db(d).TableCreate("emails").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("date_modified").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("thread").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("kind").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("from").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("message_id").Exec(ss)
		r.Db(d).Table("emails").IndexCreate("to", r.IndexCreateOpts{Multi: true}).Exec(ss)
		r.Db(d).Table("emails").IndexCreate("cc", r.IndexCreateOpts{Multi: true}).Exec(ss)
		r.Db(d).Table("emails").IndexCreate("bcc", r.IndexCreateOpts{Multi: true}).Exec(ss)
		r.Db(d).Table("emails").IndexCreateFunc("messageIDOwner", func(row r.Term) interface{} {
			return []interface{}{
				row.Field("message_id"),
				row.Field("owner"),
			}
		}).Exec(ss)
		r.Db(d).Table("emails").IndexCreateFunc("threadStatus", func(row r.Term) interface{} {
			return []interface{}{
				row.Field("thread"),
				row.Field("status"),
			}
		}).Exec(ss)

		r.Db(d).TableCreate("files").Exec(ss)
		r.Db(d).Table("files").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("files").IndexCreate("name").Exec(ss)
		r.Db(d).Table("files").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("files").IndexCreate("date_modified").Exec(ss)

		r.Db(d).TableCreate("keys").Exec(ss)
		r.Db(d).Table("keys").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("keys").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("keys").IndexCreate("date_modified").Exec(ss)
		r.Db(d).Table("keys").IndexCreate("key_id").Exec(ss)

		r.Db(d).TableCreate("labels").Exec(ss)
		r.Db(d).Table("labels").IndexCreate("name").Exec(ss)
		r.Db(d).Table("labels").IndexCreate("builtin").Exec(ss)
		r.Db(d).Table("labels").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("labels").IndexCreateFunc("nameOwnerBuiltin", func(row r.Term) interface{} {
			return []interface{}{
				row.Field("name"),
				row.Field("owner"),
				row.Field("builtin"),
			}
		}).Exec(ss)

		r.Db(d).TableCreate("threads").Exec(ss)
		r.Db(d).Table("threads").IndexCreate("name").Exec(ss)
		r.Db(d).Table("threads").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("threads").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("threads").IndexCreate("date_modified").Exec(ss)
		r.Db(d).Table("threads").IndexCreate("emails", r.IndexCreateOpts{Multi: true}).Exec(ss)
		r.Db(d).Table("threads").IndexCreate("labels", r.IndexCreateOpts{Multi: true}).Exec(ss)
		r.Db(d).Table("threads").IndexCreate("members", r.IndexCreateOpts{Multi: true}).Exec(ss)
		r.Db(d).Table("threads").IndexCreate("subject_hash").Exec(ss)
		r.Db(d).Table("threads").IndexCreate("secure").Exec(ss)
		r.Db(d).Table("threads").IndexCreateFunc("subjectOwner", func(row r.Term) interface{} {
			return []interface{}{
				row.Field("subject_hash"),
				row.Field("owner"),
			}
		}).Exec(ss)

		r.Db(d).TableCreate("tokens").Exec(ss)
		r.Db(d).Table("tokens").IndexCreate("name").Exec(ss)
		r.Db(d).Table("tokens").IndexCreate("owner").Exec(ss)
		r.Db(d).Table("tokens").IndexCreate("date_created").Exec(ss)
		r.Db(d).Table("tokens").IndexCreate("date_modified").Exec(ss)
		r.Db(d).Table("tokens").IndexCreate("type").Exec(ss)
		r.Db(d).Table("tokens").IndexCreate("expiry_date").Exec(ss)

		r.Db(d).TableCreate("webhooks").Exec(ss)
		r.Db(d).Table("webhooks").IndexCreate("target").Exec(ss)
		r.Db(d).Table("webhooks").IndexCreate("type").Exec(ss)
		r.Db(d).Table("webhooks").IndexCreateFunc("targetType", func(row r.Term) interface{} {
			return []interface{}{
				row.Field("target"),
				row.Field("type"),
			}
		}).Exec(ss)
	}

	return ss.Close()
}