Information for: DEVELOPERS   PARTNERS

Factory API examples

The Factory API enables you to interact with Acquia Cloud Site Factory from the command line by scripting often repeated events.

For information about obtaining your API key or basic usage of the Factory API, see Using the Site Factory API.

The following example scripts were created by Acquia Cloud Site Factory users, and you can adapt them to meet your needs:

Synchronizing production and development environments

The following script allows you to begin the website staging process to a specified environment, after modifying the script to fit your environment’s needs:

#!/usr/bin/env bash
## Synchronizing production and development environments on
## Acquia Cloud Site Factory
## Origin: http://docs.acquia.com/site-factory/extend/api/examples

#!/bin/sh

# Update the next two lines to provide your ACSF username and API key
user="[your_ACSF_username]"
api_key="[your_API_key]"

# Machine name of target environment
to_acsf_environment="dev"

# Update the next line to provide a comma-separated list of site IDs
sites="[123,456]"

# Update the next line to provide your Acquia Cloud Site Factory domain name
curl 'https://www.[domain].acsitefactory.com/api/v1/stage' \
    -X POST -H 'Content-Type: application/json' \
    -d "{"to_env": "${to_acsf_environment}", "sites": [ ${sites} ], "detailed_status": true}" \
    -v -u $user:$api_key

Initiating code releases

The following script begins a code and database update for the specified environment.

#!/bin/sh
## Initiate a code and database update from Acquia Cloud Site Factory
## Origin: http://docs.acquia.com/site-factory/extend/api/examples

# This script should primarily be used on non-production environments.

# Mandatory parameters:
# env : environment to run update on. Example: dev, pprod, qa2, test.
#       - the api user must exist on this environment.
#       - for security reasons, update of prod environment is *not*
#         supported and must be performed manually through UI
# branch : branch/tag to update. Example: qa-build
# update_type : code or code,db

source $(dirname "$0")/includes/global-api-settings.inc.sh

env="$1"
branch="$2"
update_type="$3"

# add comma to "code,db" if not already entered
if [ "$update_type" == "code,db" ]
then
update_type="code, db"
fi

# Edit the following line, replacing [domain] with the appropriate
# part of your domain name.

curl "https://www.${env}-[domain].acsitefactory.com/api/v1/update" \
-v -u ${user}:${api_key} -k -X POST \
-H 'Content-Type: application/json' \
-d "{\"sites_ref\": \"${branch}\", \"sites_type\": \"${update_type}\"}"

Listing all websites

The Acquia Cloud Site Factory API only returns data for 100 websites at a time. The get_all_sites function in this example script provides a method for retrieving data for all websites.

Backing up databases of multiple websites

The backup function in this example script provides a method for creating database backups for multiple websites hosted by Acquia Cloud Site Factory, and a backup_del function to iterate through existing backups, deleting all backups older than a specified age.