mirror of
https://github.com/fosrl/pangolin.git
synced 2025-05-13 22:00:45 +01:00
refactoring multiple used code parts, cleared some warnings and added more error checking
This commit is contained in:
parent
c837899d82
commit
181071e4f6
1 changed files with 58 additions and 136 deletions
170
install/main.go
170
install/main.go
|
@ -87,7 +87,15 @@ func main() {
|
||||||
|
|
||||||
if isDockerInstalled() {
|
if isDockerInstalled() {
|
||||||
if readBool(reader, "Would you like to install and start the containers?", true) {
|
if readBool(reader, "Would you like to install and start the containers?", true) {
|
||||||
pullAndStartContainers()
|
if err := pullContainers(); err != nil {
|
||||||
|
fmt.Println("Error: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := startContainers(); err != nil {
|
||||||
|
fmt.Println("Error: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -427,24 +435,24 @@ func installDocker() error {
|
||||||
apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
||||||
`, dockerArch))
|
`, dockerArch))
|
||||||
case strings.Contains(osRelease, "ID=fedora"):
|
case strings.Contains(osRelease, "ID=fedora"):
|
||||||
installCmd = exec.Command("bash", "-c", fmt.Sprintf(`
|
installCmd = exec.Command("bash", "-c", `
|
||||||
dnf -y install dnf-plugins-core &&
|
dnf -y install dnf-plugins-core &&
|
||||||
dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo &&
|
dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo &&
|
||||||
dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
||||||
`))
|
`)
|
||||||
case strings.Contains(osRelease, "ID=opensuse") || strings.Contains(osRelease, "ID=\"opensuse-"):
|
case strings.Contains(osRelease, "ID=opensuse") || strings.Contains(osRelease, "ID=\"opensuse-"):
|
||||||
installCmd = exec.Command("bash", "-c", `
|
installCmd = exec.Command("bash", "-c", `
|
||||||
zypper install -y docker docker-compose &&
|
zypper install -y docker docker-compose &&
|
||||||
systemctl enable docker
|
systemctl enable docker
|
||||||
`)
|
`)
|
||||||
case strings.Contains(osRelease, "ID=rhel") || strings.Contains(osRelease, "ID=\"rhel"):
|
case strings.Contains(osRelease, "ID=rhel") || strings.Contains(osRelease, "ID=\"rhel"):
|
||||||
installCmd = exec.Command("bash", "-c", fmt.Sprintf(`
|
installCmd = exec.Command("bash", "-c", `
|
||||||
dnf remove -y runc &&
|
dnf remove -y runc &&
|
||||||
dnf -y install yum-utils &&
|
dnf -y install yum-utils &&
|
||||||
dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo &&
|
dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo &&
|
||||||
dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin &&
|
dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin &&
|
||||||
systemctl enable docker
|
systemctl enable docker
|
||||||
`))
|
`)
|
||||||
case strings.Contains(osRelease, "ID=amzn"):
|
case strings.Contains(osRelease, "ID=amzn"):
|
||||||
installCmd = exec.Command("bash", "-c", `
|
installCmd = exec.Command("bash", "-c", `
|
||||||
yum update -y &&
|
yum update -y &&
|
||||||
|
@ -468,162 +476,76 @@ func isDockerInstalled() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCommandString(useNewStyle bool) string {
|
// executeDockerComposeCommandWithArgs executes the appropriate docker command with arguments supplied
|
||||||
if useNewStyle {
|
func executeDockerComposeCommandWithArgs(args ...string) error {
|
||||||
return "'docker compose'"
|
var cmd *exec.Cmd
|
||||||
}
|
|
||||||
return "'docker-compose'"
|
|
||||||
}
|
|
||||||
|
|
||||||
func pullAndStartContainers() error {
|
|
||||||
fmt.Println("Starting containers...")
|
|
||||||
|
|
||||||
// Check which docker compose command is available
|
|
||||||
var useNewStyle bool
|
var useNewStyle bool
|
||||||
|
|
||||||
|
if !isDockerInstalled() {
|
||||||
|
return fmt.Errorf("docker is not installed")
|
||||||
|
}
|
||||||
|
|
||||||
checkCmd := exec.Command("docker", "compose", "version")
|
checkCmd := exec.Command("docker", "compose", "version")
|
||||||
if err := checkCmd.Run(); err == nil {
|
if err := checkCmd.Run(); err == nil {
|
||||||
useNewStyle = true
|
useNewStyle = true
|
||||||
} else {
|
} else {
|
||||||
// Check if docker-compose (old style) is available
|
|
||||||
checkCmd = exec.Command("docker-compose", "version")
|
checkCmd = exec.Command("docker-compose", "version")
|
||||||
if err := checkCmd.Run(); err != nil {
|
if err := checkCmd.Run(); err == nil {
|
||||||
return fmt.Errorf("neither 'docker compose' nor 'docker-compose' command is available: %v", err)
|
useNewStyle = false
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("neither 'docker compose' nor 'docker-compose' command is available")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to execute docker compose commands
|
|
||||||
executeCommand := func(args ...string) error {
|
|
||||||
var cmd *exec.Cmd
|
|
||||||
if useNewStyle {
|
if useNewStyle {
|
||||||
cmd = exec.Command("docker", append([]string{"compose"}, args...)...)
|
cmd = exec.Command("docker", append([]string{"compose"}, args...)...)
|
||||||
} else {
|
} else {
|
||||||
cmd = exec.Command("docker-compose", args...)
|
cmd = exec.Command("docker-compose", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
// pullContainers pulls the containers using the appropriate command.
|
||||||
|
func pullContainers() error {
|
||||||
|
fmt.Println("Pulling the container images...")
|
||||||
|
|
||||||
|
if err := executeDockerComposeCommandWithArgs("-f", "docker-compose.yml", "pull", "--policy", "always"); err != nil {
|
||||||
|
return fmt.Errorf("failed to pull the containers: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull containers
|
return nil
|
||||||
fmt.Printf("Using %s command to pull containers...\n", getCommandString(useNewStyle))
|
}
|
||||||
if err := executeCommand("-f", "docker-compose.yml", "pull"); err != nil {
|
|
||||||
return fmt.Errorf("failed to pull containers: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start containers
|
// startContainers starts the containers using the appropriate command.
|
||||||
fmt.Printf("Using %s command to start containers...\n", getCommandString(useNewStyle))
|
func startContainers() error {
|
||||||
if err := executeCommand("-f", "docker-compose.yml", "up", "-d"); err != nil {
|
fmt.Println("Starting containers...")
|
||||||
|
if err := executeDockerComposeCommandWithArgs("-f", "docker-compose.yml", "up", "-d", "--force-recreate"); err != nil {
|
||||||
return fmt.Errorf("failed to start containers: %v", err)
|
return fmt.Errorf("failed to start containers: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// bring containers down
|
// stopContainers stops the containers using the appropriate command.
|
||||||
func stopContainers() error {
|
func stopContainers() error {
|
||||||
fmt.Println("Stopping containers...")
|
fmt.Println("Stopping containers...")
|
||||||
|
|
||||||
// Check which docker compose command is available
|
if err := executeDockerComposeCommandWithArgs("-f", "docker-compose.yml", "down"); err != nil {
|
||||||
var useNewStyle bool
|
|
||||||
checkCmd := exec.Command("docker", "compose", "version")
|
|
||||||
if err := checkCmd.Run(); err == nil {
|
|
||||||
useNewStyle = true
|
|
||||||
} else {
|
|
||||||
// Check if docker-compose (old style) is available
|
|
||||||
checkCmd = exec.Command("docker-compose", "version")
|
|
||||||
if err := checkCmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("neither 'docker compose' nor 'docker-compose' command is available: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to execute docker compose commands
|
|
||||||
executeCommand := func(args ...string) error {
|
|
||||||
var cmd *exec.Cmd
|
|
||||||
if useNewStyle {
|
|
||||||
cmd = exec.Command("docker", append([]string{"compose"}, args...)...)
|
|
||||||
} else {
|
|
||||||
cmd = exec.Command("docker-compose", args...)
|
|
||||||
}
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := executeCommand("-f", "docker-compose.yml", "down"); err != nil {
|
|
||||||
return fmt.Errorf("failed to stop containers: %v", err)
|
return fmt.Errorf("failed to stop containers: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// just start containers
|
// restartContainer restarts a specific container using the appropriate command.
|
||||||
func startContainers() error {
|
|
||||||
fmt.Println("Starting containers...")
|
|
||||||
|
|
||||||
// Check which docker compose command is available
|
|
||||||
var useNewStyle bool
|
|
||||||
checkCmd := exec.Command("docker", "compose", "version")
|
|
||||||
if err := checkCmd.Run(); err == nil {
|
|
||||||
useNewStyle = true
|
|
||||||
} else {
|
|
||||||
// Check if docker-compose (old style) is available
|
|
||||||
checkCmd = exec.Command("docker-compose", "version")
|
|
||||||
if err := checkCmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("neither 'docker compose' nor 'docker-compose' command is available: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to execute docker compose commands
|
|
||||||
executeCommand := func(args ...string) error {
|
|
||||||
var cmd *exec.Cmd
|
|
||||||
if useNewStyle {
|
|
||||||
cmd = exec.Command("docker", append([]string{"compose"}, args...)...)
|
|
||||||
} else {
|
|
||||||
cmd = exec.Command("docker-compose", args...)
|
|
||||||
}
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := executeCommand("-f", "docker-compose.yml", "up", "-d"); err != nil {
|
|
||||||
return fmt.Errorf("failed to start containers: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func restartContainer(container string) error {
|
func restartContainer(container string) error {
|
||||||
fmt.Printf("Restarting %s container...\n", container)
|
fmt.Println("Restarting containers...")
|
||||||
|
|
||||||
// Check which docker compose command is available
|
if err := executeDockerComposeCommandWithArgs("-f", "docker-compose.yml", "restart", container); err != nil {
|
||||||
var useNewStyle bool
|
return fmt.Errorf("failed to stop the container \"%s\": %v", container, err)
|
||||||
checkCmd := exec.Command("docker", "compose", "version")
|
|
||||||
if err := checkCmd.Run(); err == nil {
|
|
||||||
useNewStyle = true
|
|
||||||
} else {
|
|
||||||
// Check if docker-compose (old style) is available
|
|
||||||
checkCmd = exec.Command("docker-compose", "version")
|
|
||||||
if err := checkCmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("neither 'docker compose' nor 'docker-compose' command is available: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to execute docker compose commands
|
|
||||||
executeCommand := func(args ...string) error {
|
|
||||||
var cmd *exec.Cmd
|
|
||||||
if useNewStyle {
|
|
||||||
cmd = exec.Command("docker", append([]string{"compose"}, args...)...)
|
|
||||||
} else {
|
|
||||||
cmd = exec.Command("docker-compose", args...)
|
|
||||||
}
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := executeCommand("-f", "docker-compose.yml", "restart", container); err != nil {
|
|
||||||
return fmt.Errorf("failed to restart %s container: %v", container, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue