EGI-FCTF/oneacct_export

View on GitHub
examples/usr/bin/oneacct-export-cron

Summary

Maintainability
Test Coverage
#!/bin/sh

##############################################################################
# Copyright (c) 2014 CESNET
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##############################################################################

conf_dir="/etc/oneacct-export"
omnibus_base_dir="/opt/oneacct-export"

if [ "x$1" = "x--all" ] || [ "x$1" = "x-a" ]; then
    # export all available accounting records
    options=""
    blocking_timeout="3600"
elif [ "x$1" = "x--two-weeks" ]; then
    # export only records from the last two weeks
    options="--records-for '2 weeks'"
    blocking_timeout="1800"
elif [ "x$1" = "x--month" ] || [ "x$1" = "x-m" ]; then
    # export only records from the last month
    options="--records-for month"
    blocking_timeout="2700"
elif [ "x$1" = "x--two-months" ]; then
    # export only records from the last two months
    options="--records-for '2 months'"
    blocking_timeout="2700"
elif [ "x$1" = "x--six-months" ]; then
    # export only records from the last six months
    options="--records-for '6 months'"
    blocking_timeout="3600"
elif [ "x$1" = "x--year" ] || [ "x$1" = "x-y" ]; then
    # export only records from the last year
    options="--records-for year"
    blocking_timeout="3600"
elif [ "x$1" = "x" ] || [ "x$1" = "x--week" ] || [ "x$1" = "x-w" ]; then
    # export only records from the last week (default)
    options="--records-for week"
    blocking_timeout="1800"
else
    echo "Unknown option $1."
    exit 1
fi

options="$options --blocking --timeout $blocking_timeout"

if [ -f "$conf_dir/compat.one" ]; then
    options="$options --compatibility-mode"
fi

if [ -f "$conf_dir/groups.include" ]; then
    groups_include="YES"
    options="$options --include-groups --group-file $conf_dir/groups.include"
fi

if [ -f "$conf_dir/groups.exclude" ]; then
    if [ "x$groups_include" = "xYES" ]; then
        echo "'groups.include' and 'groups.exclude' cannot be combined. Pick only one!"
        exit 1
    fi

    options="$options --exclude-groups --group-file $conf_dir/groups.exclude"
fi

if [ "x$DEBUG" = "x1" ]; then
    export ONEACCT_EXPORT_LOG_LEVEL="DEBUG"
    echo "$omnibus_base_dir/bin/oneacct-export $options"
    echo "(see log files in /var/log/oneacct-export for details)"
fi

export RAILS_ENV="production"

if [ "x$SSL_CERT_DIR" = "x" ]; then
    export SSL_CERT_DIR="/etc/grid-security/certificates"
fi

$omnibus_base_dir/bin/oneacct-export $options