~patrick-hetu/charms/precise/python-django/trunk

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Patrick Hetu
  • Date: 2013-05-29 18:52:19 UTC
  • Revision ID: patrick.hetu@gmail.com-20130529185219-ja50gwwnhau9neh7
sync with the latest gunicorn and memcached charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
519
519
       for req_file in requirements_pip_files.split(','):
520
520
            pip_install_req(os.path.join(working_dir,req_file))
521
521
 
 
522
    wsgi_py_path = os.path.join(working_dir, 'wsgi.py')
 
523
    if not os.path.exists(wsgi_py_path):
 
524
        process_template('wsgi.py.tmpl', {'project_name': sanitized_unit_name, \
 
525
                                          'django_settings': django_settings}, \
 
526
                                          wsgi_py_path)
 
527
 
 
528
 
522
529
def config_changed(config_data):
523
 
    os.environ['DJANGO_SETTINGS_MODULE'] = '.'.join([sanitized_unit_name, 'settings'])
 
530
    os.environ['DJANGO_SETTINGS_MODULE'] = django_settings_modules
524
531
    django_admin_cmd = find_django_admin_cmd()
525
532
 
526
533
    site_secret_key = config_data['site_secret_key']
566
573
       for req_file in requirements_pip_files.split(','):
567
574
            pip_install_req(os.path.join(working_dir,req_file), upgrade=True)
568
575
 
 
576
 
569
577
    # Trigger WSGI reloading
570
578
    for relid in relation_ids('wsgi'):
571
579
       relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
582
590
                  'urls_dir_path': urls_dir_path,
583
591
                  'install_root': install_root,
584
592
                  'django_admin_cmd': django_admin_cmd,
 
593
                  'wsgi_user': wsgi_user,
 
594
                  'wsgi_group': wsgi_group,
585
595
                 })
586
596
 
 
597
    run('chown -R %s:%s %s' % (wsgi_user,wsgi_group, working_dir))
 
598
 
587
599
    # Trigger WSGI reloading
588
600
    for relid in relation_ids('wsgi'):
589
601
        relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
612
624
 
613
625
    process_template('engine.tmpl', templ_vars, settings_database_path % {'engine_name': 'pgsql'})
614
626
 
615
 
    run("%s syncdb --noinput --pythonpath=%s --settings=%s.settings || true" % (django_admin_cmd, install_root, sanitized_unit_name))
 
627
    run("%s syncdb --noinput --pythonpath=%s --settings=%s || true" % \
 
628
            (django_admin_cmd, install_root, django_settings_modules))
 
629
 
 
630
 
 
631
    run('chown -R %s:%s %s' % (wsgi_user,wsgi_group, working_dir))
 
632
 
 
633
    # Trigger WSGI reloading
 
634
    for relid in relation_ids('wsgi'):
 
635
        relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
616
636
 
617
637
def pgsql_relation_broken():
618
 
    pass
 
638
    run('rm %s' % settings_database_path % {'engine_name': 'pgsql'})
 
639
 
 
640
    # Trigger WSGI reloading
 
641
    for relid in relation_ids('wsgi'):
 
642
        relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
619
643
 
620
644
def mongodb_relation_joined_changed():
621
645
    packages = ["python-mongoengine"]
632
656
 
633
657
    process_template('mongodb_engine.tmpl', templ_vars, settings_database_path % {'engine_name': 'mongodb'})
634
658
 
 
659
    run('chown -R %s:%s %s' % (wsgi_user,wsgi_group, working_dir))
 
660
 
 
661
    # Trigger WSGI reloading
 
662
    for relid in relation_ids('wsgi'):
 
663
        relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
 
664
 
635
665
def mongodb_relation_broken():
636
 
    pass
 
666
    run('rm %s' % settings_database_path % {'engine_name': 'mongodb'})
 
667
 
 
668
    # Trigger WSGI reloading
 
669
    for relid in relation_ids('wsgi'):
 
670
        relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
637
671
 
638
672
def wsgi_relation_joined_changed():
639
673
    relation_set({'working_dir':working_dir})
640
674
 
641
675
    for var in config_data:
642
 
        if var.startswith('wsgi_') or var in ['env_extra', 'django_settings', 'port']:
 
676
        if var.startswith('wsgi_') or var in ['listen_ip', 'port']:
643
677
            relation_set({var: config_data[var]})
644
678
    
645
679
    if not config_data['python_path']:
648
682
def wsgi_relation_broken():
649
683
    pass
650
684
 
 
685
def cache_relation_joined_changed():
 
686
    os.environ['DJANGO_SETTINGS_MODULE'] = django_settings_modules
 
687
 
 
688
    packages = ["python-memcache"]
 
689
    apt_get_install(packages)
 
690
 
 
691
    host = relation_get("host")
 
692
    if not host:
 
693
        return
 
694
 
 
695
    templ_vars = {
 
696
       'cache_engine': 'django.core.cache.backends.memcached.MemcachedCache',
 
697
       'cache_host': relation_get("host"),
 
698
       'cache_port': relation_get("port"),
 
699
    }
 
700
 
 
701
    process_template('cache.tmpl', templ_vars, settings_database_path % {'engine_name': 'memcache'})
 
702
 
 
703
    run('chown -R %s:%s %s' % (wsgi_user,wsgi_group, working_dir))
 
704
 
 
705
 
 
706
    # Trigger WSGI reloading
 
707
    for relid in relation_ids('wsgi'):
 
708
        relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
 
709
 
 
710
def cache_relation_broken():
 
711
    run('rm %s' % settings_database_path % {'engine_name': 'memcache'})
 
712
 
 
713
    # Trigger WSGI reloading
 
714
    for relid in relation_ids('wsgi'):
 
715
        relation_set({'wsgi_timestamp': time.time()}, relation_id=relid)
 
716
 
651
717
def website_relation_joined_changed():
652
718
    gunicorn_file = "/etc/gunicorn.d/%s.conf" % sanitized_unit_name
653
719
    if os.path.exists(gunicorn_file):
681
747
wsgi_group = config_data['wsgi_group']
682
748
install_root = config_data['install_root']
683
749
application_path = config_data['application_path']
 
750
django_settings = config_data['django_settings']
684
751
 
685
752
unit_name = os.environ['JUJU_UNIT_NAME'].split('/')[0]
686
753
sanitized_unit_name = sanitize(unit_name)
690
757
else:
691
758
    working_dir = vcs_clone_dir
692
759
 
 
760
django_settings_modules = '.'.join([sanitized_unit_name, django_settings])
693
761
django_run_dir = os.path.join(working_dir, "run/")
694
762
django_logs_dir = os.path.join(working_dir, "logs/")
695
763
settings_py_path = os.path.join(working_dir, 'settings.py')
745
813
    elif hook_name == "wsgi-relation-broken":
746
814
        wsgi_relation_broken()
747
815
 
 
816
    elif hook_name in ["cache-relation-joined", "cache-relation-changed"]:
 
817
        cache_relation_joined_changed()
 
818
 
 
819
    elif hook_name == "cache-relation-broken":
 
820
        cache_relation_broken()
 
821
 
748
822
    elif hook_name in ["website-relation-joined", "website-relation-changed"]:
749
823
        website_relation_joined_changed()
750
824