Automating the Shutdown and Restart of Clever Cloud Environments with GitLab CI/CD

lenybernard

Leny BERNARD

Posted on July 4, 2023

Automating the Shutdown and Restart of Clever Cloud Environments with GitLab CI/CD

In this article, I will share how we, at Toovalu, place a high emphasis on eco-design and carbon emissions reduction in our operations. One of the ways we manifest this focus is by optimizing server usage. We employ GitLab CI/CD to automate the shutdown and restart of Clever Cloud environments, reflecting our belief that the most eco-friendly resource is the one not consumed. This article will provide insights into how we meticulously schedule the downtime of our servers during periods of low traffic, such as weekends or overnight. This efficient and strategic approach not only reduces hosting costs but also minimizes our carbon footprint. We ensure resource consumption is tailored to our needs, effectively using only what we require, when we require it.

Before we dive into the technicalities, let's cover the prerequisites:

Prerequisites

  • A Clever Cloud account with environments to manage
  • A project hosted on GitLab
  • Clever Cloud API tokens (CLEVER_SECRET and CLEVER_TOKEN) (read the doc)

Step 1: Configuring GitLab CI/CD

We'll start by setting up GitLab CI/CD with a .gitlab-ci.yml file that describes the tasks to perform. The .gitlab-ci.yml file outlines the structure and order of GitLab CI/CD pipelines.

Here is an example of a .gitlab-ci.yml configuration:


stages:
   // other stages
    - switch_env

// main jobs

include:
    - '.gitlab/.clever-cloud-environment-switcher.yml'
Enter fullscreen mode Exit fullscreen mode

Here, we have a stage named switch_env which is used to manage the shutdown and restart of our environments. We also include another configuration file called .clever-cloud-environment-switcher.yml located in our project’s .gitlab directory.

Step 2: Configuring the Environment Switcher

In the .clever-cloud-environment-switcher.yml file, we define two jobs, switch_env_off and switch_env_on, to stop and restart our environments, respectively.


stages:
    - switch_env

switch_env_off:
    stage: switch_env
    image:
        name: clevercloud/clever-tools:latest
        entrypoint: ['/bin/sh', '-c']
    script:
        - clever login --secret $CLEVER_SECRET --token $CLEVER_TOKEN
        - clever stop -a $ENVIRONMENT_NAME
    only:
        variables:
            - $PIPELINE_SCHEDULE_NAME == "switch_env_off"

switch_env_on:
    stage: switch_env
    image:
        name: clevercloud/clever-tools:latest
        entrypoint: ['/bin/sh', '-c']
    script:
        - clever login --secret $CLEVER_SECRET --token $CLEVER_TOKEN
        - clever restart -a $ENVIRONMENT_NAME
    only:
        variables:
            - $PIPELINE_SCHEDULE_NAME == "switch_env_on"
Enter fullscreen mode Exit fullscreen mode

Both jobs use the Docker image clevercloud/clever-tools which includes the Clever CLI tool. We use this tool to log into Clever Cloud with our tokens (clever login), and to stop (clever stop) or restart (clever restart) our environments.

Note that each job is only executed when the PIPELINE_SCHEDULE_NAME environment variable matches the specified value (either switch_env_off for stopping the environment or switch_env_on for restarting it).

Step 3: Configuring the Scheduled Pipelines

Now we'll set up the scheduled pipelines in GitLab to stop and restart our environments. I won't explain here the basics of Scheduled pipelines, instead read the awesome related doc here if you don't know about it. Anyway, for each environment we want to manage, we'll create two scheduled pipelines, one for switch_env_off and another for switch_env_on.

When creating each scheduled pipeline, we define two environment variables:

PIPELINE_SCHEDULE_NAME: switch_env_off 
ENVIRONMENT_NAME: preprod
Enter fullscreen mode Exit fullscreen mode

or

PIPELINE_SCHEDULE_NAME: switch_env_on 
ENVIRONMENT_NAME: staging
Enter fullscreen mode Exit fullscreen mode

Conclusion

With this setup, GitLab CI/CD will automatically handle the shutdown and restart of your Clever Cloud environments according to the schedule you set in the scheduled pipelines. This is an efficient and flexible solution for managing your resources and reducing hosting costs.

Remember to secure your Clever Cloud API tokens by storing them as secret variables in GitLab rather than in plain text in your configuration files.

💖 💪 🙅 🚩
lenybernard
Leny BERNARD

Posted on July 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024