Skip to content

Commit

Permalink
feat(cdsctl): cdsctl admin database for cdn service (#5930)
Browse files Browse the repository at this point in the history
* feat: cdsctl admin database for cdn service

$ cdsctl admin database unlock api id-to-unlock
$ cdsctl admin database unlock cdn id-to-unlock
$ cdsctl admin database delete api id-migration-to-delete
$ cdsctl admin database delete cdn id-migration-to-delete
$ cdsctl admin database list api
$ cdsctl admin database list cdn

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Sep 15, 2021
1 parent a08cd7f commit 5e6911e
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 98 deletions.
44 changes: 38 additions & 6 deletions cli/cdsctl/admin_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,66 @@ func adminDatabase() *cobra.Command {
var adminDatabaseUnlockCmd = cli.Command{
Name: "unlock",
Short: "Unlock a pending migration (Use with caution)",
Example: `
$ cdsctl admin database unlock api id-to-unlock
$ cdsctl admin database unlock cdn id-to-unlock
`,
Args: []cli.Arg{
{
Name: argServiceName,
IsValid: func(s string) bool {
return s == sdk.TypeCDN || s == sdk.TypeAPI
},
},
{Name: "id"},
},
}

func adminDatabaseUnlockFunc(v cli.Values) error {
return client.AdminDatabaseMigrationUnlock(v.GetString("id"))
func adminDatabaseUnlockFunc(args cli.Values) error {
return client.AdminDatabaseMigrationUnlock(args.GetString(argServiceName), args.GetString("id"))
}

var adminDatabaseDeleteMigrationCmd = cli.Command{
Name: "delete",
Short: "Delete a database migration from table gorp_migration (use with caution)",
Example: `
$ cdsctl admin database delete api id-migration-to-delete
$ cdsctl admin database delete cdn id-migration-to-delete
`,
Args: []cli.Arg{
{
Name: argServiceName,
IsValid: func(s string) bool {
return s == sdk.TypeCDN || s == sdk.TypeAPI
},
},
{Name: "id"},
},
}

func adminDatabaseDeleteFunc(v cli.Values) error {
return client.AdminDatabaseMigrationDelete(v.GetString("id"))
func adminDatabaseDeleteFunc(args cli.Values) error {
return client.AdminDatabaseMigrationDelete(args.GetString(argServiceName), args.GetString("id"))
}

var adminDatabaseMigrationsList = cli.Command{
Name: "list",
Short: "List all CDS DB migrations",
Example: `
$ cdsctl admin database list api
$ cdsctl admin database list cdn
`,
Args: []cli.Arg{
{
Name: argServiceName,
IsValid: func(s string) bool {
return s == sdk.TypeCDN || s == sdk.TypeAPI
},
},
},
}

func adminDatabaseMigrationsListFunc(_ cli.Values) (cli.ListResult, error) {
migrations, err := client.AdminDatabaseMigrationsList()
func adminDatabaseMigrationsListFunc(args cli.Values) (cli.ListResult, error) {
migrations, err := client.AdminDatabaseMigrationsList(args.GetString(argServiceName))
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ services:
cdn.storageUnits.buffers.redis.redis.password=cds \
cdn.storageUnits.buffers.local-buffer.local.path=/app/cdn-buffer \
cdn.storageUnits.storages.local.local.path=/app/cdn-storage \
cdn.storageUnits.storages.cds.cds.host=http://cds-api:8081 \
cdn.database.host=cds-db \
cdn.publicTCP=${HOSTNAME}:8090 \
cdn.api.http.url=http://cds-api:8081 \
cdn.url=http://cds-cdn:8089 \
cdn.URL=http://cds-cdn:8089 \
hatchery.local.commonConfiguration.url=http://cds-hatchery-local:8086 \
hatchery.local.commonConfiguration.api.http.url=http://cds-api:8081 \
hatchery.local.basedir=/app/hatchery-local \
Expand Down
12 changes: 12 additions & 0 deletions engine/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ func putPostAdminServiceCallHandler(api *API, method string) service.Handler {
}
}

func (api *API) deleteDatabaseMigrationHandler() service.Handler {
return database.AdminDeleteDatabaseMigration(api.mustDB)
}

func (api *API) postDatabaseMigrationUnlockedHandler() service.Handler {
return database.AdminDatabaseMigrationUnlocked(api.mustDB)
}

func (api *API) getDatabaseMigrationHandler() service.Handler {
return database.AdminGetDatabaseMigration(api.mustDB)
}

func (api *API) getAdminDatabaseSignatureResume() service.Handler {
return database.AdminDatabaseSignatureResume(api.mustDB, gorpmapping.Mapper)
}
Expand Down
49 changes: 0 additions & 49 deletions engine/api/database.go

This file was deleted.

12 changes: 12 additions & 0 deletions engine/cdn/cdn_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import (
"github.com/ovh/cds/engine/service"
)

func (s *Service) deleteDatabaseMigrationHandler() service.Handler {
return database.AdminDeleteDatabaseMigration(s.mustDB)
}

func (s *Service) postDatabaseMigrationUnlockedHandler() service.Handler {
return database.AdminDatabaseMigrationUnlocked(s.mustDB)
}

func (s *Service) getDatabaseMigrationHandler() service.Handler {
return database.AdminGetDatabaseMigration(s.mustDB)
}

func (s *Service) getAdminDatabaseSignatureResume() service.Handler {
return database.AdminDatabaseSignatureResume(s.mustDB, s.Mapper)
}
Expand Down
4 changes: 4 additions & 0 deletions engine/cdn/cdn_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (s *Service) initRouter(ctx context.Context) {

r.Handle("/size/item/project/{projectKey}", nil, r.GET(s.getSizeByProjectHandler))

r.Handle("/admin/database/migration/delete/{id}", nil, r.DELETE(s.deleteDatabaseMigrationHandler))
r.Handle("/admin/database/migration/unlock/{id}", nil, r.POST(s.postDatabaseMigrationUnlockedHandler))
r.Handle("/admin/database/migration", nil, r.GET(s.getDatabaseMigrationHandler))

r.Handle("/admin/database/signature", nil, r.GET(s.getAdminDatabaseSignatureResume))
r.Handle("/admin/database/signature/{entity}/roll/{pk}", nil, r.POST(s.postAdminDatabaseSignatureRollEntityByPrimaryKey))
r.Handle("/admin/database/signature/{entity}/{signer}", nil, r.GET(s.getAdminDatabaseSignatureTuplesBySigner))
Expand Down
2 changes: 1 addition & 1 deletion engine/cmd_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
migrate "github.com/rubenv/sql-migrate"
"github.com/spf13/cobra"

"github.com/ovh/cds/engine/api/database/dbmigrate"
"github.com/ovh/cds/engine/database"
"github.com/ovh/cds/engine/database/dbmigrate"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,50 @@ import (
"github.com/go-gorp/gorp"
"github.com/gorilla/mux"

"github.com/ovh/cds/engine/database/dbmigrate"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
)

type DBFunc func() *gorp.DbMap

func AdminDeleteDatabaseMigration(db DBFunc) service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
id := vars["id"]

if len(id) == 0 {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "Id is mandatory. Check id from table gorp_migrations")
}

return dbmigrate.DeleteMigrate(db().Db, id)
}
}

func AdminDatabaseMigrationUnlocked(db DBFunc) service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
id := vars["id"]

if len(id) == 0 {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "Id is mandatory. Check id from table gorp_migrations_lock")
}

return dbmigrate.UnlockMigrate(db().Db, id, gorp.PostgresDialect{})
}
}

func AdminGetDatabaseMigration(db DBFunc) service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
a, err := dbmigrate.List(db().Db)
if err != nil {
return sdk.WrapError(err, "Cannot load database migration list %d", err)
}
return service.WriteJSON(w, a, http.StatusOK)
}
}

func AdminDatabaseSignatureResume(db DBFunc, mapper *gorpmapper.Mapper) service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
var entities = mapper.ListSignedEntities()
Expand Down
2 changes: 1 addition & 1 deletion engine/migrateservice/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/rockbears/log"
migrate "github.com/rubenv/sql-migrate"

"github.com/ovh/cds/engine/api/database/dbmigrate"
"github.com/ovh/cds/engine/database"
"github.com/ovh/cds/engine/database/dbmigrate"
"github.com/ovh/cds/sdk"
)

Expand Down
21 changes: 12 additions & 9 deletions sdk/cdsclient/client_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ import (
"github.com/ovh/cds/sdk"
)

func (c *client) AdminDatabaseMigrationDelete(id string) error {
_, _, _, err := c.Request(context.Background(), "DELETE", "/admin/database/migration/delete/"+url.QueryEscape(id), nil)
func (c *client) AdminDatabaseMigrationDelete(service string, id string) error {
path := fmt.Sprintf("/admin/database/migration/delete/%s", url.QueryEscape(id))
var f = c.switchServiceCallFunc(service, http.MethodDelete, path, nil, nil)
_, err := f()
return err
}

func (c *client) AdminDatabaseMigrationsList() ([]sdk.DatabaseMigrationStatus, error) {
func (c *client) AdminDatabaseMigrationsList(service string) ([]sdk.DatabaseMigrationStatus, error) {
dlist := []sdk.DatabaseMigrationStatus{}
if _, err := c.GetJSON(context.Background(), "/admin/database/migration", &dlist); err != nil {
return nil, err
}
return dlist, nil
var f = c.switchServiceCallFunc(service, http.MethodGet, "/admin/database/migration", nil, &dlist)
_, err := f()
return dlist, err
}

func (c *client) AdminDatabaseMigrationUnlock(id string) error {
_, _, _, err := c.Request(context.Background(), "POST", "/admin/database/migration/unlock/"+url.QueryEscape(id), nil)
func (c *client) AdminDatabaseMigrationUnlock(service string, id string) error {
path := fmt.Sprintf("/admin/database/migration/unlock/%s", url.QueryEscape(id))
var f = c.switchServiceCallFunc(service, http.MethodPost, path, nil, nil)
_, err := f()
return err
}

Expand Down
6 changes: 3 additions & 3 deletions sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ type TemplateClient interface {

// Admin expose all function to CDS administration
type Admin interface {
AdminDatabaseMigrationDelete(id string) error
AdminDatabaseMigrationUnlock(id string) error
AdminDatabaseMigrationsList() ([]sdk.DatabaseMigrationStatus, error)
AdminDatabaseMigrationDelete(service string, id string) error
AdminDatabaseMigrationUnlock(service string, id string) error
AdminDatabaseMigrationsList(service string) ([]sdk.DatabaseMigrationStatus, error)
AdminDatabaseSignaturesResume(service string) (sdk.CanonicalFormUsageResume, error)
AdminDatabaseSignaturesRollEntity(service string, e string) error
AdminDatabaseSignaturesRollAllEntities(service string) error
Expand Down
Loading

0 comments on commit 5e6911e

Please sign in to comment.