Skip to content

Commit

Permalink
Fix resources cleanup and env variables passing cron jobs (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
schernysh authored Aug 21, 2020
1 parent e3bdf42 commit 5b80f76
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 90 deletions.
34 changes: 15 additions & 19 deletions buildCleanup.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
#!/bin/bash

NUMBER_OF_LATEST_VERSIONS="$1"

if [ $NUMBER_OF_LATEST_VERSIONS -gt 0 ]; then
echo "deleting prebid files older than last $NUMBER_OF_LATEST_VERSIONS versions"
else
NUMBER_OF_LATEST_VERSIONS=20
echo "deleting prebid files older than last $NUMBER_OF_LATEST_VERSIONS versions"
if [[ "$1" -gt 0 ]]; then
NUMBER_OF_PREVIOUS_VERSIONS="$1"
elif [[ ! "$NUMBER_OF_PREVIOUS_VERSIONS" -gt 0 ]]; then
NUMBER_OF_PREVIOUS_VERSIONS=2
fi

cd prebid.js
echo "=====> Deleting old versions, number of versions to keep is $NUMBER_OF_PREVIOUS_VERSIONS"

VERSION_DIRECTORIES="$(ls -d prebid_* | sort -r)"
VERSION_DIRECTORIES="$(ls -d prebid.js/prebid_* | sort -rV)"

ITERATION=0
for DIR in $VERSION_DIRECTORIES;
do
if [ $ITERATION -lt $NUMBER_OF_LATEST_VERSIONS ]; then
ITERATION=$[ITERATION+1]
continue
fi
echo "deleting directory $DIR"
rm -rf $DIR
done
for DIR in $VERSION_DIRECTORIES; do
if [[ "$ITERATION" -lt "$NUMBER_OF_PREVIOUS_VERSIONS" ]]; then
ITERATION=$((ITERATION + 1))
continue
fi
echo "Deleting directory $DIR"
rm -rf "$DIR"
done

echo "Versions clean up complete"
echo "=====> Old versions clean up complete"
50 changes: 29 additions & 21 deletions bundleCleanup.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
#!/bin/bash

BUNDLE_LIFE_PERIOD="$1"
shopt -s nullglob

if [ $BUNDLE_LIFE_PERIOD -gt 0 ]; then
echo "deleting bundles older than $BUNDLE_LIFE_PERIOD seconds"
else
BUNDLE_LIFE_PERIOD=300
echo "deleting bundles older than $BUNDLE_LIFE_PERIOD seconds"
if [[ "$1" -gt 0 ]]; then
CLEANUP_BUNDLES_OLDER_THAN_SECONDS="$1"
elif [[ ! "CLEANUP_BUNDLES_OLDER_THAN_SECONDS" -gt 0 ]]; then
CLEANUP_BUNDLES_OLDER_THAN_SECONDS=2
fi

echo "=====> Deleting bundles older than $CLEANUP_BUNDLES_OLDER_THAN_SECONDS seconds"

CURRENT_TIME=$(date +%s)

for DIR in prebid.js/prebid_*;
do
echo "checking $DIR"
BUNDLE_DIR="${DIR}/build/dist/prebid.*.js"
for FILE in $BUNDLE_DIR;
do
FILE_LAST_MODIFIED=$(stat -c%Y $FILE)
if [ "$(($CURRENT_TIME-$FILE_LAST_MODIFIED))" -gt $BUNDLE_LIFE_PERIOD ]; then
echo "deleting file $FILE"
rm -f $FILE
fi
done
done

echo "Bundles clean up complete"
for DIR in prebid.js/prebid_*; do
BUNDLE_DIR="$DIR/build/dist"

echo "Checking $BUNDLE_DIR"

if [[ ! -d "$BUNDLE_DIR" ]]; then
echo "$BUNDLE_DIR does not exist, skipping"
continue
fi

for FILE in "$BUNDLE_DIR"/prebid.*.js; do
FILE_LAST_MODIFIED=$(stat -c%Y "$FILE")
FILE_AGE=$((CURRENT_TIME - FILE_LAST_MODIFIED))

if [[ "$FILE_AGE" -gt $CLEANUP_BUNDLES_OLDER_THAN_SECONDS ]]; then
echo "Deleting file $FILE"
rm -f "$FILE"
fi
done
done

echo "=====> Bundles clean up complete"
64 changes: 29 additions & 35 deletions checkout.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
#!/bin/bash

NUMBER_OF_PREVIOUS_VERSIONS="$1"

if [ $NUMBER_OF_PREVIOUS_VERSIONS -gt 0 ]; then
echo "checking out $NUMBER_OF_PREVIOUS_VERSIONS previous versions of prebid.js"
else
if [[ "$1" -gt 0 ]]; then
NUMBER_OF_PREVIOUS_VERSIONS="$1"
elif [[ ! "$NUMBER_OF_PREVIOUS_VERSIONS" -gt 0 ]]; then
NUMBER_OF_PREVIOUS_VERSIONS=2
echo "checking out $NUMBER_OF_PREVIOUS_VERSIONS previous versions of prebid.js"
fi

echo "=====> Checking out $NUMBER_OF_PREVIOUS_VERSIONS previous versions of prebid.js"

PREBID_DIR="prebid.js"
if [ ! -d "$PREBID_DIR" ]
then
mkdir $PREBID_DIR
cd $PREBID_DIR
else
cd $PREBID_DIR
if [[ ! -d "$PREBID_DIR" ]]; then
mkdir "$PREBID_DIR"
fi

if [ ! -d working_master ]
then
git clone https://github.com/prebid/Prebid.js.git working_master
cd working_master
else
cd working_master
cd "$PREBID_DIR"

if [[ ! -d working_master ]]; then
git clone https://github.com/prebid/Prebid.js.git working_master
fi

cd working_master

git pull

for TAG in `git tag --sort=-creatordate | head -n $NUMBER_OF_PREVIOUS_VERSIONS`
do
DIR_NAME="../prebid_${TAG}"
if [ -d "$DIR_NAME" ]
then
echo "$DIR_NAME already installed"
else
echo "Copying working_master to $DIR_NAME"
cp -R ../working_master $DIR_NAME
cd $DIR_NAME
git checkout ${TAG}
npm install
gulp build
echo "$DIR_NAME installed"
fi
done
echo "update complete"
for TAG in $(git tag --sort=-creatordate | head -n "$NUMBER_OF_PREVIOUS_VERSIONS"); do
DIR_NAME="../prebid_${TAG}"
if [[ -d "$DIR_NAME" ]]; then
echo "$DIR_NAME already installed"
else
echo "Copying working_master to $DIR_NAME"
cp -R ../working_master "$DIR_NAME"
cd "$DIR_NAME"
git checkout "${TAG}"
npm install
gulp build
echo "$DIR_NAME installed"
fi
done

echo "=====> Update complete"
11 changes: 4 additions & 7 deletions cloudformation/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Parameters:
to locate and reference resources created by that stack.
ECSAMI:
Description: AMI ID
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id
Type: AWS::EC2::Image::Id
# Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
# Default: /aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id
InstanceType:
Description: EC2 instance type
Type: String
Expand Down Expand Up @@ -322,11 +323,7 @@ Resources:
Memory: !Ref 'BuilderContainerMemory'
Image: !Ref 'BuilderImageUrl'
Environment:
- Name: PERIODIC_BUILD_VERSIONS_NUM
Value: 5
- Name: INITIAL_BUILD_VERSIONS_NUM
Value: 20
- Name: KEEP_LATEST_VERSIONS_NUM
- Name: NUMBER_OF_PREVIOUS_VERSIONS
Value: 20
MountPoints:
- SourceVolume: build-cache-volume
Expand Down
12 changes: 5 additions & 7 deletions docker/Dockerfile.builder
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ RUN npm install -g gulp gulp-cli

RUN apk add --no-cache bash git

ENV PERIODIC_BUILD_VERSIONS_NUM=5 \
INITIAL_BUILD_VERSIONS_NUM=20 \
KEEP_LATEST_VERSIONS_NUM=20 \
ENV NUMBER_OF_PREVIOUS_VERSIONS=20 \
CLEANUP_BUNDLES_OLDER_THAN_SECONDS=300

WORKDIR /app
VOLUME /app/prebid.js

RUN echo "0 */3 * * * cd /app && /app/checkout.sh $PERIODIC_BUILD_VERSIONS_NUM > /proc/1/fd/1 2>/proc/1/fd/2" \
RUN echo "0 */3 * * * . /app/cron.env; cd /app; /app/checkout.sh > /proc/1/fd/1 2>/proc/1/fd/2" \
> /etc/crontabs/root && \
echo "0 1 * * * cd /app && /app/buildCleanup.sh $KEEP_LATEST_VERSIONS_NUM > /proc/1/fd/1 2>/proc/1/fd/2" \
echo "0 1 * * * . /app/cron.env; cd /app; /app/buildCleanup.sh > /proc/1/fd/1 2>/proc/1/fd/2" \
>> /etc/crontabs/root && \
echo "0 2 * * * cd /app && /app/bundleCleanup.sh $CLEANUP_BUNDLES_OLDER_THAN_SECONDS > /proc/1/fd/1 2>/proc/1/fd/2" \
echo "0 2 * * * . /app/cron.env; cd /app; /app/bundleCleanup.sh > /proc/1/fd/1 2>/proc/1/fd/2" \
>> /etc/crontabs/root

COPY checkout.sh buildCleanup.sh bundleCleanup.sh removeVersions.sh ./

CMD [ "sh", "-c", "/app/checkout.sh $INITIAL_BUILD_VERSIONS_NUM && crond -f -l 8" ]
CMD [ "sh", "-c", "printenv > /app/cron.env && /app/checkout.sh && crond -f -l 8" ]
2 changes: 1 addition & 1 deletion docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
volumes:
- build-cache-volume:/app/prebid.js
environment:
INITIAL_BUILD_VERSIONS_NUM: 2
NUMBER_OF_PREVIOUS_VERSIONS: 2
init: true
volumes:
build-cache-volume:

0 comments on commit 5b80f76

Please sign in to comment.