Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove release name flag from activate #129

Merged
merged 8 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

var (
activate_type, releaseName, composeFilePath, org_id string
develMode bool
activate_type, composeFilePath, org_id string
develMode bool
)

// registerCmd represents the register command
Expand Down Expand Up @@ -50,7 +50,7 @@ hossted activate
return
}

err := service.ActivateK8s(releaseName, develMode)
err := service.ActivateK8s(develMode)
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -96,7 +96,6 @@ hossted activate
func init() {
rootCmd.AddCommand(activateCmd)
activateCmd.Flags().StringVarP(&activate_type, "type", "t", "", "supported env type k8s|compose")
activateCmd.Flags().StringVar(&releaseName, "release_name", "", "release name (optional)")
activateCmd.Flags().StringVarP(&composeFilePath, "compose_filepath", "f", "", "compose filepath (optional)")
activateCmd.Flags().BoolVar(&develMode, "d", false, "Toggle development mode")
}
3 changes: 2 additions & 1 deletion hossted/service/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ const (
hosstedOperatorReleaseName = "hossted-operator"
trivyOperatorReleaseName = "trivy-operator"
grafanaAgentReleaseName = "hossted-grafana-agent"
releaseName = "hossted-platform"
)

// ActivateK8s imports Kubernetes clusters.
func ActivateK8s(releaseName string, develMode bool) error {
func ActivateK8s(develMode bool) error {

// emailsID, err := getEmail()
// if err != nil {
Expand Down
97 changes: 80 additions & 17 deletions hossted/service/compose/reconcile_compose.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compose

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -45,15 +46,17 @@ func ReconcileCompose(osInfo OsInfo, enableMonitoring string) error {
if err != nil {
return err
}

err = sendComposeInfo(appFilePath, osInfo)
if err != nil {
return err
}
if isComposeStateChange {
// send compose info
err = sendComposeInfo(appFilePath, osInfo)
if err != nil {
return err
}
}

return nil

}
Expand Down Expand Up @@ -135,9 +138,10 @@ func setClusterInfo(osInfo OsInfo, osFilePath string) (OsInfo, error) {

func checkUUID(osFilePath string) (string, error) {
var osData OsInfo

data, err := readFile(osFilePath)
if err != nil {
return "", err
//return "", err
}

err = yaml.Unmarshal(data, &osData)
Expand Down Expand Up @@ -191,6 +195,16 @@ type optionsState struct {
CVEScan bool `json:"cve_scan"`
}

type URLInfo struct {
URL string `json:"url"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
}

type AccessInfo struct {
URLs []URLInfo `json:"urls"`
}

type request struct {
UUID string `json:"uuid"`
OsUUID string `json:"osuuid"`
Expand All @@ -202,6 +216,7 @@ type request struct {
Memory string `json:"memory"`
OptionsState optionsState `json:"options_state"`
ComposeFile string `json:"compose_file"`
AccessInfo AccessInfo `json:"access_info"`
}

type dockerInstance struct {
Expand Down Expand Up @@ -231,6 +246,8 @@ func sendComposeInfo(appFilePath string, osInfo OsInfo) error {
composeUrl := hosstedAPIUrl + "/compose/hosts"
containersUrl := hosstedAPIUrl + "/compose/containers"

access_info := getAccessInfo(osInfo.ProjectName)

var data map[string]AppRequest
err = json.Unmarshal(composeInfo, &data)
if err != nil {
Expand All @@ -247,14 +264,15 @@ func sendComposeInfo(appFilePath string, osInfo OsInfo) error {
}
for appName, compose := range data {
newReq := request{
UUID: compose.AppAPIInfo.AppUUID,
OsUUID: compose.AppAPIInfo.OsUUID,
Email: compose.AppAPIInfo.EmailID,
OrgID: orgID,
Type: "compose",
Product: appName,
CPUNum: cpu,
Memory: mem,
UUID: compose.AppAPIInfo.AppUUID,
OsUUID: compose.AppAPIInfo.OsUUID,
Email: compose.AppAPIInfo.EmailID,
AccessInfo: *access_info,
OrgID: orgID,
Type: "compose",
Product: appName,
CPUNum: cpu,
Memory: mem,
OptionsState: optionsState{
Monitoring: true,
Logging: true,
Expand Down Expand Up @@ -397,6 +415,8 @@ func prepareComposeRequest(
return appsData, isComposeStateChange, err
}

fmt.Println(uniqueProjects)

// Create a slice of existing apps
existingApps := make(map[string]bool)
for app := range appsData {
Expand Down Expand Up @@ -563,12 +583,12 @@ func runMonitoringCompose(monitoringEnable, osUUID, appUUID string) error {
configStr = strings.Replace(configStr, "${APP_UUID}", fmt.Sprintf("\"%s\"", appUUID), -1)

// Replace MIMIR_USERNAME and MIMIR_PASSWORD placeholders
mimirUsername := os.Getenv("MIMIR_USERNAME")
mimirPassword := os.Getenv("MIMIR_PASSWORD")
mimirURL := os.Getenv("MIMIR_URL")
lokiUsername := os.Getenv("LOKI_USERNAME")
lokiPassword := os.Getenv("LOKI_PASSWORD")
lokiURL := os.Getenv("LOKI_URL")
mimirUsername := common.MIMIR_USERNAME
mimirPassword := common.MIMIR_PASSWORD
mimirURL := common.MIMIR_URL
lokiUsername := common.LOKI_USERNAME
lokiPassword := common.LOKI_PASSWORD
lokiURL := common.LOKI_URL

if mimirUsername == "" || mimirPassword == "" || mimirURL == "" {
log.Fatalf("MIMIR_USERNAME, MIMIR_URL and MIMIR_PASSWORD environment variables must be set")
Expand Down Expand Up @@ -607,3 +627,46 @@ func runMonitoringCompose(monitoringEnable, osUUID, appUUID string) error {
}
return nil
}

func getAccessInfo(filepath string) *AccessInfo {
file, err := os.Open(filepath)
if err != nil {
log.Fatalf("Failed to open file for Access Info: %s", err)
}
defer file.Close()

config := AccessInfo{
URLs: []URLInfo{
{},
},
}

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
fmt.Println("Invalid line:", line)
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])

switch key {
case "PROJECT_BASE_URL":
config.URLs[0].URL = value
case "H_EMAIL":
config.URLs[0].User = value
case "APP_PASSWORD":
config.URLs[0].Password = value
}
}

if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
return &config
}