|
| 1 | + |
| 2 | +# this script runs both, in dev and in prod, so we have to check where we |
| 3 | +# can source common.sh from. |
| 4 | +if [ -f ./common.sh ]; then |
| 5 | + source ./common.sh |
| 6 | +elif [ -f ./docker/entrypoints/common.sh ]; then |
| 7 | + source ./docker/entrypoints/common.sh |
| 8 | +fi |
| 9 | + |
| 10 | + |
| 11 | +# ---------------------------------- Helper ---------------------------------- # |
| 12 | + |
| 13 | +validate_and_add_groups() { |
| 14 | + local group_specs |
| 15 | + IFS=',' read -ra group_specs <<< "$1" |
| 16 | + |
| 17 | + for spec in "${group_specs[@]}"; do |
| 18 | + if [[ "$spec" =~ ^([a-z][a-z0-9_-]*):([0-9]+)$ ]]; then |
| 19 | + local group_name="${BASH_REMATCH[1]}" |
| 20 | + local gid="${BASH_REMATCH[2]}" |
| 21 | + |
| 22 | + process_group "$group_name" "$gid" || continue |
| 23 | + else |
| 24 | + log_warning "Invalid group specification '$spec', skipping. Format should be 'group_name:gid' where group_name starts with lowercase letter" |
| 25 | + fi |
| 26 | + done |
| 27 | +} |
| 28 | + |
| 29 | +process_group() { |
| 30 | + local group_name="$1" |
| 31 | + local gid="$2" |
| 32 | + |
| 33 | + # Handle existing group |
| 34 | + if existing_group=$(getent group "$group_name" 2>/dev/null); then |
| 35 | + local existing_gid=$(cut -d: -f3 <<< "$existing_group") |
| 36 | + [[ "$existing_gid" != "$gid" ]] && \ |
| 37 | + log_warning "Group '$group_name' exists with GID $existing_gid (expected $gid). Using existing group." |
| 38 | + log "Group '$group_name' already exists, skipping creation" |
| 39 | + else |
| 40 | + addgroup -g "$gid" "$group_name" 2>/dev/null || { |
| 41 | + log_warning "Failed to create group '$group_name' with gid $gid" |
| 42 | + return 1 |
| 43 | + } |
| 44 | + log "Created group '$group_name' with gid $gid" |
| 45 | + fi |
| 46 | + |
| 47 | + # Add user to group |
| 48 | + if id -nG beetle 2>/dev/null | grep -qw "$group_name"; then |
| 49 | + log "User beetle is already a member of group '$group_name'" |
| 50 | + else |
| 51 | + adduser beetle "$group_name" 2>/dev/null || { |
| 52 | + log_warning "Failed to add beetle user to group '$group_name'" |
| 53 | + return 1 |
| 54 | + } |
| 55 | + log "Added beetle user to group '$group_name'" |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +# --------------------------------- Main Loop -------------------------------- # |
| 60 | + |
| 61 | +if [[ -n "$EXTRA_GROUPS" ]]; then |
| 62 | + log "Adding extra groups to beetle user: $EXTRA_GROUPS" |
| 63 | + validate_and_add_groups "$EXTRA_GROUPS" |
| 64 | +fi |
| 65 | + |
0 commit comments