Skip to content

Installing pycsw with nginx,gunicorn,supervisor

Rich Signell edited this page May 5, 2015 · 2 revisions

Nginx:

(ioos)rsignell@gam:/opt/pycsw$ more /etc/nginx/sites-available/pycsw.conf
upstream pycsw {
    server 127.0.0.1:9201;
    keepalive 32;
}

server {
    listen          8000;
    client_max_body_size    4G;
    keepalive_timeout       5;
    server_name             _;

    location /csw {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP       $remote_addr;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_read_timeout 300s;

        proxy_pass   http://pycsw;
    }
}

Gunicorn:

(ioos)rsignell@gam:/opt/pycsw$ more gunicorn.conf
import multiprocessing

#bind = 'unix://${prefix}/var/run/${sites}.socket'
bind = '127.0.0.1:8000'
#bind = '127.0.0.1:9201'
#workers = multiprocessing.cpu_count() * 2 + 1
workers = 4

# environment
raw_env =  ["PYCSW_CONFIG=/opt/pycsw/usgs-cmg.cfg"]

# logging
debug = True
errorlog = '-'
loglevel = 'debug'
accesslog = '-'

Supervisor:

(ioos)rsignell@gam:/opt/pycsw$ more /etc/supervisor/conf.d/pycsw.conf
[program:pycsw]
command=/home/usgs/miniconda/envs/pycsw/bin/gunicorn -c gunicorn.conf cswapp:app
stopasgroup=true
user=usgs
numprocs=1
directory=/opt/pycsw
autostart=true
redirect_stderr=true

cswapp.py:

(ioos)rsignell@gam:/opt/pycsw$ more cswapp.py

# =================================================================
from StringIO import StringIO
import os
import sys

app_path = os.path.dirname(__file__)
sys.path.append(app_path)

from pycsw import server


def app(env, start_response):
    """WSGI wrapper"""
    config = None

    if 'PYCSW_CONFIG' in env:
        config = env['PYCSW_CONFIG']
    elif 'PYCSW_CONFIG' in os.environ:
        config = os.environ.get('PYCSW_CONFIG')
    else:
      return "Set PYCSW_CONFIG env variable to start server"

    if env['QUERY_STRING'].lower().find('config') != -1:
        for kvp in env['QUERY_STRING'].split('&'):
            if kvp.lower().find('config') != -1:
                config = kvp.split('=')[1]

    if not os.path.isabs(config):
        config = os.path.join(app_path, config)

    if 'HTTP_HOST' in env and ':' in env['HTTP_HOST']:
        env['HTTP_HOST'] = env['HTTP_HOST'].split(':')[0]

    env['local.app_root'] = app_path

    csw = server.Csw(config, env)

    gzip = False
    if ('HTTP_ACCEPT_ENCODING' in env and
            env['HTTP_ACCEPT_ENCODING'].find('gzip') != -1):
        # set for gzip compressed response
        gzip = True

    # set compression level
    if csw.config.has_option('server', 'gzip_compresslevel'):
        gzip_compresslevel = \
            int(csw.config.get('server', 'gzip_compresslevel'))
    else:
        gzip_compresslevel = 0

    contents = csw.dispatch_wsgi()

    headers = {}

    if gzip and gzip_compresslevel > 0:
        import gzip

        buf = StringIO()
        gzipfile = gzip.GzipFile(mode='wb', fileobj=buf,
                                 compresslevel=gzip_compresslevel)
        gzipfile.write(contents)
        gzipfile.close()

        contents = buf.getvalue()

        headers['Content-Encoding'] = 'gzip'

    headers['Content-Length'] = str(len(contents))
    headers['Content-Type'] = csw.contenttype

    status = '200 OK'
    start_response(status, headers.items())

    return [contents]

pycsw config file:

(ioos)rsignell@gam:/opt/pycsw$ more usgs-cmg.cfg
[server]
home=.
url=http://geoport.whoi.edu/csw
mimetype=application/xml; charset=UTF-8
encoding=UTF-8
language=en-US
maxrecords=10
#loglevel=DEBUG
#logfile=/tmp/pycsw.log
#ogc_schemas_base=http://foo
#federatedcatalogues=http://catalog.data.gov/csw
#pretty_print=true
#gzip_compresslevel=8
#domainquerytype=range
#domaincounts=true
#spatial_ranking=true
profiles=apiso

[manager]
transactions=false
allowed_ips=127.0.0.1
#csw_harvest_pagesize=10

[metadata:main]
identification_title=USGS Coastal and Marine Geoscience Modeling and Time Series
 Data Portal
identification_abstract=The USGS Coastal and Marine Geoscience Modeling and Time
 Series Data Portal contains records harvested from THREDDS data servers using t
he built-in ISO data services.
identification_keywords=coastal,marine,geoscience
identification_keywords_type=theme
identification_fees=None
identification_accessconstraints=None
provider_name=United States Geological Survey
provider_url=http://marine.usgs.gov
contact_name=Signell, Richard
contact_position=Oceanographer
contact_address=384 Woods Hole Road
contact_city=Woods Hole
contact_stateorprovince=Massachusetts
contact_postalcode=02543-1598
contact_country=United States of America
contact_phone=+01-508-457-2229
contact_fax=+01-508-457-2310
contact_email=rsignell@usgs.gov
contact_url=https://profile.usgs.gov/rsignell
contact_hours=0900h - 1700h EST
contact_instructions=During hours of service.  Off on weekends.
contact_role=pointOfContact

[repository]
# sqlite
database=sqlite:////opt/pycsw/records.db
table=records