Wagtail in Docker

django, docker, programming, Python, Windows

Over the weekend I found out about a CMS software written on top of Django called Wagtail.

The installation instructions promise an easy path. Just run:

pip install wagtail

Since I guard my global pip pretty carefully in order to reduce version collisions and whatnot, my first inclination was to see if I can test this from within a Docker container.

To start the experiment within a Docker container using Python, I start with:

mkdir wagtail
cd wagtail

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/wagtail \
 python:3.7-buster /bin/bash

I use “%CD%” because I’m on Windows. If I were on Linux/Mac, I suppose it would be something like “${PWD}” instead.

So once in, I install and initialize per the instructions from Getting started — Wagtail Documentation 3.0.1 documentation:

cd /wagtail
pip install wagtail
wagtail start hahaha 

Interestingly, that wagtail start command, in addition to generating a bunch of files typical for a Django app, also created a Dockerfile.

So maybe the installer may already have some Docker support in mind?

Reading through it and spending more time than I expected to get to work, here are the steps to:

  • Get a Wagtail project set up from scratch using Docker without having to install any Python on the host.
  • Use a volume from the host for the app AND the SQLite DB so that they can be conveniently backed up and transferred (e.g. pushed up to some code repository like Github or SVN).

Generate a new Wagtail project

Pretty much what I had above:

mkdir wagtail
cd wagtail

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/wagtail \
 python:3.7-buster /bin/bash
cd /wagtail
pip install wagtail
wagtail start hahaha
exit

I’m using “hahaha” as my project name. Substitute as needed. Also: %CD% should be ${PWD} for Linux/Mac.

Build the Image

cd hahaha
docker build -t hahaha . 

Fix up file permissions (Windows only)

Since the app should be run by the user wagtail:wagtail, fix up the permissions on the files.

For some reason this is not done correctly for Windows despite the command in Dockerfile, so a manual step is required:

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/app \
 --user root hahaha /bin/bash

chown -R wagtail:wagtail .
exit

Set Up the App

This is typical Django setup stuff:

docker run -it --rm \
 --mount type=bind,source="%CD%",target=/app \
 hahaha /bin/bash
python manage.py migrate
python manage.py createsuperuser
exit

The above will create the db.sqlite3 file to be used for the app and also set up an admin user to be used to sign into the app.

Run The App

Finally, to run the app:

docker run -it --rm -p 8000:8000 \
 --mount type=bind,source="%CD%",target=/app hahaha

The -it --rm arguments are optional, but they help in stopping and cleaning up the container during development.

The site can be accessed at, of course, http://localhost:8000/. To manage it, use the superuser created earlier to get into the Admin Interface.

Worker Timeout

And now to actually start playing around with Wagtail….

So far I’m seeing a lot of errors when requesting pages in the Admin site. They look like this:

[2022-08-01 01:48:15 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:12)
[2022-08-01 01:48:15 +0000] [12] [INFO] Worker exiting (pid: 12)
[2022-08-01 01:48:15 +0000] [13] [INFO] Booting worker with pid: 13
[2022-08-01 01:48:58 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:13)
[2022-08-01 01:48:58 +0000] [13] [INFO] Worker exiting (pid: 13)
[2022-08-01 01:48:58 +0000] [14] [INFO] Booting worker with pid: 14
[2022-08-01 01:49:56 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:14)
[2022-08-01 01:49:56 +0000] [14] [INFO] Worker exiting (pid: 14)
[2022-08-01 01:49:56 +0000] [15] [INFO] Booting worker with pid: 15
[2022-08-01 01:50:34 +0000] [10] [CRITICAL] WORKER TIMEOUT (pid:15)
[2022-08-01 01:50:34 +0000] [15] [INFO] Worker exiting (pid: 15)
[2022-08-01 01:50:34 +0000] [16] [INFO] Booting worker with pid: 16

Fast Guide to Launching an EC2 Instance w/ SSH Access

AWS, ec2, ssh, Windows

Concepts

Minimal number of concepts to understand:

  • Key pair — a pair of public and private cryptographic keys that will be used to establish a secure shell/terminal to the launched EC2 instance.
  • Security group — a group of access rules that determine what network traffic and go into (inbound rules) and go out of (outbound rules) the EC2 instance.
  • IAM role — a collection of rules that determine what AWS services the EC2 instance will have access to (and what kind of access). E.g. read-only access to S3.
  • AMI — an image that prescribes an OS and some software to run when an EC2 instance comes up.

Shared Key Pair

The only thing that is shared between EC2 and the SSH program that matters in this example is the key pair. The instructions here will describe how to create a new key pair.

Creating a Key Pair

  • Log into the AWS console. The remaining steps to launch an instance will be done in the AWS Console.
  • Access Services > EC2 > Key Pairs from AWS Console.
  • Click “Create Key Pair”
  • Give it a name “KP”
  • Once it’s created, a “.pem” file will be downloaded. Remember the name and where the file is downloaded. It will be needed later.

Create a Security Group

  • Access Services > EC2 > Security Groups
  • Click “Create Security Group” to create a security group. Name it “SG.”
  • In the “Inbound” rules, add an entry for Type SSH, Protocol TCP, Port Range 22. For the Source, select “My IP” to let the tool automatically select your IP address.
  • Add other rules to open up more ports as needed.

Create an IAM Role

  • Access Services > Security, Identity, & Compliance > IAM > Roles
  • Click “Create Role” and select “EC2” (as opposed to Lambda)
  • Click “Next: Permissions”
  • Add permissions as needed (e.g. add “AmazonS3ReadOnlyAccess” if read-only access to S3 is needed).
  • Give the role a name and description.

Launch Instance

  • Access Services > EC2 > EC2 Dashboard
  • Click “Launch Instance”
  • Select an appropriate AMI (e.g. any of the Amazon Linux ones) to use for the EC2 instance. For the instance type, start with “t2.nano” to experiment with since it’s cheapest. Once the instance is up and running, larger instance types can be used as needed.
  • Click “Next: Configure Instance Details.”
  • For IAM role, select the role created above. Everything else can stay as-is.
  • Click “Next: Add Storage.”
  • Edit as desired, but the default is fine.
  • Click “Next: Add Tags.”
  • Add tags as needed. These are optional.
  • Click “Next: Configure Security Group.”
  • Choose “Select an existing security group” and select the security group “SG” created above.
  • Click “Review and Launch.”
  • Click “Launch” after everything looks right.
  • A modal comes up to allow selection of a key pair to use to access the instance. Select “KP” as created above.
  • Continue the launch.
  • Click on the “i-xxxxxxxxx” link to see the status of the instance.
  • Wait until Instance State is “running” and Status Checks is “2/2.”
  • Note the “Public DNS (IPv4)” value. It is the host name to SSH into.

Connecting to The EC2 Instance

Windows with Bitvise SSH Client

  • Download and start Bitvise SSH Client.
  • Click “New profile”
  • Go to “Login” tab
  • Click the link “Client key manager”
  • Click “Import”
  • Change file filter to (*.*)
  • Locate the .pem file downloaded above and import it. Accept default settings.
  • In the “Server” tab, enter the Public DNS host name from above. Use port 22.
  • In the “Authentication” section, enter “ec2-user” as the Username.
  • Use “publickey” as the Initial Method.
  • For “Client key,” select the profile created earlier when importing the .pem file.
  • Click “Log in” and confirm any dialogs.

Mac OS

Change the permission of the downloaded .pem file to allow only the owner access:

chmod 400 ~/Downloads/mykey.pem

Use ssh with the .pem file:

ssh -i ~/Downloads/mykey.pem ec2-user@xx-xx-xx-xx.yyyy.amazonaws.com

Aptana w/ Rails for Windows 7

ide, programming, rails

See http://www.therealvan.com/pn/?p=186 for installing: Rails 3.0.3 & MySQL on Windows 7.

After installing those, and once work begins, chances are an IDE w/ line-debugging capabilities are needed.

Aptana Studio 3 seems to be the best (free) IDE right now:

http://www.aptana.com/products/studio3/download.html

But to get that working fully for debugging, a gem needs to be installed:

gem install ruby-debug-ide

Otherwise, the “Debug Server” option will frustratingly do nothing in the IDE.

 

 

 

Heroku on Windows 7

heroku, programming, Windows

Prerequisites

Git installed and on PATH

Installation

Sign up for a Heroku account.  Note the user email address and password.

Download and install the Heroku Toolbelt. (see https://devcenter.heroku.com/articles/quickstart)

Even if you have your own version of Ruby, it’s better to have the Heroku Toolbelt install its own copy (default behavior).  Normally it will be installed in the “ruby-1.9.3” (may be different as new versions are used by the time you read this) subdirectory which is sibling of “bin” and “data”.

Make sure heroku.exe is on the PATH:

C:devprojects>heroku version
heroku/toolbelt/3.10.1 (i386-mingw32) ruby/1.9.3

Run “heroku login” to generate a key-pair under %HOMEPATH%.ssh

Project setup

Create a git project (either via “git clone” or “git init“).

CD to your project root and run “heroku create” to setup Heroku and add the hooks into your project.

Follow instructions on how to set up your app’s auxiliary files, such as adding a “Procfile” file to your project root and adding properties files.

Project deployment

Deployment is normally done simply by running “git push heroku master” to push your code up into Heroku’s Git repo (that was set up by “heroku create” earlier).

One possible error at this point is:

C:devprojectsherokuruby-sample>git push heroku master
Permission denied (publickey).
fatal: Could not read from remote repository.

I found that git looks at %HOME%/.ssh for SSH key files, but normally %HOME% isn’t set.  So modify your environment variables to add this setting:

set HOME=%HOMEPATH%

After the above, “git push” should work.  If not, then there may be a discrepancy between the %%HOME%.sshid_rsa and %HOMEPATH%.sshid_rsa.pub files and what the SSH keys settings are in your Heroku account.

 

Rails w/ MySQL (Windows 7-64)

mysql, programming, rails

Mainly notes on setting up the following (suitable for Dreamhost’s Rails setup):

  • Ruby 1.8.7-p374
  • Rails 3.0.3
  • Ruby devkit  (from http://rubyinstaller.org/downloads/)
  • MySQL Installer 5.6.17 Community Server 64-bit
  • mysql2 GEM ‘0.2.22’

Install Ruby 1.8.7 by going to: http://rubyinstaller.org/downloads/

Install Rails 3.0.3 by: gem install rails -v 3.0.3

Latest MySQL: http://dev.mysql.com/downloads/mysql/

mysql2 GEM:

  1. Get mysql-connector-c-6.1.3-win32.zip from http://dev.mysql.com/downloads/connector/c/. Unzip to C:devtools (or wherever).
  2. Run: gem install mysql2 -v '0.2.22' -- --with-mysql-dir="c:devtoolsmysql-connector-c-6.1.3-win32"  (Use whatever path you unzipped the connector above.)

Go to the Rails app and modify the GemFile:

...
gem 'mysql2', '0.2.22'
...

Run: bundle install

Copy c:devtoolsmysql-connector-c-6.1.3-win32liblibmysql.dll to %RUBY_HOME%bin