Skip to content

Commit f86152e

Browse files
authored
fix: fallback to deploy with api when docker not found (#3699)
* fix: fallback to deploy with api when docker not found * chore: update api spec * chore: update unit tests
1 parent 55e2c45 commit f86152e

File tree

5 files changed

+31
-611
lines changed

5 files changed

+31
-611
lines changed

internal/functions/deploy/deploy.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func Run(ctx context.Context, slugs []string, useDocker bool, noVerifyJWT *bool,
5151
}
5252
opt := function.WithMaxJobs(maxJobs)
5353
if useDocker {
54-
opt = function.WithBundler(NewDockerBundler(fsys))
54+
if utils.IsDockerRunning(ctx) {
55+
opt = function.WithBundler(NewDockerBundler(fsys))
56+
} else {
57+
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "Docker is not running")
58+
}
5559
}
5660
api := function.NewEdgeRuntimeAPI(flags.ProjectRef, *utils.GetSupabase(), opt)
5761
if err := api.Deploy(ctx, functionConfig, afero.NewIOFS(fsys)); errors.Is(err, function.ErrNoDeploy) {

internal/functions/deploy/deploy_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"net/url"
78
"os"
89
"path/filepath"
910
"testing"
@@ -26,6 +27,11 @@ func TestDeployCommand(t *testing.T) {
2627
const containerId = "test-container"
2728
imageUrl := utils.GetRegistryImageUrl(utils.Config.EdgeRuntime.Image)
2829

30+
parsed, err := url.Parse(utils.Docker.DaemonHost())
31+
require.NoError(t, err)
32+
parsed.Scheme = "http:"
33+
dockerHost := parsed.String()
34+
2935
t.Run("deploys multiple functions", func(t *testing.T) {
3036
functions := []string{slug, slug + "-2"}
3137
// Setup in-memory fs
@@ -39,6 +45,9 @@ func TestDeployCommand(t *testing.T) {
3945
require.NoError(t, err)
4046
// Setup mock api
4147
defer gock.OffAll()
48+
gock.New(dockerHost).
49+
Head("/_ping").
50+
Reply(http.StatusOK)
4251
gock.New(utils.DefaultApiHost).
4352
Get("/v1/projects/" + flags.ProjectRef + "/functions").
4453
Reply(http.StatusOK).
@@ -99,6 +108,9 @@ import_map = "./import_map.json"
99108
require.NoError(t, err)
100109
// Setup mock api
101110
defer gock.OffAll()
111+
gock.New(dockerHost).
112+
Head("/_ping").
113+
Reply(http.StatusOK)
102114
gock.New(utils.DefaultApiHost).
103115
Get("/v1/projects/" + flags.ProjectRef + "/functions").
104116
Reply(http.StatusOK).
@@ -151,6 +163,9 @@ import_map = "./import_map.json"
151163
require.NoError(t, err)
152164
// Setup mock api
153165
defer gock.OffAll()
166+
gock.New(dockerHost).
167+
Head("/_ping").
168+
Reply(http.StatusOK)
154169
gock.New(utils.DefaultApiHost).
155170
Get("/v1/projects/" + flags.ProjectRef + "/functions").
156171
Reply(http.StatusOK).
@@ -214,6 +229,9 @@ verify_jwt = false
214229
require.NoError(t, err)
215230
// Setup mock api
216231
defer gock.OffAll()
232+
gock.New(dockerHost).
233+
Head("/_ping").
234+
Reply(http.StatusOK)
217235
gock.New(utils.DefaultApiHost).
218236
Get("/v1/projects/" + flags.ProjectRef + "/functions").
219237
Reply(http.StatusOK).
@@ -257,6 +275,9 @@ verify_jwt = false
257275
require.NoError(t, err)
258276
// Setup mock api
259277
defer gock.OffAll()
278+
gock.New(dockerHost).
279+
Head("/_ping").
280+
Reply(http.StatusOK)
260281
gock.New(utils.DefaultApiHost).
261282
Get("/v1/projects/" + flags.ProjectRef + "/functions").
262283
Reply(http.StatusOK).

internal/utils/docker.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,11 @@ func DockerExecOnceWithStream(ctx context.Context, containerId, workdir string,
460460
return err
461461
}
462462

463+
func IsDockerRunning(ctx context.Context) bool {
464+
_, err := Docker.Ping(ctx)
465+
return !client.IsErrConnectionFailed(err)
466+
}
467+
463468
var portErrorPattern = regexp.MustCompile("Bind for (.*) failed: port is already allocated")
464469

465470
func parsePortBindError(err error) string {

0 commit comments

Comments
 (0)