From f276a553b0f8e827455b9b23653ebbb4736cff0b Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 12 Jan 2023 11:09:55 +1100 Subject: [PATCH 1/6] Included mysql in test suite - placeholder images to be updated --- docker-compose.yml | 14 +++++++++ main.go | 13 ++++---- mysql.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 mysql.go diff --git a/docker-compose.yml b/docker-compose.yml index c892b2a..8e169e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,6 +48,20 @@ services: ports: - '27017' + mysql-5-7: + image: chrisgio/testimages:mysql-5.7 # Placeholder image - to be updated + labels: + lagoon.type: mariadb + ports: + - "3306" + + mysql-8-0: + image: chrisgio/testimages:mysql-8.0 # Placeholder image - to be updated + labels: + lagoon.type: mariadb + ports: + - "3306" + opensearch-2: image: uselagoon/opensearch-2:latest environment: diff --git a/main.go b/main.go index b77aa78..64553a0 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ func main() { r.HandleFunc("/{solr:solr-.*}", solrHandler) r.HandleFunc("/{mongo:mongo-.*}", mongoHandler) r.HandleFunc("/{opensearch:opensearch-.*}", opensearchHandler) + r.HandleFunc("/{mysql:mysql-.*}", mysqlHandler) r.HandleFunc("/", handleReq) http.Handle("/", r) log.Fatal(http.ListenAndServe(":3000", nil)) @@ -60,7 +61,7 @@ func connectorKeyValues(values []string) string { func cleanRoute(basePath string) (string, string) { cleanRoute := strings.ReplaceAll(basePath, "/", "") - localService := strings.ReplaceAll(cleanRoute, "10.", "10-") + localService := strings.ReplaceAll(cleanRoute, ".", "-") replaceHyphen := strings.ReplaceAll(localService, "-", "_") lagoonService := strings.ToUpper(replaceHyphen) return localService, lagoonService @@ -68,9 +69,9 @@ func cleanRoute(basePath string) (string, string) { // getEnv get key environment variable if exist otherwise return defalutValue func getEnv(key, defaultValue string) string { - value := os.Getenv(key) - if len(value) == 0 { - return defaultValue - } - return value + value := os.Getenv(key) + if len(value) == 0 { + return defaultValue + } + return value } diff --git a/mysql.go b/mysql.go new file mode 100644 index 0000000..693cd02 --- /dev/null +++ b/mysql.go @@ -0,0 +1,75 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + "net/http" + "os" + "strings" + + _ "github.com/go-sql-driver/mysql" +) + +var ( + mysqlVersion string + mysqlConnectionStr string +) + +func mysqlHandler(w http.ResponseWriter, r *http.Request) { + mysqlPath := r.URL.Path + localService, lagoonService := cleanRoute(mysqlPath) + mysqlUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") + mysqlPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") + mysqlHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) + mysqlPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306") + mysqlDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") + + mysqlConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUser, mysqlPassword, mysqlHost, mysqlPort, mysqlDatabase) + log.Print(fmt.Sprintf("Using %s as the connstring", mysqlConnectionStr)) + + fmt.Fprintf(w, dbConnectorPairs(mysqlConnector(mysqlConnectionStr), mysqlVersion)) +} + +func mysqlConnector(connectionString string) map[string]string { + db, err := sql.Open("mysql", connectionString) + if err != nil { + log.Print(err) + } + + defer db.Close() + + createTable := "CREATE TABLE IF NOT EXISTS env(env_key text, env_value text)" + _, err = db.Exec(createTable) + if err != nil { + log.Print(err) + } + + query := "INSERT INTO env(env_key, env_value) VALUES (?, ?)" + + for _, e := range os.Environ() { + pair := strings.SplitN(e, "=", 2) + _, err := db.Exec(query, pair[0], pair[1]) + if err != nil { + log.Print(err) + } + } + + q := "LAGOON_%" + rows, err := db.Query(`SELECT * FROM env where env_key LIKE ?`, q) + if err != nil { + log.Print(err) + } + + db.QueryRow("SELECT VERSION()").Scan(&mysqlVersion) + + defer rows.Close() + results := make(map[string]string) + for rows.Next() { + var envKey, envValue string + _ = rows.Scan(&envKey, &envValue) + results[envKey] = envValue + } + + return results +} From cb363822841e928219bca738431113b49f9117d1 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 16 Jan 2023 17:38:30 +1100 Subject: [PATCH 2/6] Updated placeholder images --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8e169e7..6539aad 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -49,14 +49,14 @@ services: - '27017' mysql-5-7: - image: chrisgio/testimages:mysql-5.7 # Placeholder image - to be updated + image: testlagoon/mysql-5.7:experimental labels: lagoon.type: mariadb ports: - "3306" mysql-8-0: - image: chrisgio/testimages:mysql-8.0 # Placeholder image - to be updated + image: testlagoon/mysql-8.0:experimental labels: lagoon.type: mariadb ports: From ef80a4886bfa326418a21eb74bbfac2cae25022b Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 9 Nov 2023 14:55:03 +1100 Subject: [PATCH 3/6] Removed commented code --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index d248166..d7d6dd0 100644 --- a/main.go +++ b/main.go @@ -69,7 +69,6 @@ func connectorKeyValues(values []string) string { func cleanRoute(basePath string) (string, string) { cleanRoute := strings.ReplaceAll(basePath, "/", "") localService := strings.ReplaceAll(cleanRoute, ".", "-") - //localService := strings.ReplaceAll(cleanRoute, "10.", "10-") replaceHyphen := strings.ReplaceAll(localService, "-", "_") lagoonService := strings.ToUpper(replaceHyphen) return localService, lagoonService From 71dbf946d9329b59005fbfe4f7fffd4b0807bf84 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Mon, 3 Jun 2024 18:04:11 +1000 Subject: [PATCH 4/6] Refactored to utilize the mariadbHandler --- TESTING_dockercompose.md | 8 +++++ docker-compose.yml | 8 ++--- main.go | 2 +- mysql.go | 75 ---------------------------------------- 4 files changed, 13 insertions(+), 80 deletions(-) delete mode 100644 mysql.go diff --git a/TESTING_dockercompose.md b/TESTING_dockercompose.md index 7f7eb95..4090c3b 100644 --- a/TESTING_dockercompose.md +++ b/TESTING_dockercompose.md @@ -45,6 +45,14 @@ docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?servic docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10-11" | grep "SERVICE_HOST=10.11" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10-11" | grep "LAGOON_TEST_VAR=internal-services-test" +# mysql-8-0 should be able to read/write data +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-0" | grep "SERVICE_HOST=8.0.34" +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-0" | grep "LAGOON_TEST_VAR=internal-services-test" + +# mysql-8-4 should be able to read/write data +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-4" | grep "SERVICE_HOST=8.4.0" +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-4" | grep "LAGOON_TEST_VAR=internal-services-test" + # postgres-12 should be able to read/write data docker compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-12" | grep "SERVICE_HOST=PostgreSQL 12" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-12" | grep "LAGOON_TEST_VAR=internal-services-test" diff --git a/docker-compose.yml b/docker-compose.yml index 69a0d12..db3c859 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -57,15 +57,15 @@ services: ports: - '5432' - mysql-5-7: - image: testlagoon/mysql-5.7:experimental + mysql-8-0: + image: testlagoon/mysql-8.0:experimental labels: lagoon.type: mariadb ports: - "3306" - mysql-8-0: - image: testlagoon/mysql-8.0:experimental + mysql-8-4: + image: testlagoon/mysql-8.4:pr-308 # Placeholder labels: lagoon.type: mariadb ports: diff --git a/main.go b/main.go index 8a50cb7..67ea4d1 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { r.HandleFunc("/mongo", mongoHandler) r.HandleFunc("/opensearch", opensearchHandler) r.HandleFunc("/storage", persistentStorageHandler) - r.HandleFunc("/mysql", mysqlHandler) + r.HandleFunc("/mysql", mariadbHandler) r.HandleFunc("/", handleReq) http.Handle("/", r) diff --git a/mysql.go b/mysql.go deleted file mode 100644 index 014c497..0000000 --- a/mysql.go +++ /dev/null @@ -1,75 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - _ "github.com/go-sql-driver/mysql" - machineryEnvVars "github.com/uselagoon/machinery/utils/variables" - "log" - "net/http" - "os" - "strings" -) - -var ( - mysqlVersion string - mysqlConnectionStr string -) - -func mysqlHandler(w http.ResponseWriter, r *http.Request) { - service := r.URL.Query().Get("service") - localService, lagoonService := cleanRoute(service) - mysqlUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") - mysqlPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") - mysqlHost := machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) - mysqlPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306") - mysqlDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") - - mysqlConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUser, mysqlPassword, mysqlHost, mysqlPort, mysqlDatabase) - log.Print(fmt.Sprintf("Using %s as the connstring", mysqlConnectionStr)) - - fmt.Fprintf(w, dbConnectorPairs(mysqlConnector(mysqlConnectionStr), mysqlVersion)) -} - -func mysqlConnector(connectionString string) map[string]string { - db, err := sql.Open("mysql", connectionString) - if err != nil { - log.Print(err) - } - - defer db.Close() - - createTable := "CREATE TABLE IF NOT EXISTS env(env_key text, env_value text)" - _, err = db.Exec(createTable) - if err != nil { - log.Print(err) - } - - query := "INSERT INTO env(env_key, env_value) VALUES (?, ?)" - - for _, e := range os.Environ() { - pair := strings.SplitN(e, "=", 2) - _, err := db.Exec(query, pair[0], pair[1]) - if err != nil { - log.Print(err) - } - } - - q := "LAGOON_%" - rows, err := db.Query(`SELECT * FROM env where env_key LIKE ?`, q) - if err != nil { - log.Print(err) - } - - db.QueryRow("SELECT VERSION()").Scan(&mysqlVersion) - - defer rows.Close() - results := make(map[string]string) - for rows.Next() { - var envKey, envValue string - _ = rows.Scan(&envKey, &envValue) - results[envKey] = envValue - } - - return results -} From 9b1900ea131130b77e81dfde97e2f92327e4b482 Mon Sep 17 00:00:00 2001 From: Toby Bellwood Date: Thu, 6 Jun 2024 07:56:43 +1000 Subject: [PATCH 5/6] Apply suggestions from code review --- TESTING_dockercompose.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TESTING_dockercompose.md b/TESTING_dockercompose.md index 4090c3b..1fae851 100644 --- a/TESTING_dockercompose.md +++ b/TESTING_dockercompose.md @@ -46,11 +46,11 @@ docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?servic docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10-11" | grep "LAGOON_TEST_VAR=internal-services-test" # mysql-8-0 should be able to read/write data -docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-0" | grep "SERVICE_HOST=8.0.34" +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-0" | grep "SERVICE_HOST=8.0" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-0" | grep "LAGOON_TEST_VAR=internal-services-test" # mysql-8-4 should be able to read/write data -docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-4" | grep "SERVICE_HOST=8.4.0" +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-4" | grep "SERVICE_HOST=8.4" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-4" | grep "LAGOON_TEST_VAR=internal-services-test" # postgres-12 should be able to read/write data From 74880a390a4f88c45b5ccaa45188584ae20d4024 Mon Sep 17 00:00:00 2001 From: Chris Goodwin <40746380+CGoodwin90@users.noreply.github.com> Date: Thu, 6 Jun 2024 09:09:20 +1000 Subject: [PATCH 6/6] Updated mysql tag Co-authored-by: Toby Bellwood --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index db3c859..2673568 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -58,7 +58,7 @@ services: - '5432' mysql-8-0: - image: testlagoon/mysql-8.0:experimental + image: testlagoon/mysql-8.0:pr-308 labels: lagoon.type: mariadb ports: