Skip to content

test deploying a cronjob & test access to env variables #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: stage
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions .ebextensions/cron-leaderonly.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
####
#### Pulled from https://github.com/awsdocs/elastic-beanstalk-samples/blob/main/configuration-files/aws-provided/instance-configuration/cron-leaderonly-linux.config
#### Per https://repost.aws/knowledge-center/cron-job-elastic-beanstalk
####

###################################################################################################
#### Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
####
#### Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
#### except in compliance with the License. A copy of the License is located at
####
#### http://aws.amazon.com/apache2.0/
####
#### or in the "license" file accompanying this file. This file is distributed on an "AS IS"
#### BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#### License for the specific language governing permissions and limitations under the License.
###################################################################################################

###################################################################################################
#### This configuration file allows a cron job to run only on one Linux instance in the environment.
####
#### The script "/usr/local/bin/test_cron.sh" will sort and compare the current instances in the
#### Auto Scaling group and if it matches the first instance in the sorted list it will exit 0.
#### This will mean that this script will only exit 0 for one of the instances in your environment.
####
#### The second script is an example of how you might use the "/usr/local/bin/test_cron.sh" script
#### to execute commands and log a timestamp to "/tmp/cron_example.log".
####
#### A cron example is setup at "/etc/cron.d/cron_example" to execute the script
#### "/usr/local/bin/cron_example.sh" every minute. A command is also run upon each deployment to
#### clear any previous versions of "/etc/cron.d/cron_example" by removing
#### "/etc/cron.d/cron_example.bak".
####
#### Note that for the first script to gather the required information, additional IAM permissions
#### will be needed to be added to a policy attached to the instance profile used by the instances
#### in the environment. The policy shown below will grant the access needed. Note that the default
#### instance profile for Elastic Beanstalk is "aws-elasticbeanstalk-ec2-role".
####
#### How to create a New Policy
#### - https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html
####
#### Adding Permissions to the Default Instance Profile
#### - https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/iam-instanceprofile.html#iam-instanceprofile-addperms
####
#### NB: Please note that as the "/usr/local/bin/test_cron.sh" script makes use of the EC2 health
#### status to determine "InService" instances in the Auto Scaling Group, the cron could fail to
#### run if the "leader" instance has failed and Auto Scaling has not yet changed the EC2 instance
#### state.
###################################################################################################
####
#### {
#### "Version": "2012-10-17",
#### "Statement": [
#### {
#### "Sid": "Stmt1409855610000",
#### "Effect": "Allow",
#### "Action": [ "autoscaling:DescribeAutoScalingGroups" ],
#### "Resource": [ "*" ]
#### },
#### {
#### "Sid": "Stmt1409855649000",
#### "Effect": "Allow",
#### "Action": [ "ec2:DescribeTags" ],
#### "Resource": [ "*" ]
#### }
#### ]
#### }
####
###################################################################################################
###################################################################################################

packages:
yum:
jq: []

files:
# "/usr/local/bin/test_cron.sh":
# mode: "000755"
# owner: root
# group: root
# content: |
# #!/bin/bash
# INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id 2>/dev/null`
# REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document 2>/dev/null | jq -r .region`

# # Find the Auto Scaling Group name from the Elastic Beanstalk environment
# ASG=`aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" \
# --region $REGION --output json | jq -r '.[][] | select(.Key=="aws:autoscaling:groupName") | .Value'`

# # Find the first instance in the Auto Scaling Group
# FIRST=`aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $ASG \
# --region $REGION --output json | \
# jq -r '.AutoScalingGroups[].Instances[] | select(.LifecycleState=="InService") | .InstanceId' | sort | head -1`

# # If the instance ids are the same exit 0
# [ "$FIRST" = "$INSTANCE_ID" ]

"/usr/local/bin/generate_sitemaps.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
# Test for Leader
# /usr/local/bin/test_cron.sh || exit
# Now run commands that should run on only 1 instance.
echo "Cron running at "`date` > /tmp/sitemaps.log
echo $UCLDC_FRONT > /tmp/sitemaps.log
echo "Testing access to environment variables inside cron job" > /tmp/sitemaps.log
# source /opt/elasticbeanstalk/deployment/env
# source /var/app/venv/*/bin/activate
# python manage.py calisphere_refresh_sitemaps --settings public_interface.settings > /tmp/sitemaps.log
# aws s3 cp /var/app/current/sitemaps/ s3://calisphere-sitemaps/ --recursive > /tmp/sitemaps.log

"/etc/cron.d/cron_sitemaps":
mode: "000644"
owner: root
group: root
content: |
* * * * * root /usr/local/bin/generate_sitemaps.sh

commands:
rm_old_cron:
command: "rm -fr /etc/cron.d/cron_sitemaps.bak"
ignoreErrors: true
10 changes: 6 additions & 4 deletions .platform/hooks/postdeploy/managecmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
source /var/app/venv/*/bin/activate
echo "starting collect statics";
python manage.py collectstatic --noinput
echo "finished collect statis; starting site maps";
python manage.py calisphere_refresh_sitemaps --settings public_interface.settings
echo "finished site maps";

echo "finished collect statics";
echo $UCLDC_FRONT;
echo "testing access to environment variables inside postdeploy hook";
# echo "finished collect statics, start downloading sitemaps";
# aws s3 cp s3://calisphere-sitemaps/ /var/app/current/sitemaps/ --recursive
# echo "finished downloading sitemaps";