Skip to content

Commit 4cf072f

Browse files
authored
Merge pull request #66 from checkr/saso/cf-mongo-migrate
Mongo migrations
2 parents 0cb6055 + fba876b commit 4cf072f

File tree

12 files changed

+1250
-33
lines changed

12 files changed

+1250
-33
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.PHONY: up server dashboard build
22

33
up:
4+
docker-compose run server /bin/sh -c 'cd server/ && go run main.go --config ./configs/codeflow.dev.yml migrate up'
45
docker-compose up -d redis mongo
56
docker-compose up server dashboard
67

README.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@ Install [Docker Compose](https://docs.docker.com/compose/install/)
77

88
Copy `server/codeflow.yml` to `server/codeflow.dev.yml`
99

10-
You should set `jwt_secret_key` and `okta_org` before starting the server!
11-
```yaml
12-
codeflow:
13-
jwt_secret_key:
14-
allowed_origins:
15-
auth:
16-
okta_org:
17-
```
18-
1910
To start the server and all dependencies run:
2011
```
21-
$ make
12+
$ make up
2213
```
14+
Check `Makefile` to see what's happening under the hood.
2315

2416
Local dashboard will be started on [http://localhost:3000](http://localhost:3000)
2517

server/configs/codeflow.yml

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ plugins:
6161
docker_host: "unix:///var/run/docker.sock"
6262
kubedeploy:
6363
workers: 1
64+
kubeconfig: "/etc/secrets/kubeconfig"
65+
environment:
66+
ssl_cert_arn:
67+
node_selector:
68+
access_log_s3_bucket:
6469
websockets:
6570
workers: 1
6671
service_address: ":3003"

server/glide.lock

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/main.go

+81
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import (
99
"syscall"
1010

1111
"github.com/checkr/codeflow/server/agent"
12+
"github.com/mattes/migrate/file"
13+
"github.com/mattes/migrate/migrate"
14+
"github.com/mattes/migrate/migrate/direction"
1215
"github.com/spf13/cobra"
1316
"github.com/spf13/viper"
1417

1518
_ "github.com/checkr/codeflow/server/plugins"
1619
_ "github.com/checkr/codeflow/server/plugins/codeflow"
20+
_ "github.com/checkr/codeflow/server/plugins/codeflow/migrations"
1721
_ "github.com/checkr/codeflow/server/plugins/docker_build"
1822
_ "github.com/checkr/codeflow/server/plugins/heartbeat"
1923
_ "github.com/checkr/codeflow/server/plugins/kubedeploy"
@@ -47,6 +51,9 @@ func init() {
4751
cobra.OnInitialize(initConfig)
4852
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./configs/.codeflow.yml)")
4953
RootCmd.AddCommand(cmdServer)
54+
RootCmd.AddCommand(cmdMigrate)
55+
cmdMigrate.AddCommand(cmdMigrateUp)
56+
cmdMigrate.AddCommand(cmdMigrateDown)
5057

5158
cmdServer.Flags().StringSliceP("run", "r", []string{}, "run plugins a,b,c")
5259
viper.BindPFlags(cmdServer.Flags())
@@ -99,3 +106,77 @@ var cmdServer = &cobra.Command{
99106
ag.Run()
100107
},
101108
}
109+
110+
var cmdMigrate = &cobra.Command{
111+
Use: "migrate [command]",
112+
Long: `...`,
113+
Run: func(cmd *cobra.Command, args []string) {
114+
115+
},
116+
}
117+
var cmdMigrateUp = &cobra.Command{
118+
Use: "up [command]",
119+
Long: `...`,
120+
Run: func(cmd *cobra.Command, args []string) {
121+
pipe := migrate.NewPipe()
122+
go migrate.Up(pipe, viper.GetString("plugins.codeflow.mongodb.uri"), "./plugins/codeflow/migrations")
123+
ok := writePipe(pipe)
124+
if !ok {
125+
os.Exit(1)
126+
}
127+
},
128+
}
129+
130+
var cmdMigrateDown = &cobra.Command{
131+
Use: "down [command]",
132+
Long: `...`,
133+
Run: func(cmd *cobra.Command, args []string) {
134+
if viper.GetString("environment") != "development" {
135+
panic("You can only use migrate down in development environment")
136+
}
137+
138+
pipe := migrate.NewPipe()
139+
go migrate.Down(pipe, viper.GetString("plugins.codeflow.mongodb.uri"), "./plugins/codeflow/migrations")
140+
ok := writePipe(pipe)
141+
if !ok {
142+
os.Exit(1)
143+
}
144+
},
145+
}
146+
147+
func writePipe(pipe chan interface{}) (ok bool) {
148+
okFlag := true
149+
if pipe != nil {
150+
for {
151+
select {
152+
case item, more := <-pipe:
153+
if !more {
154+
return okFlag
155+
}
156+
switch item.(type) {
157+
158+
case string:
159+
fmt.Println(item.(string))
160+
161+
case error:
162+
fmt.Printf("%s\n\n", item.(error).Error())
163+
okFlag = false
164+
165+
case file.File:
166+
f := item.(file.File)
167+
if f.Direction == direction.Up {
168+
fmt.Print(">")
169+
} else if f.Direction == direction.Down {
170+
fmt.Print("<")
171+
}
172+
fmt.Printf(" %s\n", f.FileName)
173+
174+
default:
175+
text := fmt.Sprint(item)
176+
fmt.Println(text)
177+
}
178+
}
179+
}
180+
}
181+
return okFlag
182+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
V001_init_users_down
2+
V001_init_projects_down
3+
V001_init_bookmarks_down
4+
V001_init_builds_down
5+
V001_init_extensions_down
6+
V001_init_features_down
7+
V001_init_releases_down
8+
V001_init_secrets_down
9+
V001_init_service_specs_down
10+
V001_init_services_down
11+
V001_init_workflows_down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
V001_init_users_up
2+
V001_init_projects_up
3+
V001_init_bookmarks_up
4+
V001_init_builds_up
5+
V001_init_extensions_up
6+
V001_init_features_up
7+
V001_init_releases_up
8+
V001_init_secrets_up
9+
V001_init_service_specs_up
10+
V001_init_services_up
11+
V001_init_workflows_up

0 commit comments

Comments
 (0)