Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add farm management support #88

Merged
merged 17 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
*.yaml

# Test binary, built with `go test -c`
*.test
Expand Down
8 changes: 8 additions & 0 deletions LocalSettingsTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
// If not running MediaWiki, exit
if ( !defined( 'MEDIAWIKI' ) ) {
exit;
}
#$wgServer = "http://localhost";
#$wgSitename = ;
#$wgMetaNamespace = ;
134 changes: 134 additions & 0 deletions cmd/add/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package add

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"

"github.com/CanastaWiki/Canasta-CLI-Go/cmd/restart"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/canasta"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/config"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/farmsettings"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/mediawiki"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/orchestrators"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/prompt"
)

func NewCmdCreate() *cobra.Command {
var instance config.Installation
var wikiName string
var domainName string
var wikiPath string
var siteName string
var databasePath string
var url string

addCmd := &cobra.Command{
Use: "add",
Short: "Add a new wiki to a Canasta instance",
RunE: func(cmd *cobra.Command, args []string) error {
var err error
wikiName, domainName, wikiPath, instance.Id, siteName, err = prompt.PromptWiki(wikiName, url, instance.Id, siteName)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Adding wiki '%s' to Canasta instance '%s'...\n", wikiName, instance.Id)
err = AddWiki(wikiName, domainName, wikiPath, siteName, databasePath, instance)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done.")
return nil
},
}

pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

addCmd.Flags().StringVarP(&wikiName, "wiki", "w", "", "ID of the new wiki")
addCmd.Flags().StringVarP(&url, "url", "u", "", "URL of the new wiki")
addCmd.Flags().StringVarP(&siteName, "site-name", "s", "", "Name of the new wiki site")
addCmd.Flags().StringVarP(&instance.Path, "path", "p", pwd, "Path to the new wiki")
addCmd.Flags().StringVarP(&instance.Id, "id", "i", "", "Canasta instance ID")
addCmd.Flags().StringVarP(&instance.Orchestrator, "orchestrator", "o", "docker-compose", "Orchestrator to use for installation")
addCmd.Flags().StringVarP(&databasePath, "database", "d", "", "Path to the existing database dump")
return addCmd
}

// addWiki accepts the Canasta instance ID, the name, domain and path of the new wiki, and the initial admin info, then creates a new wiki in the instance.
func AddWiki(name, domain, wikipath, siteName, databasePath string, instance config.Installation) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for any of these params to be non-null but empty? If so, how is that handled?

var err error

//Checking Installation existence
instance, err = canasta.CheckCanastaId(instance)
if err != nil {
return err
}

//Migrate to the new version Canasta
err = canasta.MigrateToNewVersion(instance.Path)
if err != nil {
return err
}

//Checking Running status
err = orchestrators.CheckRunningStatus(instance.Path, instance.Id, instance.Orchestrator)
if err != nil {
return err
}

//Checking Wiki existence
exists, pathComboExists, err := farmsettings.CheckWiki(instance.Path, name, domain, wikipath)
if err != nil {
return err
}
if exists {
return fmt.Errorf("A wiki with the name '%s' exists", name)
}
if pathComboExists {
return fmt.Errorf("A wiki with the same installation path '%s' in the Canasta '%s' exists", name+": "+domain+"/"+wikipath, instance.Id)
}

//Add the wiki in farmsettings
err = farmsettings.AddWiki(name, instance.Path, domain, wikipath, siteName)
if err != nil {
return err
}

// Import the database if databasePath is specified
if databasePath != "" {
err = orchestrators.ImportDatabase(name, databasePath, instance)
if err != nil {
return err
}
}

//Copy the Localsettings
err = canasta.CopySetting(instance.Path, name)
if err != nil {
return err
}

//Rewrite the Caddyfile
err = canasta.RewriteCaddy(instance.Path)
if err != nil {
return err
}

err = mediawiki.InstallOne(instance.Path, name, domain, wikipath, instance.Orchestrator)
if err != nil {
return err
}
err = restart.Restart(instance)
if err != nil {
return err
}

fmt.Println("Successfully Added the Wiki '" + name + "in Canasta instance '" + instance.Id + "'...")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't there be a space between name +" and in Canasta?


return nil
}
32 changes: 25 additions & 7 deletions cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ import (

"github.com/CanastaWiki/Canasta-CLI-Go/internal/canasta"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/config"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/farmsettings"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/mediawiki"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/orchestrators"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/prompt"
)

func NewCmdCreate() *cobra.Command {
var (
path string
orchestrator string
pwd string
name string
domain string
yamlPath string
err error
keepConfig bool
canastaInfo canasta.CanastaVariables
Expand All @@ -28,11 +33,11 @@ func NewCmdCreate() *cobra.Command {
Short: "Create a Canasta installation",
Long: "Creates a Canasta installation using an orchestrator of your choice.",
RunE: func(cmd *cobra.Command, args []string) error {
if canastaInfo, err = mediawiki.PromptUser(canastaInfo); err != nil {
if name, canastaInfo, err = prompt.PromptUser(name, yamlPath, canastaInfo); err != nil {
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, name, domain, yamlPath, orchestrator); err != nil {
fmt.Print(err.Error(), "\n")
if keepConfig {
log.Fatal(fmt.Errorf("Keeping all the containers and config files\nExiting"))
Expand All @@ -58,29 +63,42 @@ func NewCmdCreate() *cobra.Command {
createCmd.Flags().StringVarP(&path, "path", "p", pwd, "Canasta directory")
createCmd.Flags().StringVarP(&orchestrator, "orchestrator", "o", "docker-compose", "Orchestrator to use for installation")
createCmd.Flags().StringVarP(&canastaInfo.Id, "id", "i", "", "Canasta instance ID")
createCmd.Flags().StringVarP(&canastaInfo.WikiName, "wiki", "w", "", "Name of wiki")
createCmd.Flags().StringVarP(&canastaInfo.DomainName, "domain-name", "n", "localhost", "Domain name")
createCmd.Flags().StringVarP(&name, "wiki", "w", "", "Name of wiki")
createCmd.Flags().StringVarP(&domain, "domain-name", "n", "localhost", "Domain name")
createCmd.Flags().StringVarP(&canastaInfo.AdminName, "WikiSysop", "a", "", "Initial wiki admin username")
createCmd.Flags().StringVarP(&canastaInfo.AdminPassword, "password", "s", "", "Initial wiki admin password")
createCmd.Flags().StringVarP(&yamlPath, "yamlfile", "f", "", "Initial wiki yaml file")
createCmd.Flags().BoolVarP(&keepConfig, "keep-config", "k", false, "Keep the config files on installation failure")
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, name, domain, yamlPath, orchestrator string) error {
if _, err := config.GetDetails(canastaInfo.Id); err == nil {
log.Fatal(fmt.Errorf("Canasta installation with the ID already exist!"))
}
if err := farmsettings.CreateYaml(name, domain, &yamlPath); err != nil {
return err
}
if err := canasta.CloneStackRepo(orchestrator, canastaInfo.Id, &path); err != nil {
return err
}
if err := canasta.CopyEnv("", canastaInfo.DomainName, path, pwd); err != nil {
if err := canasta.CopyYaml(yamlPath, path); err != nil {
return err
}
if err := canasta.CopyEnv("", path, pwd); err != nil {
return err
}
if err := canasta.CopySettings(path); err != nil {
return err
}
if err := canasta.RewriteCaddy(path); err != nil {
return err
}
if err := orchestrators.Start(path, orchestrator); err != nil {
return err
}
if _, err := mediawiki.Install(path, orchestrator, canastaInfo); err != nil {
if _, err := mediawiki.Install(path, yamlPath, orchestrator, canastaInfo); err != nil {
return err
}
if err := config.Add(config.Installation{Id: canastaInfo.Id, Path: path, Orchestrator: orchestrator}); err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func Delete(instance config.Installation) error {
if err = config.Delete(instance.Id); err != nil {
return err
}

fmt.Println("Deleted.")
return nil
}
78 changes: 78 additions & 0 deletions cmd/export/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"

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

var (
pwd string
err error
instance config.Installation
wikiName string
outputFilePath string
)

// NewCmdExport exports the database.
func NewCmdCreate() *cobra.Command {
var instance config.Installation
var wikiName string
var outputFilePath string

pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

exportCmd := &cobra.Command{
Use: "export",
Short: "Export a database from a Canasta instance",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Exporting database for wiki '%s' from Canasta instance '%s'...\n", wikiName, instance.Id)
err := ExportDatabase(wikiName, outputFilePath, instance)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done.")
return nil
},
}

exportCmd.Flags().StringVarP(&wikiName, "wiki", "w", "", "ID of the wiki (database) to export")
exportCmd.Flags().StringVarP(&instance.Id, "id", "i", "", "Canasta instance ID")
exportCmd.Flags().StringVarP(&instance.Path, "path", "p", pwd, "Canasta installation directory")
exportCmd.Flags().StringVarP(&outputFilePath, "output", "o", "", "Output file path for the exported database")

return exportCmd
}

// ExportDatabase exports a database from a Canasta instance.
func ExportDatabase(databaseName, outputFilePath string, instance config.Installation) error {

//Checking Installation existence
instance, err = canasta.CheckCanastaId(instance)
if err != nil {
return err
}

//Checking Running status
err = orchestrators.CheckRunningStatus(instance.Path, instance.Id, instance.Orchestrator)
if err != nil {
return err
}

// Exporting the database
err = orchestrators.ExportDatabase(instance.Path, instance.Orchestrator, databaseName, outputFilePath)
if err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions cmd/extension/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func disableCmdCreate() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
extensions := strings.Split(args[0], ",")
for _, extension := range extensions {
extensionName, err := extensionsskins.CheckEnabled(extension, instance, constants)
extensionName, err := extensionsskins.CheckEnabled(extension, wiki, instance, constants)
if err != nil {
fmt.Print(err.Error() + "\n")
continue
}
extensionsskins.Disable(extensionName, instance, constants)
extensionsskins.Disable(extensionName, wiki, instance, constants)
}
return err
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/extension/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func enableCmdCreate() *cobra.Command {
fmt.Print(err.Error() + "\n")
continue
}
extensionsskins.Enable(extensionName, instance, constants)
extensionsskins.Enable(extensionName, wiki, instance, constants)
}
return err
},
Expand Down
2 changes: 2 additions & 0 deletions cmd/extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
instance config.Installation
pwd string
wiki string
err error
verbose bool
extensionCmd *cobra.Command
Expand All @@ -40,6 +41,7 @@ func NewCmdCreate() *cobra.Command {
}
extensionCmd.PersistentFlags().StringVarP(&instance.Id, "id", "i", "", "Canasta instance ID")
extensionCmd.PersistentFlags().StringVarP(&instance.Path, "path", "p", pwd, "Canasta installation directory")
extensionCmd.PersistentFlags().StringVarP(&wiki, "wiki", "w", "", "ID of the specific wiki within the Canasta farm")
extensionCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose Output")

extensionCmd.AddCommand(listCmdCreate())
Expand Down
2 changes: 1 addition & 1 deletion cmd/import/importExisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func importCanasta(pwd, canastaId, domainName, path, orchestrator, databasePath,
if err := canasta.CloneStackRepo(orchestrator, canastaId, &path); err != nil {
return err
}
if err := canasta.CopyEnv(envPath, domainName, path, pwd); err != nil {
if err := canasta.CopyEnv(envPath, path, pwd); err != nil {
return err
}
if err := canasta.CopyDatabase(databasePath, path, pwd); err != nil {
Expand Down
Loading