Restart all applications in PCF space

2.8k Views Asked by At

We have tons of services hosted in Pivotal Cloud Foundry(PCF). What is the best way to restart all micro-services in given space via scripts? Other challenge we have is we want to start services in order and introduce some delay between each service start ups. We are doing it manually right now but it is tedious and time consuming. Please suggest if anyway we can automate it. Thx.

3

There are 3 best solutions below

1
On

I would suggest you write a wrapper-script either in powershell or bash that will first execute cf apps in your space

The above command will give you the App Name. Read that text and have your wrapper-script execute cf restart <APP_NAME> in a loop

This will restart all the Apps in your space...

Regarding Introducing delay in service start ups.. I would suggest you to have a CI/CD process to have your apps deployed (a Jenkins process, for example) with which you can have a complete control over your deployments

0
On

We had the same issue, and ended up with a script like the one below to iterate through all apps across all orgs and restart them. It uses jq and the cf curl api responses to hopefully be a little more robust than just iterating over cf apps (which may trip on whitespace, etc.

#!/usr/bin/env bash

orgs=$(cf curl '/v2/organizations' | jq -r '.resources[].entity.name')

for org in $orgs; do
  cf t -o "$org"
  org_guid=$(cf org "$org" --guid)
  spaces=$(cf curl /v2/organizations/"$org_guid"/spaces | jq -r '.resources[].entity.name')
  for space in $spaces; do
    cf t -s "$space"
    space_guid=$(cf space "$space" --guid)
    apps=$(cf curl "/v2/apps?q=space_guid:${space_guid}&results-per-page=100" | jq -r '.resources[].entity.name')
    for app in $apps; do
      cf rs "${app}"
    done
  done
done
0
On

To realize what @Arun suggested:

for i in $(cf apps | grep '[0-9]/[0-9]' | cut -d" " -f1); do cf restart $i; done

To have a certain order, you could maintain a text file containing the app names in the correct order:

first_app
second_app
(...)

If the file is called app_order.txt, then to restart in order with a delay of say 30 seconds each, do:

while read i; do cf restart $i; sleep 30; done < app_order.txt