Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve build script #109

Merged
merged 7 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
113 changes: 91 additions & 22 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
# 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 {
Expand All @@ -28,6 +33,52 @@ function usage {
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,47 @@ 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 )
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