@@ -24,6 +24,7 @@ import (
24
24
"syscall"
25
25
"time"
26
26
27
+ "github.com/coder/envbuilder/constants"
27
28
"github.com/coder/envbuilder/git"
28
29
"github.com/coder/envbuilder/options"
29
30
@@ -53,31 +54,6 @@ import (
53
54
"golang.org/x/xerrors"
54
55
)
55
56
56
- const (
57
- // WorkspacesDir is the path to the directory where
58
- // all workspaces are stored by default.
59
- WorkspacesDir = "/workspaces"
60
-
61
- // EmptyWorkspaceDir is the path to a workspace that has
62
- // nothing going on... it's empty!
63
- EmptyWorkspaceDir = WorkspacesDir + "/empty"
64
-
65
- // MagicDir is where all envbuilder related files are stored.
66
- // This is a special directory that must not be modified
67
- // by the user or images.
68
- MagicDir = "/.envbuilder"
69
- )
70
-
71
- var (
72
- ErrNoFallbackImage = errors .New ("no fallback image has been specified" )
73
-
74
- // MagicFile is a file that is created in the workspace
75
- // when envbuilder has already been run. This is used
76
- // to skip building when a container is restarting.
77
- // e.g. docker stop -> docker start
78
- MagicFile = filepath .Join (MagicDir , "built" )
79
- )
80
-
81
57
// DockerConfig represents the Docker configuration file.
82
58
type DockerConfig configfile.ConfigFile
83
59
@@ -171,7 +147,7 @@ func Run(ctx context.Context, opts options.Options) error {
171
147
if err != nil {
172
148
return fmt .Errorf ("parse docker config: %w" , err )
173
149
}
174
- err = os .WriteFile (filepath .Join (MagicDir , "config.json" ), decoded , 0o644 )
150
+ err = os .WriteFile (filepath .Join (constants . MagicDir , "config.json" ), decoded , 0o644 )
175
151
if err != nil {
176
152
return fmt .Errorf ("write docker config: %w" , err )
177
153
}
@@ -237,19 +213,19 @@ func Run(ctx context.Context, opts options.Options) error {
237
213
}
238
214
239
215
defaultBuildParams := func () (* devcontainer.Compiled , error ) {
240
- dockerfile := filepath .Join (MagicDir , "Dockerfile" )
216
+ dockerfile := filepath .Join (constants . MagicDir , "Dockerfile" )
241
217
file , err := opts .Filesystem .OpenFile (dockerfile , os .O_CREATE | os .O_WRONLY , 0o644 )
242
218
if err != nil {
243
219
return nil , err
244
220
}
245
221
defer file .Close ()
246
222
if opts .FallbackImage == "" {
247
223
if fallbackErr != nil {
248
- return nil , xerrors .Errorf ("%s: %w" , fallbackErr .Error (), ErrNoFallbackImage )
224
+ return nil , xerrors .Errorf ("%s: %w" , fallbackErr .Error (), constants . ErrNoFallbackImage )
249
225
}
250
226
// We can't use errors.Join here because our tests
251
227
// don't support parsing a multiline error.
252
- return nil , ErrNoFallbackImage
228
+ return nil , constants . ErrNoFallbackImage
253
229
}
254
230
content := "FROM " + opts .FallbackImage
255
231
_ , err = file .Write ([]byte (content ))
@@ -259,7 +235,7 @@ func Run(ctx context.Context, opts options.Options) error {
259
235
return & devcontainer.Compiled {
260
236
DockerfilePath : dockerfile ,
261
237
DockerfileContent : content ,
262
- BuildContext : MagicDir ,
238
+ BuildContext : constants . MagicDir ,
263
239
}, nil
264
240
}
265
241
@@ -301,7 +277,7 @@ func Run(ctx context.Context, opts options.Options) error {
301
277
opts .Logger (log .LevelInfo , "No Dockerfile or image specified; falling back to the default image..." )
302
278
fallbackDockerfile = defaultParams .DockerfilePath
303
279
}
304
- buildParams , err = devContainer .Compile (opts .Filesystem , devcontainerDir , MagicDir , fallbackDockerfile , opts .WorkspaceFolder , false , os .LookupEnv )
280
+ buildParams , err = devContainer .Compile (opts .Filesystem , devcontainerDir , constants . MagicDir , fallbackDockerfile , opts .WorkspaceFolder , false , os .LookupEnv )
305
281
if err != nil {
306
282
return fmt .Errorf ("compile devcontainer.json: %w" , err )
307
283
}
@@ -399,7 +375,7 @@ func Run(ctx context.Context, opts options.Options) error {
399
375
// So we add them to the default ignore list. See:
400
376
// https://github.com/GoogleContainerTools/kaniko/blob/63be4990ca5a60bdf06ddc4d10aa4eca0c0bc714/cmd/executor/cmd/root.go#L136
401
377
ignorePaths := append ([]string {
402
- MagicDir ,
378
+ constants . MagicDir ,
403
379
opts .WorkspaceFolder ,
404
380
// See: https://github.com/coder/envbuilder/issues/37
405
381
"/etc/resolv.conf" ,
@@ -441,7 +417,7 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
441
417
}
442
418
443
419
// temp move of all ro mounts
444
- tempRemountDest := filepath .Join ("/" , MagicDir , "mnt" )
420
+ tempRemountDest := filepath .Join ("/" , constants . MagicDir , "mnt" )
445
421
// ignorePrefixes is a superset of ignorePaths that we pass to kaniko's
446
422
// IgnoreList.
447
423
ignorePrefixes := append ([]string {"/dev" , "/proc" , "/sys" }, ignorePaths ... )
@@ -457,7 +433,7 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
457
433
458
434
skippedRebuild := false
459
435
build := func () (v1.Image , error ) {
460
- _ , err := opts .Filesystem .Stat (MagicFile )
436
+ _ , err := opts .Filesystem .Stat (constants . MagicFile )
461
437
if err == nil && opts .SkipRebuild {
462
438
endStage := startStage ("🏗️ Skipping build because of cache..." )
463
439
imageRef , err := devcontainer .ImageFromDockerfile (buildParams .DockerfileContent )
@@ -640,7 +616,7 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
640
616
641
617
// Create the magic file to indicate that this build
642
618
// has already been ran before!
643
- file , err := opts .Filesystem .Create (MagicFile )
619
+ file , err := opts .Filesystem .Create (constants . MagicFile )
644
620
if err != nil {
645
621
return fmt .Errorf ("create magic file: %w" , err )
646
622
}
@@ -695,7 +671,7 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
695
671
696
672
// Remove the Docker config secret file!
697
673
if opts .DockerConfigBase64 != "" {
698
- c := filepath .Join (MagicDir , "config.json" )
674
+ c := filepath .Join (constants . MagicDir , "config.json" )
699
675
err = os .Remove (c )
700
676
if err != nil {
701
677
if ! errors .Is (err , fs .ErrNotExist ) {
@@ -855,7 +831,7 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
855
831
opts .Logger (log .LevelInfo , "=== Running the setup command %q as the root user..." , opts .SetupScript )
856
832
857
833
envKey := "ENVBUILDER_ENV"
858
- envFile := filepath .Join ("/" , MagicDir , "environ" )
834
+ envFile := filepath .Join ("/" , constants . MagicDir , "environ" )
859
835
file , err := os .Create (envFile )
860
836
if err != nil {
861
837
return fmt .Errorf ("create environ file: %w" , err )
@@ -958,7 +934,7 @@ ENTRYPOINT [%q]`, exePath, exePath, exePath)
958
934
// for a given repository URL.
959
935
func DefaultWorkspaceFolder (repoURL string ) (string , error ) {
960
936
if repoURL == "" {
961
- return EmptyWorkspaceDir , nil
937
+ return constants . EmptyWorkspaceDir , nil
962
938
}
963
939
parsed , err := giturls .Parse (repoURL )
964
940
if err != nil {
@@ -967,7 +943,7 @@ func DefaultWorkspaceFolder(repoURL string) (string, error) {
967
943
name := strings .Split (parsed .Path , "/" )
968
944
hasOwnerAndRepo := len (name ) >= 2
969
945
if ! hasOwnerAndRepo {
970
- return EmptyWorkspaceDir , nil
946
+ return constants . EmptyWorkspaceDir , nil
971
947
}
972
948
repo := strings .TrimSuffix (name [len (name )- 1 ], ".git" )
973
949
return fmt .Sprintf ("/workspaces/%s" , repo ), nil
@@ -1180,7 +1156,7 @@ func findDevcontainerJSON(options options.Options) (string, string, error) {
1180
1156
// folks from unwittingly deleting their entire root directory.
1181
1157
func maybeDeleteFilesystem (logger log.Func , force bool ) error {
1182
1158
kanikoDir , ok := os .LookupEnv ("KANIKO_DIR" )
1183
- if ! ok || strings .TrimSpace (kanikoDir ) != MagicDir {
1159
+ if ! ok || strings .TrimSpace (kanikoDir ) != constants . MagicDir {
1184
1160
if force {
1185
1161
bailoutSecs := 10
1186
1162
logger (log .LevelWarn , "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!" )
@@ -1190,7 +1166,7 @@ func maybeDeleteFilesystem(logger log.Func, force bool) error {
1190
1166
<- time .After (time .Second )
1191
1167
}
1192
1168
} else {
1193
- logger (log .LevelError , "KANIKO_DIR is not set to %s. Bailing!\n " , MagicDir )
1169
+ logger (log .LevelError , "KANIKO_DIR is not set to %s. Bailing!\n " , constants . MagicDir )
1194
1170
logger (log .LevelError , "To bypass this check, set FORCE_SAFE=true." )
1195
1171
return errors .New ("safety check failed" )
1196
1172
}
0 commit comments