Skip to content

Commit

Permalink
Merge pull request apache#109 from janhoy/build-script
Browse files Browse the repository at this point in the history
Improve build script
  • Loading branch information
janhoy authored Jun 26, 2024
2 parents 4d228bf + 1337500 commit 7f88bf3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ If you want to build the site without the docker image, you can install Python 3

On Windows, you can use the Windows Subsystem for Linux (WSL) to run the build script. Or you can run the docker command directly in a Terminal:

docker run --rm -w /work -p 8000:8000 -v $(pwd):/work qwe1/docker-pelican:4.8.0 pip3 install -r requirements.txt; pelican content -r -l -b 0.0.0.0
docker run --rm -ti -w /work -p 8000:8000 -v $(pwd):/work python:3-alpine sh -c "pip3 install -r requirements.txt; pelican content -r -l -b 0.0.0.0"

## Updating site during a Solr release

Expand Down
116 changes: 93 additions & 23 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,71 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Using https://hub.docker.com/r/qwe1/docker-pelican as pelican image, supports both AMD64 and ARM64
PELICAN_IMAGE="qwe1/docker-pelican:4.8.0"
DOCKER_CMD="docker run --rm -w /work -p 8000:8000 -v $(pwd):/work $PELICAN_IMAGE"
# Fail on error
set -e
#set -x

PYTHON_IMAGE="python:3-alpine"
SOLR_LOCAL_PELICAN_IMAGE="solr-pelican-image"
DOCKER_CMD="docker run --rm -ti -w /work -p 8000:8000 -v $(pwd):/work $SOLR_LOCAL_PELICAN_IMAGE"
unset SERVE
PIP_CMD="pip3 install -r requirements.txt"
PELICAN_CMD="pelican content -o output"
PELICAN_OPTS=""
export SITEURL="https://solr.apache.org/"

function usage {
echo "Usage: ./build.sh [-l] [<other pelican arguments>]"
echo "Usage: ./build.sh [-l] [-h] [<other pelican arguments>]"
echo " -l Live build and reload source changes on localhost:8000"
echo " --help Show full help for options that Pelican accepts"
}

function build_image {
echo "Building local Docker image for Pelican, called $SOLR_LOCAL_PELICAN_IMAGE."
# Make a new local image with the pip packages installed
docker rm -f solr-pelican >/dev/null 2>&1 || true
docker run --name solr-pelican -w /work -v $(pwd):/work $PYTHON_IMAGE sh -c "$PIP_CMD"
docker commit solr-pelican $SOLR_LOCAL_PELICAN_IMAGE
docker rm -f solr-pelican >/dev/null 2>&1 || true
}

function ensure_image {
if ! docker inspect $SOLR_LOCAL_PELICAN_IMAGE >/dev/null 2>&1
then
build_image
fi
}

function check_requirements_update {
# Get the last modified time of requirements.txt
local req_mod_time
if [[ $(uname) == "Darwin" ]]; then
req_mod_time=$(stat -f "%m" requirements.txt)
else
req_mod_time=$(stat -c "%Y" requirements.txt)
fi

# Get the build timestamp of the docker image
local image_build_time
image_build_time=$(docker inspect --format='{{.Created}}' $SOLR_LOCAL_PELICAN_IMAGE)

# Parse the timestamp into seconds since epoch in UTC
if [[ $(uname) == "Darwin" ]]; then
# macOS date command workaround
image_build_time=$(echo "$image_build_time" | awk -F '.' '{print $1}')
image_build_time=$(date -ju -f "%Y-%m-%dT%H:%M:%S" "$image_build_time" "+%s")
else
# Linux date command
image_build_time=$(date -d "$(echo "$image_build_time" | cut -d'.' -f1 | sed 's/T/ /; s/Z//')" --utc "+%s")
fi

# Compare the timestamps and build the image if requirements.txt is newer
if [[ $req_mod_time -gt $image_build_time ]]; then
echo "requirements.txt has been updated since the last build, rebuilding image!"
build_image
fi
}

if ! docker -v >/dev/null 2>&1
then
echo "ERROR: This script requires docker."
Expand All @@ -37,29 +88,48 @@ then
exit 2
fi

if [[ ! -z $1 ]]; then
if [[ "$1" == "-l" ]]; then
SERVE=true
shift
else
usage
if [[ "$1" == "-h" ]]; then
exit 0
elif [[ "$1" == "--help" ]]; then
echo
echo "Below is a list of other arguments you can use which will be passed to pelican."
echo
$DOCKER_CMD pelican -h
while getopts ":lbh-:" opt; do
case ${opt} in
l )
SERVE=true
;;
b )
build_image
;;
h )
usage
exit 0
fi
fi
fi
;;
- )
case "${OPTARG}" in
help )
usage
echo
echo "Below is a list of other arguments you can use which will be passed to pelican."
echo
$DOCKER_CMD pelican -h
exit 0
;;
* )
PELICAN_OPTS+="--${OPTARG} "
;;
esac
;;
\? )
PELICAN_OPTS+="-${OPTARG} "
;;
esac
done
shift $((OPTIND -1))

ensure_image
check_requirements_update
if [[ $SERVE ]]; then
echo "Building Solr site locally. Goto http://localhost:8000 to view."
echo "Edits you do to the source tree will be compiled immediately!"
$DOCKER_CMD $PIP_CMD; $PELICAN_CMD --autoreload --listen -b 0.0.0.0 $@
$DOCKER_CMD sh -c "$PELICAN_CMD --autoreload --listen -b 0.0.0.0 $PELICAN_OPTS $*"
else
echo "Building Solr site."
echo "Building Solr site locally."
echo "To build and serve live edits locally, run this script with -l argument. Use -h for help."
$DOCKER_CMD $PIP_CMD; $PELICAN_CMD $@
$DOCKER_CMD sh -c "$PELICAN_CMD $PELICAN_OPTS $*"
fi
16 changes: 8 additions & 8 deletions manual-install.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Installing Pelican by hand

The site uses [Pelican][1] for static html generation. Pelican requires [Python 3.5+][4] and can be installed with pip.
The site uses [Pelican][1] for static html generation. Pelican requires [Python 3.5+][2] and can be installed with pip.

**The `build.sh` script mentioned in REAME is the easiest way of building the site**, and you can skip this part unless you want to understand the moving parts and install things by hand.
**The `build.sh` script mentioned in README is the easiest way of building the site using Docker**.
If for some reason you want to install Python and Pelican by hand, here are the steps:

## Install Python 3

First, you need to install Python 3. You can download the latest version from the [Python website][4] or
First, you need to install Python 3. You can download the latest version from the [Python website][2] or
use your package manager to install it. For example, on macOS:

```shell
Expand All @@ -21,7 +22,7 @@ To install pelican and requirements, simply run the following command in the roo
pip3 install -r requirements.txt
```

If you run into conflicts with existing packages, a solution is to use a virtual Python environment. See the [Pelican installation page][2] for more details. These are quick commands, Linux flavor:
If you run into conflicts with existing packages, a solution is to use a virtual Python environment. See the [Pelican installation page][3] for more details. These are quick commands, Linux flavor:

```sh
python3 -m venv env
Expand All @@ -43,7 +44,6 @@ You can also tell Pelican to watch for your modifications, instead of manually r
pelican --autoreload --listen
```

Remember that on Mac/Linux you can use the `build.sh` script with `-l` option to do the same.

[1]: https://blog.getpelican.com/
[4]: https://www.python.org/downloads/
[1]: https://getpelican.com
[2]: https://www.python.org/downloads/
[3]: https://docs.getpelican.com/en/stable/install.html

0 comments on commit 7f88bf3

Please sign in to comment.