Skip to content

Commit

Permalink
add logging to logentries; replace "s3cmd sync" with "aws s3 sync"
Browse files Browse the repository at this point in the history
  • Loading branch information
dalenewby committed Mar 28, 2018
1 parent a5ced51 commit fa137ed
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 15 deletions.
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
FROM alpine:3.4

RUN apk update \
&& apk add ca-certificates python py-pip \
&& pip install s3cmd
&& apk add ca-certificates python py-pip rsyslog rsyslog-tls ca-certificates openssl \
&& pip install awscli

COPY run.py /run.py
COPY crontab.txt /etc/crontabs/root
COPY crontab.txt /crontab.txt
COPY crontab-syslog.txt /crontab-syslog.txt
COPY rsyslog.conf /etc/rsyslog.conf
COPY entry.sh /entry.sh
COPY .s3cfg /root/.s3cfg

RUN chmod 755 /entry.sh

CMD ["/entry.sh"]
CMD ["/entry.sh"]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ This image is built automatically on the Docker Hub as [silintl/sync-with-s3](ht
4. ```SOURCE_PATH``` - Source files to be synced, example: ```/var/www/uploads```
5. ```DESTINATION_PATH``` - Destination of where to sync files to, example: ```s3://my-bucket/site-uploads```
6. ```BUCKET_LOCATION``` - AWS Region for bucket, ex: ```us-east-1```
7. ```LOGENTRIES_KEY``` - (optional) If provided, the image will send command output to syslog with priority ```user.info```.

## Volumes
You will need to define volumes in your Docker configuration for sharing filesystem between your application
ccontainers and this sync container.
containers and this sync container.
9 changes: 9 additions & 0 deletions crontab-syslog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The 's3 sync' command reports progress every few seconds with lines like:
# Completed 231.4 KiB/~255.4 KiB (3.8 MiB/s) with ~2 file(s) remaining (calculating...)^M
#
# We don't want to send these lines to logentries. The final line contains
# only ASCII space characters. The 'sed' commands strip the progress lines
# and the final blank line from the output leaving only one line for each
# file transfered.
#
CRON_SCHEDULE (aws --color=off --no-paginate s3 sync SOURCE_PATH DESTINATION_PATH 2>&1) | sed -e 's/Completed .*\r//' -e '/^ *$/d' | logger -p user.info
2 changes: 1 addition & 1 deletion crontab.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CRON_SCHEDULE s3cmd sync SOURCE_PATH DESTINATION_PATH
CRON_SCHEDULE aws --color=off --no-paginate s3 sync SOURCE_PATH DESTINATION_PATH
19 changes: 18 additions & 1 deletion entry.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
#!/bin/sh

# configure crontab based on the existance of the LOGENTRIES_KEY environment variable
if [ "$LOGENTRIES_KEY" ]; then
cp /crontab-syslog.txt /etc/crontabs/root
sed -i /etc/rsyslog.conf -e "s/LOGENTRIESKEY/${LOGENTRIES_KEY}/"
rsyslogd
sleep 10 # ensure rsyslogd is running before we may need to send logs to it
else
cp /crontab.txt /etc/crontabs/root
fi

# setup env
mkdir -p /root/.aws
echo "[default]" > /root/.aws/config

echo "[default]" > /root/.aws/credentials
echo "aws_access_key_id = ${ACCESS_KEY}" >> /root/.aws/credentials
echo "aws_secret_access_key = ${SECRET_KEY}" >> /root/.aws/credentials

python /run.py

# start cron
crond -f
crond -f
3 changes: 2 additions & 1 deletion local.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ SECRET_KEY=
CRON_SCHEDULE=
SOURCE_PATH=
DESTINATION_PATH=
BUCKET_LOCATION=
BUCKET_LOCATION=
LOGENTRIES_KEY=
16 changes: 16 additions & 0 deletions rsyslog.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# if you experience problems, check:
# http://www.rsyslog.com/troubleshoot

$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)

#
# Configure TLS (logentries-specific example: https://docs.logentries.com/docs/rsyslog/)
#
$DefaultNetstreamDriverCAFile /etc/ssl/certs/ca-certificates.crt
$ActionSendStreamDriver gtls
$ActionSendStreamDriverMode 1
$ActionSendStreamDriverAuthMode x509/name
$ActionSendStreamDriverPermittedPeer *.logentries.com

$template LogentriesFormat,"LOGENTRIESKEY %msg%\n"
*.emerg,*.alert,*.crit,*.err,*.warning,user.* @@data.logentries.com:443;LogentriesFormat
12 changes: 6 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def add_env_variables(file, env_dict):
out_file.write(add_env_variables(text, env_dict))


# read file for s3cmd config file
with open('/root/.s3cfg', 'r') as in_file:
text = in_file.read()
## read file for s3cmd config file
#with open('/root/.s3cfg', 'r') as in_file:
# text = in_file.read()

# write file for s3cmd config file
with open('/root/.s3cfg', 'w') as out_file:
out_file.write(add_env_variables(text, env_dict))
## write file for s3cmd config file
#with open('/root/.s3cfg', 'w') as out_file:
# out_file.write(add_env_variables(text, env_dict))

0 comments on commit fa137ed

Please sign in to comment.