rails: New script `railsdo' and rename home symlink to the app

* New script `railsdo' to help run commands as a rails user

  * The symlink in the home directory is now always called `app'.  It
    was previously named after the application itself.
This commit is contained in:
Peter Jones 2019-01-28 16:21:37 -07:00
parent 6c7065945c
commit b537635028
No known key found for this signature in database
GPG key ID: 9DAFAA8D01941E49
4 changed files with 78 additions and 15 deletions

View file

@ -13,6 +13,7 @@ let
options = import ./options.nix { inherit config lib pkgs; };
appSystemd = import ./systemd.nix { inherit config pkgs lib; };
funcs = import ./functions.nix { inherit config; };
scripts = import ./scripts.nix { inherit lib pkgs; };
##############################################################################
# Collect all apps into a single set using the given function:
@ -111,5 +112,10 @@ in
enable = true;
config = concatMapStringsSep "\n" appLogRotation (attrValues cfg.apps);
};
# Additional packages to install in the environment:
environment.systemPackages = [
scripts.system
];
};
}

View file

@ -0,0 +1,37 @@
#!/bin/sh
################################################################################
set -e
set -u
################################################################################
usage() {
cat <<EOF
Usage: $(basename "$0") name command [option...]
Run a command while logged in as the Rails user NAME.
Example:
railsdo myapp rake routes
EOF
}
################################################################################
if [ $# -lt 2 ] || [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
usage
exit 1
fi
################################################################################
name=$1; shift
################################################################################
if ! id -u "rails-$name" > /dev/null 2>&1; then
>&2 echo "ERROR: $name doesn't appear to be a valid rails application"
exit 1
fi
################################################################################
sudo --user="rails-$name" --login sh -c "cd app && $*"

View file

@ -1,7 +1,8 @@
{ lib, pkgs, ...}:
pkgs.stdenvNoCC.mkDerivation {
name = "rails-scripts";
{
user = pkgs.stdenvNoCC.mkDerivation {
name = "rails-user-scripts";
phases = [ "installPhase" "fixupPhase" ];
installPhase = ''
@ -16,4 +17,23 @@ pkgs.stdenvNoCC.mkDerivation {
maintainers = with maintainers; [ pjones ];
platforms = platforms.all;
};
};
system = pkgs.stdenvNoCC.mkDerivation {
name = "rails-system-scripts";
phases = [ "installPhase" "fixupPhase" ];
installPhase = ''
mkdir -p $out/bin
substituteAll ${./railsdo.sh} $out/bin/railsdo
find $out/bin -type f -exec chmod 555 '{}' ';'
'';
meta = with lib; {
description = "Scripts for working with Ruby on Rails applications.";
homepage = https://git.devalot.com/pjones/phoebe/;
maintainers = with maintainers; [ pjones ];
platforms = platforms.all;
};
};
}

View file

@ -61,7 +61,7 @@ let
# Additional set up for the home directory:
mkdir -p ${app.home}/home
ln -nfs ${app.package}/share/${app.name} ${app.home}/home/${app.name}
ln -nfs ${app.package}/share/${app.name} ${app.home}/home/app
ln -nfs ${plib.attrsToShellExports "rails-${app.name}-env" (funcs.appEnv app)} ${app.home}/home/.env
cp ${./profile.sh} ${app.home}/home/.profile
chmod 0700 ${app.home}/home/.profile
@ -79,7 +79,7 @@ let
'' + optionalString (service.isMain && app.database.migrate) ''
# Migrate the database:
${pkgs.sudo}/bin/sudo --user=rails-${app.name} --login \
${scripts}/bin/db-migrate.sh \
${scripts.user}/bin/db-migrate.sh \
-r ${app.package}/share/${app.name} \
-s ${app.home}/state
'';