|
| 1 | +package do |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// RunPlaybooks runs a sequence of ansible playbook commands. |
| 11 | +func RunPlaybooks(inventory string, commands [][]string) error { |
| 12 | + for _, command := range commands { |
| 13 | + if err := RunPlaybook(inventory, command); err != nil { |
| 14 | + return err |
| 15 | + } |
| 16 | + } |
| 17 | + return nil |
| 18 | +} |
| 19 | + |
| 20 | +func BuildPgsqlAddCommands(cluster string, ips []string) ([][]string, error) { |
| 21 | + if cluster == "" { |
| 22 | + return nil, fmt.Errorf("pgsql cluster is required") |
| 23 | + } |
| 24 | + if len(ips) == 0 { |
| 25 | + return [][]string{{"pgsql.yml", "-l", cluster}}, nil |
| 26 | + } |
| 27 | + target, exists, err := buildPgsqlInstancePatterns(cluster, ips) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + return [][]string{ |
| 32 | + {"pgsql.yml", "-l", target}, |
| 33 | + {"pgsql.yml", "-l", exists, "-t", "pg_service"}, |
| 34 | + }, nil |
| 35 | +} |
| 36 | + |
| 37 | +func BuildPgsqlRmCommands(cluster string, ips []string, uninstall bool) ([][]string, error) { |
| 38 | + if cluster == "" { |
| 39 | + return nil, fmt.Errorf("pgsql cluster is required") |
| 40 | + } |
| 41 | + selector := cluster |
| 42 | + if len(ips) > 0 { |
| 43 | + target, _, err := buildPgsqlInstancePatterns(cluster, ips) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + selector = target |
| 48 | + } |
| 49 | + command := []string{"pgsql-rm.yml", "-l", selector} |
| 50 | + if uninstall { |
| 51 | + command = append(command, "-e", "pg_rm_pkg=true") |
| 52 | + } |
| 53 | + return [][]string{command}, nil |
| 54 | +} |
| 55 | + |
| 56 | +func BuildNodeAddCommand(selectors []string) ([]string, error) { |
| 57 | + selector, err := joinSelectors(selectors) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + return []string{"node.yml", "-l", selector}, nil |
| 62 | +} |
| 63 | + |
| 64 | +func BuildNodeRmCommand(selectors []string) ([]string, error) { |
| 65 | + selector, err := joinSelectors(selectors) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + return []string{"node-rm.yml", "-l", selector}, nil |
| 70 | +} |
| 71 | + |
| 72 | +func BuildNodeRepoCommand(args []string) ([]string, error) { |
| 73 | + if len(args) > 2 { |
| 74 | + return nil, fmt.Errorf("node-repo accepts at most selector and module") |
| 75 | + } |
| 76 | + command := []string{"node.yml", "-t", "node_repo"} |
| 77 | + if len(args) >= 1 && args[0] != "" { |
| 78 | + command = append(command, "-l", args[0]) |
| 79 | + } |
| 80 | + if len(args) == 2 && args[1] != "" { |
| 81 | + command = append(command, "-e", fmt.Sprintf("node_repo_modules=%s", args[1])) |
| 82 | + } |
| 83 | + return command, nil |
| 84 | +} |
| 85 | + |
| 86 | +func BuildRedisAddCommands(selector string, ports []string) ([][]string, error) { |
| 87 | + if selector == "" { |
| 88 | + return nil, fmt.Errorf("redis selector is required") |
| 89 | + } |
| 90 | + if len(ports) == 0 { |
| 91 | + return [][]string{{"redis.yml", "-l", selector}}, nil |
| 92 | + } |
| 93 | + if !isIPv4(selector) { |
| 94 | + return nil, fmt.Errorf("redis instance operations require an IP selector") |
| 95 | + } |
| 96 | + commands := make([][]string, 0, len(ports)) |
| 97 | + for _, port := range ports { |
| 98 | + if err := validateRedisPort(port); err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + commands = append(commands, []string{"redis.yml", "-l", selector, "-e", fmt.Sprintf("redis_port=%s", port)}) |
| 102 | + } |
| 103 | + return commands, nil |
| 104 | +} |
| 105 | + |
| 106 | +func BuildRedisRmCommands(selector string, ports []string, uninstall bool) ([][]string, error) { |
| 107 | + if selector == "" { |
| 108 | + return nil, fmt.Errorf("redis selector is required") |
| 109 | + } |
| 110 | + if len(ports) == 0 { |
| 111 | + command := []string{"redis-rm.yml", "-l", selector} |
| 112 | + if uninstall { |
| 113 | + command = append(command, "-e", "redis_rm_pkg=true") |
| 114 | + } |
| 115 | + return [][]string{command}, nil |
| 116 | + } |
| 117 | + if !isIPv4(selector) { |
| 118 | + return nil, fmt.Errorf("redis instance operations require an IP selector") |
| 119 | + } |
| 120 | + if uninstall { |
| 121 | + return nil, fmt.Errorf("redis package uninstall is only supported for node or cluster removal") |
| 122 | + } |
| 123 | + commands := make([][]string, 0, len(ports)) |
| 124 | + for _, port := range ports { |
| 125 | + if err := validateRedisPort(port); err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + command := []string{"redis-rm.yml", "-l", selector, "-e", fmt.Sprintf("redis_port=%s", port)} |
| 129 | + commands = append(commands, command) |
| 130 | + } |
| 131 | + return commands, nil |
| 132 | +} |
| 133 | + |
| 134 | +func buildPgsqlInstancePatterns(cluster string, ips []string) (string, string, error) { |
| 135 | + target := "&" + cluster |
| 136 | + existing := cluster |
| 137 | + for _, ip := range ips { |
| 138 | + if !isIPv4(ip) { |
| 139 | + return "", "", fmt.Errorf("invalid ip address: %s", ip) |
| 140 | + } |
| 141 | + target = ip + "," + target |
| 142 | + existing += ",!" + ip |
| 143 | + } |
| 144 | + return target, existing, nil |
| 145 | +} |
| 146 | + |
| 147 | +func joinSelectors(selectors []string) (string, error) { |
| 148 | + if len(selectors) == 0 { |
| 149 | + return "", fmt.Errorf("selector is required") |
| 150 | + } |
| 151 | + for _, selector := range selectors { |
| 152 | + if selector == "" { |
| 153 | + return "", fmt.Errorf("selector cannot be empty") |
| 154 | + } |
| 155 | + } |
| 156 | + return strings.Join(selectors, ","), nil |
| 157 | +} |
| 158 | + |
| 159 | +func validateRedisPort(port string) error { |
| 160 | + n, err := strconv.Atoi(port) |
| 161 | + if err != nil || n < 1024 || n > 65535 { |
| 162 | + return fmt.Errorf("invalid redis port: %s", port) |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func isIPv4(value string) bool { |
| 168 | + ip := net.ParseIP(value) |
| 169 | + return ip != nil && ip.To4() != nil |
| 170 | +} |
0 commit comments