-
-
Save vvuksan/66cc45e09812fbf90808 to your computer and use it in GitHub Desktop.
Acquia service automated configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$conf['fastly-api-key'] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
$service_name = "acquia-test"; | |
$backend_ip = "xxxxxxxxxxxxxxxxx"; | |
$domain_name = "www.myownacquiatest.com"; | |
include_once("./fastly_api.php"); | |
$fastly = new FastlyAPI(); | |
$fastly->AuthKey($conf['fastly-api-key']); | |
$version = 1; | |
################################################################################# | |
# Create a service | |
################################################################################# | |
$ret = $fastly->API_service_byName($service_name); | |
if ( isset($ret->versions[0]->service_id) ) { | |
$service_id = $ret->versions[0]->service_id; | |
} else { | |
$ret = $fastly->API_service_create( | |
array( | |
"name" => $service_name | |
) | |
); | |
$service_id = $ret->versions[0]->service_id; | |
print_r($ret); | |
} | |
################################################################################# | |
# Create a backend | |
################################################################################# | |
print "ADDING BACKEND: $backend_ip\n"; | |
$name = "default"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_backend( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_backend_create( $service_id, $version, | |
array( | |
"ipv4" => $backend_ip, | |
"version" => $version, | |
"name" => $name, | |
"port" => 80, | |
"service" => $service_id | |
) | |
); | |
} else { | |
print "===> Backend already exists. Skipping\n"; | |
} | |
################################################################################# | |
# Add a domain | |
################################################################################# | |
$name = $domain_name; | |
print "ADDING DOMAIN: $name\n"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_domain( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_domain_create( $service_id, $version, | |
array( | |
"name" => $domain_name | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Domain already exists. Skipping\n"; | |
} | |
###################################################################################### | |
# Create conditions | |
###################################################################################### | |
###################################################################################### | |
# Static content | |
###################################################################################### | |
$name = "Static content"; | |
print "ADDING CONDITION: $name\n"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_condition( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_condition_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"priority" => "10", | |
"type" => "CACHE", | |
"statement" => 'req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$"' | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Condition already exists. Skipping\n"; | |
} | |
###################################################################################### | |
# HTML content | |
###################################################################################### | |
$name = "HTML-Plain text responses"; | |
print "ADDING CONDITION: $name\n"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_condition( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_condition_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"priority" => "10", | |
"type" => "RESPONSE", | |
"statement" => 'resp.http.Content-Type ~ "^(?:text/(?:html|plain))"' | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Condition already exists. Skipping\n"; | |
} | |
###################################################################################### | |
# Session cookies | |
###################################################################################### | |
$name = "Session Cookies"; | |
print "ADDING CONDITION: $name\n"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_condition( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_condition_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"priority" => "10", | |
"type" => "REQUEST", | |
"statement" => 'req.http.Cookie ~ "(^|;\s*)(S?SESS[a-zA-Z0-9]*)=" || req.http.Cookie ~ "(NO_CACHE|PERSISTENT_LOGIN_[a-zA-Z0-9]+)"' | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Condition already exists. Skipping\n"; | |
} | |
###################################################################################### | |
# Session cookies | |
###################################################################################### | |
$name = "Session Cookies NOT PRESENT"; | |
print "ADDING CONDITION: $name\n"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_condition( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_condition_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"priority" => "10", | |
"type" => "REQUEST", | |
"statement" => 'req.http.Cookie !~ "(^|;\s*)(S?SESS[a-zA-Z0-9]*)=" && req.http.Cookie !~ "(NO_CACHE|PERSISTENT_LOGIN_[a-zA-Z0-9]+)"' | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Condition already exists. Skipping\n"; | |
} | |
$name = "Session Cookies NOT PRESENT CACHE SETTING"; | |
print "ADDING CONDITION: $name\n"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_condition( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_condition_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"priority" => "10", | |
"type" => "CACHE", | |
"statement" => 'req.http.Cookie !~ "(^|;\s*)(S?SESS[a-zA-Z0-9]*)=" && req.http.Cookie !~ "(NO_CACHE|PERSISTENT_LOGIN_[a-zA-Z0-9]+)"' | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Condition already exists. Skipping\n"; | |
} | |
###################################################################################### | |
# Match for cron requests | |
###################################################################################### | |
$name = "Match for cron requests"; | |
print "ADDING CONDITION: $name\n"; | |
# Check that the condition doesn't exist | |
$ret = $fastly->API_condition( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_condition_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"priority" => "10", | |
"type" => "REQUEST", | |
"statement" => 'req.url ~ "^/cron.php" || req.http.User-Agent ~ "simpletest"' | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Condition already exists. Skipping\n"; | |
} | |
###################################################################################### | |
###################################################################################### | |
# Headers | |
###################################################################################### | |
###################################################################################### | |
$name = "Delete cookies in responses"; | |
print "ADDING HEADER: $name\n"; | |
$ret = $fastly->API_header( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_header_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"type" => "cache", | |
"action" => "delete", | |
"dst" => "http.Set-Cookie", | |
"cache_condition" => "Static content", | |
"priority" => "10" | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Header already exists. Skipping\n"; | |
} | |
$name = "Set cache-control to no cache for HTML"; | |
print "ADDING HEADER: $name\n"; | |
$ret = $fastly->API_header( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_header_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"type" => "response", | |
"action" => "set", | |
"dst" => "http.cache-control", | |
"src" => "\"max-age: 0, private\"", | |
"response_condition" => "HTML-Plain text responses", | |
"priority" => "10" | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Header already exists. Skipping\n"; | |
} | |
$name = "Remove cookies if they are not whitelisted"; | |
print "ADDING HEADER: $name\n"; | |
$ret = $fastly->API_header( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_header_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"type" => "request", | |
"action" => "delete", | |
"dst" => "http.cookie", | |
"request_condition" => "Session Cookies NOT PRESENT", | |
"priority" => "99" | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Header already exists. Skipping\n"; | |
} | |
###################################################################################### | |
###################################################################################### | |
# Request settings | |
###################################################################################### | |
###################################################################################### | |
$name = "Pass on cookie"; | |
print "ADDING REQUEST SETTING: $name\n"; | |
$ret = $fastly->API_request_settings( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_request_settings_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"action" => "pass", | |
"request_condition" => "Session Cookies", | |
"xff" => "append", | |
"max_stale_age" => "60" | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Request Setting already exists. Skipping\n"; | |
} | |
$name = "Pass on cron or special requests"; | |
print "ADDING REQUEST SETTING: $name\n"; | |
$ret = $fastly->API_request_settings( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_request_settings_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"action" => "pass", | |
"request_condition" => "Match for cron requests", | |
"xff" => "append", | |
"max_stale_age" => "60" | |
) | |
); | |
print_r($ret); | |
} else { | |
print "===> Request Setting already exists. Skipping\n"; | |
} | |
###################################################################################### | |
###################################################################################### | |
# Cache settings | |
###################################################################################### | |
###################################################################################### | |
$name = "Cache on no cookie"; | |
print "ADDING CACHE SETTING: $name\n"; | |
$ret = $fastly->API_cache_settings( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_cache_settings_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"action" => "cache", | |
"cache_condition" => "Session Cookies NOT PRESENT CACHE SETTING", | |
"stale_ttl" => "120", | |
"ttl" => "86400" ) | |
); | |
print_r($ret); | |
} else { | |
print "===> Cache Setting already exists. Skipping\n"; | |
} | |
$name = "Cache static content"; | |
print "ADDING CACHE SETTING: $name\n"; | |
$ret = $fastly->API_cache_settings( $service_id, $version, $name ); | |
if ( isset($ret->msg) and $ret->msg == "ERROR" ) { | |
$ret = $fastly->API_cache_settings_create( $service_id, $version, | |
array( | |
"name" => $name, | |
"action" => "cache", | |
"cache_condition" => "Static content", | |
"stale_ttl" => "120", | |
"ttl" => "86400" ) | |
); | |
print_r($ret); | |
} else { | |
print "===> Cache Setting already exists. Skipping\n"; | |
} | |
#################################################################### | |
# Validate configuration | |
#################################################################### | |
$result = $fastly->API_version_validate( $service_id, $version); | |
if ( $result ) { | |
print "Service config validated. Activating. You can view config here https://app.fastly.com/#configure/service/$service_id \n"; | |
print_r($fastly->API_version_activate($service_id, $version)); | |
} else { | |
die("Configuration is not validating. Please check it https://app.fastly.com/#configure/service/$service_id\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment