#!/bin/bash
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# 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.
set -eux

install-packages libyaml-dev pkg-config python-dev build-essential libmysqlclient-dev \
 libffi-dev libpq-dev libxslt-dev libsqlite3-dev libldap2-dev libsasl2-dev

mkdir -p /opt/stack/venvs
virtualenv /opt/stack/venvs/pypi-mirror
/opt/stack/venvs/pypi-mirror/bin/pip install -U /opt/stack/pypi-mirror
ln -sf /opt/stack/venvs/pypi-mirror/bin/run-mirror /usr/local/bin/run-mirror

useradd pypi-mirror --system -d /var/run/pypi-mirror -s /bin/false

mkdir -p /etc/pypi-mirror
cat <<EOF > /etc/pypi-mirror/mirror.yaml
cache-root: /var/cache/pypi-mirror/pypi/download

mirrors:
 - name: openstack
   projects:
     - https://git.openstack.org/openstack/requirements
   output: /var/cache/pypi-mirror/pypi/mirror
EOF

mkdir -p /var/cache/pypi-mirror/pypi/download
mkdir -p /var/cache/pypi-mirror/pypi/mirror

chown -R pypi-mirror.pypi-mirror /var/cache/pypi-mirror
chmod a+rx /var/cache/pypi-mirror

cat <<EOF > /etc/cron.d/pypi-mirror-update
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
15 * * * * pypi-mirror run-mirror -b remotes/origin/master --verbose -c /etc/pypi-mirror/mirror.yaml 2>&1 | logger -t pypi-mirror
EOF
