Utilities - HVboom/HowTo-DigitalOcean GitHub Wiki

Script apply_skel.sh

Copy standard files from /etc/skel into all Home directories. This will only copy new files or directories, but not override existing ones. The file needs to be executed with root permissions like sudo apply_skel.sh.

```Bash
#!/usr/bin/env bash

# Verzeichnis, aus dem die Dateien und Verzeichnisse kopiert werden sollen
SKEL_DIR="/etc/skel"

# Funktion zum rekursiven Kopieren neuer Dateien und Verzeichnisse
copy_skel_contents() {
  local source_dir=$1
  local target_dir=$2
  local username=$3

  # Durchlaufe alle Dateien und Verzeichnisse im Quellverzeichnis
  for item in "$source_dir"/* "$source_dir"/.*; do
    # Überprüfen, ob das Item existiert
    [ -e "$item" ] || continue

    local basename=$(basename "$item")

    # Ignoriere die Verzeichnisse "." und ".."
    if [ "$basename" != "." ] && [ "$basename" != ".." ]; then
      local target_item="$target_dir/$basename"

      if [ -d "$item" ]; then
        # Wenn das Item ein Verzeichnis ist
        if [ ! -d "$target_item" ]; then
          # Wenn das Zielverzeichnis nicht existiert, kopiere es
          cp -r "$item" "$target_item"
          chown -R "$username:$username" "$target_item"
        else
          # Wenn das Zielverzeichnis existiert, rekursiv die Inhalte kopieren
          copy_skel_contents "$item" "$target_item" "$username"
        fi
      else
        # Wenn das Item eine Datei ist
        if [ ! -e "$target_item" ]; then
          cp "$item" "$target_item"
          chown "$username:$username" "$target_item"
        fi
      fi
    fi
  done
}

# Alle Benutzer durchlaufen
for USER_HOME in /home/*; do
  if [ -d "$USER_HOME" ]; then
    USERNAME=$(basename "$USER_HOME")
    copy_skel_contents "$SKEL_DIR" "$USER_HOME" "$USERNAME"
  fi
done
```

Check permissions

  • Usage: sudo -u <user> /usr/local/share/bin/check_permissions <file>

    e.g. sudo -u www /usr/local/share/bin/check_permissions /home/git/gitlab/config.ru

  • Script /usr/local/share/bin/check_permissions

    #!/usr/bin/env bash
    
    file=$1
    # Handle non-absolute paths
    if ! [[ "$file" == /* ]] ; then
        path=.
    fi
    dirname "$file" | tr '/' $'\n' | while read part ; do
        path="$path/$part"
        # Check for execute permissions
        if ! [[ -x "$path" ]] ; then
            echo "'$path' is blocking access."
        fi
    done
    if ! [[ -r "$file" ]] ; then
        echo "'$file' is not readable."
    fi
  • Usage: cd $HOME/RubyOnRails && create_rails_project <project> [<ruby version | 2.4> <rails version | 5-1-stable>]

    e.g. cd $HOME/RubyOnRails && create_rails_project BuildingIntegrationModeling

  • Script /usr/local/share/bin/create_rails_project

    #!/usr/bin/env bash
    
    # TODO: check parameter & display usage section
    project=$1
    ruby_version='ruby-'${2:-2.4}
    rails_version=${3:-5-1-stable}
    
    project_path=$HOME/RubyOnRails/$project
    gemfile=$project_path/Gemfile
    
    # TODO: check RVM environment - rvm list known - ...
    # source necessary RVM scripts into current shell environment
    source $HOME/.rvm/scripts/rvm
    type rvm | head -n 1 | grep -q "rvm is a function"
    success=$?
    if ! [[ $success ]]
    then
      echo "RVM is not installed - $success"
      exit $success
    fi
    
    # Save an existing project
    # TODO: create save directory path using a timestamp
    if [[ -e $project_path ]]
    then
      saved_project_path=$project_path'.SAVED'
      echo "Project already exists - saved as $saved_project_path."
      mv $project_path $saved_project_path
    fi
    
    # Create Gemfile for edge rails
    mkdir $project_path
    cat <<EOF >$gemfile
    #ruby=$ruby_version
    #ruby-gemset=$project
    #
    source 'https://rubygems.org'
    
    gem 'rails', github: 'rails/rails', branch: '$rails_version'
    EOF
    
    # Install rails from GitHub repository - RVM environment automatically starts the bundler
    cd $project_path
    
    # Create rails project
    # --dev   : using the edge version
    # --force : update the Gemfile
    # --skip-bundle : the Gemfile has to be fixed first
    #    the changed Gemfile does not contain the Gemset definition anymore
    #    the path to the Rails gem is wrong
    bundle exec rails new . --force --skip-bundle --database=mysql
    
    # Ensure to restart the application
    touch $project_path/tmp/restart.txt
⚠️ **GitHub.com Fallback** ⚠️