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

Commit

Permalink
Merge pull request #11 from sinistro14/dev
Browse files Browse the repository at this point in the history
Add machine management scripts
  • Loading branch information
sinistro14 authored Sep 13, 2018
2 parents 7e858cf + 420adce commit 9aa5cee
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 0 deletions.
199 changes: 199 additions & 0 deletions src/edenbox/loganalyser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
# Created by https://www.gitignore.io/api/python,pycharm

### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

### PyCharm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
.idea/sonarlint

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

### Python Patch ###
.venv/

### Python.VirtualEnv Stack ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json


# End of https://www.gitignore.io/api/python,pycharm
63 changes: 63 additions & 0 deletions tools/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash

# Contants

readonly LINE="================================================================"

# Script

LOGFILE="<log_path>" # location of the log file to which the output will be sent
(
RETRYIN="1h" # time after which the operation will be retried, in case of error

DAYLIMIT=15 # defines limit value for first half of the month snapshot
MONTHLIMIT=3 # defines number of months that are stored in snapshots

DAY=$(date +'%d') # actual day
MONTH=$(date +'%m') # actual month

V0=($((10#$MONTH % 10#$MONTHLIMIT))) # mod based value, 1-January, 2-February, March-0, and onwards...

if [ $DAY -lt $DAYLIMIT ]; then V1=0; # 0 for the first half of the month
else V1=1; # 1 for the second half of the month
fi

if [ $# -eq 0 ]; then # if there are no args, use default scheduled version
VERSION=$V0"-"$V1; # snapshot version number (0..2)-(0..1)
else VERSION=$1; # if an arg was provided, use it as version identifier
fi

DATE=$(date +'%d-%m-%Y') # current date
AUTH="<auth_URL>" # REST API authenticaton URL
TOKEN="<token>" # authentication token [URGENT] needs to be updated monthly
IMAGE="<machine>-Backup-"$VERSION # name of the snapshot to be created
DESCR="<machine_descr> - Backup - "$DATE # description of the snapshot to be created
STARTTIME=$(date +'%X %d-%m-%Y') # time at which the snapshot process started

echo $LINE
echo "Saving image - "$IMAGE
echo "Starting upload - "$STARTTIME

until snf-mkimage / -u $IMAGE.diskdump -r "$DESCR" \
-a $AUTH -t $TOKEN \
--disable-sysprep cleanup-userdata \
--disable-sysprep cleanup-passwords \
--force

# --disable-sysprep cleanup-passwords will avoid accounts to be locked and passwords deleted
# --disable-sysprep cleanup-userdata will avoid user data to be deleted
# force will overwrite any snapshot with the same name

do
echo "Error uploading snapshot"
echo "Will retry operation in "$RETRYIN
sleep $RETRYIN
echo $LINE
echo "Restarting - "$(date +'%X %d-%m-%Y')
done

ENDTIME=$(date +'%X %d-%m-%Y') # time at which the snapshot process ended
echo "Upload finished - "$ENDTIME
echo $LINE
echo ""
) |& tee -a "$LOGFILE" # output is both sent to stdout and log file
10 changes: 10 additions & 0 deletions tools/logrotate/backup
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/var/log/backup/*.log {
daily
missingok
rotate 4
compress
notifempty
copytruncate
dateext
dateformat %d-%m-%Y
}
17 changes: 17 additions & 0 deletions tools/logrotate/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Logrotate
Configuration files of the logrotate utility.

## Description
Brief outline of the how's and why's of the employed configuration files.

### Backup
Attempt to rotate the log each day, but only if it is not empty.
Method used in order to avoid finer grained treatment based on date analysis.

### Update
Keeps the last 4 weeks of update logs, compressed.
The log file is rotated weekly.

### Final Notes
Logrotate allows automatical cleanup of old logs, therefore, avoiding the need
of manual management of all of the generated files.
10 changes: 10 additions & 0 deletions tools/logrotate/update
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/var/log/update/*.log {
weekly
missingok
rotate 4
compress
notifempty
copytruncate
dateext
dateformat -%m-%Y
}
85 changes: 85 additions & 0 deletions tools/manageuser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bash

# CONSTANTS
readonly PREFIX="eden_"

# Show Usage and exit
usage ()
{
echo "SYNOPSIS"
echo "./"${0##*/}" -{OPTIONS} <username>"
echo ""
echo "OPTIONS - only one can be used, only the leftmost argument will be used"
echo "-c Creates user with <username>, creates and sets up ssh access scheme"
echo "-u Creates user with <username> and sets up ssh access with key ./<username>.pub"
exit 1
}

# Create a user, using KEYFILE as ssh key
createUser()
{
local USER=$1
local KEYFILE=$2
local USERHOME="/home/"$USER
local USERSSH=$USERHOME"/.ssh"
adduser $USER --gecos ""
chown $USER $KEYFILE
mkdir $USERSSH
chmod 700 $USERSSH
cat $KEYFILE >> $USERSSH/authorizedkeys
chmod 600 $USERSSH"/authorizedkeys"
chown $USER:$USER $USERSSH"/authorizedkeys"
chown $USER:$USER $USERSSH
}

# Create 2048 RSA-key pair
createKey()
{
local PREFIX=$PREFIX
local USER=$1
local KEYNAME=$PREFIX$USER
ssh-keygen -t rsa -b 2048 -P "" -C "" -f $KEYNAME
mv $KEYNAME $KEYNAME.priv
chmod 400 $KEYNAME.priv
chmod 444 $KEYNAME.pub
echo "Choose a password to encrypt the resulting zip file"
zip -o $KEYNAME --encrypt $KEYNAME.pub $KEYNAME.priv
}

# Delete a user
delete()
{
local USER=$1
deluser --remove-home $USER
}

#MAIN

# Exit if no args provided
[ -z "$1" ] && usage;

while getopts c:u:d:h opt;
do
case "${opt}"
in
c)
USER=$OPTARG
KEYFILE=$PREFIX$USER.pub
createKey $USER
createUser $USER $KEYFILE
break;;
u)
USER=$OPTARG
KEYFILE=$PREFIX$USER.pub
createUser $USER $KEYFILE
break;;
d)
USER=$OPTARG
delete $USER
break;;
h | ? | *) usage;;
esac
done
shift $((OPTIND -1))

exit 0
Loading

0 comments on commit 9aa5cee

Please sign in to comment.