Tasks

Bespin’s mechanism for doing anything are tasks. By default Bespin comes with a number of tasks as describe below:

Default tasks

tail
Tail the deployment of a stack

show

Show what stacks we have in layered order.

When combined with the --flat option, the stacks are shown as a flat list instead of in layers.

deploy
Deploy a particular stack
params
Print out the params
become
Print export statements for assuming an amazon iam role
outputs
Print out the outputs
execute
Exec a command using assumed credentials
bastion
SSH into the bastion
downtime
Downtime this stack in alerting systems
instances
Find and ssh into instances
list_tasks
List the available_tasks
undowntime
UnDowntime this stack in alerting systems
deploy_plan
Deploy a predefined list of stacks in order
sanity_check
Sanity check a stack and it’s dependencies
num_instances
Count the number of running instances.
print_variable
Prints out a variable from the stack
scale_instances
Change the number of instances in the stack’s auto_scaling_group
encrypt_password
Convert plain text password into crypto text
publish_artifacts
Build and publish an artifact
sanity_check_plan
sanity check a predefined list of stacks in order
confirm_deployment
Confirm deployment via SNS notification for each instance and/or url checks
validate_templates
Validates all stack templates and parameters against CloudFormation
clean_old_artifacts
Cleanup old artifacts
wait_for_dns_switch
Periodically check dns until all our sites point to where they should be pointing to for specified environment
command_on_instances
Run a shell command on all the instances in the stack
sync_netscaler_config
Sync netscaler configuration with the specified netscaler
switch_dns_traffic_to
Switch dns traffic to some environment
create_stackdriver_event
Create an event in stackdriver
enable_server_in_netscaler
Disable a server in the netscaler
note_deployment_in_newrelic
Note the deployment in newrelic
disable_server_in_netscaler
Enable a server in the netscaler
resume_cloudformation_actions
Resumes all schedule actions on a cloudformation stack
suspend_cloudformation_actions
Suspends all schedule actions on a cloudformation stack

Custom Tasks

There are two ways you can create custom tasks.

The first way is to define tasks as part of a stack definition:

---

stacks:
  app:
    [..]

    tasks:
      deploy_app:
        action: deploy

Will mean that you can run bespin deploy_app dev and it will run the deploy action for your app stack.

Tasks have several options:

action
The task to run. Note that the stack will default to the stack you’ve defined this task on.
options
Extra options to merge into the stack configuration when running the task.
overrides
Extra options to merge into the root of the configuration when running the task.
description
A description that is shown for this task when you ask Bespin to list all the tasks.

The second way of defining custom tasks is with the extra_imports option.

For example, let’s say you have the following layout:

bespin.yml
app.json
scripts.py

And your bespin.yml looked like:

---

bespin:
  extra_imports:
  - ["{config_root}", "scripts"]

stacks:
  app:
    [..]

Then before Bespin looks for tasks it will first import the python module named scripts that lives in the folder where bespin.yml is defined. So in this case, the scripts.py.

The only thing scripts.py needs is a __bespin__(bespin, task_maker) method where bespin is the Bespin object and task_maker is a function that may be used to register tasks.

For example:

def __bespin__(bespin, task_maker):
  task_maker("deploy_app", "Deploy the app stack", action="deploy").specify_stack("app")

Here we have defined the deploy_app action that will deploy the app stack.

We can do something more interesting if we also define a custom action:

from bespin.tasks import a_task

def __bespin__(bespin, task_maker):
  task_maker("list_amis", "List amis with a particular tag")

@a_task(needs_credentials=True)
def list_amis(overview, configuration, **kwargs):
  credentials = configuration['bespin'].credentials
  amis = credentials.ec2.get_all_images(filters={"tag:application": "MyCreatedAmis"})
  for ami in amis:
    print(ami.id)

And then we can do bespin list_amis dev and it will find all the Amis that have an application tag with MyCreatedAmis.