Example #1
0
// NewTestServer creates a new initialised Laika httptest.Server. The server
// root credentials are "root" as username and password. It contains an
// environment named "test" with an enabled featured named "test_feature",
// and a user whose username is "user" and password is "password".
func NewTestServer(t *testing.T) *httptest.Server {
	s, err := store.NewMySQLStore(
		os.Getenv("LAIKA_MYSQL_USERNAME"),
		os.Getenv("LAIKA_MYSQL_PASSWORD"),
		os.Getenv("LAIKA_MYSQL_HOST"),
		os.Getenv("LAIKA_MYSQL_PORT"),
		os.Getenv("LAIKA_MYSQL_DBNAME"),
	)
	require.NoError(t, err)

	err = s.Ping()
	require.NoError(t, err)

	err = s.Migrate()
	require.NoError(t, err)

	user := models.User{
		Username:     "******" + store.Token(),
		PasswordHash: "awesome_password",
	}
	err = s.CreateUser(&user)
	require.NoError(t, err)

	server, err := NewServer(ServerConfig{
		Store:        s,
		RootUsername: "******",
		RootPassword: "******",
	})
	require.NoError(t, err)

	return httptest.NewServer(server)
}
Example #2
0
// CreateTestFeature creates a random feature to be used during testing.
func CreateTestFeature(t *testing.T) *models.Feature {
	s, err := store.NewMySQLStore(
		os.Getenv("LAIKA_MYSQL_USERNAME"),
		os.Getenv("LAIKA_MYSQL_PASSWORD"),
		os.Getenv("LAIKA_MYSQL_HOST"),
		os.Getenv("LAIKA_MYSQL_PORT"),
		os.Getenv("LAIKA_MYSQL_DBNAME"),
	)
	require.NoError(t, err)

	env, err := s.GetEnvironmentByName("test")
	if err != nil {
		env = &models.Environment{
			Name: "test",
		}
		err = s.CreateEnvironment(env)
		require.NoError(t, err)
	}

	feature := &models.Feature{
		Name: "test_feature" + store.Token(),
		Status: map[string]bool{
			"test": true,
		},
	}

	err = s.CreateFeature(feature)
	require.NoError(t, err)

	return feature
}
Example #3
0
File: main.go Project: MEDIGO/laika
func main() {
	app := cli.NewApp()
	app.Author = "MEDIGO GmbH"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   "port",
			Value:  "8000",
			Usage:  "Service port",
			EnvVar: "LAIKA_PORT",
		},
		cli.IntFlag{
			Name:   "timeout",
			Value:  10,
			Usage:  "Shutdown timeout",
			EnvVar: "LAIKA_TIMEOUT",
		},
		cli.StringFlag{
			Name:   "mysql-host",
			Value:  "mysql",
			Usage:  "MySQL host",
			EnvVar: "LAIKA_MYSQL_HOST",
		},
		cli.StringFlag{
			Name:   "mysql-port",
			Value:  "3306",
			Usage:  "MySQL port",
			EnvVar: "LAIKA_MYSQL_PORT",
		},
		cli.StringFlag{
			Name:   "mysql-username",
			Value:  "root",
			Usage:  "MySQL username",
			EnvVar: "LAIKA_MYSQL_USERNAME",
		},
		cli.StringFlag{
			Name:   "mysql-password",
			Value:  "root",
			Usage:  "MySQL password",
			EnvVar: "LAIKA_MYSQL_PASSWORD",
		},
		cli.StringFlag{
			Name:   "mysql-dbname",
			Value:  "laika",
			Usage:  "MySQL dbname",
			EnvVar: "LAIKA_MYSQL_DBNAME",
		},
		cli.StringFlag{
			Name:   "statsd-host",
			Value:  "localhost",
			Usage:  "Statsd host",
			EnvVar: "LAIKA_STATSD_HOST",
		},
		cli.StringFlag{
			Name:   "statsd-port",
			Value:  "8125",
			Usage:  "Statsd port",
			EnvVar: "LAIKA_STATSD_PORT",
		},
		cli.StringFlag{
			Name:   "root-username",
			Usage:  "Root username",
			EnvVar: "LAIKA_ROOT_USERNAME",
		},
		cli.StringFlag{
			Name:   "root-password",
			Usage:  "Root password",
			EnvVar: "LAIKA_ROOT_PASSWORD",
		},
		cli.StringFlag{
			Name:   "slack-webhook-url",
			Usage:  "Slack webhook URL",
			EnvVar: "LAIKA_SLACK_WEBHOOK_URL",
		},
	}
	app.Commands = []cli.Command{
		{
			Name:  "run",
			Usage: "Runs laika's feature flag service",
			Action: func(c *cli.Context) {
				store, err := store.NewMySQLStore(
					c.GlobalString("mysql-username"),
					c.GlobalString("mysql-password"),
					c.GlobalString("mysql-host"),
					c.GlobalString("mysql-port"),
					c.GlobalString("mysql-dbname"),
				)

				if err != nil {
					log.Fatal("Failed to create Store: ", err)
				}

				stats, err := statsd.New(c.GlobalString("statsd-host") + ":" + c.GlobalString("statsd-port"))
				if err != nil {
					log.Fatal("Failed to create Statsd client: ", err)
				}

				notifier := notifier.NewSlackNotifier(c.GlobalString("slack-webhook-url"))

				server, err := api.NewServer(api.ServerConfig{
					RootUsername: c.GlobalString("root-username"),
					RootPassword: c.GlobalString("root-password"),
					Store:        store,
					Stats:        stats,
					Notifier:     notifier,
				})
				if err != nil {
					log.Fatal("Failed to create server: ", err)

				}

				log.Info("Starting server on port ", c.GlobalString("port"))
				graceful.Run(":"+c.GlobalString("port"), time.Duration(c.Int("timeout"))*time.Second, server)
			},
		},
		{
			Name:  "migrate",
			Usage: "Migrates the store schema to the latest available version",
			Action: func(c *cli.Context) {
				store, err := store.NewMySQLStore(
					c.GlobalString("mysql-username"),
					c.GlobalString("mysql-password"),
					c.GlobalString("mysql-host"),
					c.GlobalString("mysql-port"),
					c.GlobalString("mysql-dbname"),
				)

				if err != nil {
					log.Fatal("Failed to create Store: ", err)
				}

				if err := store.Ping(); err != nil {
					log.Fatal("Failed to connect with store: ", err)
				}

				if err := store.Migrate(); err != nil {
					log.Fatal("Failed to migrate store schema: ", err)
				}

			},
		},
	}

	app.Run(os.Args)
}