Skip to content

Commit

Permalink
menu: remove assumption user ID is 1000
Browse files Browse the repository at this point in the history
On menu.sh start, store current UID and GID to .env, if they are missing.
Use these to run services that support custom user.
  • Loading branch information
ukkopahis committed Apr 29, 2022
1 parent 15dca57 commit 2fc86ca
Show file tree
Hide file tree
Showing 25 changed files with 81 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .templates/blynk_server/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ blynk_server:
restart: unless-stopped
environment:
- TZ=Etc/UTC
- IOTSTACK_UID=1000
- IOTSTACK_GID=1000
- IOTSTACK_UID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- IOTSTACK_GID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
ports:
- "8180:8080"
- "8440:8440"
Expand Down
4 changes: 2 additions & 2 deletions .templates/domoticz/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ domoticz:
restart: unless-stopped
network_mode: bridge
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
# - TZ=
# - WEBROOT=domoticz

4 changes: 2 additions & 2 deletions .templates/gitea/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ gitea:
- "7920:3000/tcp"
- "2222:22/tcp"
environment:
- USER_UID=1000
- USER_GID=1000
- USER_UID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- USER_GID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
volumes:
- ./volumes/gitea/data:/data
- /etc/timezone:/etc/timezone:ro
Expand Down
4 changes: 2 additions & 2 deletions .templates/heimdall/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ heimdall:
image: ghcr.io/linuxserver/heimdall
container_name: heimdall
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- TZ=Europe/Paris
volumes:
- ./volumes/heimdall/config:/config
Expand Down
4 changes: 2 additions & 2 deletions .templates/homebridge/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ homebridge:
restart: unless-stopped
environment:
- TZ=Etc/UTC
- PGID=1000
- PUID=1000
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- HOMEBRIDGE_CONFIG_UI=1
- HOMEBRIDGE_CONFIG_UI_PORT=8581
volumes:
Expand Down
4 changes: 2 additions & 2 deletions .templates/homer/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ homer:
image: b4bz/homer:latest
container_name: homer
environment:
- UID=1000
- GID=1000
- UID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- GID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
volumes:
- ./volumes/homer/assets:/www/assets
ports:
Expand Down
4 changes: 2 additions & 2 deletions .templates/mariadb/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ mariadb:
container_name: mariadb
environment:
- TZ=Etc/UTC
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- MYSQL_ROOT_PASSWORD=%randomAdminPassword%
- MYSQL_DATABASE=default
- MYSQL_USER=mariadbuser
Expand Down
4 changes: 2 additions & 2 deletions .templates/n8n/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ n8n:
# - N8N_BASIC_AUTH_USER=<USER>
# - N8N_BASIC_AUTH_PASSWORD=<PASSWORD>

# - PGID=1000
# - PUID=1000
# - PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
# - PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
# - USBDEVICES=/dev/ttyAMA0
# - PACKAGES=mc

4 changes: 2 additions & 2 deletions .templates/nextcloud/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ nextcloud_db:
restart: unless-stopped
environment:
- TZ=Etc/UTC
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- MYSQL_ROOT_PASSWORD=%randomPassword%
- MYSQL_PASSWORD=%randomMySqlPassword%
- MYSQL_DATABASE=nextcloud
Expand Down
4 changes: 2 additions & 2 deletions .templates/plex/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plex:
container_name: plex
network_mode: host
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- VERSION=docker
volumes:
- ./volumes/plex/config:/config
Expand Down
4 changes: 2 additions & 2 deletions .templates/prometheus/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ prometheus:
ports:
- "9090:9090"
environment:
- IOTSTACK_UID=1000
- IOTSTACK_GID=1000
- IOTSTACK_UID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- IOTSTACK_GID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
volumes:
- ./volumes/prometheus/data:/prometheus
command:
Expand Down
4 changes: 2 additions & 2 deletions .templates/python/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ python:
restart: unless-stopped
environment:
- TZ=Etc/UTC
- IOTSTACK_UID=1000
- IOTSTACK_GID=1000
- IOTSTACK_UID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- IOTSTACK_GID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
# ports:
# - "external:internal"
volumes:
Expand Down
4 changes: 2 additions & 2 deletions .templates/qbittorrent/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
image: linuxserver/qbittorrent
container_name: qbittorrent
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- UMASK_SET=022
- WEBUI_PORT=15080
volumes:
Expand Down
4 changes: 2 additions & 2 deletions .templates/syncthing/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
container_name: syncthing
hostname: raspberrypi #optional
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- HOME=/app
- TZ=Etc/UTC
volumes:
Expand Down
4 changes: 2 additions & 2 deletions .templates/transmission/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ transmission:
image: linuxserver/transmission
container_name: transmission
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- TZ=Etc/UTC
volumes:
- ./volumes/transmission/config:/config
Expand Down
4 changes: 2 additions & 2 deletions .templates/wireguard/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ wireguard:
image: ghcr.io/linuxserver/wireguard
restart: unless-stopped
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- TZ=Etc/UTC
- SERVERURL=your.dynamic.dns.name
- SERVERPORT=51820
Expand Down
12 changes: 10 additions & 2 deletions docs/Basic_setup/Backup-and-Restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ There are 2 ways to run backups:

The command that's run from the command line can also be executed from a cronjob:

```0 2 * * * cd /home/pi/IOTstack && /bin/bash ./scripts/backup.sh```
```
0 2 * * * cd /home/pi/IOTstack && /bin/bash ./scripts/backup.sh
```

The current directory of bash must be in IOTstack's directory, to ensure that it can find the relative paths of the files it's meant to back up. In the example above, it's assume that it's inside the `pi` user's home directory.

Expand Down Expand Up @@ -60,7 +62,13 @@ There are 2 ways to run a restore:
* From the menu: `Backup and Restore` > `Restore from backup`
* Running the following command: `bash ./scripts/restore.sh`

**Important**: The restore script assumes that the IOTstack directory is fresh, as if it was just cloned. If it is not fresh, errors may occur, or your data may not correctly be restored even if no errors are apparent.
**Important**:

* The restore script assumes that the IOTstack directory is fresh, as if it was
just cloned. If it is not fresh, errors may occur, or your data may not
correctly be restored even if no errors are apparent.
* When reinstalling Raspberry or migrating to a new installation, use the same
user and ID (usually pi and 1000) as when you created the backup.

*Note*: It is suggested that you test that your backups can be restored after initially setting up, and anytime you add or remove a service. Major updates to services can also break backups.

Expand Down
11 changes: 6 additions & 5 deletions docs/Basic_setup/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ IOTstack makes the following assumptions:
$ sudo apt upgrade -y
```

4. You are logged-in as the user "pi".
5. User "pi" has the user ID 1000.
6. The home directory for user "pi" is `/home/pi/`.
7. IOTstack is installed at `/home/pi/IOTstack` (with that exact spelling).
For brevity, this documentation makes the following assumptions, but these are
not technical requirements. If you use a different configuration, you'll just
have to change the suggested commands correspondingly:

If the first three assumptions hold, assumptions four through six are Raspberry Pi defaults on a clean installation. The seventh is what you get if you follow these instructions faithfully.
- You are logged-in as the user "pi".
- The home directory for user "pi" is `/home/pi/`.
- IOTstack is installed at `/home/pi/IOTstack` (with that exact spelling).

Please don't read these assumptions as saying that IOTstack will not run on other hardware, other operating systems, or as a different user. It is just that IOTstack gets most of its testing under these conditions. The further you get from these implicit assumptions, the more your mileage may vary.

Expand Down
4 changes: 2 additions & 2 deletions docs/Containers/Home-Assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ your RPi hostname is raspberrypi)
cap_add:
- NET_ADMIN
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- TZ=Etc/UTC
- URL=<yourdomain>.duckdns.org
- SUBDOMAINS=wildcard
Expand Down
4 changes: 2 additions & 2 deletions docs/Containers/NextCloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ nextcloud_db:
restart: unless-stopped
environment:
- TZ=Etc/UTC
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- MYSQL_ROOT_PASSWORD=«root_password»
- MYSQL_PASSWORD=«user_password»
- MYSQL_DATABASE=nextcloud
Expand Down
4 changes: 2 additions & 2 deletions docs/Containers/Prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ The IOTstack implementation of *Prometheus* supports two environment variables:

```yaml
environment:
- IOTSTACK_UID=1000
- IOTSTACK_GID=1000
- IOTSTACK_UID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- IOTSTACK_GID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
```
Those variables control ownership of the [Configuration directory](#configDir) and its contents. Those environment variables are present in the standard IOTstack service definition for *Prometheus* and have the effect of assigning ownership to "pi:pi".
Expand Down
10 changes: 5 additions & 5 deletions docs/Containers/Python.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ When you select Python in the menu:
restart: unless-stopped
environment:
- TZ=Etc/UTC
- IOTSTACK_UID=1000
- IOTSTACK_GID=1000
- IOTSTACK_UID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- IOTSTACK_GID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
# ports:
# - "external:internal"
volumes:
Expand All @@ -48,7 +48,7 @@ The service definition contains a number of customisation points:

1. `restart: unless-stopped` assumes your Python script will run in an infinite loop. If your script is intended to run once and terminate, you should remove this directive.
2. `TZ=Etc/UTC` should be set to your local time-zone. Never use quote marks on the right hand side of a `TZ=` variable.
3. If you are running as a different user ID, you may want to change both `IOTSTACK_UID` and `IOTSTACK_GID` to appropriate values.
3. If you need a different user ID, you may want to change both `IOTSTACK_UID` and `IOTSTACK_GID` to appropriate values. By default they are defined to be the same as the current user when you run the menu, and saved to the file `~/IOTstack/.env`, changing this file will affect many services which may not be what you want.

Notes:

Expand Down Expand Up @@ -444,8 +444,8 @@ Proceed like this:
restart: unless-stopped restart: unless-stopped
environment: environment:
- TZ=Etc/UTC - TZ=Etc/UTC
- IOTSTACK_UID=1000 - IOTSTACK_UID=1000
- IOTSTACK_GID=1000 - IOTSTACK_GID=1000
- IOTSTACK_UID=${IOTSTACK_UID:?...} - IOTSTACK_UID=${IOTSTACK_UID:?...}
- IOTSTACK_GID=${IOTSTACK_GID:?...} - IOTSTACK_GID=${IOTSTACK_GID:?...}
# ports: # ports:
# - "external:internal" # - "external:internal"
volumes: volumes:
Expand Down
8 changes: 4 additions & 4 deletions docs/Containers/WireGuard.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ wireguard:
image: ghcr.io/linuxserver/wireguard
restart: unless-stopped
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- TZ=Etc/UTC
- SERVERURL=your.dynamic.dns.name
- SERVERPORT=51820
Expand Down Expand Up @@ -247,8 +247,8 @@ You will need to create the `compose-override.yml` **before** running the menu t
services:
wireguard:
environment:
- PUID=1000
- PGID=1000
- PUID=${IOTSTACK_UID:?IOTSTACK_UID must be defined in ~/IOTstack/.env}
- PGID=${IOTSTACK_GID:?IOTSTACK_GID must be defined in ~/IOTstack/.env}
- TZ=Australia/Sydney
- SERVERURL=downunda.duckdns.org
- SERVERPORT=51820
Expand Down
18 changes: 16 additions & 2 deletions menu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ function do_checks() {
fi
}

function do_dotenv_defaults() {
echo "Checking .env is setting IOTSTACK_UID and IOTSTACK_GID"
grep -qs '^IOTSTACK_UID=' .env || {
echo "Adding missing definitions to .env (using UID:GID=$(id --user):$(id --group))"
echo "# Changing IOTSTACK_UID or IOTSTACK_GID after you have started the stack is not" >> .env
echo "# supported. File owners in the 'volumes'-folder won't automatically update to" >> .env
echo "# match, resulting in various problems." >> .env
echo "IOTSTACK_UID=$(id --user)" >> .env
}
grep -qs '^IOTSTACK_GID=' .env || echo "IOTSTACK_GID=$(id --group)" >> .env
}

function do_help() {
echo "USAGE:
$0 [OPTIONS...]"
Expand All @@ -397,7 +409,7 @@ do
--branch) CURRENT_BRANCH=${2:-$(git name-rev --name-only HEAD)}
;;
--no-check)
NO_CHECK=true
NO_CHECKS=true
echo "Skipping preflight checks."
;;
--run-env-setup) # Sudo cannot be run from inside functions.
Expand Down Expand Up @@ -431,10 +443,12 @@ do
shift
done

if [[ -z "$NO_CHECK" ]]; then
if [[ -z "$NO_CHECKS" ]]; then
do_checks
fi

do_dotenv_defaults

# This section is temporary, it's just for notifying people of potential breaking changes.
if [[ -f .new_install ]]; then
echo "Existing installation detected."
Expand Down
2 changes: 2 additions & 0 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ bash ./scripts/backup_restore/pre_backup_complete.sh >> $LOGFILE 2>&1
echo "./services/" >> $BACKUPLIST
echo "./volumes/" >> $BACKUPLIST
[ -f "./docker-compose.yml" ] && echo "./docker-compose.yml" >> $BACKUPLIST
[ -f "./docker-compose.override.yml" ] && echo "./docker-compose.override.yml" >> $BACKUPLIST
[ -f "./.env" ] && echo "./.env" >> $BACKUPLIST
[ -f "./compose-override.yml" ] && echo "./compose-override.yml" >> $BACKUPLIST
[ -f "./extra" ] && echo "./extra" >> $BACKUPLIST
[ -f "./.tmp/databases_backup" ] && echo "./.tmp/databases_backup" >> $BACKUPLIST
Expand Down

0 comments on commit 2fc86ca

Please sign in to comment.