Exemplo n.º 1
0
def initialize():
    global gsettings
    pyas2_settings = {}
    if hasattr(settings, 'PYAS2'):
        pyas2_settings = settings.PYAS2
    if not gsettings:
        gsettings['environment'] = pyas2_settings.get('ENVIRONMENT','production')
        gsettings['port'] = pyas2_settings.get('PORT', 8080)
        gsettings['ssl_certificate'] = pyas2_settings.get('SSLCERTIFICATE',None)
        gsettings['ssl_private_key'] = pyas2_settings.get('SSLPRIVATEKEY',None)
        gsettings['environment_text'] = pyas2_settings.get('ENVIRONMENTTEXT','Production')
        gsettings['environment_text_color'] = pyas2_settings.get('ENVIRONMENTTEXTCOLOR','Black')
        gsettings['root_dir'] = settings.BASE_DIR
        gsettings['python_path'] = pyas2_settings.get('PYTHONPATH', sys.executable)
        gsettings['managepy_path'] = as2utils.join(settings.BASE_DIR, 'manage.py')
        gsettings['daemon_port'] = pyas2_settings.get('DAEMONPORT', 16388)
        if pyas2_settings.get('DATADIR') and os.path.isdir(pyas2_settings.get('DATADIR')): 
            gsettings['root_dir'] = pyas2_settings.get('DATADIR')
        gsettings['payload_receive_store'] = as2utils.join(gsettings['root_dir'], 'messages', '__store', 'payload', 'received')
        gsettings['payload_send_store'] = as2utils.join(gsettings['root_dir'], 'messages', '__store', 'payload', 'sent')
        gsettings['mdn_receive_store'] = as2utils.join(gsettings['root_dir'], 'messages', '__store', 'mdn', 'received')
        gsettings['mdn_send_store'] = as2utils.join(gsettings['root_dir'], 'messages', '__store', 'mdn', 'sent')
        gsettings['log_dir'] = as2utils.join(gsettings['root_dir'], 'logging')
        for sett in ['payload_receive_store', 'payload_send_store', 'mdn_receive_store', 'mdn_send_store', 'log_dir']:
            as2utils.dirshouldbethere(gsettings[sett])
        gsettings['log_level'] = pyas2_settings.get('LOGLEVEL','INFO')
        gsettings['log_console'] = pyas2_settings.get('LOGCONSOLE',True)
        gsettings['log_console_level'] = pyas2_settings.get('LOGCONSOLELEVEL','STARTINFO')
        gsettings['max_retries'] = pyas2_settings.get('MAXRETRIES',30)
        gsettings['mdn_url'] = pyas2_settings.get('MDNURL','http://localhost:8080/pyas2/as2receive')
        gsettings['async_mdn_wait'] = pyas2_settings.get('ASYNCMDNWAIT',30)
        gsettings['max_arch_days'] = pyas2_settings.get('MAXARCHDAYS',30)
        gsettings['minDate'] = 0 - gsettings['max_arch_days']
Exemplo n.º 2
0
 def handle(self, *args, **options):
     if len(args) != 3:
         raise CommandError(
             _(u'Insufficient number of arguments specified, please check help for correct usage'
               ))
     try:
         org = models.Organization.objects.get(as2_name=args[0])
     except models.Organization.DoesNotExist:
         raise CommandError(_(u'Organization "%s" does not exist' %
                              args[0]))
     try:
         partner = models.Partner.objects.get(as2_name=args[1])
     except models.Partner.DoesNotExist:
         raise CommandError(_(u'Partner "%s" does not exist' % args[1]))
     if not os.path.isfile(args[2]):
         raise CommandError(
             _(u'Payload at location "%s" does not exist' % args[2]))
     if options['delete'] and not os.access(args[2], os.W_OK):
         raise CommandError('Insufficient file permission for payload %s' %
                            args[2])
     outdir = as2utils.join(pyas2init.gsettings['payload_send_store'],
                            time.strftime('%Y%m%d'))
     as2utils.dirshouldbethere(outdir)
     outfile = as2utils.join(outdir, os.path.basename(args[2]))
     shutil.copy2(args[2], outfile)
     if options['delete']:
         os.remove(args[2])
     payload = models.Payload.objects.create(
         name=os.path.basename(args[2]),
         file=outfile,
         content_type=partner.content_type)
     message = models.Message.objects.create(
         message_id=email.utils.make_msgid().strip('<>'),
         partner=partner,
         organization=org,
         direction='OUT',
         status='IP',
         payload=payload)
     try:
         payload = as2lib.build_message(message)
         as2lib.send_message(message, payload)
     except Exception, e:
         txt = as2utils.txtexc()
         reporttxt = _(u'Failed to send message, error:\n%(txt)s') % {
             'txt': txt
         }
         pyas2init.logger.error(reporttxt)
         message.status = 'E'
         models.Log.objects.create(
             message=message,
             status='E',
             text=_(u'Failed to send message, error is %s' % e))
         message.save()
         ### Send mail here
         as2utils.senderrorreport(
             message, _(u'Failed to send message, error is %s' % e))
         sys.exit(2)
Exemplo n.º 3
0
def update_dirs():
    partners = Partner.objects.all()
    orgs = Organization.objects.all()
    for partner in partners:
	for org in orgs:
	    as2utils.dirshouldbethere(as2utils.join(pyas2init.gsettings['root_dir'], 'messages', org.as2_name, 'inbox', partner.as2_name))
    for org in orgs:
	for partner in partners:
	    as2utils.dirshouldbethere(as2utils.join(pyas2init.gsettings['root_dir'], 'messages', partner.as2_name, 'outbox', org.as2_name))
Exemplo n.º 4
0
def check_pdirs(sender, instance, created, **kwargs):
    orgs = Organization.objects.all()
    for org in orgs:
        as2utils.dirshouldbethere(
            as2utils.join(pyas2init.gsettings['root_dir'], 'messages',
                          org.as2_name, 'inbox', instance.as2_name))
        as2utils.dirshouldbethere(
            as2utils.join(pyas2init.gsettings['root_dir'], 'messages',
                          instance.as2_name, 'outbox', org.as2_name))
Exemplo n.º 5
0
def check_odirs(sender, instance, created, **kwargs):
    partners = Partner.objects.all()
    for partner in partners:
        as2utils.dirshouldbethere(
            as2utils.join(pyas2init.gsettings['root_dir'], 'messages',
                          instance.as2_name, 'inbox', partner.as2_identifier))
        as2utils.dirshouldbethere(
            as2utils.join(pyas2init.gsettings['root_dir'], 'messages',
                          partner.as2_identifier, 'outbox', instance.as2_name))
Exemplo n.º 6
0
 def handle(self, *args, **options):
     if len(args) != 3:
         raise CommandError(
             _(u'Insufficient number of arguments specified, please check help for correct usage'
               ))
     try:
         org = models.Organization.objects.get(as2_name=args[0])
     except models.Organization.DoesNotExist:
         raise CommandError(
             _(u'Organization "%s" does not exist' % args[0]))
     try:
         partner = models.Partner.objects.get(as2_name=args[1])
     except models.Partner.DoesNotExist:
         raise CommandError(_(u'Partner "%s" does not exist' % args[1]))
     if not os.path.isfile(args[2]):
         raise CommandError(
             _(u'Payload at location "%s" does not exist' % args[2]))
     if options['delete'] and not os.access(args[2], os.W_OK):
         raise CommandError(
             'Insufficient file permission for payload %s' % args[2])
     outdir = as2utils.join(pyas2init.gsettings['payload_send_store'],
                            time.strftime('%Y%m%d'))
     as2utils.dirshouldbethere(outdir)
     outfile = as2utils.join(outdir, os.path.basename(args[2]))
     shutil.copy2(args[2], outfile)
     if options['delete']:
         os.remove(args[2])
     payload = models.Payload.objects.create(
         name=os.path.basename(args[2]),
         file=outfile,
         content_type=partner.content_type)
     message = models.Message.objects.create(
         message_id=email.utils.make_msgid().strip('<>'),
         partner=partner,
         organization=org,
         direction='OUT',
         status='IP',
         payload=payload)
     try:
         payload = as2lib.build_message(message)
         as2lib.send_message(message, payload)
     except Exception, e:
         txt = as2utils.txtexc()
         reporttxt = _(u'Failed to send message, error:\n%(txt)s') % {
             'txt': txt
         }
         pyas2init.logger.error(reporttxt)
         message.status = 'E'
         models.Log.objects.create(
             message=message,
             status='E',
             text=_(u'Failed to send message, error is %s' % e))
         message.save()
         ### Send mail here
         as2utils.senderrorreport(
             message, _(u'Failed to send message, error is %s' % e))
         sys.exit(2)
Exemplo n.º 7
0
def initialize():
    """ Function initializes the global variables for pyAS2 """

    global gsettings
    pyas2_settings = {}
    if hasattr(settings, 'PYAS2'):
        pyas2_settings = settings.PYAS2
    if not gsettings:
        gsettings['environment'] = pyas2_settings.get('ENVIRONMENT',
                                                      'production')
        gsettings['port'] = pyas2_settings.get('PORT', 8080)
        gsettings['ssl_certificate'] = pyas2_settings.get(
            'SSLCERTIFICATE', None)
        gsettings['ssl_private_key'] = pyas2_settings.get(
            'SSLPRIVATEKEY', None)
        gsettings['environment_text'] = pyas2_settings.get(
            'ENVIRONMENTTEXT', 'Production')
        gsettings['environment_text_color'] = pyas2_settings.get(
            'ENVIRONMENTTEXTCOLOR', 'Black')
        gsettings['root_dir'] = settings.BASE_DIR
        gsettings['python_path'] = pyas2_settings.get('PYTHONPATH',
                                                      sys.executable)
        gsettings['managepy_path'] = as2utils.join(settings.BASE_DIR,
                                                   'manage.py')
        gsettings['daemon_port'] = pyas2_settings.get('DAEMONPORT', 16388)
        if pyas2_settings.get('DATADIR') and os.path.isdir(
                pyas2_settings.get('DATADIR')):
            gsettings['root_dir'] = pyas2_settings.get('DATADIR')
        gsettings['payload_receive_store'] = as2utils.join(
            gsettings['root_dir'], 'messages', '__store', 'payload',
            'received')
        gsettings['payload_send_store'] = as2utils.join(
            gsettings['root_dir'], 'messages', '__store', 'payload', 'sent')
        gsettings['mdn_receive_store'] = as2utils.join(gsettings['root_dir'],
                                                       'messages', '__store',
                                                       'mdn', 'received')
        gsettings['mdn_send_store'] = as2utils.join(gsettings['root_dir'],
                                                    'messages', '__store',
                                                    'mdn', 'sent')
        gsettings['log_dir'] = as2utils.join(gsettings['root_dir'], 'logging')
        for sett in [
                'payload_receive_store', 'payload_send_store',
                'mdn_receive_store', 'mdn_send_store', 'log_dir'
        ]:
            as2utils.dirshouldbethere(gsettings[sett])
        gsettings['log_level'] = pyas2_settings.get('LOGLEVEL', 'INFO')
        gsettings['log_console'] = pyas2_settings.get('LOGCONSOLE', True)
        gsettings['log_console_level'] = pyas2_settings.get(
            'LOGCONSOLELEVEL', 'STARTINFO')
        gsettings['max_retries'] = pyas2_settings.get('MAXRETRIES', 30)
        gsettings['mdn_url'] = pyas2_settings.get(
            'MDNURL', 'http://localhost:8080/pyas2/as2receive')
        gsettings['async_mdn_wait'] = pyas2_settings.get('ASYNCMDNWAIT', 30)
        gsettings['max_arch_days'] = pyas2_settings.get('MAXARCHDAYS', 30)
        gsettings['minDate'] = 0 - gsettings['max_arch_days']
Exemplo n.º 8
0
def update_dirs():
    partners = Partner.objects.all()
    orgs = Organization.objects.all()
    for partner in partners:
        for org in orgs:
            as2utils.dirshouldbethere(
                as2utils.join(pyas2init.gsettings['root_dir'], 'messages',
                              org.as2_name, 'inbox', partner.as2_identifier))
    for org in orgs:
        for partner in partners:
            as2utils.dirshouldbethere(
                as2utils.join(pyas2init.gsettings['root_dir'], 'messages',
                              partner.as2_identifier, 'outbox', org.as2_name))
Exemplo n.º 9
0
def check_pdirs(sender, instance, created, **kwargs):
    orgs = Organization.objects.all()
    for org in orgs:
        as2utils.dirshouldbethere(as2utils.join(pyas2init.gsettings['root_dir'], 'messages', org.as2_name, 'inbox', instance.as2_name))
        as2utils.dirshouldbethere(as2utils.join(pyas2init.gsettings['root_dir'], 'messages', instance.as2_name, 'outbox', org.as2_name))
Exemplo n.º 10
0
def check_odirs(sender, instance, created, **kwargs):
    partners = Partner.objects.all()
    for partner in partners:
        as2utils.dirshouldbethere(as2utils.join(pyas2init.gsettings['root_dir'], 'messages', instance.as2_name, 'inbox', partner.as2_name))
        as2utils.dirshouldbethere(as2utils.join(pyas2init.gsettings['root_dir'], 'messages', partner.as2_name, 'outbox', instance.as2_name))
Exemplo n.º 11
0
    def handle(self, *args, **options):
        # Check if organization and partner exists
        try:
            org = models.Organization.objects.get(
                as2_name=options['organization_as2name'])
        except models.Organization.DoesNotExist:
            raise CommandError(
                _(u'Organization "%s" does not exist' %
                  options['organization_as2name']))
        try:
            partner = models.Partner.objects.get(
                as2_name=options['partner_as2name'])
        except models.Partner.DoesNotExist:
            raise CommandError(
                _(u'Partner "%s" does not exist' % options['partner_as2name']))

        # Check if file exists and we have the right permissions
        if not os.path.isfile(options['path_to_payload']):
            raise CommandError(
                _(u'Payload at location "%s" does not exist' %
                  options['path_to_payload']))
        if options['delete'] and not os.access(options['path_to_payload'],
                                               os.W_OK):
            raise CommandError('Insufficient file permission for payload %s' %
                               options['path_to_payload'])

        # Copy the file to the store
        output_dir = as2utils.join(pyas2init.gsettings['payload_send_store'],
                                   time.strftime('%Y%m%d'))
        as2utils.dirshouldbethere(output_dir)
        outfile = as2utils.join(output_dir,
                                os.path.basename(options['path_to_payload']))
        shutil.copy2(options['path_to_payload'], outfile)

        # Delete original file if option is set
        if options['delete']:
            os.remove(options['path_to_payload'])

        # Create the payload and message objects
        payload = models.Payload.objects.create(
            name=os.path.basename(options['path_to_payload']),
            file=outfile,
            content_type=partner.content_type)
        message = models.Message.objects.create(
            message_id=email.utils.make_msgid().strip('<>'),
            partner=partner,
            organization=org,
            direction='OUT',
            status='IP',
            payload=payload)

        # Build and send the AS2 message
        try:
            payload = as2lib.build_message(message)
            as2lib.send_message(message, payload)
        except Exception:
            message.status = 'E'
            txt = traceback.format_exc(None).decode('utf-8', 'ignore')
            message.adv_status = \
                _(u'Failed to send message, error:\n%(txt)s') % {'txt': txt}
            pyas2init.logger.error(message.adv_status)

            models.Log.objects.create(message=message,
                                      status='E',
                                      text=message.adv_status)
            message.save()

            # Send mail here
            as2utils.senderrorreport(message, message.adv_status)
            sys.exit(2)

        sys.exit(0)