-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathdeploy.go
More file actions
218 lines (211 loc) · 7.5 KB
/
deploy.go
File metadata and controls
218 lines (211 loc) · 7.5 KB
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
package deploy
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/functions/delete"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
"github.com/supabase/cli/pkg/config"
"github.com/supabase/cli/pkg/function"
)
func Run(ctx context.Context, slugs []string, useDocker bool, noVerifyJWT *bool, importMapPath string, maxJobs uint, prune bool, fsys afero.Fs) error {
// Load function config and project id
if err := flags.LoadConfig(fsys); err != nil {
return err
} else if len(slugs) > 0 {
slugs = utils.RemoveDuplicates(slugs)
for _, s := range slugs {
if err := utils.ValidateFunctionSlug(s); err != nil {
return err
}
}
} else if slugs, err = GetFunctionSlugs(fsys); err != nil {
return err
}
// TODO: require all functions to be deployed from config for v2
if len(slugs) == 0 {
return errors.Errorf("No Functions specified or found in %s", utils.Bold(utils.FunctionsDir))
}
// Flag import map is specified relative to current directory instead of workdir
cwd, err := os.Getwd()
if err != nil {
return errors.Errorf("failed to get working directory: %w", err)
}
if len(importMapPath) > 0 {
if !filepath.IsAbs(importMapPath) {
importMapPath = filepath.Join(utils.CurrentDirAbs, importMapPath)
}
if importMapPath, err = filepath.Rel(cwd, importMapPath); err != nil {
return errors.Errorf("failed to resolve relative path: %w", err)
}
}
functionConfig, err := GetFunctionConfig(slugs, importMapPath, noVerifyJWT, fsys)
if err != nil {
return err
}
// Deploy new and updated functions
opt := function.WithMaxJobs(maxJobs)
if useDocker {
if utils.IsDockerRunning(ctx) {
opt = function.WithBundler(NewDockerBundler(fsys))
} else {
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "Docker is not running")
}
}
api := function.NewEdgeRuntimeAPI(flags.ProjectRef, *utils.GetSupabase(), opt)
if err := api.Deploy(ctx, functionConfig, afero.NewIOFS(fsys)); errors.Is(err, function.ErrNoDeploy) {
fmt.Fprintln(os.Stderr, err)
return nil
} else if err != nil {
return err
}
deployed := make([]string, 0, len(slugs))
for _, slug := range slugs {
if functionConfig[slug].Enabled {
deployed = append(deployed, slug)
}
}
fmt.Printf("Deployed Functions on project %s: %s\n", utils.Aqua(flags.ProjectRef), strings.Join(deployed, ", "))
url := fmt.Sprintf("%s/project/%v/functions", utils.GetSupabaseDashboardURL(), flags.ProjectRef)
fmt.Println("You can inspect your deployment in the Dashboard: " + url)
if !prune {
return nil
}
return pruneFunctions(ctx, functionConfig)
}
func GetFunctionSlugs(fsys afero.Fs) (slugs []string, err error) {
pattern := filepath.Join(utils.FunctionsDir, "*", "index.ts")
paths, err := afero.Glob(fsys, pattern)
if err != nil {
return nil, errors.Errorf("failed to glob function slugs: %w", err)
}
for _, path := range paths {
slug := filepath.Base(filepath.Dir(path))
if utils.FuncSlugPattern.MatchString(slug) {
slugs = append(slugs, slug)
}
}
// Add all function slugs declared in config file
for slug := range utils.Config.Functions {
slugs = append(slugs, slug)
}
return utils.RemoveDuplicates(slugs), nil
}
func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, fsys afero.Fs) (config.FunctionConfig, error) {
// Although some functions do not require import map, it's more convenient to setup
// vscode deno extension with a single import map for all functions.
fallbackExists := true
functionsUsingDeprecatedGlobalFallback := []string{}
functionsUsingDeprecatedImportMap := []string{}
if _, err := fsys.Stat(utils.FallbackImportMapPath); errors.Is(err, os.ErrNotExist) {
fallbackExists = false
} else if err != nil {
return nil, errors.Errorf("failed to fallback import map: %w", err)
}
functionConfig := make(config.FunctionConfig, len(slugs))
for _, name := range slugs {
function, ok := utils.Config.Functions[name]
if !ok {
function.Enabled = false
function.VerifyJWT = true
}
// Precedence order: flag > config > fallback
functionDir := filepath.Join(utils.FunctionsDir, name)
if len(function.Entrypoint) == 0 {
function.Entrypoint = filepath.Join(functionDir, "index.ts")
}
if len(importMapPath) > 0 {
function.ImportMap = importMapPath
} else if len(function.ImportMap) == 0 {
denoJsonPath := filepath.Join(functionDir, "deno.json")
denoJsoncPath := filepath.Join(functionDir, "deno.jsonc")
importMapPath := filepath.Join(functionDir, "import_map.json")
if _, err := fsys.Stat(denoJsonPath); err == nil {
function.ImportMap = denoJsonPath
} else if _, err := fsys.Stat(denoJsoncPath); err == nil {
function.ImportMap = denoJsoncPath
} else if _, err := fsys.Stat(importMapPath); err == nil {
function.ImportMap = importMapPath
functionsUsingDeprecatedImportMap = append(functionsUsingDeprecatedImportMap, name)
} else if fallbackExists {
function.ImportMap = utils.FallbackImportMapPath
functionsUsingDeprecatedGlobalFallback = append(functionsUsingDeprecatedGlobalFallback, name)
}
}
if noVerifyJWT != nil {
function.VerifyJWT = !*noVerifyJWT
}
functionConfig[name] = function
}
if len(functionsUsingDeprecatedImportMap) > 0 {
fmt.Fprintln(os.Stderr,
utils.Yellow("WARNING:"),
"Functions using deprecated import_map.json (please migrate to deno.json):",
utils.Aqua(strings.Join(functionsUsingDeprecatedImportMap, ", ")),
)
}
if len(functionsUsingDeprecatedGlobalFallback) > 0 {
fmt.Fprintln(os.Stderr,
utils.Yellow("WARNING:"),
"Functions using fallback import map:",
utils.Aqua(strings.Join(functionsUsingDeprecatedGlobalFallback, ", ")),
)
fmt.Fprintln(os.Stderr,
"Please use recommended per function dependency declaration ",
utils.Aqua("https://supabase.com/docs/guides/functions/import-maps"),
)
}
return functionConfig, nil
}
// pruneFunctions deletes functions that exist remotely but not locally
func pruneFunctions(ctx context.Context, functionConfig config.FunctionConfig) error {
resp, err := utils.GetSupabase().V1ListAllFunctionsWithResponse(ctx, flags.ProjectRef)
if err != nil {
return errors.Errorf("failed to list functions: %w", err)
} else if resp.JSON200 == nil {
return errors.Errorf("unexpected list functions status %d: %s", resp.StatusCode(), string(resp.Body))
}
// No need to delete disabled functions
var toDelete []string
for _, deployed := range *resp.JSON200 {
if deployed.Status == api.FunctionResponseStatusREMOVED {
continue
} else if _, exists := functionConfig[deployed.Slug]; exists {
continue
}
toDelete = append(toDelete, deployed.Slug)
}
if len(toDelete) == 0 {
fmt.Fprintln(os.Stderr, "No Functions to prune.")
return nil
}
// Confirm before pruning functions
msg := fmt.Sprintln(confirmPruneAll(toDelete))
if shouldDelete, err := utils.NewConsole().PromptYesNo(ctx, msg, false); err != nil {
return err
} else if !shouldDelete {
return errors.New(context.Canceled)
}
for _, slug := range toDelete {
fmt.Fprintln(os.Stderr, "Deleting Function:", slug)
if err := delete.Undeploy(ctx, flags.ProjectRef, slug); errors.Is(err, delete.ErrNoDelete) {
fmt.Fprintln(utils.GetDebugLogger(), err)
} else if err != nil {
return err
}
}
return nil
}
func confirmPruneAll(pending []string) string {
msg := fmt.Sprintln("Do you want to delete the following Functions from your project?")
for _, slug := range pending {
msg += fmt.Sprintf(" • %s\n", utils.Bold(slug))
}
return msg
}