-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
config.go
1111 lines (966 loc) · 36.2 KB
/
config.go
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ddevapp
import (
"bytes"
"fmt"
"github.com/Masterminds/sprig"
"github.com/drud/ddev/pkg/dockerutil"
"github.com/drud/ddev/pkg/nodeps"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
"github.com/drud/ddev/pkg/globalconfig"
"regexp"
"runtime"
"github.com/drud/ddev/pkg/fileutil"
"github.com/drud/ddev/pkg/output"
"github.com/drud/ddev/pkg/util"
"github.com/drud/ddev/pkg/version"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Regexp pattern to determine if a hostname is valid per RFC 1123.
var hostRegex = regexp.MustCompile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
// init() is for testing situations only, allowing us to override the default webserver type
// or caching behavior
func init() {
// This is for automated testing only. It allows us to override the webserver type.
if testWebServerType := os.Getenv("DDEV_TEST_WEBSERVER_TYPE"); testWebServerType != "" {
nodeps.WebserverDefault = testWebServerType
}
if testNFSMount := os.Getenv("DDEV_TEST_USE_NFSMOUNT"); testNFSMount != "" {
nodeps.NFSMountEnabledDefault = true
}
if testMutagen := os.Getenv("DDEV_TEST_USE_MUTAGEN"); testMutagen == "true" {
nodeps.MutagenEnabledDefault = true
}
}
// NewApp creates a new DdevApp struct with defaults set and overridden by any existing config.yml.
func NewApp(appRoot string, includeOverrides bool) (*DdevApp, error) {
runTime := util.TimeTrack(time.Now(), fmt.Sprintf("ddevapp.NewApp(%s)", appRoot))
defer runTime()
app := &DdevApp{}
if appRoot == "" {
app.AppRoot, _ = os.Getwd()
} else {
app.AppRoot = appRoot
}
homeDir, _ := os.UserHomeDir()
if appRoot == filepath.Dir(globalconfig.GetGlobalDdevDir()) || app.AppRoot == homeDir {
return nil, fmt.Errorf("ddev config is not useful in your home directory (%s)", homeDir)
}
if !fileutil.FileExists(app.AppRoot) {
return app, fmt.Errorf("project root %s does not exist", app.AppRoot)
}
app.ConfigPath = app.GetConfigPath("config.yaml")
app.Type = nodeps.AppTypePHP
app.PHPVersion = nodeps.PHPDefault
app.MariaDBVersion = nodeps.MariaDBDefaultVersion
app.WebserverType = nodeps.WebserverDefault
app.NFSMountEnabled = nodeps.NFSMountEnabledDefault
app.NFSMountEnabledGlobal = globalconfig.DdevGlobalConfig.NFSMountEnabledGlobal
app.MutagenEnabled = nodeps.MutagenEnabledDefault
app.MutagenEnabledGlobal = globalconfig.DdevGlobalConfig.MutagenEnabledGlobal
app.FailOnHookFail = nodeps.FailOnHookFailDefault
app.FailOnHookFailGlobal = globalconfig.DdevGlobalConfig.FailOnHookFailGlobal
app.RouterHTTPPort = nodeps.DdevDefaultRouterHTTPPort
app.RouterHTTPSPort = nodeps.DdevDefaultRouterHTTPSPort
app.PHPMyAdminPort = nodeps.DdevDefaultPHPMyAdminPort
app.PHPMyAdminHTTPSPort = nodeps.DdevDefaultPHPMyAdminHTTPSPort
app.MailhogPort = nodeps.DdevDefaultMailhogPort
app.MailhogHTTPSPort = nodeps.DdevDefaultMailhogHTTPSPort
// Provide a default app name based on directory name
app.Name = filepath.Base(app.AppRoot)
app.OmitContainerGlobal = globalconfig.DdevGlobalConfig.OmitContainersGlobal
app.ProjectTLD = nodeps.DdevDefaultTLD
app.UseDNSWhenPossible = true
// These should always default to the latest image/tag names from the Version package.
app.WebImage = version.GetWebImage()
app.DBAImage = version.GetDBAImage()
// Load from file if available. This will return an error if the file doesn't exist,
// and it is up to the caller to determine if that's an issue.
if _, err := os.Stat(app.ConfigPath); !os.IsNotExist(err) {
_, err = app.ReadConfig(includeOverrides)
if err != nil {
return app, fmt.Errorf("%v exists but cannot be read. It may be invalid due to a syntax error.: %v", app.ConfigPath, err)
}
}
// If MySQLVersion is now non-default/non-empty, then empty
// MariaDBVersion in its favor.
if app.MySQLVersion != "" {
app.MariaDBVersion = ""
}
app.SetApptypeSettingsPaths()
// Rendered yaml is not there until after ddev config or ddev start
if fileutil.FileExists(app.ConfigPath) && fileutil.FileExists(app.DockerComposeFullRenderedYAMLPath()) {
content, err := fileutil.ReadFileIntoString(app.DockerComposeFullRenderedYAMLPath())
if err != nil {
return app, err
}
err = yaml.Unmarshal([]byte(content), &app.ComposeYaml)
if err != nil {
return app, err
}
_, err = app.ReadConfig(includeOverrides)
if err != nil {
return app, fmt.Errorf("%v exists but cannot be read. It may be invalid due to a syntax error: %v", app.ConfigPath, err)
}
}
return app, nil
}
// GetConfigPath returns the path to an application config file specified by filename.
func (app *DdevApp) GetConfigPath(filename string) string {
return filepath.Join(app.AppRoot, ".ddev", filename)
}
// WriteConfig writes the app configuration into the .ddev folder.
func (app *DdevApp) WriteConfig() error {
// Work against a copy of the DdevApp, since we don't want to actually change it.
appcopy := *app
// Only set the images on write if non-default values have been specified.
if appcopy.WebImage == version.GetWebImage() {
appcopy.WebImage = ""
}
// If the DBImage is actually just created/equal to the maria or mysql version
// then remove it from the output.
if appcopy.DBImage == version.GetDBImage(nodeps.MariaDB, appcopy.MariaDBVersion) || appcopy.DBImage == version.GetDBImage(nodeps.MySQL, appcopy.MySQLVersion) {
appcopy.DBImage = ""
}
if appcopy.DBAImage == version.GetDBAImage() {
appcopy.DBAImage = ""
}
if appcopy.DBAImage == version.GetDBAImage() {
appcopy.DBAImage = ""
}
if appcopy.MailhogPort == nodeps.DdevDefaultMailhogPort {
appcopy.MailhogPort = ""
}
if appcopy.MailhogHTTPSPort == nodeps.DdevDefaultMailhogHTTPSPort {
appcopy.MailhogHTTPSPort = ""
}
if appcopy.PHPMyAdminPort == nodeps.DdevDefaultPHPMyAdminPort {
appcopy.PHPMyAdminPort = ""
}
if appcopy.PHPMyAdminHTTPSPort == nodeps.DdevDefaultPHPMyAdminHTTPSPort {
appcopy.PHPMyAdminHTTPSPort = ""
}
if appcopy.ProjectTLD == nodeps.DdevDefaultTLD {
appcopy.ProjectTLD = ""
}
// If mariadb-version is "" and mysql-version is not set, then set mariadb-version to default
if appcopy.MariaDBVersion == "" && appcopy.MySQLVersion == "" {
appcopy.MariaDBVersion = nodeps.MariaDBDefaultVersion
}
// We now want to reserve the port we're writing for HostDBPort and HostWebserverPort and so they don't
// accidentally get used for other projects.
err := app.UpdateGlobalProjectList()
if err != nil {
return err
}
// Don't write default working dir values to config
defaults := appcopy.DefaultWorkingDirMap()
for service, defaultWorkingDir := range defaults {
if app.WorkingDir[service] == defaultWorkingDir {
delete(appcopy.WorkingDir, service)
}
}
err = PrepDdevDirectory(filepath.Dir(appcopy.ConfigPath))
if err != nil {
return err
}
cfgbytes, err := yaml.Marshal(appcopy)
if err != nil {
return err
}
// Append hook information and sample hook suggestions.
cfgbytes = append(cfgbytes, []byte(ConfigInstructions)...)
cfgbytes = append(cfgbytes, appcopy.GetHookDefaultComments()...)
err = os.WriteFile(appcopy.ConfigPath, cfgbytes, 0644)
if err != nil {
return err
}
// Allow project-specific post-config action
err = appcopy.PostConfigAction()
if err != nil {
return err
}
// Write example Dockerfiles into build directories
contents := []byte(`
# You can copy this Dockerfile.example to Dockerfile to add configuration
# or packages or anything else to your webimage
ARG BASE_IMAGE
FROM $BASE_IMAGE
RUN npm install --global gulp-cli
`)
err = WriteImageDockerfile(app.GetConfigPath("web-build")+"/Dockerfile.example", contents)
if err != nil {
return err
}
contents = []byte(`
# You can copy this Dockerfile.example to Dockerfile to add configuration
# or packages or anything else to your dbimage
ARG BASE_IMAGE
FROM $BASE_IMAGE
RUN echo "Built from ` + app.GetDBImage() + `" >/var/tmp/built-from.txt
`)
err = WriteImageDockerfile(app.GetConfigPath("db-build")+"/Dockerfile.example", contents)
if err != nil {
return err
}
return nil
}
// UpdateGlobalProjectList updates any information about project that
// is tracked in global project list:
// - approot
// - configured host ports
// checks that configured host ports are not already
// reserved by another project
func (app *DdevApp) UpdateGlobalProjectList() error {
portsToReserve := []string{}
if app.HostDBPort != "" {
portsToReserve = append(portsToReserve, app.HostDBPort)
}
if app.HostWebserverPort != "" {
portsToReserve = append(portsToReserve, app.HostWebserverPort)
}
if app.HostHTTPSPort != "" {
portsToReserve = append(portsToReserve, app.HostHTTPSPort)
}
if len(portsToReserve) > 0 {
err := globalconfig.CheckHostPortsAvailable(app.Name, portsToReserve)
if err != nil {
return err
}
}
err := globalconfig.ReservePorts(app.Name, portsToReserve)
if err != nil {
return err
}
err = globalconfig.SetProjectAppRoot(app.Name, app.AppRoot)
if err != nil {
return err
}
return nil
}
// ReadConfig reads project configuration from the config.yaml file
// It does not attempt to set default values; that's NewApp's job.
func (app *DdevApp) ReadConfig(includeOverrides bool) ([]string, error) {
// Load config.yaml
err := app.LoadConfigYamlFile(app.ConfigPath)
if err != nil {
return []string{}, fmt.Errorf("unable to load config file %s: %v", app.ConfigPath, err)
}
configOverrides := []string{}
// Load config.*.y*ml after in glob order
if includeOverrides {
glob := filepath.Join(filepath.Dir(app.ConfigPath), "config.*.y*ml")
configOverrides, err = filepath.Glob(glob)
if err != nil {
return []string{}, err
}
for _, item := range configOverrides {
err = app.LoadConfigYamlFile(item)
if err != nil {
return []string{}, fmt.Errorf("unable to load config file %s: %v", item, err)
}
}
}
return append([]string{app.ConfigPath}, configOverrides...), nil
}
// LoadConfigYamlFile loads one config.yaml into app, overriding what might be there.
func (app *DdevApp) LoadConfigYamlFile(filePath string) error {
source, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("could not find an active ddev configuration at %s have you run 'ddev config'? %v", app.ConfigPath, err)
}
// validate extend command keys
err = validateHookYAML(source)
if err != nil {
return fmt.Errorf("invalid configuration in %s: %v", app.ConfigPath, err)
}
// ReadConfig config values from file.
err = yaml.Unmarshal(source, app)
if err != nil {
return err
}
return nil
}
// WarnIfConfigReplace just messages user about whether config is being replaced or created
func (app *DdevApp) WarnIfConfigReplace() {
if app.ConfigExists() {
util.Warning("You are reconfiguring the project at %s.\nThe existing configuration will be updated and replaced.", app.AppRoot)
} else {
util.Success("Creating a new ddev project config in the current directory (%s)", app.AppRoot)
util.Success("Once completed, your configuration will be written to %s\n", app.ConfigPath)
}
}
// PromptForConfig goes through a set of prompts to receive user input and generate an Config struct.
func (app *DdevApp) PromptForConfig() error {
app.WarnIfConfigReplace()
for {
err := app.promptForName()
if err == nil {
break
}
output.UserOut.Printf("%v", err)
}
if err := app.docrootPrompt(); err != nil {
return err
}
err := app.AppTypePrompt()
if err != nil {
return err
}
err = app.ConfigFileOverrideAction()
if err != nil {
return err
}
err = app.ValidateConfig()
if err != nil {
return err
}
return nil
}
// ValidateProjectName checks to see if the project name works for a proper hostname
func ValidateProjectName(name string) error {
match := hostRegex.MatchString(name)
if !match {
return fmt.Errorf("%s is not a valid project name. Please enter a project name in your configuration that will allow for a valid hostname. See https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames for valid hostname requirements", name)
}
return nil
}
// ValidateConfig ensures the configuration meets ddev's requirements.
func (app *DdevApp) ValidateConfig() error {
// validate project name
if err := ValidateProjectName(app.Name); err != nil {
return err
}
// validate hostnames
for _, hn := range app.GetHostnames() {
// If they have provided "*.<hostname>" then ignore the *. part.
hn = strings.TrimPrefix(hn, "*.")
if hn == "ddev.site" {
return fmt.Errorf("wildcarding the full hostname or using 'ddev.site' as fqdn is not allowed because other projects would not work in that case")
}
if !hostRegex.MatchString(hn) {
return fmt.Errorf("invalid hostname: %s. See https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames for valid hostname requirements", hn).(invalidHostname)
}
}
// validate apptype
if !IsValidAppType(app.Type) {
return fmt.Errorf("invalid app type: %s", app.Type).(invalidAppType)
}
// validate PHP version
if !nodeps.IsValidPHPVersion(app.PHPVersion) {
return fmt.Errorf("unsupported PHP version: %s, ddev (%s) only supports the following versions: %v", app.PHPVersion, runtime.GOARCH, nodeps.GetValidPHPVersions()).(invalidPHPVersion)
}
// validate webserver type
if !nodeps.IsValidWebserverType(app.WebserverType) {
return fmt.Errorf("unsupported webserver type: %s, ddev (%s) only supports the following webserver types: %s", app.WebserverType, runtime.GOARCH, nodeps.GetValidWebserverTypes()).(invalidWebserverType)
}
if !nodeps.IsValidOmitContainers(app.OmitContainers) {
return fmt.Errorf("unsupported omit_containers: %s, ddev (%s) only supports the following for omit_containers: %s", app.OmitContainers, runtime.GOARCH, nodeps.GetValidOmitContainers()).(InvalidOmitContainers)
}
if app.MariaDBVersion != "" {
// Validate mariadb version
if !nodeps.IsValidMariaDBVersion(app.MariaDBVersion) {
return fmt.Errorf("unsupported mariadb_version: %s, ddev (%s) only supports the following versions: %s", app.MariaDBVersion, runtime.GOARCH, nodeps.GetValidMariaDBVersions()).(invalidMariaDBVersion)
}
}
if app.MySQLVersion != "" {
// Validate /mysql version
if !nodeps.IsValidMySQLVersion(app.MySQLVersion) {
if len(nodeps.GetValidMySQLVersions()) == 0 {
return fmt.Errorf("MySQL is not yet supported on your architecture (%s) because mysql does not provide packages (or docker images)", runtime.GOARCH)
}
return fmt.Errorf("unsupported mysql_version: %s; ddev (%s) only supports the following versions %s", app.MySQLVersion, runtime.GOARCH, nodeps.GetValidMySQLVersions()).(invalidMySQLVersion)
}
}
// Validate db versions
if app.MariaDBVersion != "" && app.MySQLVersion != "" {
return fmt.Errorf("both mariadb_version (%v) and mysql_version (%v) are set, but they are mutually exclusive", app.MariaDBVersion, app.MySQLVersion)
}
// golang on windows is not able to time.LoadLocation unless
// go is installed... so skip validation on Windows
if runtime.GOOS != "windows" {
_, err := time.LoadLocation(app.Timezone)
if err != nil {
// golang on Windows is often not able to time.LoadLocation.
// It often works if go is installed and $GOROOT is set, but
// that's not the norm for our users.
return fmt.Errorf("invalid timezone %s: %v", app.Timezone, err)
}
}
return nil
}
// DockerComposeYAMLPath returns the absolute path to where the
// base generated yaml file should exist for this project.
func (app *DdevApp) DockerComposeYAMLPath() string {
return app.GetConfigPath(".ddev-docker-compose-base.yaml")
}
// DockerComposeFullRenderedYAMLPath returns the absolute path to where the
// the complete generated yaml file should exist for this project.
func (app *DdevApp) DockerComposeFullRenderedYAMLPath() string {
return app.GetConfigPath(".ddev-docker-compose-full.yaml")
}
// GetHostname returns the primary hostname of the app.
func (app *DdevApp) GetHostname() string {
return strings.ToLower(app.Name) + "." + app.ProjectTLD
}
// GetHostnames returns an array of all the configured hostnames.
func (app *DdevApp) GetHostnames() []string {
// Use a map to make sure that we have unique hostnames
// The value is useless, so just use the int 1 for assignment.
nameListMap := make(map[string]int)
nameListArray := []string{}
if !IsRouterDisabled(app) {
for _, name := range app.AdditionalHostnames {
name = strings.ToLower(name)
nameListMap[name+"."+app.ProjectTLD] = 1
}
for _, name := range app.AdditionalFQDNs {
name = strings.ToLower(name)
nameListMap[name] = 1
}
// Make sure the primary hostname didn't accidentally get added, it will be prepended
delete(nameListMap, app.GetHostname())
// Now walk the map and extract the keys into an array.
for k := range nameListMap {
nameListArray = append(nameListArray, k)
}
sort.Strings(nameListArray)
// We want the primary hostname to be first in the list.
nameListArray = append([]string{app.GetHostname()}, nameListArray...)
}
return nameListArray
}
// WriteDockerComposeYAML writes a .ddev-docker-compose-base.yaml and related to the .ddev directory.
func (app *DdevApp) WriteDockerComposeYAML() error {
var err error
f, err := os.Create(app.DockerComposeYAMLPath())
if err != nil {
return err
}
defer util.CheckClose(f)
rendered, err := app.RenderComposeYAML()
if err != nil {
return err
}
_, err = f.WriteString(rendered)
if err != nil {
return err
}
files, err := app.ComposeFiles()
if err != nil {
return err
}
fullContents, _, err := dockerutil.ComposeCmd(files, "config")
if err != nil {
return err
}
// Replace `docker-compose config`'s full-path usage with relative pathing
// for https://youtrack.jetbrains.com/issue/WI-61976 - PhpStorm
// This is an ugly an shortsighted approach, but otherwise we'd have to parse the yaml.
// Note that this issue with docker-compose config was fixed in docker-compose 2.0.0RC4
// so it's in Docker Desktop 4.1.0.
// https://github.com/docker/compose/issues/8503#issuecomment-930969241
fullContents = strings.Replace(fullContents, fmt.Sprintf("source: %s\n", app.AppRoot), "source: ../\n", -1)
fullHandle, err := os.Create(app.DockerComposeFullRenderedYAMLPath())
if err != nil {
return err
}
_, err = fullHandle.WriteString(fullContents)
if err != nil {
return err
}
return nil
}
// CheckCustomConfig warns the user if any custom configuration files are in use.
func (app *DdevApp) CheckCustomConfig() {
// Get the path to .ddev for the current app.
ddevDir := filepath.Dir(app.ConfigPath)
customConfig := false
if _, err := os.Stat(filepath.Join(ddevDir, "nginx-site.conf")); err == nil && app.WebserverType == nodeps.WebserverNginxFPM {
util.Warning("Using custom nginx configuration in nginx-site.conf")
customConfig = true
}
nginxFullConfigPath := app.GetConfigPath("nginx_full/nginx-site.conf")
sigFound, _ := fileutil.FgrepStringInFile(nginxFullConfigPath, DdevFileSignature)
if !sigFound && app.WebserverType == nodeps.WebserverNginxFPM {
util.Warning("Using custom nginx configuration in %s", nginxFullConfigPath)
customConfig = true
}
apacheFullConfigPath := app.GetConfigPath("apache/apache-site.conf")
sigFound, _ = fileutil.FgrepStringInFile(apacheFullConfigPath, DdevFileSignature)
if !sigFound && app.WebserverType != nodeps.WebserverNginxFPM {
util.Warning("Using custom apache configuration in %s", apacheFullConfigPath)
customConfig = true
}
nginxPath := filepath.Join(ddevDir, "nginx")
if _, err := os.Stat(nginxPath); err == nil {
nginxFiles, err := filepath.Glob(nginxPath + "/*.conf")
util.CheckErr(err)
if len(nginxFiles) > 0 {
util.Warning("Using nginx snippets: %v", nginxFiles)
customConfig = true
}
}
mysqlPath := filepath.Join(ddevDir, "mysql")
if _, err := os.Stat(mysqlPath); err == nil {
mysqlFiles, err := filepath.Glob(mysqlPath + "/*.cnf")
util.CheckErr(err)
if len(mysqlFiles) > 0 {
util.Warning("Using custom mysql configuration: %v", mysqlFiles)
customConfig = true
}
}
phpPath := filepath.Join(ddevDir, "php")
if _, err := os.Stat(phpPath); err == nil {
phpFiles, err := filepath.Glob(phpPath + "/*.ini")
util.CheckErr(err)
if len(phpFiles) > 0 {
util.Warning("Using custom PHP configuration: %v", phpFiles)
customConfig = true
}
}
if customConfig {
util.Warning("Custom configuration takes effect when container is created,\nusually on start, use 'ddev restart' if you're not seeing it take effect.")
}
}
// CheckDeprecations warns the user if anything in use is deprecated.
func (app *DdevApp) CheckDeprecations() {
}
type composeYAMLVars struct {
Name string
Plugin string
AppType string
MailhogPort string
HostMailhogPort string
DBAPort string
DBPort string
HostPHPMyAdminPort string
DdevGenerated string
HostDockerInternalIP string
ComposeVersion string
DisableSettingsManagement bool
MountType string
WebMount string
WebBuildContext string
DBBuildContext string
WebBuildDockerfile string
DBBuildDockerfile string
SSHAgentBuildContext string
OmitDB bool
OmitDBA bool
OmitRouter bool
OmitSSHAgent bool
BindAllInterfaces bool
MariaDBVolumeName string
MutagenEnabled bool
MutagenVolumeName string
NFSMountEnabled bool
NFSSource string
NFSMountVolumeName string
DockerIP string
IsWindowsFS bool
NoProjectMount bool
Hostnames []string
Timezone string
ComposerVersion string
Username string
UID string
GID string
AutoRestartContainers bool
FailOnHookFail bool
WebWorkingDir string
DBWorkingDir string
DBAWorkingDir string
WebEnvironment []string
}
// RenderComposeYAML renders the contents of .ddev/.ddev-docker-compose*.
func (app *DdevApp) RenderComposeYAML() (string, error) {
var doc bytes.Buffer
var err error
templ, err := template.New("compose template").Funcs(sprig.TxtFuncMap()).Parse(DDevComposeTemplate)
if err != nil {
return "", err
}
templ, err = templ.Parse(DDevComposeTemplate)
if err != nil {
return "", err
}
hostDockerInternalIP, err := dockerutil.GetHostDockerInternalIP()
if err != nil {
util.Warning("Could not determine host.docker.internal IP address: %v", err)
}
// The fallthrough default for hostDockerInternalIdentifier is the
// hostDockerInternalHostname == host.docker.internal
webEnvironment := globalconfig.DdevGlobalConfig.WebEnvironment
localWebEnvironment := app.WebEnvironment
for _, v := range localWebEnvironment {
// docker-compose won't accept a duplicate environment value
if !nodeps.ArrayContainsString(webEnvironment, v) {
webEnvironment = append(webEnvironment, v)
}
}
uid, gid, username := util.GetContainerUIDGid()
_, err = app.GetProvider("")
if err != nil {
return "", err
}
templateVars := composeYAMLVars{
Name: app.Name,
Plugin: "ddev",
AppType: app.Type,
MailhogPort: GetPort("mailhog"),
HostMailhogPort: app.HostMailhogPort,
DBAPort: GetPort("dba"),
DBPort: GetPort("db"),
HostPHPMyAdminPort: app.HostPHPMyAdminPort,
DdevGenerated: DdevFileSignature,
HostDockerInternalIP: hostDockerInternalIP,
ComposeVersion: version.DockerComposeFileFormatVersion,
DisableSettingsManagement: app.DisableSettingsManagement,
OmitDB: nodeps.ArrayContainsString(app.GetOmittedContainers(), nodeps.DBContainer),
OmitDBA: nodeps.ArrayContainsString(app.GetOmittedContainers(), nodeps.DBAContainer) || nodeps.ArrayContainsString(app.OmitContainers, nodeps.DBContainer),
OmitRouter: nodeps.ArrayContainsString(app.GetOmittedContainers(), globalconfig.DdevRouterContainer),
OmitSSHAgent: nodeps.ArrayContainsString(app.GetOmittedContainers(), "ddev-ssh-agent"),
BindAllInterfaces: app.BindAllInterfaces,
MutagenEnabled: (app.IsMutagenEnabled()),
NFSMountEnabled: (app.NFSMountEnabled || app.NFSMountEnabledGlobal) && !app.IsMutagenEnabled(),
NFSSource: "",
IsWindowsFS: runtime.GOOS == "windows",
NoProjectMount: app.NoProjectMount,
MountType: "bind",
WebMount: "../",
Hostnames: app.GetHostnames(),
Timezone: app.Timezone,
ComposerVersion: app.ComposerVersion,
Username: username,
UID: uid,
GID: gid,
WebBuildContext: "./web-build",
DBBuildContext: "./db-build",
WebBuildDockerfile: "../.webimageBuild/Dockerfile",
DBBuildDockerfile: "../.dbimageBuild/Dockerfile",
AutoRestartContainers: globalconfig.DdevGlobalConfig.AutoRestartContainers,
FailOnHookFail: app.FailOnHookFail || app.FailOnHookFailGlobal,
WebWorkingDir: app.GetWorkingDir("web", ""),
DBWorkingDir: app.GetWorkingDir("db", ""),
DBAWorkingDir: app.GetWorkingDir("dba", ""),
WebEnvironment: webEnvironment,
MariaDBVolumeName: app.GetMariaDBVolumeName(),
NFSMountVolumeName: app.GetNFSMountVolumeName(),
}
if app.NFSMountEnabled || app.NFSMountEnabledGlobal {
templateVars.MountType = "volume"
templateVars.WebMount = "nfsmount"
templateVars.NFSSource = app.AppRoot
// Workaround for Catalina sharing nfs as /System/Volumes/Data
if runtime.GOOS == "darwin" && fileutil.IsDirectory(filepath.Join("/System/Volumes/Data", app.AppRoot)) {
templateVars.NFSSource = filepath.Join("/System/Volumes/Data", app.AppRoot)
}
if runtime.GOOS == "windows" {
// WinNFSD can only handle a mountpoint like /C/Users/rfay/workspace/d8git
// and completely chokes in C:\Users\rfay...
templateVars.NFSSource = dockerutil.MassageWindowsNFSMount(app.AppRoot)
}
}
if app.IsMutagenEnabled() {
templateVars.MutagenVolumeName = GetMutagenVolumeName(app)
}
// Add web and db extra dockerfile info
// If there is a user-provided Dockerfile, use that as the base and then add
// our extra stuff like usernames, etc.
// The db-build and web-build directories are used for context
// so must exist. They usually do.
err = os.MkdirAll(app.GetConfigPath("db-build"), 0755)
if err != nil {
return "", err
}
err = os.MkdirAll(app.GetConfigPath("web-build"), 0755)
if err != nil {
return "", err
}
err = WriteBuildDockerfile(app.GetConfigPath(".webimageBuild/Dockerfile"), app.GetConfigPath("web-build/Dockerfile"), app.WebImageExtraPackages, app.ComposerVersion)
if err != nil {
return "", err
}
err = WriteBuildDockerfile(app.GetConfigPath(".dbimageBuild/Dockerfile"), app.GetConfigPath("db-build/Dockerfile"), app.DBImageExtraPackages, "")
if err != nil {
return "", err
}
// SSH agent just needs extra to add the official related user, nothing else
err = WriteBuildDockerfile(filepath.Join(globalconfig.GetGlobalDdevDir(), ".sshimageBuild/Dockerfile"), "", nil, "")
if err != nil {
return "", err
}
templateVars.DockerIP, err = dockerutil.GetDockerIP()
if err != nil {
return "", err
}
if app.BindAllInterfaces {
templateVars.DockerIP = "0.0.0.0"
}
err = templ.Execute(&doc, templateVars)
return doc.String(), err
}
// WriteBuildDockerfile writes a Dockerfile to be used in the
// docker-compose 'build'
// It may include the contents of .ddev/<container>-build
func WriteBuildDockerfile(fullpath string, userDockerfile string, extraPackages []string, composerVersion string) error {
// Start with user-built dockerfile if there is one.
err := os.MkdirAll(filepath.Dir(fullpath), 0755)
if err != nil {
return err
}
// Normal starting content is just the arg and base image
contents := `
ARG BASE_IMAGE
FROM $BASE_IMAGE
`
// If there is a user dockerfile, start with its contents
if userDockerfile != "" && fileutil.FileExists(userDockerfile) {
contents, err = fileutil.ReadFileIntoString(userDockerfile)
if err != nil {
return err
}
}
contents = contents + `
ARG username
ARG uid
ARG gid
RUN (groupadd --gid $gid "$username" || groupadd "$username" || true) && (useradd -l -m -s "/bin/bash" --gid "$username" --comment '' --uid $uid "$username" || useradd -l -m -s "/bin/bash" --gid "$username" --comment '' "$username" || useradd -l -m -s "/bin/bash" --gid "$gid" --comment '' "$username")
`
if extraPackages != nil {
contents = contents + `
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests ` + strings.Join(extraPackages, " ") + "\n"
}
// If composerVersion is set, and composer is in the container,
// run composer self-update to the version (or --1 or --2)
var composerSelfUpdateArg string
switch composerVersion {
case "1":
composerSelfUpdateArg = "--1"
case "2":
composerSelfUpdateArg = "--2"
default:
composerSelfUpdateArg = composerVersion
}
// If composerVersion is not set, we don't need to self-update.
// Currently by default it will be composer v1 because of upstream setting
// Try composer self-update twice because of troubles with composer downloads
// breaking testing.
if composerVersion != "" {
contents = contents + fmt.Sprintf(`
RUN if command -v composer >/dev/null 2>&1 ; then export XDEBUG_MODE=off && (composer self-update %s || composer self-update %s ) && chmod 777 /usr/local/bin/composer; fi
`, composerSelfUpdateArg, composerSelfUpdateArg)
}
return WriteImageDockerfile(fullpath, []byte(contents))
}
// WriteImageDockerfile writes a dockerfile at the fullpath (including the filename)
func WriteImageDockerfile(fullpath string, contents []byte) error {
err := os.MkdirAll(filepath.Dir(fullpath), 0755)
if err != nil {
return err
}
err = os.WriteFile(fullpath, contents, 0644)
if err != nil {
return err
}
return nil
}
// prompt for a project name.
func (app *DdevApp) promptForName() error {
if app.Name == "" {
dir, err := os.Getwd()
// if working directory name is invalid for hostnames, we shouldn't suggest it
if err == nil && hostRegex.MatchString(filepath.Base(dir)) {
app.Name = filepath.Base(dir)
}
}
name := util.Prompt("Project name", app.Name)
if err := ValidateProjectName(name); err != nil {
return err
}
app.Name = name
return nil
}
// AvailableDocrootLocations returns an of default docroot locations to look for.
func AvailableDocrootLocations() []string {
return []string{
"_www",
"docroot",
"htdocs",
"html",
"pub",
"public",
"web",
"web/public",
"webroot",
}
}
// DiscoverDefaultDocroot returns the default docroot directory.
func DiscoverDefaultDocroot(app *DdevApp) string {
// Provide use the app.Docroot as the default docroot option.
var defaultDocroot = app.Docroot
if defaultDocroot == "" {
for _, docroot := range AvailableDocrootLocations() {
if _, err := os.Stat(filepath.Join(app.AppRoot, docroot)); err != nil {
continue
}
if fileutil.FileExists(filepath.Join(app.AppRoot, docroot, "index.php")) {
defaultDocroot = docroot
break
}
}
}
return defaultDocroot
}
// Determine the document root.
func (app *DdevApp) docrootPrompt() error {
// Determine the document root.
util.Warning("\nThe docroot is the directory from which your site is served.\nThis is a relative path from your project root at %s", app.AppRoot)
output.UserOut.Println("You may leave this value blank if your site files are in the project root")
var docrootPrompt = "Docroot Location"
var defaultDocroot = DiscoverDefaultDocroot(app)
// If there is a default docroot, display it in the prompt.
if defaultDocroot != "" {
docrootPrompt = fmt.Sprintf("%s (%s)", docrootPrompt, defaultDocroot)
} else if cd, _ := os.Getwd(); cd == filepath.Join(app.AppRoot, defaultDocroot) {
// Preserve the case where the docroot is the current directory
docrootPrompt = fmt.Sprintf("%s (current directory)", docrootPrompt)
} else {
// Explicitly state 'project root' when in a subdirectory
docrootPrompt = fmt.Sprintf("%s (project root)", docrootPrompt)
}
fmt.Print(docrootPrompt + ": ")
app.Docroot = util.GetInput(defaultDocroot)
// Ensure the docroot exists. If it doesn't, prompt the user to verify they entered it correctly.
fullPath := filepath.Join(app.AppRoot, app.Docroot)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
util.Warning("Warning: the provided docroot at %s does not currently exist.", fullPath)
// Ask the user for permission to create the docroot
if !util.Confirm(fmt.Sprintf("Create docroot at %s?", fullPath)) {
return fmt.Errorf("docroot must exist to continue configuration")
}
if err = os.MkdirAll(fullPath, 0755); err != nil {
return fmt.Errorf("unable to create docroot: %v", err)
}
util.Success("Created docroot at %s.", fullPath)
}
return nil
}
// ConfigExists determines if a ddev config file exists for this application.
func (app *DdevApp) ConfigExists() bool {
if _, err := os.Stat(app.ConfigPath); os.IsNotExist(err) {
return false
}
return true
}
// AppTypePrompt handles the Type workflow.
func (app *DdevApp) AppTypePrompt() error {
validAppTypes := strings.Join(GetValidAppTypes(), ", ")
typePrompt := fmt.Sprintf("Project Type [%s]", validAppTypes)
// First, see if we can auto detect what kind of site it is so we can set a sane default.
detectedAppType := app.DetectAppType()
// If the detected detectedAppType is php, we'll ask them to confirm,
// otherwise go with it.
// If we found an application type just set it and inform the user.
util.Success("Found a %s codebase at %s.", detectedAppType, filepath.Join(app.AppRoot, app.Docroot))
typePrompt = fmt.Sprintf("%s (%s)", typePrompt, detectedAppType)