|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "collector/utils" |
| 5 | + "fmt" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type ServiceTypeConfig struct { |
| 12 | + CollectorType Collector `yaml:"collector_type"` |
| 13 | +} |
| 14 | + |
| 15 | +const ( |
| 16 | + As400Jar = "as400-extractor-2.2.0-jar-with-dependencies.jar" |
| 17 | + JavaVersion = "jdk-11.0.13.8" |
| 18 | + CollectorConfigFile = "collector-config.yaml" |
| 19 | + SERV_LOG = "utmstack_collector.log" |
| 20 | +) |
| 21 | + |
| 22 | +type ServiceConfig struct { |
| 23 | + Name string |
| 24 | + DisplayName string |
| 25 | + Description string |
| 26 | + CMDRun string |
| 27 | + CMDArgs []string |
| 28 | + CMDPath string |
| 29 | +} |
| 30 | + |
| 31 | +type Collector string |
| 32 | + |
| 33 | +var ( |
| 34 | + AS400 Collector = "as400" |
| 35 | +) |
| 36 | + |
| 37 | +func IsValidCollector(c string) bool { |
| 38 | + switch c { |
| 39 | + case string(AS400): |
| 40 | + return true |
| 41 | + default: |
| 42 | + return false |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func GetJavaPath() string { |
| 47 | + currentPath, _ := utils.GetMyPath() |
| 48 | + switch runtime.GOOS { |
| 49 | + case "windows": |
| 50 | + return filepath.Join(currentPath, "dependencies", JavaVersion, "bin", "java.exe") |
| 51 | + default: |
| 52 | + return "" |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func GetAs400Command(action, host, connectionKey string) (string, []string) { |
| 57 | + currentPath, _ := utils.GetMyPath() |
| 58 | + cmd := "" |
| 59 | + args := []string{} |
| 60 | + |
| 61 | + action = strings.ToUpper(action) |
| 62 | + switch runtime.GOOS { |
| 63 | + case "windows": |
| 64 | + cmd = GetJavaPath() |
| 65 | + case "linux": |
| 66 | + cmd = "java" |
| 67 | + default: |
| 68 | + return "unknown operating system", nil |
| 69 | + } |
| 70 | + |
| 71 | + args = append(args, "-jar", filepath.Join(currentPath, As400Jar), fmt.Sprintf("-option=%s", action)) |
| 72 | + if action == "INSTALL" { |
| 73 | + args = append(args, fmt.Sprintf("-collector-manager-host=%s", host), "-collector-manager-port=9000", "-logs-port=50051", fmt.Sprintf("-connection-key=%s", connectionKey)) |
| 74 | + } |
| 75 | + return cmd, args |
| 76 | +} |
| 77 | + |
| 78 | +func ValidateParams(params []string) bool { |
| 79 | + action := params[1] |
| 80 | + collector := params[2] |
| 81 | + if !IsValidCollector(collector) { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + if action == "install" { |
| 86 | + if len(params) < 5 { |
| 87 | + return false |
| 88 | + } |
| 89 | + } else if action == "uninstall" { |
| 90 | + if len(params) < 3 { |
| 91 | + return false |
| 92 | + } |
| 93 | + } else { |
| 94 | + return false |
| 95 | + } |
| 96 | + |
| 97 | + return true |
| 98 | +} |
| 99 | + |
| 100 | +func GetServiceConfig(c Collector) ServiceConfig { |
| 101 | + path, _ := utils.GetMyPath() |
| 102 | + switch c { |
| 103 | + case AS400: |
| 104 | + cmd, args := GetAs400Command("run", "", "") |
| 105 | + return ServiceConfig{ |
| 106 | + Name: "UTMStackAS400Collector", |
| 107 | + DisplayName: "UTMStack AS400 Collector", |
| 108 | + Description: "UTMStack AS400 Collector", |
| 109 | + CMDRun: cmd, |
| 110 | + CMDArgs: args, |
| 111 | + CMDPath: path, |
| 112 | + } |
| 113 | + default: |
| 114 | + return ServiceConfig{} |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func SaveConfig(config *ServiceTypeConfig) error { |
| 119 | + path, err := utils.GetMyPath() |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to get current path: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + if err := utils.WriteYAML(filepath.Join(path, CollectorConfigFile), config); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func ReadConfig() (*ServiceTypeConfig, error) { |
| 132 | + path, err := utils.GetMyPath() |
| 133 | + if err != nil { |
| 134 | + return nil, fmt.Errorf("failed to get current path: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + config := &ServiceTypeConfig{} |
| 138 | + if err = utils.ReadYAML(filepath.Join(path, CollectorConfigFile), config); err != nil { |
| 139 | + return nil, fmt.Errorf("error reading config file: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + return config, nil |
| 143 | +} |
0 commit comments