Last week we showed you how to list the resources associated with your MongoDB Atlas environment via a simple Python program. Let’s extend this program this week with a more useful feature, the ability to pause and resume clusters. We can use the Atlas Management API to do this via the “Pause Cluster” menu entry.
However, when we pause a cluster the Atlas environment will restart the cluster after seven days. Also, both pausing and resuming require a login, navigation etc. Basically, it’s a drag to do this regularly. If you are running clusters for development they are rarely required late at night or at weekends.
It would be great to have a simple script to pause and resume these clusters using the project ID and cluster name. Then we could run this script in crontab or our own favorite scheduling program and pause and resume clusters on a defined schedule. We have rewritten the py-atlas-list.py script to do exactly that.
The extended py-atlas-list.py script allows you to both list resources and pause and/or resume clusters using their project ID and cluster name.
$ python py-atlas-cluster.py -h usage: py-atlas-cluster.py [-h] [--username USERNAME] [--apikey APIKEY] [--project_id PROJECT_ID] [--org_id ORG_ID] [--pause PAUSE_CLUSTER_NAME] [--resume RESUME_CLUSTER_NAME] [--list] optional arguments: -h, --help show this help message and exit --username USERNAME MongoDB Atlas username --apikey APIKEY MongoDB Atlas API key --project_id PROJECT_ID specify project for cluster that is to be paused --org_id ORG_ID specify an organisation to limit what is listed --pause PAUSE_CLUSTER_NAME pause named cluster in project specified by --project_id --resume RESUME_CLUSTER_NAME resume named cluster in project specified by --project_id --list List of the complete org hierarchy $
To pause a cluster just run:
$ python py-atlas-cluster.py --list --org_id XXXXXXXXXXXXXXXXXXXX175c 1. Org : 'Open Data at MongoDB', id=XXXXXXXXXXXXXXXXXXXX175c 1. Proj : 'JD Stitch Demos', id=XXXXXXXXXXXXXXXXXXXXcb08 1. cluster: 'stitch', id=XXXXXXXXXXXXXXXXXXXX5697 paused=True 2. Proj : 'MUGAlyser', id=XXXXXXXXXXXXXXXXXXXX9bab 1. cluster: 'MUGAlyser', id=XXXXXXXXXXXXXXXXXXXXbfba paused=False 3. Proj : 'Open Data', id=XXXXXXXXXXXXXXXXXXXX8010 1. cluster: 'Utility', id=XXXXXXXXXXXXXXXXXXXX1a03 paused=True 2. cluster: 'MOT', id=XXXXXXXXXXXXXXXXXXXX94dd paused=False 3. cluster: 'Foodapedia', id=XXXXXXXXXXXXXXXXXXXX9fbf paused=False 4. cluster: 'UKPropertyPrices', id=XXXXXXXXXXXXXXXXXXXX7ac5 paused=False 5. cluster: 'New-York-Taxi', id=XXXXXXXXXXXXXXXXXXXXa18a paused=False 6. cluster: 'demodata', id=XXXXXXXXXXXXXXXXXXXX2cf8 paused=False
To get the project ID look for the id field for the Proj entry. To get the cluster name just look for the string in quotes after the cluster identifier. We have highlighted the project ID and the cluster name we are going to use.
Now to pause the cluster just run:
$ python py-atlas-cluster.py --project_id XXXXXXXXXXXXXXXXXXXX9bab --pause MUGAlyser Pausing cluster: 'MUGAlyser' $
To resume a cluster just use the --resume argument instead of the --pause argument. Want to pause or resume more than one cluster in a single project? You can, just by adding multiple --pause or --resume arguments.
Now, you just need to add this script to your favourite scheduler. Note for this example I have already set the environment variables
ATLAS_APIKEY so we don’t need to pass them in on the command line.
Now go save some money on your development clusters. Your boss will thank you!