Skip to content

maidstone-hackspace/maidstone-hackspace-website

Repository files navigation

https://cdn.rawgit.com/maidstone-hackspace/administration/2ede7cb1/images/hackspace-banner.png

Maidstone Hackspace website

https://pyup.io/repos/github/maidstone-hackspace/maidstone-hackspace-website/shield.svg

Repository for the maidstone hackspace website, feel free to fork this site for your own Hackspace.

Social

Test site sharing links / cards with twitter and facebook https://cards-dev.twitter.com/validator https://developers.facebook.com/tools/debug/

Requirements

Before getting started make sure you have git, docker and docker-compose installed on your machine. The simplest way to setup this site is to use docker-compose so please install that from this site https://docs.docker.com/engine/installation/ and make sure the quick start guide works https://docs.docker.com/machine/get-started/ then you can use the commands below to test and make changes.

Setup

Steps to get the site running for the first time

First clone the project

To get started checkout the project to your machine, with the command below.

git clone https://github.com/maidstone-hackspace/maidstone-hackspace-website.git

Then copy and edit `env` file

Once checked out you need to have a `.env` file. The simplest way to do this is copy the `env.example`, you can edit it as required. One edit you will need to make is to change

DJANGO_SETTINGS_MODULE=config.settings.test

to

DJANGO_SETTINGS_MODULE=config.settings.local 

Then build your containers to install the sites requirements

Once you have your `.env` file in place you can build your containers locally. You only need to do this when first starting or if requirements change.

docker-compose -f local.yml build

If you receive a similar error to the following:

    with open(full_path, 'rb') as f:
PermissionError: [Errno 13] Permission denied: 'maidstone-hackspace-website/bucket/.minio.sys/format.json'

This is because internally docker runs as root and so files created on your file system may be created as root.

You can either re-run with `sudo` or delete the entire `bucket/.minio.sys` folder.

Next start your new containers containing a database and django

docker-compose -f local.yml up

Once it’s running, there’s a few commands to run the first time before you can use it.

Run migrations to setup the database

To run django commands just prefix them with docker-compose -f local.yml run django

docker-compose -f local.yml run --rm django python manage.py migrate

Download static content

docker-compose -f local.yml run --rm django python manage.py collectstatic

Find the website address

Every time you start the website with the docker-compose -f local.yml up command the IPAddress of the website could change. To find it you can run docker ps, but you may find the following command more handy.

docker ps -q | xargs docker inspect --format="{{printf \"%.40s\" .Name}} @ {{printf \"%.20s\" .Config.Image}} @ http://{{if ne \"\" .NetworkSettings.IPAddress}}{{ printf \"%.22s\" .NetworkSettings.IPAddress}}{{else}}{{range .NetworkSettings.Networks}}{{printf \"%.22s\" .IPAddress}}{{end}}{{end}} @ {{printf \"%.10s\" .State.Status}}" | column -t -s@ -c 80

This should return something like this:

/maidstonehackspacewebsite_nginx_1          olymk2/nginx            http://172.18.0.12    running
/maidstonehackspacewebsite_celerybeat_1     maidstonehackspacewe    http://172.18.0.10    running
/maidstonehackspacewebsite_celeryworker_    maidstonehackspacewe    http://172.18.0.11    running
/maidstonehackspacewebsite_django_1         maidstonehackspacewe    http://172.18.0.9     running
/maidstonehackspacewebsite_django_bjoren    maidstonehackspacewe    http://172.18.0.8     running
/maidstonehackspacewebsite_django_gunico    maidstonehackspacewe    http://172.18.0.7     running
/maidstonehackspacewebsite_postgres_1       maidstonehackspacewe    http://172.18.0.6     running
/maidstonehackspacewebsite_bucket_1         minio/minio             http://172.18.0.5     running
/maidstonehackspacewebsite_directory_1      osixia/openldap:1.2.    http://172.18.0.4     running
/maidstonehackspacewebsite_mailhog_1        mailhog/mailhog         http://172.18.0.3     running
/maidstonehackspacewebsite_redis_1          redis:latest            http://172.18.0.2     running

Look for the nginx line, in this instance the IPAddress is 172.18.0.12. This IPAddress will be used in the rest of the examples below, you will need to substitue with your IPAddress.

At this point the website should be up and running and you should be able to access it by going to the nginx IPAddress, e.g., http://172.18.0.12/

Create the admin user.

Once created you can login at http://172.18.0.12/trustee

docker-compose -f local.yml run --rm django python manage.py createsuperuser

Generate dummy data

docker-compose -f local.yml run --rm django python manage.py generate_test_data

Migrations / Managing default data

If you want to export some data you entered into the admin area you can use dumpdata and loaddata to export and import.

docker-compose -f local.yml run --rm django python manage.py dumpdata feeds > mhackspace/feeds/fixtures/defaults.json
docker-compose -f local.yml run --rm django python manage.py loaddata mhackspace/feeds/fixtures/defaults.json

Django Commands

docker-compose -f local.yml run --rm django python manage.py list_subscriptions

Render image variations, if you change the images sizes this will be needed

docker-compose -f local.yml run --rm django python manage.py rendervariations 'blog.Post.image' --replace

Api

import requests 

url = 'http://127.0.0.1:8180/api/v1/rfidAuth/'
data = {
  'rfid': '4996',
  'device': '7bff6053-77ef-4250-ac11-8a119fd05a0e'
}

# client = RequestsClient()
response = requests.post(
    'http://127.0.0.1:8180/api/v1/rfidAuth/',
    data={'rfid': '238e', 'device': 'e8f27231-8093-4477-8906-e5ae1b12dbd6'})
#requests.get(url)
return response.status_code

letsencrypt cert setup

Setup / create new certs

letsencrypt certonly --renew --webroot -w /var/www/.well-known -d stage.maidstone-hackspace.org.uk
letsencrypt certonly --webroot -w /var/www/.well-known -d stage.maidstone-hackspace.org.uk

Automation of renewal process create a file called /etc/cron.monthly/letsencrypt-renew.sh and make it executable with chmod +x, then place your above commands in the file like in the example below.

#!/bin/bash
letsencrypt certonly --webroot --renew-by-default --agree-tos -w /var/www/.well-known -d stage.maidstone-hackspace.org.uk

Postgres tips

Connect to the database inside container to run sql commands.

docker-compose -fstage.yml run --rm postgres psql -U postgres

Backups

Create a backup file with today’s date

docker exec -t {CONTAINER_NAME} pg_dump -Fp -c -U postgresuser > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql
docker exec {CONTAINER_NAME} bash -lc 'pg_dump --format custom vmdb_production' > dump_`date +%d-%m-%Y"_"%H_%M_%S`.pgdata

Import previously made backup

docker exec -i -u {USER} {CONTAINER_NAME}  pg_restore --verbose --no-acl --no-owner --clean --role=postgres -Upostgres --dbname={DATABASE_USER} < dump_31-01-2018_13_09_24.pgdata
  docker exec -i -u {USER} {CONTAINER_NAME} pg_restore -C --clean -d {DATABASE_USER} < dump_31-01-2018_13_09_24.pgdata
  cat dump_27-01-2018_14_26_09.sql | docker exec -i {CONTAINER_ID} psql -U postgres

letsencrypt config

letsencrypt certonly --renew --webroot -w /var/www/.well-known -d maidstone-hackspace.org.uk -d maidstone-hackspace.org.uk -d www.maidstone-hackspace.org.uk

CMD [“nginx”, “-g”, “daemon off;”] sudo chmod -R a+rX static/

(let ((default-directory "/docker:hackdev_django_1:/app"))
  (python-shell-make-comint "python manage.py shell" "Python" 'show))

Test

(setq python-shell-interpreter "/docker:hackdev_django_1:/usr/local/bin/python")
(setq python-environment-directory "/docker:hackdev_django_1:/")