Browse Source

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.
pjones/monitoring
Peter J. Jones 9 months ago
parent
commit
b537635028
Signed by: Peter Jones <pjones@devalot.com> GPG Key ID: 9DAFAA8D01941E49

+ 6
- 0
modules/services/web/rails/default.nix 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
];
};
}

+ 37
- 0
modules/services/web/rails/railsdo.sh 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 && $*"

+ 33
- 13
modules/services/web/rails/scripts.nix View File

@@ -1,19 +1,39 @@
{ lib, pkgs, ...}:

pkgs.stdenvNoCC.mkDerivation {
name = "rails-scripts";
phases = [ "installPhase" "fixupPhase" ];
{
user = pkgs.stdenvNoCC.mkDerivation {
name = "rails-user-scripts";
phases = [ "installPhase" "fixupPhase" ];

installPhase = ''
mkdir -p $out/bin
substituteAll ${./db-migrate.sh} $out/bin/db-migrate.sh
find $out/bin -type f -exec chmod 555 '{}' ';'
'';
installPhase = ''
mkdir -p $out/bin
substituteAll ${./db-migrate.sh} $out/bin/db-migrate.sh
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;
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;
};
};

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;
};
};
}

+ 2
- 2
modules/services/web/rails/systemd.nix 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
'';

Loading…
Cancel
Save