#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.


import base64
import json
import logging
import os
import shutil
import sys
import urllib
import urllib2
try:
    import cPickle as pickle
except Exception:
    import pickle


class SpoolSentry(object):
    def __init__(self, spool_dir, sentry_url, key=None):
        self.spool_dir = spool_dir
        self.sentry_url = sentry_url
        self.key = key

    def process(self):
        for fname in os.listdir(self.spool_dir):
            if fname == "processed":
                continue
            try:
                sourcefile = "%s/%s" % (self.spool_dir, fname)
                with open(sourcefile) as f:
                    fdata = f.read()
                data_from_json = json.loads(fdata)
                data = self.build_data(data_from_json)
                self.send_data(data)
                destfile = "%s/processed/%s" % (self.spool_dir, fname)
                shutil.move(sourcefile, destfile)
            except Exception:
                logging.exception("Unable to upload record %s", fname)
                raise

    def build_data(self, filejson):
        env = {'SERVER_NAME': 'unknown', 'SERVER_PORT': '0000',
               'SCRIPT_NAME': '/unknown/', 'PATH_INFO': 'unknown'}
        if filejson['env']:
            env = json.loads(filejson['env'])
        url = "http://%s:%s%s%s" % (env['SERVER_NAME'], env['SERVER_PORT'],
                                    env['SCRIPT_NAME'], env['PATH_INFO'])
        rv = {'logger': filejson['logger'], 'level': logging.ERROR,
              'server_name': filejson['host'], 'url': url,
              'message': filejson['message'],
              'traceback': filejson['traceback']}
        rv['data'] = {}
        if filejson['env']:
            rv['data']['META'] = env
        if filejson['request_id']:
            rv['data']['request_id'] = filejson['request_id']
        return rv

    def send_data(self, data):
        data = {'data': base64.b64encode(pickle.dumps(data).encode('zlib')),
                'key': self.key}
        req = urllib2.Request(self.sentry_url)
        res = urllib2.urlopen(req, urllib.urlencode(data))
        if res.getcode() != 200:
            raise Exception("Bad HTTP code: %s" % res.getcode())
        txt = res.read()

if __name__ == '__main__':
    sentryurl = 'http://127.0.0.1/sentry/store/'
    key = ''
    spooldir = '/var/spool/nova'
    if len(sys.argv) > 1:
        sentryurl = sys.argv[1]
    if len(sys.argv) > 2:
        key = sys.argv[2]
    if len(sys.argv) > 3:
        spooldir = sys.argv[3]
    SpoolSentry(spooldir, sentryurl, key).process()
