Skip to content

Commit

Permalink
Merge pull request #56 from tsteenkamp/master
Browse files Browse the repository at this point in the history
Add '--label', '--variables' and '--variable' options to control Rancher labels and environment variables.
  • Loading branch information
cdrx authored Sep 19, 2018
2 parents 3b91f57 + 548d840 commit bd85114
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ Options:
(needs --new-image option)
--labels Will add Rancher labels to the service being
created by passing a comma separated list.
--label KEY VALUE Alternative way of adding a Rancher label to the
service.
You can pass this option multiple times to create
multiple labels.
--variables Will add environment variables to the service being
created by passing a comma separated list.
--variable KEY VALUE Alternative way of adding environment variables to the
service.
You can pass this option multiple times to create
multiple environment variables.
--help Show this message and exit.
```
Expand Down
68 changes: 53 additions & 15 deletions rancher_gitlab_deploy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@
help="If specified, create Rancher stack and service if they don't exist")
@click.option('--labels', default=None,
help="If specified, add a comma separated list of key=values to add to the service")
@click.option('--label', default=None, multiple=True,
help="If specified, add a Rancher label to the service", type=(str, str))
@click.option('--variables', default=None,
help="If specified, add a comma separated list of key=values to add to the service")
@click.option('--variable', default=None, multiple=True,
help="If specified, add a environment variable to the service", type=(str, str))
@click.option('--debug/--no-debug', default=False,
help="Enable HTTP Debugging")
@click.option('--ssl-verify/--no-ssl-verify', default=True,
help="Disable certificate checks. Use this to allow connecting to a HTTPS Rancher server using an self-signed certificate")
def main(rancher_url, rancher_key, rancher_secret, environment, stack, service, new_image, batch_size, batch_interval, start_before_stopping, upgrade_timeout, wait_for_upgrade_to_finish, rollback_on_error, finish_upgrade, sidekicks, new_sidekick_image, create, labels, debug, ssl_verify):
def main(rancher_url, rancher_key, rancher_secret, environment, stack, service, new_image, batch_size, batch_interval, start_before_stopping, upgrade_timeout, wait_for_upgrade_to_finish, rollback_on_error, finish_upgrade, sidekicks, new_sidekick_image, create, labels, label, variables, variable, debug, ssl_verify):
"""Performs an in service upgrade of the service specified on the command line"""

if debug:
Expand All @@ -73,10 +79,43 @@ def main(rancher_url, rancher_key, rancher_secret, environment, stack, service,

# Set verify based on --ssl-verify/--no-ssl-verify option
session.verify = ssl_verify

# 0 -> Authenticate all future requests
session.auth = (rancher_key, rancher_secret)


# Check for labels and environment variables to set
defined_labels = {}

if labels is not None:
labels_as_array = labels.split(',')

for label_item in labels_as_array:
key, value = label_item.split('=')
defined_labels[key] = value

if label:
for item in label:
key = item[0]
value = item[1]
defined_labels[key] = value


defined_environment_variables = {}

if variables is not None:
variables_as_array = variables.split(',')

for variable_item in variables_as_array:
key, value = variable_item.split('=')
defined_environment_variables[key] = value

if variable:
for item in variable:
key = item[0]
value = item[1]
defined_environment_variables[key] = value

# 1 -> Find the environment id in Rancher
try:
r = session.get("%s/projects?limit=1000" % api)
Expand Down Expand Up @@ -116,7 +155,7 @@ def main(rancher_url, rancher_key, rancher_secret, environment, stack, service,
stacks = r.json()['data']

for s in stacks:

if s['name'].lower() == stack.lower():
stack = s
break
Expand Down Expand Up @@ -158,23 +197,15 @@ def main(rancher_url, rancher_key, rancher_secret, environment, stack, service,
break
else:

defined_labels = {}

if labels is not None:
labels_as_array = labels.split(',')

for label in labels_as_array:
key, value = label.split('=')
defined_labels[key] = value

if create:
new_service = {
'name': service.lower(),
'stackId': stack['id'],
'startOnCreate': True,
'launchConfig': {
'imageUuid': ("docker:%s" % new_image),
'labels': defined_labels
'labels': defined_labels,
'environment': defined_environment_variables
}
}
try:
Expand Down Expand Up @@ -232,12 +263,19 @@ def main(rancher_url, rancher_key, rancher_secret, environment, stack, service,
'batchSize': batch_size,
'intervalMillis': batch_interval * 1000, # rancher expects miliseconds
'startFirst': start_before_stopping,
'launchConfig': {},
'launchConfig': {
},
'secondaryLaunchConfigs': []
}}
# copy over the existing config
upgrade['inServiceStrategy']['launchConfig'] = service['launchConfig']

if defined_labels:
upgrade['inServiceStrategy']['launchConfig']['labels'].update(defined_labels)

if defined_environment_variables:
upgrade['inServiceStrategy']['launchConfig']['environment'].update(defined_environment_variables)

# new_sidekick_image parameter needs secondaryLaunchConfigs loaded
if sidekicks or new_sidekick_image:
# copy over existing sidekicks config
Expand Down Expand Up @@ -279,7 +317,7 @@ def main(rancher_url, rancher_key, rancher_secret, environment, stack, service,
if rollback_on_error:
bail(message, exit=False)
warn("Processing image rollback...")

try:
r = session.post("%s/projects/%s/services/%s/?action=rollback" % (
api, environment_id, service['id']
Expand Down

0 comments on commit bd85114

Please sign in to comment.