Permalink
Browse files

Shellouts: add {ENVIRONMENT} var, passing thru positional command-lin…

…e arg

Users of external service discovery can use the environment name as an
additional key for looking up instances. (Previously this could be worked
around by setting HOST to a different shellout for each environment, but the
new {ENVIRONMENT} var prevents the need for redundant configuration.)
  • Loading branch information...
evanelias committed Jan 3, 2017
1 parent b04a62d commit 42d9005508c662c20b4d22a69ad6d62c643196bf
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
File renamed without changes.
File renamed without changes.
View
@@ -126,10 +126,18 @@ Most other commands (`skeema diff`, `skeema push`, `skeema pull`, `skeema lint`)
### Options with variable interpolation
Some string-type options are interpreted as external commands to execute. These options support interpolation of variable placeholders, which appear in all-caps and are wrapped in braces like `{VARNAME}`. For example, this line may appear in a .skeema file to configure use of pt-online-schema-change:
Some string-type options, such as [alter-wrapper](options.md#alter-wrapper), are always interpreted as external commands to execute. A few other string-type options, such as [host](options.md#host), are optionally interpreted as external commands if the value is wrapped in backticks.
In either case, the external command-line supports interpolation of variable placeholders, which appear in all-caps and are wrapped in braces like `{VARNAME}`. For example, this line may appear in a .skeema file to configure use of pt-online-schema-change:
```ini
alter-wrapper=/usr/local/bin/pt-online-schema-change --alter {CLAUSES} D={SCHEMA},t={TABLE},h={HOST},P={PORT},u={USER},p={PASSWORD}
```
Or this line might be used in a .skeema file to configure service discovery for dynamically mapping the directory to database instances, based on the environment name and directory name:
```ini
host=`/path/to/service_discovery_lookup.sh {ENVIRONMENT} {DIRPARENT}`
```
The placeholders are automatically replaced with the correct values for the current operation. Each option lists what variables it supports.
View
@@ -132,6 +132,7 @@ This command supports use of special variables. Skeema will dynamically replace
* `{SCHEMA}` -- schema name defined by the [schema](#schema) option for the directory being processed
* `{USER}` -- MySQL username defined by the [user](#user) option either via command-line or option file
* `{PASSWORD}` -- MySQL password defined by the [password](#password) option either via command-line or option file
* `{ENVIRONMENT}` -- environment name from the first positional arg on the command-line, or "production" if none specified
* `{DDL}` -- Full `ALTER TABLE` statement, including all clauses
* `{TABLE}` -- table name that this ALTER is for
* `{SIZE}` -- size of table that this ALTER is for, in bytes. This will always be 0 for tables without any rows.
@@ -232,6 +233,7 @@ This command supports use of special variables. Skeema will dynamically replace
* `{SCHEMA}` -- schema name defined by the [schema](#schema) option for the directory being processed
* `{USER}` -- MySQL username defined by the [user](#user) option either via command-line or option file
* `{PASSWORD}` -- MySQL password defined by the [password](#password) option either via command-line or option file
* `{ENVIRONMENT}` -- environment name from the first positional arg on the command-line, or "production" if none specified
* `{DDL}` -- Full DDL statement, including all clauses
* `{TABLE}` -- table name that this DDL is for
* `{SIZE}` -- size of table that this DDL is for, in bytes. This will always be 0 for tables without any rows, or for `CREATE TABLE` statements.
@@ -321,11 +323,12 @@ Environments using external service discovery systems should set [host](#host) t
* `{SCHEMA}` -- literal value of the [schema](#schema) option for the directory being processed
* `{USER}` -- MySQL username defined by the [user](#user) option either via command-line or option file
* `{PASSWORD}` -- MySQL password defined by the [password](#password) option either via command-line or option file
* `{ENVIRONMENT}` -- environment name from the first positional arg on the command-line, or "production" if none specified
* `{DIRNAME}` -- The base name of the directory being processed
* `{DIRPARENT}` -- The base name of the parent of the directory being processed
* `{DIRPATH}` -- The full (absolute) path of the directory being processed
Above, "the directory being processed" refers to a leaf directory defining the [schema option](#schema) and containing \*.sql files. To configure generic discovery lookups using directory names, you could use a directory structure of repo_root/pool_name/schema_name/\*.sql and then specify ``host=`/path/to/service_discovery_lookup.sh {DIRPARENT}` `` in repo_root/.skeema. In this case `{DIRPARENT}` will end up being the pool name to pass to service discovery.
Above, "the directory being processed" refers to a leaf directory defining the [schema option](#schema) and containing \*.sql files. To configure generic discovery lookups using a combination of environment name and directory names, you could use a directory structure of repo_root/pool_name/schema_name/\*.sql and then specify ``host=`/path/to/service_discovery_lookup.sh {ENVIRONMENT} {DIRPARENT}` `` in repo_root/.skeema. In this case `{DIRPARENT}` will end up being the pool name to pass to service discovery.
The external command should only return addresses of master instances, never replicas.
@@ -422,6 +425,7 @@ Some sharded environments need more flexibility -- for example, where some schem
* `{PORT}` -- port number for the database instance being processed
* `{USER}` -- MySQL username defined by the [user](#user) option either via command-line or option file
* `{PASSWORD}` -- MySQL password defined by the [password](#password) option either via command-line or option file
* `{ENVIRONMENT}` -- environment name from the first positional arg on the command-line, or "production" if none specified
* `{DIRNAME}` -- The base name of the directory being processed.
* `{DIRPARENT}` -- The base name of the parent of the directory being processed.
* `{DIRPATH}` -- The full (absolute) path of the directory being processed.
View
@@ -136,6 +136,11 @@ func NewInterpolatedShellOut(command string, dir *Dir, extra map[string]string)
}
}
// If the command has an "environment" positional arg, add its value as-is too
if _, hasEnvironment := dir.Config.CLI.Command.OptionValue("environment"); hasEnvironment {
values["ENVIRONMENT"] = dir.Config.Get("environment")
}
hostSource := dir.Config.Source("host")
if file, ok := hostSource.(*mycli.File); ok {
values["HOSTDIR"] = path.Base(file.Dir)

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 42d9005

Please sign in to comment.