-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo.go
105 lines (90 loc) · 3.17 KB
/
mongo.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
package main
import (
"bytes"
"context"
"fmt"
machineryEnvVars "github.com/uselagoon/machinery/utils/variables"
"log"
"net/http"
"os"
"regexp"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
mongoConnectionStr string
mongoHost string
database string
)
func mongoHandler(w http.ResponseWriter, r *http.Request) {
service := r.URL.Query().Get("service")
localService, lagoonService := cleanRoute(service)
mongoUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon")
mongoPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon")
mongoHost = machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService)
mongoPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "27017")
mongoDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon")
if mongoHost != localService {
mongoConnectionStr = fmt.Sprintf("mongodb://%s:%s@%s:%s/%s", mongoUser, mongoPassword, mongoHost, mongoPort, mongoDatabase)
database = mongoDatabase
} else {
mongoConnectionStr = fmt.Sprintf("mongodb://%s:%s", mongoHost, mongoPort)
database = mongoDatabase
}
log.Print(fmt.Sprintf("Using %s as the connstring", mongoConnectionStr))
fmt.Fprintf(w, mongoConnector(mongoConnectionStr, database))
}
func cleanMongoOutput(docs []primitive.M) string {
valStr := fmt.Sprint(docs)
r := regexp.MustCompile(`(?:LAGOON_\w*)\s\w*:(?:[^\[\]\)\(]*)`)
matches := r.FindAllString(valStr, -1)
var mongoResults []string
for _, str := range matches {
mongoVals := strings.ReplaceAll(str, "value:", "")
mongoResults = append(mongoResults, mongoVals)
}
b := new(bytes.Buffer)
for _, value := range mongoResults {
v := strings.SplitN(value, " ", 2)
fmt.Fprintf(b, "\"%s=%s\"\n", v[0], v[1])
}
host := fmt.Sprintf(`"SERVICE_HOST=%s"`, mongoHost)
mongoOutput := host + "\n" + b.String()
return mongoOutput
}
func mongoConnector(connectionString string, database string) string {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(connectionString))
if err != nil {
log.Print(err)
}
envCollection := client.Database(database).Collection("env-vars")
deleteFilter := bson.D{{}}
_, err = envCollection.DeleteMany(context.TODO(), deleteFilter)
if err != nil {
log.Print(err)
}
environmentVariables := []interface{}{}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
bsonData := bson.D{{Key: "Key", Value: pair[0]}, {Key: "value", Value: pair[1]}}
environmentVariables = append(environmentVariables, bsonData)
if err != nil {
log.Print(err)
}
}
_, err = envCollection.InsertMany(context.TODO(), environmentVariables)
if err != nil {
log.Print(err)
}
filter := bson.D{{Key: "Key", Value: primitive.Regex{Pattern: "LAGOON", Options: ""}}}
cursor, _ := envCollection.Find(context.TODO(), filter, options.Find().SetProjection(bson.M{"_id": 0}))
var docs []bson.M
if err = cursor.All(context.TODO(), &docs); err != nil {
log.Print(err)
}
mongoOutput := cleanMongoOutput(docs)
return mongoOutput
}