1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17 package serverinit
18
19 import (
20 "fmt"
21 "os"
22 "strings"
23
24 "perkeep.org/internal/osutil"
25 "perkeep.org/pkg/env"
26 "perkeep.org/pkg/types/serverconfig"
27
28 "cloud.google.com/go/compute/metadata"
29 )
30
31 const (
32
33
34
35 useDBNamesConfig = "useDBNamesConfig"
36 )
37
38
39
40
41 func DefaultEnvConfig() (*Config, error) {
42 if !env.OnGCE() {
43 return nil, nil
44 }
45 auth := "none"
46 user, _ := metadata.InstanceAttributeValue("camlistore-username")
47 pass, _ := metadata.InstanceAttributeValue("camlistore-password")
48 confBucket, err := metadata.InstanceAttributeValue("camlistore-config-dir")
49 if confBucket == "" || err != nil {
50 return nil, fmt.Errorf("VM instance metadata key 'camlistore-config-dir' not set: %v", err)
51 }
52 blobBucket, err := metadata.InstanceAttributeValue("camlistore-blob-dir")
53 if blobBucket == "" || err != nil {
54 return nil, fmt.Errorf("VM instance metadata key 'camlistore-blob-dir' not set: %v", err)
55 }
56 if user != "" && pass != "" {
57 auth = "userpass:" + user + ":" + pass
58 }
59
60 if v := osutil.SecretRingFile(); !strings.HasPrefix(v, "/gcs/") {
61 return nil, fmt.Errorf("Internal error: secret ring path on GCE should be at /gcs/, not %q", v)
62 }
63 keyID, secRing, err := getOrMakeKeyring()
64 if err != nil {
65 return nil, err
66 }
67
68 highConf := &serverconfig.Config{
69 Auth: auth,
70 HTTPS: true,
71 Identity: keyID,
72 IdentitySecretRing: secRing,
73 GoogleCloudStorage: ":" + strings.TrimPrefix(blobBucket, "gs://"),
74 PackRelated: true,
75 ShareHandler: true,
76 }
77
78 hostName, _ := metadata.InstanceAttributeValue("camlistore-hostname")
79
80
81
82
83
84
85
86
87 if hostName != "" && !strings.HasSuffix(hostName, "camlistore.net") {
88 highConf.BaseURL = fmt.Sprintf("https://%s", hostName)
89 highConf.Listen = "0.0.0.0:443"
90 } else {
91 panic("unsupported legacy configuration using camlistore.net is no longer supported")
92 }
93
94
95 mysqlPort := os.Getenv("MYSQLDB_PORT")
96 if !strings.HasPrefix(mysqlPort, "tcp://") {
97
98
99 highConf.KVFile = "/index.kv"
100 return genLowLevelConfig(highConf)
101 }
102 hostPort := strings.TrimPrefix(mysqlPort, "tcp://")
103 highConf.MySQL = "root@" + hostPort + ":"
104 configVersion, err := metadata.InstanceAttributeValue("perkeep-config-version")
105 if configVersion == "" || err != nil {
106
107
108 highConf.DBUnique = useDBNamesConfig
109 } else if configVersion != "1" {
110 return nil, fmt.Errorf("unexpected value for VM instance metadata key 'perkeep-config-version': %q", configVersion)
111 }
112
113 conf, err := genLowLevelConfig(highConf)
114 if err != nil {
115 return nil, err
116 }
117
118 if err := conf.readFields(); err != nil {
119 return nil, err
120 }
121 return conf, nil
122 }