Skip to content

Repository files navigation

CfScript

Gem VersionBuild Status

CfScript is a DSL for scripting the Cloud Foundry CLI, with a focus on application deployment and management. See Supported Commands

# example.rbrequire'cf_script'cfspace: :developmentdoworkers=appsending_with: 'worker'workers.eachdo |worker|
appworkerdoshow:state,:memory,:instancesstopifstarted?progress'Setting ENV variable...'set_env'SOME_NAME','SOME_VALUE'progress"Restaging #{name} in #{current_space}..."restageendendend

To see the cf commands that get executed, run with TRACE=1.

TRACE=1 ruby example.rb

Requirements

  • ruby >= 2.2
  • cf >= 6.0

Install

Install the gem with:

gem install cf_script

Or, add it to your project's Gemfile:

gem 'cf_script'


Execution Scopes

There are three execution scopes/blocks; cf, space, and app:

The cf Block Scope

The cf scope is the top-level scope, it includes evertything between the do/end following a call to cf. The scope saves the current target on entry and restores it on exit.

# cf target => stagingcfspace: :developmentdo# cf target => developmentend# cf target => staging

The cf block accepts the following options:

  • api: Sets the API endpoint (optional)
  • org: Sets the target organization (optional)
  • username/password: Credentials to use for logging in. (optional)
  • space: Sets the target space (optional)

Note: To save the current target the cf block will execute cf target on entry, and if it changed within the block, it wil execute cf target SPACE on exit to restore it.

The space Block Scope

The space scope is a sub-scope and can only appear within a cf block. It also saves the current target on entry and restores, if changed, it on exit.

# before cf block, target => stagingcfspace: :developmentdo# inside cf block, target => developmentspace:productiondo# inside space block, target => productionend# still inside cf block, target => developmentend# after cf block, target => staging

The app Block Scope

The app scope can appear within a cf or space blocks. Within the block the selected app is the target of commands, this means that commands that take an application name as their first argument can be called without it.

cfdoapp:apidoenv.eachdo |name,value|
unset_envnameendstarted? ? restart : startendend

The app method accepts a string or an AppInfo object as a name argument.

The apps Method

The apps method mirrors the cf apps command. When called without a block, it returns an AppList object that acts as an array of AppInfo objects, each of which contains the information in each row output by cf apps.

cfdo# Get the list of AppInfo objectsspace_apps=apps# Iterate over the AppInfo objectsapps.eachdo |app_info|
# ...endend

When the apps method is called with a block, it excutes the block within the context of each app.

cfdoappsdostarted? ? restart : startendend

This is equivalent to:

cfdoapps.eachdo |app_info|
appapp_infodostarted? ? restart : startendendend

Supported Commands

CommandsCLI command
General
apicf api URL
authcf auth USER PASSWORD
logincf login -u USER -p PASSWORD [OPTIONS]
logoutcf logout
targetcf target [-s SPACE -o ORG]
Applications
appscf apps
appcf app APP_NAME
startcf start APP_NAME
stopcf stop APP_NAME
restartcf restart APP_NAME
pushcf push APP_NAME [OPTIONS]
restagecf restage APP_NAME
scalecf scale APP_NAME [OPTIONS]
renamecf rename APP_NAME NEW_APP_NAME
deletecf delete APP_NAME [-f -r]
envcf env APP_NAME
set-envcf set-env APP_NAME VAR_NAME VAR_VALUE
unset-envcf unset-env APP_NAME VAR_NAME
restart-app-instancecf restart-app-instance APP_NAME INDEX
Routes
routescf routes
check-routecf check-route HOST DOMAIN
create-routecf create-route SPACE DOMAIN [-n HOSTNAME]
map-routecf map-route APP_NAME DOMAIN [-n HOSTNAME]
unmap-routecf unmap-route APP_NAME DOMAIN [-n HOSTNAME]
delete-routecf delete-route DOMAIN [-n HOSTNAME] [-f]
Spaces
spacescf spaces
spacecf space SPACE

Building

The project uses the standard rubygems package tasks, so:

To build your cloned copy of the gem, run:

rake build

To install the gem from the cloned project, run:

rake install

Known Problems/Limitations

  • Obviously everything depends on the output of cf, so if that changes things will break. This is not unique to cf_script, but it is something worth noting. The centralized parsing of cf's output should reduce the impact of change on scripts, but there is no guarantee.

  • The fixtures were collected manually and they might become a pain to manage. Will investigate creating a tool/script that automates their collection from cf.

  • I18N, n'existe pas, mais, it's not impossible. If implemented, it should be based on the latest translation files from the cf repository, and the process of pulling the files automated. In the meantime, might explicity add LANG to the ENV variables passed to cf.

  • Windows? No clues. Probably not without some changes.

About

A simple DSL for scripting the Cloud Foundry CLI

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages