-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
232 lines (197 loc) · 6.77 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package install
import (
"encoding/json"
"errors"
"os"
"strconv"
"github.com/microsoft/tyger/cli/internal/install"
"github.com/microsoft/tyger/cli/internal/install/dockerinstall"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
func NewApiCommand(parentCommand *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "api",
Short: "Manage the tyger API",
Long: "Manage the tyger API",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
return errors.New("a command is required")
},
}
cmd.AddCommand(newApiInstallCommand())
cmd.AddCommand(newApiUninstallCommand())
cmd.AddCommand(NewMigrationsCommand())
cmd.AddCommand(NewGenerateSingingKeyCommand())
return cmd
}
func newApiInstallCommand() *cobra.Command {
flags := commonFlags{}
cmd := cobra.Command{
Use: "install -f CONFIG.yml",
Short: "Install the Typer API",
Long: "Install the Typer API",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
ctx, installer := commonPrerun(cmd.Context(), &flags)
log.Info().Msg("Starting Tyger API install")
err := installer.InstallTyger(ctx)
if err != nil {
if err != install.ErrAlreadyLoggedError {
log.Fatal().Err(err).Send()
}
os.Exit(1)
}
log.Info().Msg("Install complete")
},
}
addCommonFlags(&cmd, &flags)
return &cmd
}
func newApiUninstallCommand() *cobra.Command {
flags := commonFlags{}
deleteData := false
cmd := cobra.Command{
Use: "uninstall -f CONFIG.yml",
Short: "Uninstall the Typer API",
Long: "Uninstall the Typer API",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
ctx, installer := commonPrerun(cmd.Context(), &flags)
log.Info().Msg("Starting Tyger API uninstall")
if err := installer.UninstallTyger(ctx, deleteData); err != nil {
if err != install.ErrAlreadyLoggedError {
log.Fatal().Err(err).Send()
}
os.Exit(1)
}
log.Info().Msg("Uninstall complete")
},
}
addCommonFlags(&cmd, &flags)
cmd.Flags().BoolVar(&deleteData, "delete-data", deleteData, "Permanently delete data (Docker only)")
return &cmd
}
func NewMigrationsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "migration",
Aliases: []string{"migrations"},
Short: "Manage the tyger API database",
Long: "Manage the tyger API database",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
return errors.New("a command is required")
},
}
cmd.AddCommand(NewMigrationsListCommand())
cmd.AddCommand(NewMigrationApplyCommand())
cmd.AddCommand(NewMigrationLogsCommand())
return cmd
}
func NewMigrationApplyCommand() *cobra.Command {
flags := commonFlags{}
targetVersion := 0
latest := false
wait := false
offline := false
cmd := &cobra.Command{
Use: "apply -f CONFIG.yml",
Short: "Apply tyger database migrations",
Long: "Apply tyger database migrations",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if !latest && targetVersion == 0 {
log.Fatal().Msg("Either --latest or --target-version must be specified")
}
if latest && targetVersion != 0 {
log.Fatal().Msg("Only one of --latest or --target-version can be specified")
}
ctx, installer := commonPrerun(cmd.Context(), &flags)
if err := installer.ApplyMigrations(ctx, targetVersion, latest, offline, wait); err != nil {
log.Fatal().Err(err).Send()
}
},
}
cmd.Flags().IntVar(&targetVersion, "target-version", targetVersion, "The target version to migrate to")
cmd.Flags().BoolVar(&latest, "latest", latest, "Migrate to the latest version")
cmd.Flags().BoolVar(&wait, "wait", wait, "Wait for the migration to complete")
cmd.Flags().BoolVar(&offline, "offline", offline, "Do not coordinate with replicas to ensure uninterrupted service")
addCommonFlags(cmd, &flags)
return cmd
}
func NewMigrationLogsCommand() *cobra.Command {
flags := commonFlags{}
cmd := &cobra.Command{
Use: "logs ID -f CONFIG.yml",
Short: "Get the logs of a database migration",
Long: "Get the logs of a database migration",
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx, installer := commonPrerun(cmd.Context(), &flags)
id, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal().Msg("The ID argument must be an integer")
}
if err := installer.GetMigrationLogs(ctx, id, os.Stdout); err != nil {
log.Fatal().Err(err).Send()
}
},
}
addCommonFlags(cmd, &flags)
return cmd
}
func NewMigrationsListCommand() *cobra.Command {
flags := commonFlags{}
all := false
cmd := &cobra.Command{
Use: "list -f CONFIG.yml",
Short: "List the tyger API database migrations",
Long: "List the tyger API database migrations",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
ctx, installer := commonPrerun(cmd.Context(), &flags)
versions, err := installer.ListDatabaseVersions(ctx, all)
if err != nil {
log.Fatal().Err(err).Msg("Failed list database versions")
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(versions); err != nil {
log.Fatal().Err(err).Send()
}
},
}
addCommonFlags(cmd, &flags)
cmd.Flags().BoolVar(&all, "all", all, "Show all versions, including those that have been applied")
return cmd
}
func NewGenerateSingingKeyCommand() *cobra.Command {
publicFile := ""
privateFile := ""
cmd := &cobra.Command{
Use: "generate-signing-key --public FILE.pem --private FILE.pem",
Short: "Generate a new signing key pair",
Long: "Generate a new signing key pair",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if err := dockerinstall.GenerateSigningKeyPair(publicFile, privateFile); err != nil {
log.Fatal().Err(err).Send()
}
},
}
cmd.Flags().StringVar(&publicFile, "public", publicFile, "The file to write the public key to")
cmd.MarkFlagRequired("public")
cmd.Flags().StringVar(&privateFile, "private", privateFile, "The file to write the private key to")
cmd.MarkFlagRequired("private")
return cmd
}