Skip to content

Commit

Permalink
16 ability to change steam userid (#18)
Browse files Browse the repository at this point in the history
* #16 - Added PUID and PGID environment variables

* Disabled auto labeler for now

* Fixed timezone issue and updated readme

* Readme changes and version fix

* Removed Labeler
  • Loading branch information
mbround18 authored Feb 9, 2021
1 parent 27a01f7 commit 1d97f18
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 101 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
target/
tmp/
docs/
.github/
15 changes: 0 additions & 15 deletions .github/labeler.yml

This file was deleted.

11 changes: 0 additions & 11 deletions .github/workflows/labeler.yml

This file was deleted.

31 changes: 10 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ RUN apt-get update \
&& apt-get install -y \
htop net-tools nano \
netcat curl wget \
cron
cron sudo

# Set up timezone information
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Copy hello-cron file to the cron.d directory
COPY --chown=steam:steam src/cron/auto-update /etc/cron.d/auto-update
Expand All @@ -46,33 +45,23 @@ RUN chmod 0644 /etc/cron.d/auto-update
# Apply cron job
RUN crontab /etc/cron.d/auto-update

# Setup Directories
RUN usermod -d /home/steam steam \
&& mkdir -p /home/steam/valheim \
&& chown -R steam:steam /home/steam/valheim \
&& mkdir -p /home/steam/scripts \
&& chown -R steam:steam /home/steam/scripts

USER steam

# Server Specific env variables.
ENV NAME "Valheim Docker"
ENV WORLD "Dedicated"
ENV PORT "2456"
ENV PASSWORD ""
ENV AUTO_UPDATE "0"

COPY --from=ScriptSanitize --chown=steam:steam --chmod=755 /data/scripts/*.sh /home/steam/scripts/
COPY --from=RustBuilder --chown=steam:steam --chmod=755 /data/odin/target/release /home/steam/.odin
COPY --from=ScriptSanitize --chmod=755 /data/scripts/*.sh /home/steam/scripts/
COPY --from=ScriptSanitize --chmod=755 /data/scripts/init.sh /init.sh
COPY --from=RustBuilder --chmod=755 /data/odin/target/release /home/steam/.odin

RUN mkdir -p /home/steam/valheim \
&& echo "export PATH=\"/home/steam/.odin:$PATH\"" >> /home/steam/.bashrc \
&& chown -R steam:steam /home/steam/ \
&& chown -R steam:steam /home/steam/valheim \
&& cp /home/steam/steamcmd/linux64/steamclient.so /home/steam/valheim

WORKDIR /home/steam/valheim
#WORKDIR /home/steam/valheim

#RUN wget -O /etc/sudoers.d/sudo-lecture-disable https://raw.githubusercontent.com/Whonix/usability-misc/master/etc/sudoers.d/sudo-lecture-disable?raw=True
ENV PUID=1000
ENV PGID=1000
RUN usermod -u ${PUID} steam \
&& groupmod -g ${PGID} steam

ENTRYPOINT ["/bin/bash", "/home/steam/scripts/entrypoint.sh"]
ENTRYPOINT ["/bin/bash", "/init.sh"]
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

| Variable | Default | Required | Description |
|-------------|------------------------|----------|-------------|
| PUID | `1000` | FALSE | Sets the User Id of the steam user. |
| PGID | `1000` | FALSE | Sets the Group Id of the steam user. |
| NAME | `Valheim Docker` | TRUE | The name of your server! Make it fun and unique! |
| WORLD | `Dedicated` | TRUE | This is used to generate the name of your world. |
| PORT | `2456` | TRUE | Sets the port your server will listen on. Take not it will also listen on +2 (ex: 2456, 2457, 2458) |
| PASSWORD | `12345` | TRUE | Set this to something unique! |
| TZ | `America/Los_Angeles` | FALSE | Sets what timezone your container is running on. This is used for timestamps and cron jobs. [Click Here for which timezones are valid.](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) |
| AUTO_UPDATE | `0` | FALSE | Set to `1` if you want your container to auto update! This means at 1 am it will update, stop, and then restart your server. |


### Docker Compose

```yaml
Expand Down Expand Up @@ -61,49 +64,61 @@ cargo install --git https://github.com/mbround18/valheim-docker.git --branch mai

### Usage

![![Main Menu](https://github.com/mbround18/valheim-docker/blob/main/docs/assets/main-menu.png?raw=true)](./docs/assets/main-menu.png)
![Main Menu](./docs/assets/main-menu.png)

#### Install Valheim

```sh
odin install
```

![Install Menu](./docs/assets/install-menu.png)

#### Start Valheim

```sh
odin start
```

![![start menu](https://github.com/mbround18/valheim-docker/blob/main/docs/assets/start-menu.png?raw=true)](./docs/assets/start-menu.png)
![Start Menu](./docs/assets/start-menu.png)

#### Stop Valheim

```sh
odin stop
```

![Install Menu](./docs/assets/stop-menu.png)

## Versions:

- latest (Stable):
- Readme update to include the versions section and environment variables section.
- Readme update to include the versions section and environment variables section.
- [#18] Changed to `root` as the default user to allow updated steams User+Group IDs.
- [#18] Fixed issue with the timezone not persisting.
- To exec into the container you now have to include the `-u|--user` argument to access steam directly. Example `docker-compose exec --user steam valheim bash`
- There is now a `dry-run` command argument on `odin` to preview what the command would do.
- You can run with `-d|--debug` to get verbose logging of what `odin` is doing.
- 1.1.1 (Stable):
- Includes PR [#10] to fix the double world argument.
- 1.1.0 (Stable):
- Includes fix for [#3] and [#8].
- Includes a fix for [#3] and [#8].
- Improves the script interface and separation of concerns, files now have a respective code file that supports interactions for cleaner development experience.
- Docker image is cleaned up to provide a better usage experience. There is now an `AUTO_UPDATE` feature.
- Has a bug where the script has two entries for the world argument.
- 1.0.0 (Stable):
- It works! It will start your server and stop when you shutdown.
- It works! It will start your server and stop when you shut down.
- This supports passing in environment variables or arguments to `odin`
- Has a bug in which it does not read passed in variables appropriately to Odin. Env variables are not impacted see [#3].

[//]: <> (Github Issues below...........)
[#18]: https://github.com/mbround18/valheim-docker/pull/18
[#10]: https://github.com/mbround18/valheim-docker/pull/10
[#8]: https://github.com/mbround18/valheim-docker/issues/8
[#3]: https://github.com/mbround18/valheim-docker/issues/3


[//]: <> (Links below...................)

[Valheim]: https://www.valheimgame.com/

[//]: <> (Image Base Url: https://github.com/mbround18/valheim-docker/blob/main/docs/assets/name.png?raw=true)
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ version: "3"
services:
valheim:
image: mbround18/valheim:latest
user: 1000:1000
environment:
- PORT=30017
- NAME="Created With Valheim Docker"
- PASSWORD="Banana Phone"
- TZ=America/Chicago
build:
context: .
dockerfile: ./Dockerfile
Expand Down
Binary file added docs/assets/install-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/main-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/start-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/stop-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/cli.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: odin
version: "0.1.0"
version: "stub"
author: mbround18
about: Installs and Runs Valheim
args:
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use log::{SetLoggerError, LevelFilter, debug};
use crate::logger::OdinLogger;
use crate::executable::handle_exit_status;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
static LOGGER: OdinLogger = OdinLogger;
static GAME_ID: i64 = 896660;

Expand All @@ -28,7 +29,10 @@ fn setup_logger(debug: bool) -> Result<(), SetLoggerError> {
fn main() {
// The YAML file is found relative to the current file, similar to how modules are found
let yaml = load_yaml!("cli.yaml");
let matches = App::from(yaml).get_matches();
let app = App::from(yaml)
.version(VERSION);

let matches = app.get_matches();

setup_logger(matches.is_present("debug")).unwrap();

Expand Down
37 changes: 25 additions & 12 deletions src/scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
#!/usr/bin/env bash
cd /home/steam/valheim || exit 1
STEAM_UID=${PUID:=1000}
STEAM_GID=${PGID:=1000}

initialize () {
echo "###########################################################################"
echo "# Valheim Server - $(date)"
echo "# $1"
echo "# STEAM_UID ${UID} - STEAM_GUID ${GID} "
echo "###########################################################################"
echo "
###########################################################################
Valheim Server - $(date)
STEAM_UID ${STEAM_UID} - STEAM_GUID ${STEAM_GID}
$1
###########################################################################
"
}

log () {
echo "[Valheim]: $1"
echo "[Valheim][steam]: $1"
}


initialize "Installing Valheim via Odin"
#
#export TEMP_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
#export LD_LIBRARY_PATH=/home/steam/valheim/linux64:${LD_LIBRARY_PATH}


export SteamAppId=892970
export PATH="/home/steam/.odin:$PATH"


# Setting up server
log "Running Install..."
odin install

log "Herding Cats..."

log "Starting server..."
odin start

cleanup() {
Expand All @@ -34,7 +42,12 @@ cleanup() {

trap cleanup INT TERM EXIT

initialize "Starting Valheim Server..." >> /home/steam/valheim/output.log
initialize "
Valheim Server Started...
Keep an eye out for 'Game server connected' in the log!
(this indicates its online without any errors.)
" >> /home/steam/valheim/output.log
tail -f /home/steam/valheim/output.log

while :; do
Expand Down
33 changes: 0 additions & 33 deletions src/scripts/entrypoint_root.sh

This file was deleted.

54 changes: 54 additions & 0 deletions src/scripts/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

echo "
###########################################################################
Valheim Server - $(date)
Initializing your container...
###########################################################################
"

log () {
echo "[Valheim][root]: $1"
}


# shellcheck disable=SC2039
if [ "${EUID}" -ne 0 ]
then log "Please run as root"
exit
fi

log "Switching UID and GID"
# shellcheck disable=SC2086
usermod -u ${PUID} steam
# shellcheck disable=SC2086
groupmod -g ${PGID} steam


log "Setting up file systems"
STEAM_UID=${PUID:=1000}
STEAM_GID=${PGID:=1000}
mkdir -p /home/steam/valheim
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/valheim
mkdir -p /home/steam/scripts
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/scripts
mkdir -p /home/steam/valheim
echo "export PATH=\"/home/steam/.odin:$PATH\"" >> /home/steam/.bashrc
cp /home/steam/steamcmd/linux64/steamclient.so /home/steam/valheim
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/valheim

# Launch run.sh with user steam (-p allow to keep env variables)
log "Launching as steam..."
cd /home/steam/valheim || exit 1;

su -s /bin/bash --login steam -c "
PORT=${PORT} \
NAME=${NAME} \
WORLD=${WORLD} \
PASSWORD=${PASSWORD} \
/bin/bash /home/steam/scripts/entrypoint.sh
"

0 comments on commit 1d97f18

Please sign in to comment.