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

Commit

Permalink
Add command "dbdeployer info"
Browse files Browse the repository at this point in the history
"dbdeployer info" improves dbdeployer usage in scripts

Sub-commands:
* "dbdeployer info version [short-version] [all]"
  shows the latest version [of a given short-version]
* "dbdeployer info defaults field-name"
  shows the value for a specific field in defaults.
  See README.md for usage
  • Loading branch information
datacharmer committed May 5, 2019
1 parent 2f7e976 commit e8216d7
Show file tree
Hide file tree
Showing 15 changed files with 1,564 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .build/COMPATIBLE_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.29.0
1.30.0
2 changes: 1 addition & 1 deletion .build/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.29.0
1.30.0
14 changes: 14 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
1.30.0 05-May-2019
BUGS FIXED
- Fix Issue #72 Catalog operations can clash with multiple dbdeployer runs
- Fix wrong description for "defaults update"
- Fix wrong description for "cookbook create"
- Fix option --sort-by incorrectly assigned to command "cookbook" instead
of subcommand "cookbook list"
NEW FEATURES
- Add command "dbdeployer info" to improve dbdeployer usage in scripts
TESTING
- Add test for parallel usage
- Reinstate parallel deployment in functional test
- Improve replication between sandboxes test, by checking
for existing versions better.
1.29.0 30-Apr-2019
ADJUSTMENTS
- Change upgrade procedure for MySQL 8.0.16+
Expand Down
123 changes: 110 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,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.29.0 (30-Apr-2019 17:41 UTC)
Documentation updated for version 1.30.0 (05-May-2019 07:21 UTC)

[![Build Status](https://travis-ci.org/datacharmer/dbdeployer.svg "Travis CI status")](https://travis-ci.org/datacharmer/dbdeployer)

Expand All @@ -11,7 +11,7 @@ Documentation updated for version 1.29.0 (30-Apr-2019 17:41 UTC)
- [Main operations](#Main-operations)
- [Database server flavors](#Database-server-flavors)
- [Getting remote tarballs](#Getting-remote-tarballs)
- [Practical examples](#Practical-examples)
- [Practical examples (cookbook)](#Practical-examples)
- [Standard and non-standard basedir names](#Standard-and-non-standard-basedir-names)
- [Using short version numbers](#Using-short-version-numbers)
- [Multiple sandboxes, same version and type](#Multiple-sandboxes-same-version-and-type)
Expand All @@ -31,6 +31,7 @@ Documentation updated for version 1.29.0 (30-Apr-2019 17:41 UTC)
- [Dedicated admin address](#Dedicated-admin-address)
- [Obtaining sandbox metadata](#Obtaining-sandbox-metadata)
- [Replication between sandboxes](#Replication-between-sandboxes)
- [Using dbdeployer in scripts](#Using-dbdeployer-in-scripts)
- [Compiling dbdeployer](#Compiling-dbdeployer)
- [Generating additional documentation](#Generating-additional-documentation)
- [Command line completion](#Command-line-completion)
Expand All @@ -48,7 +49,7 @@ Get the one for your O.S. from [dbdeployer releases](https://github.com/datachar

For example:

$ VERSION=1.29.0
$ VERSION=1.30.0
$ OS=linux
$ origin=https://github.com/datacharmer/dbdeployer/releases/download/v$VERSION
$ wget $origin/dbdeployer-$VERSION.$OS.tar.gz
Expand Down Expand Up @@ -83,7 +84,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.29.0
dbdeployer version 1.30.0


$ dbdeployer -h
Expand All @@ -103,6 +104,7 @@ The program doesn't have any dependencies. Everything is included in the binary.
export Exports the command structure in JSON format
global Runs a given command in every sandbox
help Help about any command
info Shows information about dbdeployer environment samples
remote Manages remote tarballs
sandboxes List installed sandboxes
unpack unpack a tarball into the binary directory
Expand Down Expand Up @@ -497,9 +499,8 @@ dbdeployer will detect the latest versions available in you system. If you don't
show Shows the contents of a given recipe

Flags:
--flavor string For which flavor this recipe is
-h, --help help for cookbook
--sort-by string Sort order for the list (name, flavor, script) (default "name")
--flavor string For which flavor this recipe is
-h, --help help for cookbook



Expand Down Expand Up @@ -1371,25 +1372,121 @@ Examples:
~/sandboxes/group_8_0_15_2/replicate_from ms_8_0_15_1
```

# Using dbdeployer in scripts

dbdeployer has been designed to simplify automated operations. Using it in scripts is easy, as shown in the [cookbook examples](#Practical-examples).
In addition to run operations on sandboxes, dbdeployer can also provide information about the environment in a way that is suitable for scripting.

For example, if you want to deploy a sandbox using the most recent 5.7 binaries, you may run `dbdeployer versions`, look which versions are available, and pick the most recent one. But dbdeployer 1.30.0 can aytomate this procedure using `dbdeployer info version 5.7`. This command will print the latest 5.7 binaries to the standard output, allowing us to create dynamic scripts such as:

```bash
# the absolute latest version
latest=$(dbdeployer info version)
latest57=$(dbdeployer info version 5.7)
latest80=$(dbdeployer info version 8.0)

if [ -z "$latest" ]
then
echo "No versions found"
exit 1
fi

echo "The latest version is $latest"

if [ -n "$latest57" ]
then
echo "# latest for 5.7 : $latest57"
dbdeployer deploy single $latest57
fi

if [ -n "$latest80" ]
then
echo "# latest for 8.0 : $latest80"
dbdeployer deploy single $latest80
fi
```

$ dbdeployer info version -h
Displays the latest version available for deployment.
If a short version is indicated (such as 5.7, or 8.0), only the versions belonging to that short
version are searched.
If "all" is indicated after the short version, displays all versions belonging to that short version.

Usage:
dbdeployer info version [short-version|all] [all] [flags]

Examples:

# Shows the latest version available
$ dbdeployer info version
8.0.16

# shows the latest version belonging to 5.7
$ dbdeployer info version 5.7
5.7.26

# shows the latest version for every short version
$ dbdeployer info version all
5.0.96 5.1.73 5.5.53 5.6.41 5.7.26 8.0.16

# shows all the versions for a given short version
$ dbdeployer info version 8.0 all
8.0.11 8.0.12 8.0.13 8.0.14 8.0.15 8.0.16


Flags:
-h, --help help for version



Similarly to `versions`, the `defaults` subcommand allows us to get dbdeployer metadata in a way that can be used in scripts

$ dbdeployer info defaults -h
Displays one field of the defaults.

Usage:
dbdeployer info defaults field-name [flags]

Examples:

$ dbdeployer info defaults master-slave-base-port


Flags:
-h, --help help for defaults



For example

```
$ dbdeployer info defaults sandbox-prefix
msb_
$ dbdeployer info defaults master-slave-ptrefix
rsandbox_
```
You can ask for any fields from the defaults (see `dbdeployer defaults list` for the field names).

# Compiling dbdeployer

Should you need to compile your own binaries for dbdeployer, follow these steps:

1. Make sure you have go 1.10+ installed in your system, and that the ``$GOPATH`` variable is set.
2. Run ``go get -u 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. Run ``./scripts/build.sh {linux|OSX} 1.29.0``
5. If you need the docs enabled binaries (see the section "Generating additional documentation") run ``MKDOCS=1 ./scripts/build.sh {linux|OSX} 1.29.0``
4. Run ``./scripts/build.sh {linux|OSX} 1.30.0``
5. If you need the docs enabled binaries (see the section "Generating additional documentation") run ``MKDOCS=1 ./scripts/build.sh {linux|OSX} 1.30.0``

# Generating additional documentation

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.29.0-docs.linux.tar.gz
* dbdeployer-1.29.0-docs.osx.tar.gz
* dbdeployer-1.29.0.linux.tar.gz
* dbdeployer-1.29.0.osx.tar.gz
* dbdeployer-1.30.0-docs.linux.tar.gz
* dbdeployer-1.30.0-docs.osx.tar.gz
* dbdeployer-1.30.0.linux.tar.gz
* dbdeployer-1.30.0.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
5 changes: 3 additions & 2 deletions cmd/delete_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (

func sandboxesUsingBinariesDir(basedir, binariesDir string) []string {
var sandboxes []string

sandboxList, err := defaults.ReadCatalog()
var sandboxList defaults.SandboxCatalog
var err error
sandboxList, err = defaults.ReadCatalog()
common.ErrCheckExitf(err, 1, "error getting sandboxes from catalog: %s", err)
fullPath := path.Join(basedir, binariesDir)
for _, sb := range sandboxList {
Expand Down
167 changes: 167 additions & 0 deletions cmd/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// DBDeployer - The MySQL Sandbox
// Copyright © 2006-2018 Giuseppe Maxia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"regexp"
"strings"

"github.com/spf13/cobra"

"github.com/datacharmer/dbdeployer/common"
"github.com/datacharmer/dbdeployer/cookbook"
"github.com/datacharmer/dbdeployer/defaults"
"github.com/datacharmer/dbdeployer/globals"
)

func displayDefaults(cmd *cobra.Command, args []string) {
if len(args) < 1 {
common.Exit(1,
"'defaults' requires a label",
"Example: dbdeployer info defaults master-slave-base-port")
}
label := args[0]
defaultsMap := defaults.DefaultsToMap()
value, ok := defaultsMap[label]
if ok {
fmt.Println(value)
} else {
fmt.Printf("# ERROR: field %s not found in defaults\n", label)
}
}

func displayAllVersions(basedir, wantedVersion, flavor string) {
result := ""
var versionInfoList []common.VersionInfo = common.GetVersionInfoFromDir(basedir)
for _, verInfo := range versionInfoList {
versionList, err := common.VersionToList(verInfo.Version)
if err != nil {
common.Exitf(1, "error retrieving version list from %s", verInfo.Version)
}
shortVersion := fmt.Sprintf("%d.%d", versionList[0], versionList[1])
if wantedVersion == shortVersion || strings.ToLower(wantedVersion) == "all" {
if verInfo.Flavor == flavor {
if result != "" {
result += " "
}
result += verInfo.Version
}
}
}
if result != "" {
fmt.Println(result)
}
}

func displayVersion(cmd *cobra.Command, args []string) {
wantedVersion := ""
allVersions := ""
if len(args) > 0 {
wantedVersion = args[0]
}

reNotFound := regexp.MustCompile(cookbook.VersionNotFound)
if len(args) > 1 {
allVersions = args[1]
}
flavor, _ := cmd.Flags().GetString(globals.FlavorLabel)
if flavor == "" {
flavor = common.MySQLFlavor
}
if strings.ToLower(allVersions) == "all" {
basedir, err := getAbsolutePathFromFlag(cmd, "sandbox-binary")
common.ErrCheckExitf(err, 1, "error getting absolute path for 'sandbox-binary'")
displayAllVersions(basedir, wantedVersion, flavor)
} else {
if strings.ToLower(wantedVersion) == "all" {
result := ""
for _, v := range globals.SupportedAllVersions {
latest := cookbook.GetLatestVersion(v, flavor)
if !reNotFound.MatchString(latest) {
if result != "" {
result += " "
}
result += latest
}
}
if result != "" {
fmt.Println(result)
}
} else {
latest := cookbook.GetLatestVersion(wantedVersion, flavor)
if !reNotFound.MatchString(latest) {
fmt.Println(latest)

}
}
}
}

var infoCmd = &cobra.Command{
Use: "info",
Short: "Shows information about dbdeployer environment samples",
Long: `Shows current information about defaults and environment.`,
}

var infoDefaultsCmd = &cobra.Command{
Use: "defaults field-name",

Short: "displays a defaults value",
Example: `
$ dbdeployer info defaults master-slave-base-port
`,
Long: `Displays one field of the defaults.`,
Run: displayDefaults,
Annotations: map[string]string{"export": ExportAnnotationToJson(StringExport)},
}

var infoVersionCmd = &cobra.Command{
Use: "version [short-version|all] [all]",

Short: "displays the latest version available",
Example: `
# Shows the latest version available
$ dbdeployer info version
8.0.16
# shows the latest version belonging to 5.7
$ dbdeployer info version 5.7
5.7.26
# shows the latest version for every short version
$ dbdeployer info version all
5.0.96 5.1.73 5.5.53 5.6.41 5.7.26 8.0.16
# shows all the versions for a given short version
$ dbdeployer info version 8.0 all
8.0.11 8.0.12 8.0.13 8.0.14 8.0.15 8.0.16
`,
Long: `Displays the latest version available for deployment.
If a short version is indicated (such as 5.7, or 8.0), only the versions belonging to that short
version are searched.
If "all" is indicated after the short version, displays all versions belonging to that short version.
`,
Run: displayVersion,
Annotations: map[string]string{"export": ExportAnnotationToJson(StringExport)},
}

func init() {
rootCmd.AddCommand(infoCmd)
infoCmd.AddCommand(infoDefaultsCmd)
infoCmd.AddCommand(infoVersionCmd)
setPflag(infoCmd, globals.FlavorLabel, "", "", "", "For which flavor this info is", false)
}
Loading

0 comments on commit e8216d7

Please sign in to comment.