Automatically Purging Varnish Cache Using Cloudways API

by Fahad Saleh  October 5, 2016

Cloudways recently launched its native API, giving you the power to interact with your server without even opening the web browser.

purging-varnish-cache-weekly-banner

In this article, I will explain how to create and setup a PHP script that uses the Cloudways API to purge one or all of your server’s Varnish cache. I will also add the script to the Cron list so that it runs weekly. The PHP script is the perfect introduction to the Cloudways API.

The API Key

The first order of business is the generation of the API Key from here.

The Code

The code starts with the creation of the Cloudways API client class.

CloudwaysAPI.class.php

This is the basic client class. More functions could be added to the class as the need arises. For now, focus on the prepare_access_token()  function that is called inside the constructor.

Authentication

Cloudways uses OAuth for authentication. The process of OAuth is:

Client sends request with API key

Client ——-> <secret-unique-API-key> ——> API

On successful validation, API returns an access token

Client <——-  <unique-API-access-token> <—– API

The client uses the unique access token inside the header(‘Authorization: Bearer <unique-api-access-token>’) to make all subsequent requests

Client  ——-> <unique-API-access-token> + request —–> API

In the API class, the prepare_access_token() takes care of this process automatically. It calls the /oauth/access_token endpoint using the request function and sets the accessToken variable to a valid access token. The request function is generic and will handle all other endpoints that might be added further. It will also automatically refresh expired OAuth access token (the token expires after 3600 seconds of inactivity) and retry the request.

Now that the boilerplate is out of the way, I can start adding more functionality.

I will add one more function to the class (inside CloudwaysAPI.class.php) for listing the servers.

This function will call the API using the generic request function and return a list of servers along with their information.

At this point, it is time to create the actual script. I could add the code for the script inside CloudwaysAPI.class.php file. However, keeping future development purposes in view, I will keep them separate.

Create the following file and replace the API key with the one generated above. Also, replace the email.

purge_varnish.php

I hope that the script is self-explanatory. I include CloudwayAPI.class.php (created earlier) and create a CloudwaysAPIClient object using my credentials (email and API key).

At this point, OAuth has been taken care of and I can use start using the cw_api object to make the API calls.

Next, I will call the API to get the list of servers and will loop through the list.

Ignore the commented out lines for now as I have not implemented the service_varnish function yet.

Ignore the commented out lines for now as I have not implemented the service_varnish function yet.

Running this script will output the server ID along with its server label/name.

ID -> 58430, SERVER -> myTestServer

The get_servers function gives all the information that I need for purging the Varnish cache, including server ID.

Now, let’s add another API endpoint. Add the following function to your CloudwaysAPIClient class inside CloudwayAPI.class.php:

Uncomment the following lines from the purge_varnish.php file

At this point, running the script will purge Varnish cache for all of the servers on the list. For a specific server, just use the server name:

The Cron Job

Now add a Cron job for the script. On Cloudways, you can use the application Cron job management page. In the common settings dropdown, choose once a week, enter the script name (relative to your public_html folder).

At this point, the purpose of this tutorial has been achieved and the Varnish cache for the servers will be refreshed weekly. The boilerplate for working with the CloudwaysAPI is also ready.

Something Extra

I will now make sure that the Cron job was successfully added by adding the following function to the CloudwaysAPIClient class. This retrieves the Cron job list for the servers:

Also, I will modify the script:

This will return information about the Cron job.

I hope that you will have no issues in implementing this script. In future tutorials, I will explore Cloudways API in more detail. If you have a question or would like to add to the discussion, please leave a comment.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Fahad Saleh

Fahad Saleh is a DevOps Engineer at Cloudways

Stay Connected:

You Might Also Like...