Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 049be61807eaa3652f6b06b0ae7cdff54f162451 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
#*******************************************************************************
# Copyright (c) 2019 IBM Corporation and others.
#
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#     Sravan Lakkimsetti - initial API and implementation
#*******************************************************************************

# Utility to clean build machine
echo -e "\n\tDaily clean of ${HOSTNAME} download server on $(date )\n"

#
# Checks whether a build can be retained or not.
# returns 0  if the build can be retained
#
function canBeRetained ()
{
  retval=1
  buildName=$1
  grep BUILD_FAILED ${buildName}/buildproperties.shsource >/dev/null 2>&1
  if [ $? -ne 0 ]; then #build didn't fail
    if [ -f "${buildName}/buildUnstable" ]; then  #build is marked unstable so should not be retained
      retval=1
    else
      retval=0
    fi
  fi
  return $retval
}

#
# remove a build
#

function removeBuild ()
{
  buildname=$1
  rm -fr $buildname
  RC=$?
  if [[ $RC = 0 ]];then
    echo -e "Removed: $buildname"
  else
    echo -e "\n\tAn Error occurred removing $buildname. RC: $RC"
  fi
}

cDir="/home/data/httpd/download.eclipse.org/eclipse/downloads/drops4"
buildType="I*"
allOldBuilds=$( find ${cDir} -maxdepth 1 -type d -ctime +5 -name "${buildType}"|sort )
nbuilds=$( find ${cDir} -maxdepth 1 -type d -name "${buildType}" | wc -l )
echo -e "\tNumber of I-builds before cleaning: $nbuilds"

# Make sure we leave at least 4 on DL server, no matter how old
# To avoid 'ls' see http://mywiki.wooledge.org/ParsingLs
# technically, applies to "find" as well.
shopt -s nullglob
count=0
files=(${cDir}/${buildType})
# We count on files being "ordered" by date/timestamp in name
for  ((i=${#files[@]}-1; i>=0; --i)); do
  newest[$count]="${files[$i]}"
  count=$(( count + 1 ))
  if [ $count -gt 3 ]
  then
    break
  fi
done

areNotToDelete=$(printf '%s\n' "${newest[@]}" | paste -sd '|')

currentWeekNum=0 #week number from start of the year user to identify the week in which the build is created
found=0 #it will be 1 when we found a build that can be retained in that week

for buildname in ${allOldBuilds}; do
  if [[ $buildname =~ $areNotToDelete ]]
  then
    echo -e "\tDEBUG: Not removed (since one of 4 newest, even though old): \n\t$buildname"
  else
    buildId=$(basename $buildname) #extract buildId
    yy=$(echo $buildId|cut -b2-5)  #extract year from buildId
    mm=$(echo $buildId|cut -b6-7)  #extract month
    dd=$(echo $buildId|cut -b8-9)  #extract day
    day=${mm}/${dd}/${yy}          #construct build date
    dayOfWeek=$(date -d $day +%u)  #get the day of the week like monday, tue, etc
    weekNum=$(date -d $day +%U)    #get the week number from start of the year

    #special case for Sunday. unix considers sunday as the start of the week. but for us we need to consider
    #monday as start of the week. For this purpose we subtract 1 to place the build in previous week
    if [ $dayOfWeek -eq 7 ]; then
      weekNum=$(expr $weekNum - 1)
      if [ $weekNum -le 0 ]; then  #check for the yearend
        weekNum=53
      fi
    fi

    canBeRetained ${buildname}
    retain=$?

    if [ $weekNum -eq $currentWeekNum ]; then
      if [ $retain -eq 0 -a $found -ne 1 ]; then  # we didn't found a build that can be retained in the current week
        found=1
      else
        removeBuild $buildname
      fi
    else #week changed
      currentWeekNum=$weekNum
      found=0
      if [ $retain -eq 0 -a $dayOfWeek -eq 1 ]; then
        found=1
      else
        removeBuild $buildname
      fi
    fi
  fi
done

nbuilds=$( find ${cDir} -maxdepth 1 -type d -name "${buildType}" | wc -l )
echo -e "\tNumber of N-builds after cleaning: $nbuilds"

Back to the top