#!/usr/bin/env python

# oio-blob-registrator.py
# Copyright (C) 2015-2018 OpenIO SAS, as part of OpenIO SDS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from oio.common.green import eventlet_monkey_patch
eventlet_monkey_patch()

import argparse
import sys

from oio.cli import make_logger_args_parser, get_logger_from_args
from oio.blob.registrator import BlobRegistrator


def make_arg_parser():
    log_parser = make_logger_args_parser()
    descr = """
Especially for my favorite customer!
The script runs the chunks on the given rawx volume and
it registers them in their container.
"""
    parser = argparse.ArgumentParser(description=descr, parents=[log_parser])

    parser = argparse.ArgumentParser(description=descr, parents=[log_parser])
    parser.add_argument('namespace', help="Namespace")
    parser.add_argument(
        "action", metavar='action', choices=('insert', 'update', 'check'),
        help="""
A value among 'insert', 'update' and 'check'.
'insert': Should the script insert the chunkks in the container,
without overriding the chunks in place.
'update': Should the script update the meta2 with the xattr.
'check': Only query the meta2 to check for the presence
of the chunks in the volume.
""")
    parser.add_argument('volume', help="The volume path to use")
    parser.add_argument('container_ids', metavar='<container_id>', nargs='*',
                        help="The container IDs to use")

    parser.add_argument(
        '--chunks-per-second', type=int,
        help='Max chunks per second. '
             '(default=%s)' % BlobRegistrator.DEFAULT_CHUNKS_PER_SECOND)
    parser.add_argument(
        '--report-interval', type=int,
        help='Report interval in seconds '
             '(default=%d)' % BlobRegistrator.DEFAULT_REPORT_INTERVAL)

    return parser


def main():
    args = make_arg_parser().parse_args()

    conf = {}
    conf['namespace'] = args.namespace
    conf['action'] = args.action

    logger = get_logger_from_args(args, default_conf=conf)

    if args.chunks_per_second is not None:
        conf['chunks_per_second'] = args.chunks_per_second
    if args.report_interval is not None:
        conf['report_interval'] = args.report_interval

    success = False
    try:
        registrator = BlobRegistrator(conf, logger, args.volume,
                                      args.container_ids)
        success = registrator.pass_volume()
    except Exception as e:
        logger.exception('ERROR in registrator: %s' % e)
    if not success:
        sys.exit(1)


if __name__ == '__main__':
    main()
