Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
More improvements to Raspbian image generation #skip_ci
Browse files Browse the repository at this point in the history
  • Loading branch information
GuLinux committed May 17, 2020
1 parent 96549fd commit bfe6431
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 16 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SET(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}${VERSION_SUFFIX})
SET(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/LICENSE)
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Marco Gulino <marco.gulino@gmail.com>")
SET(CPACK_DEBIAN_FILE_NAME "AstroPhotoPlus-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}-Ubuntu_Debian.deb")

## Debian package dependencies
# System dependencies
Expand Down
25 changes: 25 additions & 0 deletions scripts/raspbian-gen/deploy-image-release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
import requests
import os
from github import Github

workdir = os.path.abspath(os.path.dirname(__file__))
print(workdir)

with open(os.path.join(workdir, 'pi-gen/deploy/astrophotoplus.version'), 'r') as f:
astrophotoplus_version = f.readline().strip()

release_file = [f for f in os.listdir(os.path.join(workdir, 'pi-gen/deploy')) if f.endswith('AstroPhoto-Plus-{}.zip'.format(astrophotoplus_version))]
if not release_file:
raise RuntimeError('Zip file for AstroPhoto Plus version {} not found'.format(astrophotoplus_version))
release_file = release_file[0]

print('Pushing {} to {}'.format(release_file, astrophotoplus_version))

github = Github(os.environ['GITHUB_OAUTH_USER'], os.environ['GITHUB_OAUTH_TOKEN'])
repo = github.get_repo('GuLinux/AstroPhoto-Plus')
release = repo.get_release('v{}'.format(astrophotoplus_version))
print('Release found, uploading asset...')
release.upload_asset(os.path.join(workdir, 'pi-gen/deploy', release_file), 'Raspberry Pi Image')
print('Done.')

2 changes: 2 additions & 0 deletions scripts/raspbian-gen/files/copy-astrophotoplus-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
mv -v "${ROOTFS_DIR}/tmp/astrophotoplus.version" ../../deploy/
2 changes: 2 additions & 0 deletions scripts/raspbian-gen/files/provisioning-launcher
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export UNATTENDED_SETUP=y
echo " *** Launching raspbian provisioning script" >&2
bash /tmp/provisioning.sh
rm -vf /tmp/provisioning.sh
ASTROPHOTOPLUS_VERSION="$( dpkg -s astrophotoplus | grep '^Version' | cut -d' ' -f2 )"
echo "$ASTROPHOTOPLUS_VERSION" > /tmp/astrophotoplus.version

# Extra step: install python dependencies
sudo -H -u "$TARGET_USER" /usr/lib/AstroPhotoPlus/backend/start-server --only-deps
Expand Down
124 changes: 108 additions & 16 deletions scripts/raspbian-gen/raspbian-gen
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@ rpigen_path="$raspbian_gen_root/pi-gen"
launcher_script="$raspbian_gen_root/files/provisioning-launcher"
provisioning_script="$project_dir/scripts/setup/ubuntu-raspbian.sh"
copy_script="$raspbian_gen_root/files/copy-provisioning"
get_version_script="$raspbian_gen_root/files/copy-astrophotoplus-version"

USE_QEMU=0
DEPLOY_ZIP=0
USE_DOCKER=0
PRESERVE_CONTAINER=0
STAGE_LIST="$( eval echo stage{0..2} )"
CONTINUE=""

FIRST_USER_NAME="stargazer"
FIRST_USER_PASS="AstroPhoto-Plus"
TARGET_HOSTNAME="AstroPhoto-Plus"


show_help() {
cat >&2 <<EOF
Usage: $0 [options].
Available options:
-q|--qemu create a Qemu image for testing
-r|--rm remove and rebuild the docker image
-c|--continue continue a stopped build
-z|--zip zip the destination file
-d|--docker use docker to build image
--remove-pigen remove pi-gen repository
-q|--qemu create a Qemu image for testing
-r|--rm remove and rebuild the docker image
-c|--continue continue a stopped build
-z|--zip zip the destination file
-d|--docker use docker to build image
--remove-pigen remove pi-gen repository
--preserve-container Keep docker container after build
--stages <stages> List of stages to build (default: 0..2)
EOF
exit "${1:-1}"
}
Expand All @@ -40,14 +50,21 @@ while [ -n "$1" ]; do
CONTINUE=1
;;
--remove-pigen)
rm -rf $rpigen_path
rm -rf "$rpigen_path"
;;
-z|--zip)
DEPLOY_ZIP=1
;;
-d|--docker)
USE_DOCKER=1
;;
--preserve-container)
PRESERVE_CONTAINER=1
;;
--stages)
STAGE_LIST="$( eval echo stage{$2})"
shift
;;
*)
show_help
;;
Expand All @@ -68,25 +85,100 @@ sudo modprobe loop

cat > config <<EOF
IMG_NAME="AstroPhoto-Plus"
FIRST_USER_NAME="stargazer"
FIRST_USER_PASS="AstroPhoto-Plus"
FIRST_USER_NAME="$FIRST_USER_NAME"
FIRST_USER_PASS="$FIRST_USER_PASS"
TARGET_HOSTNAME="$TARGET_HOSTNAME"
ENABLE_SSH=1
DEPLOY_ZIP=$DEPLOY_ZIP
USE_QEMU=${USE_QEMU}
#STAGE_LIST="stage2"
STAGE_LIST="stage0 stage1 stage2"
STAGE_LIST="$STAGE_LIST"
PRESERVE_CONTAINER=${PRESERVE_CONTAINER}
EOF

mkdir -p stage2/99-install-astrophotoplus/files
mkdir -p stage2/98-install-astrophotoplus/files
mkdir -p stage2/99-get-astrophotoplus-version

cp -v "$provisioning_script" stage2/98-install-astrophotoplus/files/provisioning.sh
cp -v "$copy_script" stage2/98-install-astrophotoplus/00-run.sh
cp -v "$launcher_script" stage2/98-install-astrophotoplus/01-run-chroot.sh
cp -v "$get_version_script" stage2/99-get-astrophotoplus-version/01-run.sh

cp -v "$provisioning_script" stage2/99-install-astrophotoplus/files/provisioning.sh
cp -v "$copy_script" stage2/99-install-astrophotoplus/00-run.sh
cp -v "$launcher_script" stage2/99-install-astrophotoplus/01-run-chroot.sh
sed -i 's/-lite//g' "stage2/EXPORT_IMAGE"
(
cd "$rpigen_path"
git checkout -f export-image/prerun.sh
patch -p1 <<'EOF'
diff --git a/export-image/prerun.sh b/export-image/prerun.sh
index 8bbc566..2fc1abd 100755
--- a/export-image/prerun.sh
+++ b/export-image/prerun.sh
@@ -39,8 +39,14 @@ BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)
-BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}")
-ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}")
+echo -n "Waiting for losetup..."
+while [ -z "$BOOT_DEV" ]; do
+ BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}")
+ ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}")
+ [ -z "$BOOT_DEV" ] && sleep 1
+ echo -n .
+done
+echo
echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
diff --git a/stage2/EXPORT_IMAGE b/stage2/EXPORT_IMAGE
index aa8f936..79ec11a 100644
--- a/stage2/EXPORT_IMAGE
+++ b/stage2/EXPORT_IMAGE
@@ -1,4 +1,4 @@
-IMG_SUFFIX="-lite"
+IMG_SUFFIX=""
if [ "${USE_QEMU}" = "1" ]; then
export IMG_SUFFIX="${IMG_SUFFIX}-qemu"
fi
EOF
)

if [ "$USE_DOCKER" == 1 ]; then
CONTINUE="$CONTINUE" ./build-docker.sh
else
CONTINUE="$CONTINUE" ./build.sh
fi

set -x
if [ -r "$rpigen_path"/deploy/*-AstroPhoto-Plus.img ]; then
ASTROPHOTOPLUS_VERSION=$( cat "$rpigen_path"/deploy/astrophotoplus.version )
ASTROPHOTOPLUS_IMAGE_DEPLOY="$( ls "$rpigen_path"/deploy/*-AstroPhoto-Plus.img | head -n 1 )"

ASTROPHOTOPLUS_IMAGE="${ASTROPHOTOPLUS_IMAGE_DEPLOY%%.img}-$ASTROPHOTOPLUS_VERSION.img"
ASTROPHOTOPLUS_ZIPFILE="${ASTROPHOTOPLUS_IMAGE%%.img}.zip"
mv -v "$ASTROPHOTOPLUS_IMAGE_DEPLOY" "$ASTROPHOTOPLUS_IMAGE"
cat > "$rpigen_path"/deploy/ReadMe.md <<EOF
Follow the instruction on the [Raspberry Pi website](https://www.raspberrypi.org/documentation/installation/installing-images/README.md?target=_blank) to write the image to the micro sd card.
When the sd card is ready, insert it into the Raspberry Pi, and connect the power supply.
As soon as it finishes booting, you should notice a new wifi network named \`$TARGET_HOSTNAME-<NNNN>\`. Connect to it using the password \`AstroPhoto-Plus\`.
You can then point your browser to [http://AstroPhoto-Plus.local](http://AstroPhoto-Plus.local). If that doesn't work, the IP address is usually the first on your network, for instance [http://10.42.0.1](http://10.42.0.1)
To use SSH to connect to your *AstroPhoto Plus* box, use the above address (astrophoto-plus.local), using the following credentials:
- Username: $FIRST_USER_NAME
- Password: $FIRST_USER_PASS
That's it! You're now running *AstroPhoto Plus*.
If you need to change wifi settings, there is a network management utility in the *Settings* module.
EOF
(
cd "$rpigen_path/deploy"
zip --move -9 "$ASTROPHOTOPLUS_ZIPFILE" "$( basename "$ASTROPHOTOPLUS_IMAGE")" "ReadMe.md"
)
else
echo "*** Error creating Raspberry Pi image: file not found" >&2
exit 1
fi

0 comments on commit bfe6431

Please sign in to comment.