retry
This commit is contained in:
parent
8fb4e0f3da
commit
7ce835c4de
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
69
main.go
69
main.go
|
@ -67,12 +67,10 @@ func main() {
|
|||
isSwarm := isDockerSwarmStack(*stackName, containers)
|
||||
|
||||
if isSwarm {
|
||||
// Handle Docker Swarm stack export
|
||||
err = exportSwarmStackToComposeFile(*stackName, *outputFile)
|
||||
if err != nil {
|
||||
log.Fatalf("Error exporting Docker Swarm stack %s: %v", *stackName, err)
|
||||
}
|
||||
fmt.Printf("docker-compose.yml generated successfully for Docker Swarm stack '%s' in %s\n", *stackName, *outputFile)
|
||||
// Notify the user that exporting Swarm stacks isn't fully supported
|
||||
fmt.Printf("Warning: The stack '%s' appears to be a Docker Swarm stack. Export functionality for Swarm stacks may not provide complete configurations.\n", *stackName)
|
||||
fmt.Println("Skipping export for Docker Swarm stacks. Please use `docker stack` commands directly for managing Swarm stacks.")
|
||||
return
|
||||
} else {
|
||||
// Handle Docker Compose project export
|
||||
err = exportComposeProjectToComposeFile(*stackName, containers, *outputFile)
|
||||
|
@ -186,65 +184,6 @@ func isDockerSwarmStack(stackName string, containers []Container) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// exportSwarmStackToComposeFile exports a Docker Swarm stack to a docker-compose.yml file.
|
||||
func exportSwarmStackToComposeFile(stackName string, outputFile string) error {
|
||||
// Fetch stack details using `docker stack services` and construct the docker-compose.yml
|
||||
cmd := exec.Command("docker", "stack", "services", stackName, "--format", "{{.Name}} {{.Image}} {{.Ports}} {{.Replicas}}")
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching Docker Swarm stack services: %v", err)
|
||||
}
|
||||
|
||||
composeConfig := make(map[string]interface{})
|
||||
composeConfig["services"] = make(map[string]interface{})
|
||||
|
||||
scanner := bufio.NewScanner(&out)
|
||||
for scanner.Scan() {
|
||||
fields := strings.Fields(scanner.Text())
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
serviceName := fields[0]
|
||||
image := fields[1]
|
||||
|
||||
serviceConfig := map[string]interface{}{
|
||||
"image": image,
|
||||
}
|
||||
|
||||
// Handle additional fields like ports and replicas
|
||||
if len(fields) > 2 {
|
||||
// Parse ports and replicas if available
|
||||
ports := fields[2]
|
||||
replicas := fields[3]
|
||||
serviceConfig["deploy"] = map[string]interface{}{
|
||||
"replicas": replicas,
|
||||
}
|
||||
if ports != "" {
|
||||
serviceConfig["ports"] = []string{ports}
|
||||
}
|
||||
}
|
||||
|
||||
composeConfig["services"].(map[string]interface{})[serviceName] = serviceConfig
|
||||
}
|
||||
|
||||
// Convert to YAML
|
||||
yamlData, err := yaml.Marshal(&composeConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating YAML for Docker Swarm stack %s: %v", stackName, err)
|
||||
}
|
||||
|
||||
// Write YAML to a file
|
||||
err = os.WriteFile(outputFile, yamlData, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing to file %s: %v", outputFile, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// exportComposeProjectToComposeFile exports a Docker Compose project to a docker-compose.yml file.
|
||||
func exportComposeProjectToComposeFile(stack string, containers []Container, outputFile string) error {
|
||||
// Map to hold docker-compose configuration
|
||||
|
|
Loading…
Reference in New Issue