Skip to content
This repository has been archived by the owner on Mar 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #90 from Modicrumb/dev
Browse files Browse the repository at this point in the history
Logging added to bash files and logs appended to /var/log/cakephp/cak…
  • Loading branch information
bravo-kernel committed Mar 3, 2018
2 parents c8c3163 + e6f674a commit 7736acf
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .cakebox/bash/backup-installer.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env bash

source /cakebox/bash/logger.sh

SCRIPTENTRY

EXECUTABLE="/usr/local/bin/multibackup"
CONFIG_LOCAL="/cakebox/templates/.multibackup.conf"
CONFIG_BOX="/home/vagrant/.multibackup.conf"
Expand All @@ -8,17 +12,20 @@ CRON_FILE="/etc/cron.d/backup-liveconfig"
printf %63s |tr " " "-"
printf '\n'
printf "Checking automated backups\n"
INFO "Checking automated backups"
printf %63s |tr " " "-"
printf '\n'

# Do nothing if the multibackup executable already exists
if [ -f "$EXECUTABLE" ]; then
echo "* Skipping: multibackup executable already exists"
INFO "* Skipping: multibackup executable already exists"
exit 0
fi

# Install tar-multibackup as described at https://github.com/frdmn/tar-multibackup
echo "* Installing multibackup"
INFO "* Installing multibackup"
cd /usr/local/src
git clone https://github.com/frdmn/tar-multibackup.git
ln -sf /usr/local/src/tar-multibackup/multibackup /usr/local/bin/multibackup
Expand All @@ -29,27 +36,38 @@ ln -sf /usr/local/src/tar-multibackup/multibackup /usr/local/bin/multibackup
# - last known uploaded files
# - all databases (percona/mysql hot backup)
echo "* Placing default configuration file"
INFO "* Placing default configuration file"
OUTPUT=$(cp "$CONFIG_LOCAL" "$CONFIG_BOX")
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: non-zero cp exit code ($EXITCODE)"
ERROR "FATAL: non-zero cp exit code ($EXITCODE)"
exit 1
fi

# Set file permissions on config file to vagrant user
echo "* Setting configuration file permissions"
INFO "* Setting configuration file permissions"
OUTPUT=$(sudo chown vagrant:vagrant "$CONFIG_BOX" -R 2>&1)
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: non-zero chown exit code ($EXITCODE)"
ERROR "FATAL: non-zero chown exit code ($EXITCODE)"
exit 1
fi

# Create daily cron (runs at 5:00 AM)
echo "* Creating daily cron job"
INFO "* Creating daily cron job"
echo '0 5 * * * root CONFIG=/home/vagrant/.multibackup.conf /usr/local/bin/multibackup &>/dev/null' > "$CRON_FILE"

# Provisioning feedback
echo "Installation completed successfully!"

INFO "Installation completed successfully!"

SCRIPTEXIT
13 changes: 13 additions & 0 deletions .cakebox/bash/check-ssh-agent.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#!/usr/bin/env bash

source /cakebox/bash/logger.sh

SCRIPTENTRY

printf %63s |tr " " "-"
printf '\n'
printf "Sanity checking SSH Agent Forwarding\n"
INFO "Sanity checking SSH Agent Forwarding"
printf %63s |tr " " "-"
printf '\n'

# Show user
USER=$(whoami 2>&1)
echo "Running checks as user $USER"

INFO "Running checks as user $USER"

# Show status of SSH Agent
echo "SSH Agent details:"
OUTPUT=$(ssh-agent 2>&1)
Expand All @@ -19,8 +26,14 @@ do
echo "=> $line"
done

INFO "SSH Agent details: ($OUTPUT)"

# Show loaded keys
echo "SSH Forwarded keys:"
OUTPUT=$(ssh-add -l 2>&1)
EXITCODE=$?
echo "=> $OUTPUT"

INFO "SSH Forwarded keys: ($OUTPUT)"

SCRIPTEXIT
20 changes: 20 additions & 0 deletions .cakebox/bash/console-installer.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env bash

source /cakebox/bash/logger.sh

SCRIPTENTRY

# Convenience variables
KITCHEN_FILE="/cakebox/console/webroot/index.htm"
REPOSITORY="alt3/cakebox-console"
Expand All @@ -10,6 +14,7 @@ DIR_NAME="console"
# Remove /webroot/index.html used by test-kitchen if needed
if [ -f "$KITCHEN_FILE" ]; then
echo "* Preparing installation directory"
INFO "* Preparing installation directory"
rm -rfv "$TARGET_DIR"/*
fi

Expand All @@ -24,6 +29,7 @@ fi
printf %63s |tr " " "-"
printf '\n'
printf "Please wait... installing Cakebox Commands and Dashboard"
INFO "Please wait... installing Cakebox Commands and Dashboard"
printf %63s |tr " " "-"
printf '\n'

Expand All @@ -34,39 +40,50 @@ OUTPUT=$(sudo composer self-update 2>&1)
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: non-zero composer self-update exit code ($EXITCODE)"
ERROR "FATAL: non-zero composer self-update exit code ($EXITCODE)"
exit 1
fi

# Update composer cache permissions
echo "* Updating Composer cache permissions"
INFO "* Updating Composer cache permissions"
OUTPUT=$(sudo chown vagrant:vagrant /home/vagrant/.composer -R 2>&1)
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: non-zero chown exit code ($EXITCODE)"
ERROR "FATAL: non-zero chown exit code ($EXITCODE)"
exit 1
fi

# Create the project
echo "* Creating project"
INFO "* Creating project"
cd /cakebox
OUTPUT=$(composer create-project -sdev --no-install --keep-vcs --no-interaction "$REPOSITORY":"$VERSION" "$DIR_NAME" 2>&1)
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: non-zero composer create-project exit code ($EXITCODE)"
ERROR "FATAL: non-zero composer create-project exit code ($EXITCODE)"
exit 1
fi
cd "$DIR_NAME"

# Round up by Composer installing
echo "* Composer installing"
INFO "* Composer installing"
OUTPUT=$(composer install --prefer-dist --no-dev 2>&1)
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: non-zero composer install exit code ($EXITCODE)"
ERROR "FATAL: non-zero composer install exit code ($EXITCODE)"
exit 1
fi

Expand All @@ -75,3 +92,6 @@ chmod +x /cakebox/console/bin/cake

# Provisioning feedback
echo "Installation completed successfully!"
INFO "Installation completed successfully!"

SCRIPTEXIT
75 changes: 75 additions & 0 deletions .cakebox/bash/logger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash

SCRIPT_LOG=/var/log/cakephp/cakebox.cli.log

## Given a timestamp, message, and tag generate a LogStash Log
function generate_log() {
declare -A logArray
logArray[timestamp]="\"$1\""
logArray[source]='"cakebox"'
logArray[fields]="{\"channel\":\"cli.cakebox\",\"level\":100,\"ctxt_scope\":[]}"
logArray[message]="\"$2\""
logArray[tags]="[\"cli.cakebox\", \"$3\"]"
logArray[type]='"cakephp"'

printf '{'
for lkey in "${!logArray[@]}"
do
printf '"@%s":%s,' "$lkey" "${logArray[$lkey]}"
done
echo '}'

}


function SCRIPTENTRY(){
timestamp=$( date -u +"%Y-%m-%dT%H:%M:%S.%3N%:z" )
script_name=`basename "$0"`
script_name="${script_name%.*}"
message="started $script_name"
generate_log "$timestamp" "$message" 'debug' >> $SCRIPT_LOG
}

function SCRIPTEXIT(){
script_name=`basename "$0"`
script_name="${script_name%.*}"
message="exited $script_name"
generate_log "$timestamp" "$message" 'debug' >> $SCRIPT_LOG
}

function ENTRY(){
local cfn="${FUNCNAME[1]}"
timestamp=$( date -u +"%Y-%m-%dT%H:%M:%S.%3N%:z" )
message="$cfn $FUNCNAME"
generate_log "$timestamp" "$message" 'debug' >> $SCRIPT_LOG
}

function EXIT(){
local cfn="${FUNCNAME[1]}"
timestamp=$( date -u +"%Y-%m-%dT%H:%M:%S.%3N%:z" )
message="$cfn $FUNCNAME"
generate_log "$timestamp" "$message" 'debug' >> $SCRIPT_LOG
}


function INFO(){
local function_name="${FUNCNAME[1]}"
local message="$1"
timestamp=$( date -u +"%Y-%m-%dT%H:%M:%S.%3N%:z" )
generate_log "$timestamp" "$message" 'info' >> $SCRIPT_LOG
}


function DEBUG(){
local function_name="${FUNCNAME[1]}"
local message="$1"
timestamp=$( date -u +"%Y-%m-%dT%H:%M:%S.%3N%:z" )
generate_log "$timestamp" "$message" 'debug' >> $SCRIPT_LOG
}

function ERROR(){
local function_name="${FUNCNAME[1]}"
local message="$1"
timestamp=$( date -u +"%Y-%m-%dT%H:%M:%S.%3N%:z" )
generate_log "$timestamp" "$message" 'error' >> $SCRIPT_LOG
}
13 changes: 13 additions & 0 deletions .cakebox/bash/motd-updater.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env bash

source /cakebox/bash/logger.sh

SCRIPTENTRY

MOTD_TEMPLATE="/cakebox/templates/motd-20-cakebox-banner"
MOTD_TARGET="/etc/update-motd.d/20-cakebox-banner"

Expand All @@ -9,9 +13,12 @@ printf "Updating motd\n"
printf %63s |tr " " "-"
printf '\n'

INFO "Updating motd"

# Do nothing if template and target are identical
if cmp -s "$MOTD_TEMPLATE" "$MOTD_TARGET" ; then
echo "* Skipping: motd is already up-to-date"
INFO "* Skipping: motd is already up-to-date"
exit 0
fi

Expand All @@ -21,13 +28,19 @@ OUTPUT=$(cp "$MOTD_TEMPLATE" "$MOTD_TARGET")
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: non-zero cp exit code ($EXITCODE)"
ERROR "FATAL: non-zero cp exit code ($EXITCODE)"
exit 1
fi

# Update motd
echo "* Effectuating new motd"
INFO "* Effectuating new motd"
run-parts /etc/update-motd.d/

# Provisioning feedback
echo "MOTD update completed successfully!"
INFO "MOTD update completed successfully!"

SCRIPTEXIT
16 changes: 16 additions & 0 deletions .cakebox/bash/ssh-authentication.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

source /cakebox/bash/logger.sh

# --------------------------------------------------------------------
# Creates a new authorized_keys file for the vagrant user with (only)
# the yaml-specified public key. Thus effectively:
Expand All @@ -17,6 +19,7 @@
# SSH timeouts running vagrant reload --provision against a running vm )
# --------------------------------------------------------------------

SCRIPTENTRY

# Convenience variables
PUBLIC_KEY=$1
Expand All @@ -32,38 +35,51 @@ printf "Restricting Cakebox SSH logins\n"
printf %63s |tr " " "-"
printf '\n'

INFO "Restricting Cakebox SSH logins"

# Do nothing if yaml-specified public key is already the only key in authorized_keys
if diff "$AUTHORIZED_KEYS" "$SSH_DIR/$PUBLIC_KEY" >/dev/null ; then
echo "* Skipping: SSH logins already require yaml-specified private key ($PRIVATE_KEY)"
INFO "Skipping: SSH Logins already require yaml-specified private key ($PRIVATE_KEY)"
exit 0
fi

# Still here, verify the public key is valid before applying (to prevent locking out user)
echo "* Validating yaml-specified public key ($PUBLIC_KEY)"
INFO "* Validating yaml-specified public key ($PUBLIC_KEY)"
OUTPUT=$(ssh-keygen -l -f "$SSH_DIR/$PUBLIC_KEY" 2>&1)
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
echo "FATAL: key did not pass validation, make sure it is in OpenSSH format"
ERROR "FATAL: key did not pass validation, make sure it is in OpenSSH format"
exit 1
fi

# Make yaml-specified public key the only key in authorized_keys
echo "* Replacing current public keys in $AUTHORIZED_KEYS"
INFO "* Replacing current public keys in $AUTHORIZED_KEYS"
cat "$SSH_DIR/$PUBLIC_KEY" > "$AUTHORIZED_KEYS"

# Remove Vagrant 1.7.x secure private key on host using Synced Folder
if [ -f "$VAGRANT_17X_KEY" ]; then
echo "* Removing Vagrant 1.7.x generated private key"
INFO "* Removing Vagrant 1.7.x generated private key"
OUTPUT=$(rm "$VAGRANT_17X_KEY" 2>&1)
EXITCODE=$?
if [ "$EXITCODE" -ne 0 ]; then
echo $OUTPUT
DEBUG $OUTPUT
echo "FATAL: error removing key"
ERROR "FATAL: error removing key"
exit 1
fi
fi

# All done
echo "* SSH logins now require yaml-specified private key ($PRIVATE_KEY)"
echo "Command completed successfully"

INFO "* SSH logins now require yaml-specified private key ($PRIVATE_KEY)"

SCRIPTEXIT

0 comments on commit 7736acf

Please sign in to comment.