summaryrefslogtreecommitdiffstats
path: root/scripts/maintain/prunerc.sh
blob: 166ede6dc1ce0273558536a57568ac3f192398c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /bin/bash

setup=""
commit=0
prune_stopped=0

while [ -n "$1" ]; do
  case "$1" in
    -c | --commit ) commit=1; shift ;;
    -s | --stopped ) prune_stopped=1; shift ;;
    -n | --setup ) setup="-n $2"; shift 2;;
    * ) echo "$0 [-n project] [--commit] [--stopped]"; break ;;
  esac
done


dcs="$(oc $setup get dc | grep -v NAME)"

while read -r dc; do
    name=$(echo "$dc" | awk '{ print $1 }')
    revision=$(echo "$dc" | awk '{ print $2 }')
    dc_desired=$(echo "$dc" | awk '{ print $3 }')
    dc_current=$(echo "$dc" | awk '{ print $4 }')
    
    rcname="$name-$revision"
    rc="$(oc $setup get rc $rcname | grep -v NAME)"
    desired=$(echo "$rc" | awk '{ print $2 }')
    current=$(echo "$rc" | awk '{ print $3 }')
    ready=$(echo "$rc" | awk '{ print $4 }')

    if [ $dc_desired -ne $dc_current ]; then
        [ $commit -eq 1 ] && echo "Skipping faulty dc/$name"
        continue
    elif [ $desired -ne $current -o $desired -ne $ready ]; then
        [ $commit -eq 1 ] && echo "Skipping not completely ready dc/$name"
        continue
    elif [ $desired -eq 0 -a $prune_stopped -ne 1 ]; then
        [ $commit -eq 1 ] && echo "Skipping stopped dc/$name (last one could be faulty)"
        continue
    fi


    rcs="$(oc $setup get rc -l openshift.io/deployment-config.name=$name | grep -v NAME)"
    
    while read -r oldrc; do
        oldname=$(echo "$oldrc" | awk '{ print $1 }')
        desired=$(echo "$oldrc" | awk '{ print $2 }')

        [ $oldname = $rcname ] && continue
        [ $desired -ne 0 ] && continue
        
        if [ $commit -eq 1 ]; then
            echo "Removing: $oldname (keeping $rcname[$ready])" 
            oc $setup delete rc "$oldname"
        else 
            echo "Will remove: $oldname (keeping $rcname[$ready])" 
        fi
    done <<< "$rcs"
    
done <<< "$dcs"