diff --git a/docker-compose.yml b/docker-compose.yml index 0fd3bd2..36f253a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,6 +25,7 @@ services: - STORAGE_LOCATION=/app/files volumes: - scratch:/app/files + - /var/run/docker.sock:/var/run/docker.sock mariadb-10-5: image: uselagoon/mariadb-10.5:latest diff --git a/go.mod b/go.mod index 93a1f70..ee6734a 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/uselagoon/machinery v0.0.29 github.com/vanng822/go-solr v0.10.0 go.mongodb.org/mongo-driver v1.16.1 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -26,5 +27,5 @@ require ( github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect golang.org/x/crypto v0.22.0 // indirect golang.org/x/sync v0.7.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/text v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 82d9b14..342aefe 100644 --- a/go.sum +++ b/go.sum @@ -83,11 +83,13 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/landingTemplate.html b/landingTemplate.html new file mode 100644 index 0000000..e97222c --- /dev/null +++ b/landingTemplate.html @@ -0,0 +1,28 @@ + + + + + Internal Services Test + + +

Internal Services Test

+

This project was designed to perform a functional test on a given native Lagoon service.

+

It works by:

+ +

To check a service, use this format http://0.0.0.0:3000/{service-type}?service={service-name}

+

Available Services:

+ + + diff --git a/main.go b/main.go index 67ea4d1..156183c 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,25 @@ import ( "bytes" "fmt" "github.com/gorilla/mux" + machinery "github.com/uselagoon/machinery/utils/variables" + "gopkg.in/yaml.v3" + "html/template" "log" "net/http" + "os" + "regexp" "strings" "time" ) -type funcType func() map[string]string +type LandingPageData struct { + Services []Service +} + +type Service struct { + Type string + Name string +} func main() { r := mux.NewRouter() @@ -28,6 +40,53 @@ func main() { log.Fatal(http.ListenAndServe(":3000", handler(r))) } +// getServices Parses docker-compose.yml and returns a list of services +func getServices() []Service { + type BuildConfig struct { + Context string `yaml:"context,omitempty"` + Dockerfile string `yaml:"dockerfile,omitempty"` + } + + type ServiceConfig struct { + Image string `yaml:"image,omitempty"` + Labels map[string]string `yaml:"labels,omitempty"` + Build BuildConfig `yaml:"build,omitempty"` + Ports []string `yaml:"ports,omitempty"` + Volumes []string `yaml:"volumes,omitempty"` + } + + type DockerCompose struct { + Services map[string]ServiceConfig `yaml:"services"` + Volumes map[string]interface{} `yaml:"volumes,omitempty"` + } + + compose := &DockerCompose{} + data, err := os.ReadFile("docker-compose.yml") + if err != nil { + log.Fatalf("Error reading docker-compose.yml: %v", err) + } + + err = yaml.Unmarshal(data, compose) + if err != nil { + log.Fatalf("Error parsing docker-compose.yml: %v", err) + } + + var serviceList []Service + typeRegexp := regexp.MustCompile(`^\w*`) + for service := range compose.Services { + if service != "web" { + serviceType := typeRegexp.FindString(service) + serviceList = append(serviceList, Service{Type: serviceType, Name: service}) + } + } + storage := machinery.GetEnv("STORAGE_LOCATION", "") + if storage != "" { + serviceList = append(serviceList, Service{Type: "storage", Name: storage}) + } + + return serviceList +} + func handler(m *mux.Router) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { driver := strings.ReplaceAll(r.URL.Path, "/", "") @@ -44,9 +103,20 @@ func handler(m *mux.Router) http.HandlerFunc { } func handleReq(w http.ResponseWriter, r *http.Request) { - var funcToCall []funcType - for _, conFunc := range funcToCall { - fmt.Fprintf(w, dbConnectorPairs(conFunc(), "")) + tmpl, err := template.ParseFiles("landingTemplate.html") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + serviceList := getServices() + data := LandingPageData{ + Services: serviceList, + } + + err = tmpl.Execute(w, data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) } }