Commit 26bc523b authored by Jeena Huneidi's avatar Jeena Huneidi Committed by Addshore
Browse files

Add dev alias, replace docker with mwdd

Creates an alias called dev for the default development environment.
This will be useful when other environments are added to the cli.
Replaces the docker command with mwdd to remove duplicate functionality.

Also update the test to be mwdd / docker focused

Change-Id: Ibb2798f438f9372289694f0bb4ce5dbb087d18dc
parent 5ca7ba8d
......@@ -9,17 +9,21 @@ trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# Output commands
set -x
# Setup the test config
mkdir ~/.mwcli
echo '{"dev_mode":"mwdd"}' > ~/.mwcli/config.json
# Output version
./mw version
# Setup & Create
./mw mwdd env set PORT 8080
./mw mwdd env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
./mw mwdd mediawiki create
./mw docker env set PORT 8080
./mw docker env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
./mw docker mediawiki create
# Validate the basic stuff
./mw mwdd docker-compose ps
./mw mwdd env list
./mw docker docker-compose ps
./mw docker env list
cat ~/.mwcli/mwdd/default/.env
CURL=$(curl -s -L -N http://default.mediawiki.mwdd.localhost:8080) && echo $CURL && echo $CURL | grep -q "The MediaWiki logo"
......@@ -29,38 +33,38 @@ echo "//require_once "$IP/includes/PlatformSettings.php";" >> mediawiki/LocalSet
echo "require_once '/mwdd/MwddSettings.php';" >> mediawiki/LocalSettings.php
# Install sqlite & check
./mw mwdd mediawiki install
./mw docker mediawiki install
CURL=$(curl -s -L -N http://default.mediawiki.mwdd.localhost:8080) && echo $CURL && echo $CURL | grep -q "MediaWiki has been installed"
# docker-compose: Make sure it appears to work
./mw mwdd docker-compose ps -- --services | grep -q "mediawiki"
./mw docker docker-compose ps -- --services | grep -q "mediawiki"
# cd to mediawiki
cd mediawiki
# composer: Make sure a command works in root of the repo
./../mw mwdd mediawiki composer home | grep -q "https://www.mediawiki.org/"
./../mw docker mediawiki composer home | grep -q "https://www.mediawiki.org/"
# phpunit: Make sure a command works in the root of the repo
./../mw mwdd mediawiki phpunit ./tests/phpunit/unit/includes/PingbackTest.php | grep -q "OK "
./../mw docker mediawiki phpunit ./tests/phpunit/unit/includes/PingbackTest.php | grep -q "OK "
# exec: Make sure a command works in the root of the repo
./../mw mwdd mediawiki exec ls | grep -q "LocalSettings.php"
./../mw docker mediawiki exec ls | grep -q "LocalSettings.php"
# cd to Vector
cd skins/Vector
# composer: Make sure a command works from the Vector directory
./../../../mw mwdd mediawiki composer home | grep -q "http://gerrit.wikimedia.org/g/mediawiki/skins/Vector"
./../../../mw docker mediawiki composer home | grep -q "http://gerrit.wikimedia.org/g/mediawiki/skins/Vector"
# phpunit: Make sure a command works from the Vector directory
./../../../mw mwdd mediawiki phpunit ./../../tests/phpunit/unit/includes/PingbackTest.php | grep -q "OK "
./../../../mw docker mediawiki phpunit ./../../tests/phpunit/unit/includes/PingbackTest.php | grep -q "OK "
# exec: Make sure a command works from the Vector directory
# Right now this just executes in the MediaWiki directory
./../../../mw mwdd mediawiki exec ls | grep -q "LocalSettings.php"
./../../../mw docker mediawiki exec ls | grep -q "LocalSettings.php"
# cd back again
cd ./../../../
# Destroy it all
./mw mwdd destroy
./mw docker destroy
# And make sure only 1 exists after
docker ps
docker ps | wc -l | grep -q "1"
......@@ -9,17 +9,21 @@ trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# Output commands
set -x
# Setup the test config
mkdir ~/.mwcli
echo '{"dev_mode":"mwdd"}' > ~/.mwcli/config.json
# Output version
./mw version
# Setup & Create
./mw mwdd env set PORT 8080
./mw mwdd env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
./mw mwdd mediawiki create
./mw docker env set PORT 8080
./mw docker env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
./mw docker mediawiki create
# Validate the basic stuff
./mw mwdd docker-compose ps
./mw mwdd env list
./mw docker docker-compose ps
./mw docker env list
cat ~/.mwcli/mwdd/default/.env
curl -s -L -N http://default.mediawiki.mwdd.localhost:8080 | grep -q "The MediaWiki logo"
......@@ -29,15 +33,15 @@ echo "//require_once "$IP/includes/PlatformSettings.php";" >> mediawiki/LocalSet
echo "require_once '/mwdd/MwddSettings.php';" >> mediawiki/LocalSettings.php
# Turn on all of the services
./mw mwdd mysql-replica create
./mw mwdd postgres create
./mw mwdd phpmyadmin create
./mw mwdd adminer create
./mw docker mysql-replica create
./mw docker postgres create
./mw docker phpmyadmin create
./mw docker adminer create
# Install everything
./mw mwdd mediawiki install --dbname mysqlwiki --dbtype mysql
./mw mwdd mediawiki install --dbname postgreswiki --dbtype postgres
./mw mwdd mediawiki install
./mw docker mediawiki install --dbname mysqlwiki --dbtype mysql
./mw docker mediawiki install --dbname postgreswiki --dbtype postgres
./mw docker mediawiki install
# Check the DB tools
CURL=$(curl -s -L -N http://phpmyadmin.mwdd.localhost:8080) && echo $CURL && echo $CURL | grep -q "Open new phpMyAdmin window"
......@@ -53,7 +57,7 @@ docker ps
docker ps | wc -l | grep -q "10"
# Destroy it all
./mw mwdd destroy
./mw docker destroy
# And make sure only 1 exists after
docker ps
docker ps | wc -l | grep -q "1"
......@@ -9,40 +9,44 @@ trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# Output commands
set -x
# Setup the test config
mkdir ~/.mwcli
echo '{"dev_mode":"mwdd"}' > ~/.mwcli/config.json
# Output version
./mw version
# Setup & Create
./mw mwdd env set PORT 8080
./mw mwdd env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
./mw mwdd mediawiki create
./mw mwdd mysql create
./mw docker env set PORT 8080
./mw docker env set MEDIAWIKI_VOLUMES_CODE $(pwd)/mediawiki
./mw docker mediawiki create
./mw docker mysql create
echo "<?php" >> mediawiki/LocalSettings.php
echo "//require_once "$IP/includes/PlatformSettings.php";" >> mediawiki/LocalSettings.php
echo "require_once '/mwdd/MwddSettings.php';" >> mediawiki/LocalSettings.php
# Validate the basic stuff
./mw mwdd docker-compose ps
./mw mwdd env list
./mw docker docker-compose ps
./mw docker env list
cat ~/.mwcli/mwdd/default/.env
# Install & check
./mw mwdd mediawiki install --dbname mysqlwiki --dbtype mysql
./mw docker mediawiki install --dbname mysqlwiki --dbtype mysql
CURL=$(curl -s -L -N http://mysqlwiki.mediawiki.mwdd.localhost:8080) && echo $CURL && echo $CURL | grep -q "MediaWiki has been installed"
# Suspend and resume and check the site is still there
./mw mwdd mysql suspend
./mw mwdd mysql resume
./mw docker mysql suspend
./mw docker mysql resume
sleep 2
CURL=$(curl -s -L -N http://mysqlwiki.mediawiki.mwdd.localhost:8080) && echo $CURL && echo $CURL | grep -q "MediaWiki has been installed"
# Destroy and restart mysql, reinstalling mediawiki
./mw mwdd mysql destroy
./mw mwdd mysql create
./mw mwdd mediawiki install --dbname mysqlwiki --dbtype mysql
./mw docker mysql destroy
./mw docker mysql create
./mw docker mediawiki install --dbname mysqlwiki --dbtype mysql
CURL=$(curl -s -L -N http://mysqlwiki.mediawiki.mwdd.localhost:8080) && echo $CURL && echo $CURL | grep -q "MediaWiki has been installed"
# Destroy it all
./mw mwdd destroy
./mw docker destroy
docker ps
docker ps | wc -l | grep -q "1"
......@@ -7,10 +7,9 @@ Take a look at the user facing docs https://www.mediawiki.org/wiki/Cli
## Docker
There are currently 2 subcommands:
There is currently 1 subcommand:
- `docker` allows interacting with MediaWiki core's docker-compose development environment. (See `mw help docker`)
- `mwdd` allows interacting with a new version of the MediaWiki-docker-dev development environment. (See `mw help mwdd`)
- `docker` allows interacting with a new version of the MediaWiki-docker-dev development environment. (See `mw help docker`)
## Contributing
......
......@@ -17,8 +17,6 @@ limitations under the License.
package cmd
import (
"fmt"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/config"
"github.com/spf13/cobra"
)
......@@ -29,14 +27,14 @@ var configCmd = &cobra.Command{
}
var configShowCmd = &cobra.Command{
Use: "show",
Short: "Shows the raw config",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(config.LoadFromDisk())
},
Use: "show",
Short: "Shows the raw config",
Run: func(cmd *cobra.Command, args []string) {
config.LoadFromDisk().PrettyPrint()
},
}
func init() {
rootCmd.AddCommand(configCmd)
configCmd.AddCommand(configShowCmd)
configCmd.AddCommand(configShowCmd)
}
/*Package cmd is used for command line.
Copyright © 2020 Kosta Harlan <kosta@kostaharlan.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"bytes"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/manifoldco/promptui"
"github.com/briandowns/spinner"
"github.com/spf13/cobra"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/docker"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/exec"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/mediawiki"
)
var dockerCmd = &cobra.Command{
Use: "docker",
Short: "The MediaWiki-Docker development environment",
RunE: nil,
}
func mediawikiOrFatal() mediawiki.MediaWiki {
MediaWiki, err := mediawiki.ForCurrentWorkingDirectory()
if err != nil {
log.Fatal("❌ Please run this command within the root of the MediaWiki core repository.")
os.Exit(1)
}
return MediaWiki
}
var dockerStartCmd = &cobra.Command{
Use: "start",
Short: "Start the development environment",
Run: func(cmd *cobra.Command, args []string) {
Spinner := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
Spinner.Prefix = "Starting the development environment "
Spinner.FinalMSG = Spinner.Prefix + "(done)\n"
options := exec.HandlerOptions{
Spinner: Spinner,
Verbosity: Verbosity,
HandleError: handlePortError,
}
exec.RunCommand(options, docker.ComposeCommand("up", "-d"))
MediaWiki := mediawikiOrFatal()
if isLinuxHost() {
fileCreated, err := docker.EnsureDockerComposeUserOverrideExists()
if fileCreated {
fmt.Println("Creating docker-compose.override.yml for correct user ID and group ID mapping from host to container")
}
if err != nil {
log.Fatal(err)
}
}
MediaWiki.EnsureCacheDirectory()
if docker.MediaWikiComposerDependenciesNeedInstallation(exec.HandlerOptions{Verbosity: Verbosity}) {
fmt.Println("MediaWiki has some external dependencies that need to be installed")
installComposer := func() {
Spinner := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
Spinner.Prefix = "Installing Composer dependencies (this may take a few minutes) "
Spinner.FinalMSG = Spinner.Prefix + "(done)\n"
options := exec.HandlerOptions{
Spinner: Spinner,
Verbosity: Verbosity,
}
docker.MediaWikiComposerUpdate(options)
}
if NonInteractive {
installComposer()
} else {
prompt := promptui.Prompt{
IsConfirm: true,
Label: "Install dependencies now",
}
_, err := prompt.Run()
if err == nil {
installComposer()
}
}
}
if !MediaWiki.VectorIsPresent() {
downloadVector := func() {
Spinner := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
Spinner.Prefix = "Downloading Vector "
Spinner.FinalMSG = Spinner.Prefix + "(done)\n"
options := exec.HandlerOptions{
Spinner: Spinner,
Verbosity: Verbosity,
HandleError: func(stderr bytes.Buffer, err error) {
if err != nil {
log.Fatal(err)
}
},
}
MediaWiki.GitCloneVector(options)
}
if NonInteractive {
downloadVector()
} else {
prompt := promptui.Prompt{
IsConfirm: true,
Label: "Download and use the Vector skin",
}
_, err := prompt.Run()
if err == nil {
downloadVector()
}
}
}
if !MediaWiki.LocalSettingsIsPresent() {
installMediawiki := func() {
Spinner := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
Spinner.Prefix = "Installing "
Spinner.FinalMSG = Spinner.Prefix + "(done)\n"
options := exec.HandlerOptions{
Spinner: Spinner,
Verbosity: Verbosity,
}
docker.MediaWikiInstall(options)
}
if NonInteractive {
installMediawiki()
} else {
prompt := promptui.Prompt{
IsConfirm: true,
Label: "Install MediaWiki database tables and create LocalSettings.php",
}
_, err := prompt.Run()
if err == nil {
installMediawiki()
}
}
}
printSuccess()
},
}
var dockerExecCmd = &cobra.Command{
Use: "exec [service] [command] [args]",
Short: "Run a command in the specified container",
Args: cobra.MinimumNArgs(2),
PreRun: func(cmd *cobra.Command, args []string) {
mediawiki.CheckIfInCoreDirectory()
},
Run: func(cmd *cobra.Command, args []string) {
options := exec.HandlerOptions{
Verbosity: Verbosity,
}
if Detach {
args = append([]string{"-d"}, args...)
}
if Privileged {
args = append([]string{"--privileged"}, args...)
}
if User != "" {
args = append([]string{"-u", User}, args...)
}
if Index != "" {
args = append([]string{fmt.Sprintf("--index=%v", Index)}, args...)
}
for _, keyvar := range Env {
args = append([]string{fmt.Sprintf("-e %v", keyvar)}, args...)
}
if Workdir != "" {
args = append([]string{fmt.Sprintf("-w %v", Workdir)}, args...)
}
if NoTTY {
args = append([]string{"-T"}, args...)
exec.RunCommand(options, docker.ComposeCommand("exec", args...))
} else {
exec.RunTTYCommand(options, docker.ComposeCommand("exec", args...))
}
},
}
var dockerDestroyCmd = &cobra.Command{
Use: "destroy [service...]",
Short: "destroys the development environment or specified containers",
Run: func(cmd *cobra.Command, args []string) {
MediaWiki := mediawikiOrFatal()
options := exec.HandlerOptions{
Verbosity: Verbosity,
}
runArgs := append([]string{"-sfv"}, args...)
exec.RunTTYCommand(options, docker.ComposeCommand("rm", runArgs...))
if len(args) == 0 || contains(args, "mediawiki") {
MediaWiki.RenameLocalSettings()
MediaWiki.DeleteCache()
MediaWiki.DeleteVendor()
}
},
}
func contains(slice []string, s string) bool {
for _, i := range slice {
if s == i {
return true
}
}
return false
}
var dockerStopCmd = &cobra.Command{
Use: "stop",
Short: "Stop development environment",
PreRun: func(cmd *cobra.Command, args []string) {
mediawiki.CheckIfInCoreDirectory()
},
Run: func(cmd *cobra.Command, args []string) {
Spinner := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
Spinner.Prefix = "Stopping development environment "
Spinner.FinalMSG = Spinner.Prefix + "(done)\n"
options := exec.HandlerOptions{
Spinner: Spinner,
Verbosity: Verbosity,
}
exec.RunCommand(options, docker.ComposeCommand("stop"))
},
}
var dockerStatusCmd = &cobra.Command{
Use: "status",
Short: "List development environment status",
PreRun: func(cmd *cobra.Command, args []string) {
mediawiki.CheckIfInCoreDirectory()
},
Run: func(cmd *cobra.Command, args []string) {
options := exec.HandlerOptions{
Verbosity: Verbosity,
}
exec.RunCommand(options, docker.ComposeCommand("ps"))
},
}
func printSuccess() {
options := exec.HandlerOptions{
Verbosity: Verbosity,
HandleStdout: func(stdout bytes.Buffer) {
// Replace 0.0.0.0 in the output with localhost
fmt.Printf("Success! View MediaWiki-Docker at http://%s",
strings.Replace(stdout.String(), "0.0.0.0", "localhost", 1))
},
}
exec.RunCommand(options, docker.ComposeCommand("port", "mediawiki", "8080"))
}
func handlePortError(stderr bytes.Buffer, err error) {
stdoutStderr := stderr.Bytes()
portError := strings.Index(string(stdoutStderr), " failed: port is already allocated")
if portError > 0 {
// TODO: This assumes a port that is four characters long.
log.Fatalf("Port %s is already allocated! \n\nPlease override the port via MW_DOCKER_PORT in the .env file\nYou can use the 'docker env' command to do this\nSee `mw docker env --help` for more information.",
string(stdoutStderr[portError-4:])[0:4])
} else if err != nil && stderr.String() != "" {
fmt.Printf("\n%s\n%s\n", "STDERR:", stderr.String())
}
}
func isLinuxHost() bool {
unameCommand := exec.Command("uname")
stdout, err := unameCommand.CombinedOutput()
if err != nil {
log.Fatal(err)
}
return string(stdout) == "Linux\n"
}
func init() {
dockerCmd.PersistentFlags().IntVarP(&Verbosity, "verbosity", "v", 1, "verbosity level (1-2)")
rootCmd.AddCommand(dockerCmd)
dockerStartCmd.Flags().BoolVarP(&NonInteractive, "acceptPrompts", "y", false, "Answer yes to all prompts")
dockerExecCmd.Flags().BoolVarP(&Detach, "detach", "d", false, "Detached mode: Run command in the background.")
dockerExecCmd.Flags().BoolVarP(&Privileged, "privileged", "p", false, "Give extended privileges to the process.")
dockerExecCmd.Flags().StringVarP(&User, "user", "u", "", "Run the command as this user.")
dockerExecCmd.Flags().BoolVarP(&NoTTY, "TTY", "T", false, "Disable pseudo-tty allocation. By default a TTY is allocated")
dockerExecCmd.Flags().StringVarP(&Index, "index", "i", "", "Index of the container if there are multiple instances of a service [default: 1]")
dockerExecCmd.Flags().StringSliceVarP(&Env, "env", "e", []string{}, "Set environment variables. Can be used multiple times")
dockerExecCmd.Flags().StringVarP(&Workdir, "workdir", "w", "", "Path to workdir directory for this command.")
dockerCmd.AddCommand(dockerStartCmd)
dockerCmd.AddCommand(dockerStopCmd)
dockerCmd.AddCommand(dockerStatusCmd)
dockerCmd.AddCommand(dockerDestroyCmd)
dockerCmd.AddCommand(dockerExecCmd)
}
/*Package cmd is used for command line.
Copyright © 2020 Addshore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/cmd"
)
var dockerEnvCmd = cmd.Env("Provides subcommands for interacting with development environment variables");
var dockerEnvDeleteCmd = cmd.EnvDelete(func()string{return mediawikiOrFatal().Directory()});
var dockerEnvSetCmd = cmd.EnvSet(func()string{return mediawikiOrFatal().Directory()});
var dockerEnvGetCmd = cmd.EnvGet(func()string{return mediawikiOrFatal().Directory()});
var dockerEnvListCmd = cmd.EnvList(func()string{return mediawikiOrFatal().Directory()});
var dockerEnvWhereCmd = cmd.EnvWhere(func()string{return mediawikiOrFatal().Directory()});
func init() {
dockerCmd.AddCommand(dockerEnvCmd)
dockerEnvCmd.AddCommand(dockerEnvWhereCmd)
dockerEnvCmd.AddCommand(dockerEnvSetCmd)
dockerEnvCmd.AddCommand(dockerEnvGetCmd)
dockerEnvCmd.AddCommand(dockerEnvListCmd)
dockerEnvCmd.AddCommand(dockerEnvDeleteCmd)
}
......@@ -30,15 +30,15 @@ import (
)