Skip to content

Commit

Permalink
Add support for copying override file on create/import (#91)
Browse files Browse the repository at this point in the history
* Add support for copying override file on create/import

* Fix typo

* Move override file copy command into orchestrators

* Switch boolean back to filename

* Fixed logging statement paramters

* Support pwd

* Made help text for the flags a bit clearer
  • Loading branch information
cicalese authored Jul 27, 2023
1 parent 096a69d commit dcf8a9d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
9 changes: 7 additions & 2 deletions cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewCmdCreate() *cobra.Command {
err error
keepConfig bool
canastaInfo canasta.CanastaVariables
override string
)
createCmd := &cobra.Command{
Use: "create",
Expand All @@ -32,7 +33,7 @@ func NewCmdCreate() *cobra.Command {
log.Fatal(err)
}
fmt.Println("Creating Canasta installation '" + canastaInfo.Id + "'...")
if err = createCanasta(canastaInfo, pwd, path, orchestrator); err != nil {
if err = createCanasta(canastaInfo, pwd, path, orchestrator, override); err != nil {
fmt.Print(err.Error(), "\n")
if keepConfig {
log.Fatal(fmt.Errorf("Keeping all the containers and config files\nExiting"))
Expand Down Expand Up @@ -63,11 +64,12 @@ func NewCmdCreate() *cobra.Command {
createCmd.Flags().StringVarP(&canastaInfo.AdminName, "WikiSysop", "a", "", "Initial wiki admin username")
createCmd.Flags().StringVarP(&canastaInfo.AdminPassword, "password", "s", "", "Initial wiki admin password")
createCmd.Flags().BoolVarP(&keepConfig, "keep-config", "k", false, "Keep the config files on installation failure")
createCmd.Flags().StringVarP(&override, "override", "r", "", "Name of a file to copy to docker-compose.override.yml")
return createCmd
}

// importCanasta accepts all the keyword arguments and create a installation of the latest Canasta.
func createCanasta(canastaInfo canasta.CanastaVariables, pwd, path, orchestrator string) error {
func createCanasta(canastaInfo canasta.CanastaVariables, pwd, path, orchestrator, override string) error {
if _, err := config.GetDetails(canastaInfo.Id); err == nil {
log.Fatal(fmt.Errorf("Canasta installation with the ID already exist!"))
}
Expand All @@ -77,6 +79,9 @@ func createCanasta(canastaInfo canasta.CanastaVariables, pwd, path, orchestrator
if err := canasta.CopyEnv("", canastaInfo.DomainName, path, pwd); err != nil {
return err
}
if err := orchestrators.CopyOverrideFile(path, orchestrator, override, pwd); err != nil {
return err
}
if err := orchestrators.Start(path, orchestrator); err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/import/importExisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewCmdCreate() *cobra.Command {
databasePath string
localSettingsPath string
envPath string
override string
canastaId string
domainName string
err error
Expand All @@ -35,7 +36,7 @@ func NewCmdCreate() *cobra.Command {
return err
}
fmt.Println("Importing Canasta")
if err := importCanasta(pwd, canastaId, domainName, path, orchestrator, databasePath, localSettingsPath, envPath); err != nil {
if err := importCanasta(pwd, canastaId, domainName, path, orchestrator, databasePath, localSettingsPath, envPath, override); err != nil {
fmt.Print(err.Error(), "\n")
if keepConfig {
log.Fatal(fmt.Errorf("Keeping all the containers and config files\nExiting"))
Expand Down Expand Up @@ -65,13 +66,14 @@ func NewCmdCreate() *cobra.Command {
importCmd.Flags().StringVarP(&databasePath, "database", "d", "", "Path to the existing database dump")
importCmd.Flags().StringVarP(&localSettingsPath, "localsettings", "l", "", "Path to the existing LocalSettings.php")
importCmd.Flags().StringVarP(&envPath, "env", "e", "", "Path to the existing .env file")
importCmd.Flags().StringVarP(&override, "override", "r", "", "Name of a file to copy to docker-compose.override.yml")
importCmd.Flags().BoolVarP(&keepConfig, "keep-config", "k", false, "Keep the config files on installation failure")

return importCmd
}

// importCanasta copies LocalSettings.php and databasedump to create canasta from a previous mediawiki installation
func importCanasta(pwd, canastaId, domainName, path, orchestrator, databasePath, localSettingsPath, envPath string) error {
func importCanasta(pwd, canastaId, domainName, path, orchestrator, databasePath, localSettingsPath, envPath, override string) error {
if _, err := config.GetDetails(canastaId); err == nil {
log.Fatal(fmt.Errorf("Canasta installation with the ID already exist!"))
}
Expand All @@ -87,6 +89,9 @@ func importCanasta(pwd, canastaId, domainName, path, orchestrator, databasePath,
if err := canasta.CopyLocalSettings(localSettingsPath, path, pwd); err != nil {
return err
}
if err := orchestrators.CopyOverrideFile(path, orchestrator, override, pwd); err != nil {
return err
}
if err := orchestrators.Start(path, orchestrator); err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions internal/orchestrators/orchestrators.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ func GetRepoLink(orchestrator string) string {
return repo
}

func CopyOverrideFile(path, orchestrator, sourceFilename, pwd string) error {
if sourceFilename != "" {
logging.Print("Copying override file\n")
switch orchestrator {
case "docker-compose":
sourceFilename = pwd + "/" + sourceFilename
var overrideFilename = path + "/docker-compose.override.yml"
logging.Print(fmt.Sprintf("Copying %s to %s\n", sourceFilename, overrideFilename))
err, output := execute.Run("", "cp", sourceFilename, overrideFilename)
if err != nil {
logging.Fatal(fmt.Errorf(output))
}
default:
logging.Fatal(fmt.Errorf("orchestrator: %s is not available", orchestrator))
}
}
return nil
}

func Start(path, orchestrator string) error {
logging.Print("Starting Canasta\n")
switch orchestrator {
Expand Down

0 comments on commit dcf8a9d

Please sign in to comment.