Skip to content

Commit

Permalink
feat(storage): link storage api version with remote
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Oct 9, 2023
1 parent 27570cf commit dbcace8
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 8 deletions.
19 changes: 18 additions & 1 deletion internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func PostRun(projectRef string, stdout io.Writer, fsys afero.Fs) error {
func linkServices(ctx context.Context, projectRef string, fsys afero.Fs) {
// Ignore non-fatal errors linking services
var wg sync.WaitGroup
wg.Add(5)
wg.Add(6)
go func() {
defer wg.Done()
if err := linkDatabaseVersion(ctx, projectRef, fsys); err != nil {
Expand All @@ -112,6 +112,12 @@ func linkServices(ctx context.Context, projectRef string, fsys afero.Fs) {
fmt.Fprintln(os.Stderr, err)
}
}()
go func() {
defer wg.Done()
if err := linkStorageVersion(ctx, projectRef, fsys); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()
go func() {
defer wg.Done()
if err := linkPooler(ctx, projectRef); err != nil {
Expand Down Expand Up @@ -180,6 +186,17 @@ func linkGotrueVersion(ctx context.Context, projectRef string, fsys afero.Fs) er
return afero.WriteFile(fsys, utils.GotrueVersionPath, []byte(version), 0644)
}

func linkStorageVersion(ctx context.Context, projectRef string, fsys afero.Fs) error {
version, err := tenant.GetStorageVersion(ctx, projectRef)
if err != nil {
return err
}
if err := utils.MkdirIfNotExistFS(fsys, filepath.Dir(utils.StorageVersionPath)); err != nil {
return err
}
return afero.WriteFile(fsys, utils.StorageVersionPath, []byte(version), 0644)
}

func linkDatabase(ctx context.Context, config pgconn.Config, options ...func(*pgx.ConnConfig)) error {
conn, err := utils.ConnectRemotePostgres(ctx, config, options...)
if err != nil {
Expand Down
16 changes: 13 additions & 3 deletions internal/link/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,20 @@ func TestLinkCommand(t *testing.T) {
Reply(200).
JSON(api.V1PgbouncerConfigResponse{})
// Link versions
auth := tenant.HealthResponse{Version: "v2.74.2"}
gock.New("https://" + utils.GetSupabaseHost(project)).
Get("/auth/v1/health").
Reply(200).
JSON(auth)
rest := tenant.SwaggerResponse{Info: tenant.SwaggerInfo{Version: "11.1.0"}}
gock.New("https://" + utils.GetSupabaseHost(project)).
Get("/rest/v1/").
Reply(200).
JSON(rest)
auth := tenant.HealthResponse{Version: "v2.74.2"}
gock.New("https://" + utils.GetSupabaseHost(project)).
Get("/auth/v1/health").
Get("/storage/v1/version").
Reply(200).
JSON(auth)
BodyString("0.40.4")
postgres := api.DatabaseResponse{
Host: utils.GetSupabaseDbHost(project),
Version: "15.1.0.117",
Expand Down Expand Up @@ -233,6 +237,9 @@ func TestLinkCommand(t *testing.T) {
gock.New("https://" + utils.GetSupabaseHost(project)).
Get("/rest/v1/").
ReplyError(errors.New("network error"))
gock.New("https://" + utils.GetSupabaseHost(project)).
Get("/storage/v1/version").
ReplyError(errors.New("network error"))
gock.New(utils.DefaultApiHost).
Get("/v1/projects").
ReplyError(errors.New("network error"))
Expand Down Expand Up @@ -271,6 +278,9 @@ func TestLinkCommand(t *testing.T) {
gock.New("https://" + utils.GetSupabaseHost(project)).
Get("/rest/v1/").
ReplyError(errors.New("network error"))
gock.New("https://" + utils.GetSupabaseHost(project)).
Get("/storage/v1/version").
ReplyError(errors.New("network error"))
gock.New(utils.DefaultApiHost).
Get("/v1/projects").
ReplyError(errors.New("network error"))
Expand Down
8 changes: 7 additions & 1 deletion internal/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Run(ctx context.Context, fsys afero.Fs) error {
linked := make(map[string]string)
if projectRef, err := utils.LoadProjectRef(fsys); err == nil {
var wg sync.WaitGroup
wg.Add(3)
wg.Add(4)
go func() {
defer wg.Done()
if version, err := GetDatabaseVersion(ctx, projectRef); err == nil {
Expand All @@ -56,6 +56,12 @@ func Run(ctx context.Context, fsys afero.Fs) error {
linked[utils.Config.Api.Image] = version
}
}()
go func() {
defer wg.Done()
if version, err := tenant.GetStorageVersion(ctx, projectRef); err == nil {
linked[utils.Config.Storage.Image] = version
}
}()
wg.Wait()
}

Expand Down
11 changes: 11 additions & 0 deletions internal/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ var Config = config{
Realtime: realtime{
IpVersion: AddressIPv6,
},
Storage: storage{
Image: StorageImage,
},
Auth: auth{
Image: GotrueImage,
Email: email{
Expand Down Expand Up @@ -240,6 +243,7 @@ type (

storage struct {
Enabled bool `toml:"enabled"`
Image string `toml:"-"`
FileSizeLimit sizeInBytes `toml:"file_size_limit"`
}

Expand Down Expand Up @@ -440,6 +444,13 @@ func LoadConfigFS(fsys afero.Fs) error {
return fmt.Errorf("Invalid config for realtime.ip_version. Must be one of: %v", allowed)
}
}
// Validate storage config
if Config.Storage.Enabled {
if version, err := afero.ReadFile(fsys, StorageVersionPath); err == nil && len(version) > 0 && Config.Db.MajorVersion > 14 {
index := strings.IndexByte(StorageImage, ':')
Config.Storage.Image = StorageImage[:index+1] + string(version)
}
}
// Validate studio config
if Config.Studio.Enabled {
if Config.Studio.Port == 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ var (
PostgresVersionPath = filepath.Join(SupabaseDirPath, TempDir, "postgres-version")
GotrueVersionPath = filepath.Join(SupabaseDirPath, TempDir, "gotrue-version")
RestVersionPath = filepath.Join(SupabaseDirPath, TempDir, "rest-version")
StorageVersionPath = filepath.Join(SupabaseDirPath, TempDir, "storage-version")
CurrBranchPath = filepath.Join(SupabaseDirPath, ".branches", "_current_branch")
MigrationsDir = filepath.Join(SupabaseDirPath, "migrations")
FunctionsDir = filepath.Join(SupabaseDirPath, "functions")
Expand Down
30 changes: 27 additions & 3 deletions internal/utils/tenant/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func GetJsonResponse[T any](ctx context.Context, url, apiKey string) (*T, error)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil || len(body) == 0 {
body = []byte(fmt.Sprintf("Error status %d", resp.StatusCode))
if err != nil {
return nil, err
}
return nil, errors.New(string(body))
return nil, fmt.Errorf("Error status %d: %s", resp.StatusCode, body)
}
// Parses response
var data T
Expand All @@ -85,3 +85,27 @@ func GetJsonResponse[T any](ctx context.Context, url, apiKey string) (*T, error)
}
return &data, nil
}

func GetTextResponse(ctx context.Context, url, apiKey string) (string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return "", err
}
if len(apiKey) > 0 {
req.Header.Add("apikey", apiKey)
}
// Sends request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Error status %d: %s", resp.StatusCode, body)
}
return string(body), nil
}
27 changes: 27 additions & 0 deletions internal/utils/tenant/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tenant

import (
"context"
"errors"
"fmt"

"github.com/supabase/cli/internal/utils"
)

var errStorageVersion = errors.New("Storage version not found.")

func GetStorageVersion(ctx context.Context, projectRef string) (string, error) {
apiKey, err := GetApiKeys(ctx, projectRef)
if err != nil {
return "", err
}
url := fmt.Sprintf("https://%s/storage/v1/version", utils.GetSupabaseHost(projectRef))
data, err := GetTextResponse(ctx, url, apiKey.Anon)
if err != nil {
return "", err
}
if len(data) == 0 || data == "0.0.0" {
return "", errStorageVersion
}
return "v" + data, nil
}

0 comments on commit dbcace8

Please sign in to comment.