Skip to content

Commit

Permalink
Merge pull request #78 from PiyushRaj927/docker
Browse files Browse the repository at this point in the history
Fix issue #59: add support for compose v2
  • Loading branch information
jeffw16 authored May 31, 2023
2 parents fb6d00f + b8181f7 commit 096a69d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 34 deletions.
33 changes: 29 additions & 4 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
createCmd "github.com/CanastaWiki/Canasta-CLI-Go/cmd/create"
deleteCmd "github.com/CanastaWiki/Canasta-CLI-Go/cmd/delete"
extensionCmd "github.com/CanastaWiki/Canasta-CLI-Go/cmd/extension"
Expand All @@ -13,15 +14,16 @@ import (
startCmd "github.com/CanastaWiki/Canasta-CLI-Go/cmd/start"
stopCmd "github.com/CanastaWiki/Canasta-CLI-Go/cmd/stop"
versionCmd "github.com/CanastaWiki/Canasta-CLI-Go/cmd/version"

"github.com/CanastaWiki/Canasta-CLI-Go/internal/config"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/logging"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/orchestrators"

"github.com/spf13/cobra"
"path/filepath"
)

var (
verbose bool
verbose bool
OrchestratorPath string
)

var rootCmd = &cobra.Command{
Expand All @@ -32,6 +34,23 @@ var rootCmd = &cobra.Command{
logging.SetVerbose(verbose)
logging.Print("Setting verbose")
},
Run: func(cmd *cobra.Command, args []string) {
if OrchestratorPath != "" {
OrchestratorPath, err := filepath.Abs(OrchestratorPath)
if err != nil {
logging.Fatal(err)
}
var orchestrator = config.Orchestrator{
Id: "docker-compose",
Path: OrchestratorPath}
err = config.AddOrchestrator(orchestrator)
if err != nil {
logging.Fatal(err)
}
fmt.Printf("Path to Orchestrator %s set to %s", orchestrator.Id, orchestrator.Path)
}

},
}

func Execute() {
Expand All @@ -42,8 +61,8 @@ func Execute() {
}

func init() {
orchestrators.CheckDependencies()
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
rootCmd.Flags().StringVarP(&OrchestratorPath, "docker-path", "d", "", "path to docker-compose")

rootCmd.AddCommand(createCmd.NewCmdCreate())
rootCmd.AddCommand(deleteCmd.NewCmdCreate())
Expand All @@ -58,4 +77,10 @@ func init() {
rootCmd.AddCommand(stopCmd.NewCmdCreate())
rootCmd.AddCommand(versionCmd.NewCmdCreate())
rootCmd.CompletionOptions.DisableDefaultCmd = true
cobra.OnInitialize(func() {
if OrchestratorPath == "" {
orchestrators.CheckDependencies()
}

})
}
11 changes: 4 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,12 @@ else
echo "Docker appears to be installed at $loc but is not executable; please check permissions."
fi

loc=$(which docker-compose)
if [ -z $loc ]
then
echo "Docker Compose is not installed; please follow the guide at https://docs.docker.com/compose/install/ to install it."
elif [ -x $loc ]
if ! docker compose version;
then
echo "Checking for presence of Docker Compose... found."
echo "Docker Compose is not installed; please follow the guide at https://docs.docker.com/compose/install/ to install it.
Alternatively, set the Compose executable path in canasta CLI using \"canasta -d PATH\""
else
echo "Docker Compose appears to be installed at $loc but is not executable; please check permissions."
echo "Checking for presence of Docker Compose... found."
fi

echo "Please make sure you have a working kubectl if you wish to use Kubernetes as an orchestrator."
Expand Down
42 changes: 34 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path"
"syscall"
Expand All @@ -19,7 +18,12 @@ type Installation struct {
Id, Path, Orchestrator string
}

type Orchestrator struct {
Id, Path string
}

type Canasta struct {
Orchestrators map[string]Orchestrator
Installations map[string]Installation
}

Expand All @@ -37,6 +41,14 @@ func Exists(canastaId string) bool {
return existingInstallations.Installations[canastaId].Id != ""
}

func OrchestratorExists(orchestrator string) bool {
err := read(&existingInstallations)
if err != nil {
logging.Fatal(err)
}
return existingInstallations.Orchestrators[orchestrator].Path != ""
}

func ListAll() {
err := read(&existingInstallations)
if err != nil {
Expand Down Expand Up @@ -75,6 +87,25 @@ func Add(details Installation) error {
return err
}

func AddOrchestrator(details Orchestrator) error {
if existingInstallations.Orchestrators == nil {
existingInstallations.Orchestrators = map[string]Orchestrator{}
}
if details.Id != "docker-compose" {
return fmt.Errorf("orchestrator %s is not suported", details.Id)
}
existingInstallations.Orchestrators[details.Id] = details
err := write(existingInstallations)
return err
}

func GetOrchestrator(orchestrator string) Orchestrator {
if OrchestratorExists(orchestrator) {
return existingInstallations.Orchestrators[orchestrator]
}
return Orchestrator{}
}

func Delete(canastaID string) error {
if Exists(canastaID) {
delete(existingInstallations.Installations, canastaID)
Expand Down Expand Up @@ -149,17 +180,12 @@ func init() {
directory = GetConfigDir()
confFile = path.Join(directory, confFile)

_, err := exec.LookPath("docker-compose")
if err != nil {
log.Fatal(fmt.Errorf("docker-compose should be installed! (%s)", err))
}

// Checks for the conf.json file
_, err = os.Stat(confFile)
_, err := os.Stat(confFile)
if os.IsNotExist(err) {
// Creating conf.json
log.Print("Creating " + confFile)
err := write(Canasta{Installations: map[string]Installation{}})
err := write(Canasta{Installations: map[string]Installation{}, Orchestrators: map[string]Orchestrator{}})
if err != nil {
log.Fatal(err)
}
Expand Down
75 changes: 60 additions & 15 deletions internal/orchestrators/orchestrators.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ import (
"fmt"
"os/exec"

"github.com/CanastaWiki/Canasta-CLI-Go/internal/config"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/execute"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/logging"
)

func CheckDependencies() {
_, err := exec.LookPath("docker-compose")
if err != nil {
logging.Fatal(fmt.Errorf("docker-compose should be installed! (%s)", err))
compose := config.GetOrchestrator("docker-compose")
if compose.Path != "" {
cmd := exec.Command(compose.Path, "version")
err := cmd.Run()
if err != nil {
logging.Fatal(fmt.Errorf("unable to execute compose (%s) \n", err))
}
} else {
cmd := exec.Command("docker", "compose", "version")
err := cmd.Run()
if err != nil {
logging.Fatal(fmt.Errorf("docker compose should be installed! (%s) \n", err))
}
}
}

Expand All @@ -30,9 +41,17 @@ func Start(path, orchestrator string) error {
logging.Print("Starting Canasta\n")
switch orchestrator {
case "docker-compose":
err, output := execute.Run(path, "docker-compose", "up", "-d")
if err != nil {
return fmt.Errorf(output)
compose := config.GetOrchestrator("docker-compose")
if compose.Path != "" {
err, output := execute.Run(path, compose.Path, "up", "-d")
if err != nil {
return fmt.Errorf(output)
}
} else {
err, output := execute.Run(path, "docker", "compose", "up", "-d")
if err != nil {
return fmt.Errorf(output)
}
}
default:
logging.Fatal(fmt.Errorf("orchestrator: %s is not available", orchestrator))
Expand All @@ -44,9 +63,18 @@ func Stop(path, orchestrator string) error {
logging.Print("Stopping the containers\n")
switch orchestrator {
case "docker-compose":
err, output := execute.Run(path, "docker-compose", "down")
if err != nil {
return fmt.Errorf(output)
compose := config.GetOrchestrator("docker-compose")
if compose.Path != "" {
err, output := execute.Run(path, compose.Path, "down")
if err != nil {
return fmt.Errorf(output)

}
} else {
err, output := execute.Run(path, "docker", "compose", "down")
if err != nil {
return fmt.Errorf(output)
}
}
default:
logging.Fatal(fmt.Errorf("orchestrator: %s is not available", orchestrator))
Expand All @@ -67,8 +95,15 @@ func StopAndStart(path, orchestrator string) error {
func DeleteContainers(path, orchestrator string) (string, error) {
switch orchestrator {
case "docker-compose":
err, output := execute.Run(path, "docker-compose", "down", "-v")
return output, err
compose := config.GetOrchestrator("docker-compose")
if compose.Path != "" {

err, output := execute.Run(path, compose.Path, "down", "-v")
return output, err
} else {
err, output := execute.Run(path, "docker", "compose", "down", "-v")
return output, err
}
default:
logging.Fatal(fmt.Errorf("orchestrator: %s is not available", orchestrator))
}
Expand All @@ -87,11 +122,21 @@ func ExecWithError(path, orchestrator, container, command string) (string, error

switch orchestrator {
case "docker-compose":
cmd := exec.Command("docker-compose", "exec", "-T", container, "/bin/bash", "-c", command)
if path != "" {
cmd.Dir = path
compose := config.GetOrchestrator("docker-compose")
if compose.Path != "" {

cmd := exec.Command(compose.Path, "exec", "-T", container, "/bin/bash", "-c", command)
if path != "" {
cmd.Dir = path
}
outputByte, err = cmd.CombinedOutput()
} else {
cmd := exec.Command("docker", "compose", "exec", "-T", container, "/bin/bash", "-c", command)
if path != "" {
cmd.Dir = path
}
outputByte, err = cmd.CombinedOutput()
}
outputByte, err = cmd.CombinedOutput()
default:
logging.Fatal(fmt.Errorf("orchestrator: %s is not available", orchestrator))
}
Expand Down

0 comments on commit 096a69d

Please sign in to comment.