Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
Code refactoring and bug fixing
Browse files Browse the repository at this point in the history
- Merged pull request #11 from percona-csalguero/include_version_in_dir
- Fixed Issue #12 "deploying a sandbox with invalid version does not fail"
- Fixed minor bugs
- Removed unnecessary parameter in CreateSingleSandbox
- Added instructions to call CreateSingleSandbox from other apps
  See ./docs/coding
- Minor code refactoring for Exit calls
- Added mock sandbox creation for unit tests
  • Loading branch information
datacharmer committed May 5, 2018
1 parent 78820df commit 7c3dfdc
Show file tree
Hide file tree
Showing 32 changed files with 614 additions and 429 deletions.
9 changes: 9 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
1.4.1 05-May-2018
- Merged pull request #11 from percona-csalguero/include_version_in_dir
- Fixed Issue #12 "deploying a sandbox with invalid version does not fail"
- Fixed minor bugs
- Removed unnecessary parameter in CreateSingleSandbox
- Added instructions to call CreateSingleSandbox from other apps
See ./docs/coding
- Minor code refactoring for Exit calls
- Added mock sandbox creation for unit tests
1.4.0 28-Apr-2018
NEW FEATURES:
- Added option --enable-mysqlx (MySQL 5.7.12+)
Expand Down
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[DBdeployer](https://github.com/datacharmer/dbdeployer) is a tool that deploys MySQL database servers easily.
This is a port of [MySQL-Sandbox](https://github.com/datacharmer/mysql-sandbox), originally written in Perl, and re-designed from the ground up in [Go](https://golang.org). See the [features comparison](https://github.com/datacharmer/dbdeployer/blob/master/docs/features.md) for more detail.

Documentation updated for version 1.4.0 (28-Apr-2018 12:56 UTC)
Documentation updated for version 1.4.1 (05-May-2018 20:43 UTC)

## Installation

Expand All @@ -13,7 +13,7 @@ Get the one for your O.S. from [dbdeployer releases](https://github.com/datachar

For example:

$ VERSION=1.4.0
$ VERSION=1.4.1
$ origin=https://github.com/datacharmer/dbdeployer/releases/download/$VERSION
$ wget $origin/dbdeployer-$VERSION.linux.tar.gz
$ tar -xzf dbdeployer-$VERSION.linux.tar.gz
Expand Down Expand Up @@ -47,7 +47,7 @@ For example:
The program doesn't have any dependencies. Everything is included in the binary. Calling *dbdeployer* without arguments or with ``--help`` will show the main help screen.

$ dbdeployer --version
dbdeployer version 1.4.0
dbdeployer version 1.4.1


$ dbdeployer -h
Expand Down Expand Up @@ -459,7 +459,7 @@ Here's how:
3306,
33060
],
"timestamp": "Sat Apr 28 14:56:12 CEST 2018"
"timestamp": "Sat May 5 22:43:32 CEST 2018"
}


Expand Down Expand Up @@ -496,7 +496,7 @@ Here's how:
3306,
33060
],
"timestamp": "Sat Apr 28 14:56:12 CEST 2018"
"timestamp": "Sat May 5 22:43:32 CEST 2018"
}


Expand Down Expand Up @@ -674,18 +674,18 @@ Should you need to compile your own binaries for dbdeployer, follow these steps:
2. Run ``go get github.com/datacharmer/dbdeployer``. This will import all the code that is needed to build dbdeployer.
3. Change directory to ``$GOPATH/src/github.com/datacharmer/dbdeployer``.
4. From the folder ``./pflag``, copy the file ``string_slice.go`` to ``$GOPATH/src/github.com/spf13/pflag``.
5. Run ``./build.sh {linux|OSX} 1.4.0``
6. If you need the docs enabled binaries (see the section "Generating additional documentation") run ``MKDOCS=1 ./build.sh {linux|OSX} 1.4.0``
5. Run ``./build.sh {linux|OSX} 1.4.1``
6. If you need the docs enabled binaries (see the section "Generating additional documentation") run ``MKDOCS=1 ./build.sh {linux|OSX} 1.4.1``

## Generating additional documentation

Between this file and [the API API list](https://github.com/datacharmer/dbdeployer/blob/master/docs/API-1.1.md), you have all the existing documentation for dbdeployer.
Between this file and [the API API list](https://github.com/datacharmer/dbdeployer/blob/master/docs/API/API-1.1.md), you have all the existing documentation for dbdeployer.
Should you need additional formats, though, dbdeployer is able to generate them on-the-fly. Tou will need the docs-enabled binaries: in the distribution list, you will find:

* dbdeployer-1.4.0-docs.linux.tar.gz
* dbdeployer-1.4.0-docs.osx.tar.gz
* dbdeployer-1.4.0.linux.tar.gz
* dbdeployer-1.4.0.osx.tar.gz
* dbdeployer-1.4.1-docs.linux.tar.gz
* dbdeployer-1.4.1-docs.osx.tar.gz
* dbdeployer-1.4.1.linux.tar.gz
* dbdeployer-1.4.1.osx.tar.gz

The executables containing ``-docs`` in their name have the same capabilities of the regular ones, but in addition they can run the *hidden* command ``tree``, with alias ``docs``.

Expand Down Expand Up @@ -740,6 +740,10 @@ Then, you can use completion as follows:
$ dbdeployer deploy single --b[tab][tab]
--base-port= --bind-address=

## Using dbdeployer source for other projects

If you need to create sandboxes from other Go apps, see [dbdeployer-as-a-library.md](https://github.com/datacharmer/dbdeployer/blob/master/docs/coding/dbdeployer-as-a-library.md).

## Semantic versioning

As of version 1.0.0, dbdeployer adheres to the principles of [semantic versioning](https://semver.org/). A version number is made of Major, Minor, and Revision. When changes are applied, the following happens:
Expand All @@ -748,7 +752,7 @@ As of version 1.0.0, dbdeployer adheres to the principles of [semantic versionin
* Backward-compatible new features increment the **Minor** number.
* Backward incompatible changes (either features or bug fixes that break compatibility with the API) increment the **Major** number.

The starting API is defined in [API-1.0.md](https://github.com/datacharmer/dbdeployer/blob/master/docs/API-1.0.md) (generated manually.)
The file [API-1.1.md](https://github.com/datacharmer/dbdeployer/blob/master/docs/API-1.1.md) contains the same API definition, but was generated automatically and can be used to better compare the initial API with further version.
The starting API is defined in [API-1.0.md](https://github.com/datacharmer/dbdeployer/blob/master/docs/API/API-1.0.md) (generated manually.)
The file [API-1.1.md](https://github.com/datacharmer/dbdeployer/blob/master/docs/API/API-1.1.md) contains the same API definition, but was generated automatically and can be used to better compare the initial API with further version.


33 changes: 13 additions & 20 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
func UnpreserveSandbox(sandbox_dir, sandbox_name string) {
full_path := sandbox_dir + "/" + sandbox_name
if !common.DirExists(full_path) {
fmt.Printf("Directory '%s' not found\n", full_path)
os.Exit(1)
common.Exit(1, fmt.Sprintf("Directory '%s' not found", full_path))
}
preserve := full_path + "/no_clear_all"
if !common.ExecExists(preserve) {
Expand All @@ -44,22 +43,19 @@ func UnpreserveSandbox(sandbox_dir, sandbox_name string) {
is_multiple = false
}
if !common.ExecExists(clear) {
fmt.Printf("Executable '%s' not found\n", clear)
os.Exit(1)
common.Exit(1, fmt.Sprintf("Executable '%s' not found", clear))
}
no_clear := full_path + "/no_clear"
if is_multiple {
no_clear = full_path + "/no_clear_all"
}
err := os.Remove(clear)
if err != nil {
fmt.Printf("Error while removing %s \n%s\n",clear, err)
os.Exit(1)
common.Exit(1, fmt.Sprintf("Error while removing %s \n%s",clear, err))
}
err = os.Rename(no_clear, clear)
if err != nil {
fmt.Printf("Error while renaming script\n%s\n", err)
os.Exit(1)
common.Exit(1, fmt.Sprintf("Error while renaming script\n%s", err))
}
fmt.Printf("Sandbox %s unlocked\n",sandbox_name)
}
Expand All @@ -69,8 +65,7 @@ func UnpreserveSandbox(sandbox_dir, sandbox_name string) {
func PreserveSandbox(sandbox_dir, sandbox_name string) {
full_path := sandbox_dir + "/" + sandbox_name
if !common.DirExists(full_path) {
fmt.Printf("Directory '%s' not found\n", full_path)
os.Exit(1)
common.Exit(1, fmt.Sprintf("Directory '%s' not found", full_path))
}
preserve := full_path + "/no_clear_all"
if !common.ExecExists(preserve) {
Expand All @@ -87,8 +82,7 @@ func PreserveSandbox(sandbox_dir, sandbox_name string) {
is_multiple = false
}
if !common.ExecExists(clear) {
fmt.Printf("Executable '%s' not found\n", clear)
os.Exit(1)
common.Exit(1, fmt.Sprintf("Executable '%s' not found", clear))
}
no_clear := full_path + "/no_clear"
clear_cmd := "clear"
Expand All @@ -100,8 +94,7 @@ func PreserveSandbox(sandbox_dir, sandbox_name string) {
}
err := os.Rename(clear, no_clear)
if err != nil {
fmt.Printf("Error while renaming script.\n%s\n",err)
os.Exit(1)
common.Exit(1, fmt.Sprintf( "Error while renaming script.\n%s",err))
}
template := sandbox.SingleTemplates["sb_locked_template"].Contents
var data = common.Smap{
Expand All @@ -121,9 +114,9 @@ func PreserveSandbox(sandbox_dir, sandbox_name string) {

func LockSandbox(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Printf("'lock' requires the name of a sandbox (or ALL)")
fmt.Printf("Example: dbdeployer admin lock msb_5_7_21")
os.Exit(1)
common.Exit(1,
"'lock' requires the name of a sandbox (or ALL)",
"Example: dbdeployer admin lock msb_5_7_21")
}
flags := cmd.Flags()
sandbox := args[0]
Expand All @@ -143,9 +136,9 @@ func LockSandbox(cmd *cobra.Command, args []string) {

func UnlockSandbox(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Printf("'unlock' requires the name of a sandbox (or ALL)")
fmt.Printf("Example: dbdeployer admin unlock msb_5_7_21")
os.Exit(1)
common.Exit(1,
"'unlock' requires the name of a sandbox (or ALL)",
"Example: dbdeployer admin unlock msb_5_7_21")
}
flags := cmd.Flags()
sandbox := args[0]
Expand Down
16 changes: 6 additions & 10 deletions cmd/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/datacharmer/dbdeployer/common"
"github.com/datacharmer/dbdeployer/defaults"
"github.com/spf13/cobra"
"os"
)

func ShowDefaults(cmd *cobra.Command, args []string) {
Expand All @@ -38,8 +37,7 @@ func RemoveDefaults(cmd *cobra.Command, args []string) {

func LoadDefaults(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Printf("'load' requires a file name\n")
os.Exit(1)
common.Exit(1,"'load' requires a file name")
}
filename := args[0]
new_defaults := defaults.ReadDefaultsFile(filename)
Expand All @@ -53,23 +51,21 @@ func LoadDefaults(cmd *cobra.Command, args []string) {

func ExportDefaults(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Printf("'export' requires a file name\n")
os.Exit(1)
common.Exit(1,"'export' requires a file name")
}
filename := args[0]
if common.FileExists(filename) {
fmt.Printf("File %s already exists. Will not overwrite\n", filename)
os.Exit(1)
common.Exit(1, fmt.Sprintf("File %s already exists. Will not overwrite", filename))
}
defaults.WriteDefaultsFile(filename, defaults.Defaults())
fmt.Printf("# Defaults exported to file %s\n", filename)
}

func UpdateDefaults(cmd *cobra.Command, args []string) {
if len(args) < 2 {
fmt.Printf("'update' requires a label and a value\n")
fmt.Printf("Example: dbdeployer defaults update master-slave-base-port 17500\n")
os.Exit(1)
common.Exit(1,
"'update' requires a label and a value",
"Example: dbdeployer defaults update master-slave-base-port 17500")
}
label := args[0]
value := args[1]
Expand Down
79 changes: 9 additions & 70 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,91 +21,31 @@ import (
"github.com/datacharmer/dbdeployer/common"
"github.com/datacharmer/dbdeployer/concurrent"
"github.com/datacharmer/dbdeployer/defaults"
"github.com/datacharmer/dbdeployer/sandbox"
"github.com/spf13/cobra"
"os"
)

func RemoveSandbox(sandbox_dir, sandbox string, run_concurrently bool) (exec_list []concurrent.ExecutionList) {
full_path := sandbox_dir + "/" + sandbox
if !common.DirExists(full_path) {
fmt.Printf("Directory '%s' not found\n", full_path)
os.Exit(1)
}
preserve := full_path + "/no_clear_all"
if !common.ExecExists(preserve) {
preserve = full_path + "/no_clear"
}
if common.ExecExists(preserve) {
fmt.Printf("The sandbox %s is locked\n",sandbox)
fmt.Printf("You need to unlock it with \"dbdeployer admin unlock\"\n",)
return
}
stop := full_path + "/stop_all"
if !common.ExecExists(stop) {
stop = full_path + "/stop"
}
if !common.ExecExists(stop) {
fmt.Printf("Executable '%s' not found\n", stop)
os.Exit(1)
}

if run_concurrently {
var eCommand1 = concurrent.ExecCommand{
Cmd : stop,
Args : []string{},
}
exec_list = append(exec_list, concurrent.ExecutionList{0, eCommand1})
} else {
fmt.Printf("Running %s\n", stop)
err, _ := common.Run_cmd(stop)
if err != nil {
fmt.Printf("Error while stopping sandbox %s\n", full_path)
os.Exit(1)
}
}

cmd_str := "rm"
rm_args := []string{"-rf", full_path}
if run_concurrently {
var eCommand2 = concurrent.ExecCommand{
Cmd : cmd_str,
Args : rm_args,
}
exec_list = append(exec_list, concurrent.ExecutionList{1, eCommand2})
} else {
for _, item := range rm_args {
cmd_str += " " + item
}
fmt.Printf("Running %s\n", cmd_str)
err, _ := common.Run_cmd_with_args("rm", rm_args)
if err != nil {
fmt.Printf("Error while deleting sandbox %s\n", full_path)
os.Exit(1)
}
fmt.Printf("Sandbox %s deleted\n", full_path)
}
// fmt.Printf("%#v\n",exec_list)
return
}

func DeleteSandbox(cmd *cobra.Command, args []string) {
var exec_lists []concurrent.ExecutionList
if len(args) < 1 {
fmt.Println("Sandbox name (or \"ALL\") required.")
fmt.Println("You can run 'dbdeployer sandboxes for a list of available deployments'")
os.Exit(1)
common.Exit(1,
"Sandbox name (or \"ALL\") required.",
"You can run 'dbdeployer sandboxes for a list of available deployments'")
}
flags := cmd.Flags()
sandbox := args[0]
sandbox_name := args[0]
confirm, _ := flags.GetBool("confirm")
run_concurrently, _ := flags.GetBool("concurrent")
if os.Getenv("RUN_CONCURRENTLY") != "" {
run_concurrently = true
}
skip_confirm, _ := flags.GetBool("skip-confirm")
sandbox_dir, _ := flags.GetString("sandbox-home")
deletion_list := []common.SandboxInfo{common.SandboxInfo{sandbox, false}}
if sandbox == "ALL" || sandbox == "all" {
deletion_list := []common.SandboxInfo{common.SandboxInfo{sandbox_name, false}}
if sandbox_name == "ALL" || sandbox_name == "all" {
confirm = true
if skip_confirm {
confirm = false
Expand Down Expand Up @@ -147,16 +87,15 @@ func DeleteSandbox(cmd *cobra.Command, args []string) {
if answer == "y" || answer == "Y" {
fmt.Println("Proceeding with deletion")
} else {
fmt.Println("Execution interrupted by user")
os.Exit(0)
common.Exit(0, "Execution interrupted by user")
}
}
}
for _, sb := range deletion_list {
if sb.Locked {
fmt.Printf("Sandbox %s is locked\n",sb.SandboxName)
} else {
exec_list := RemoveSandbox(sandbox_dir, sb.SandboxName, run_concurrently)
exec_list := sandbox.RemoveSandbox(sandbox_dir, sb.SandboxName, run_concurrently)
for _, list := range exec_list {
exec_lists = append(exec_lists, list)
}
Expand Down
Loading

0 comments on commit 7c3dfdc

Please sign in to comment.