* Support tasks in the new Upstart formalism.

* Swap task: fixed removing disabled swap devices.
* Swap task: specified the type of swapDevices.

svn path=/nixos/branches/modular-nixos/; revision=16396
This commit is contained in:
Eelco Dolstra 2009-07-16 14:51:49 +00:00
parent 7cb4503ad6
commit 6119c399d8
3 changed files with 126 additions and 68 deletions

View File

@ -236,21 +236,18 @@ in
target = "nix.machines";
};
services.extraJobs = [
jobs = pkgs.lib.singleton
{ name = "nix-daemon";
job = ''
start on startup
stop on shutdown
respawn
script
startOn = "startup";
script =
''
export PATH=${if config.nix.distributedBuilds then "${pkgs.openssh}/bin:" else ""}${pkgs.openssl}/bin:${nix}/bin:$PATH
${config.nix.envVars}
exec ${nix}/bin/nix-worker --daemon > /dev/null 2>&1
end script
'';
}
];
'';
};
environment.shellInit =
''

View File

@ -11,10 +11,19 @@ let
jobText = if job.job != "" then job.job else
''
# Upstart job `${job.name}'. This is a generated file. Do not edit.
description "${job.description}"
${if job.startOn != "" then "start on ${job.startOn}" else ""}
${if job.stopOn != "" then "start on ${job.stopOn}" else ""}
${if isList job.startOn then
# This is a hack to support or-dependencies on Upstart 0.3.
concatMapStrings (x: "start on ${x}\n") job.startOn
else if job.startOn != "" then
"start on ${job.startOn}"
else ""
}
${if job.stopOn != "" then "stop on ${job.stopOn}" else ""}
${concatMapStrings (n: "env ${n}=${getAttr n job.environment}\n") (attrNames job.environment)}
@ -24,15 +33,27 @@ let
end script
'' else ""}
${if true then
# Simulate jobs without a main process (which Upstart 0.3
# doesn't support) using a semi-infinite sleep.
''
exec ${if job.exec != "" then job.exec else "sleep 1e100"}
''
else ""}
${if job.script != "" && job.exec != "" then
abort "Job ${job.name} has both a `script' and `exec' attribute."
else if job.script != "" then
''
script
${job.script}
end script
''
else if job.exec != "" then
''
exec ${job.exec}
''
else
# Simulate jobs without a main process (which Upstart 0.3
# doesn't support) using a semi-infinite sleep.
''
exec sleep 1e100
''
}
${if job.respawn then "respawn" else ""}
${if job.respawn && !job.task then "respawn" else ""}
${if job.postStop != "" then ''
stop script
@ -134,7 +155,8 @@ in
};
startOn = mkOption {
type = types.string;
# !!! Re-enable this once we're on Upstart >= 0.6.
#type = types.string;
default = "";
description = ''
The Upstart event that triggers this job to be started.
@ -179,6 +201,15 @@ in
'';
};
script = mkOption {
type = types.string;
default = "";
description = ''
Shell commands executed as the job's main process. Can be
specified instead of the <varname>exec</varname> attribute.
'';
};
respawn = mkOption {
type = types.bool;
default = true;
@ -188,6 +219,16 @@ in
'';
};
task = mkOption {
type = types.bool;
default = false;
description = ''
Whether this job is a task rather than a service. Tasks
are executed only once, while services are restarted when
they exit.
'';
};
environment = mkOption {
type = types.attrs;
default = {};

View File

@ -2,18 +2,27 @@
let
###### interface
inherit (pkgs) utillinux;
inherit (pkgs.lib) mkOption filter types;
toPath = x: if x.device != null then x.device else "/dev/disk/by-label/${x.label}";
in
{
###### interface
options = {
swapDevices = pkgs.lib.mkOption {
swapDevices = mkOption {
default = [];
example = [
{ device = "/dev/hda7"; }
{ device = "/var/swapfile"; }
{ label = "bigswap"; }
];
description = "
description = ''
The swap devices and swap files. These must have been
initialised using <command>mkswap</command>. Each element
should be an attribute set specifying either the path of the
@ -21,59 +30,70 @@ let
of the swap device (<literal>label</literal>, see
<command>mkswap -L</command>). Using a label is
recommended.
";
'';
type = types.list types.optionSet;
options = {
device = mkOption {
default = null;
example = "/dev/sda3";
type = types.nullOr types.string;
description = ''
Path of the device.
'';
};
label = mkOption {
default = null;
example = "swap";
type = types.nullOr types.string;
description = "
Label of the device. Can be used instead of <varname>device</varname>.
";
};
};
};
};
###### implementation
###### implementation
config = {
inherit (pkgs) utillinux lib;
jobs = pkgs.lib.singleton
{ name = "swap";
swapDevices = config.swapDevices;
task = true;
startOn = ["startup" "new-devices"];
devicesByPath =
map (x: x.device) (lib.filter (x: x ? device) swapDevices);
devicesByLabel =
map (x: x.label) (lib.filter (x: x ? label) swapDevices);
script =
''
swapDevices=${toString (map toPath config.swapDevices)}
for device in $swapDevices; do
${utillinux}/sbin/swapon "$device" || true
done
in
# Remove swap devices not listed in swapDevices.
for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do
found=
for device in $swapDevices; do
device=$(readlink -f $device)
if test "$used" = "$device"; then found=1; fi
done
if test -z "$found"; then
${utillinux}/sbin/swapoff "$used" || true
fi
done
'';
};
};
{
require = [options];
services.extraJobs = [{
name = "swap";
job = ''
start on startup
start on new-devices
script
for device in ${toString devicesByPath}; do
${utillinux}/sbin/swapon "$device" || true
done
for label in ${toString devicesByLabel}; do
${utillinux}/sbin/swapon -L "$label" || true
done
# Remove swap devices not listed in swapDevices.
# !!! disabled because it doesn't work with labels
#for used in $(cat /proc/swaps | grep '^/' | sed 's/ .*//'); do
# found=
# for device in $ {toString swapDevices}; do
# if test "$used" = "$device"; then found=1; fi
# done
# if test -z "$found"; then
# ${utillinux}/sbin/swapoff "$used" || true
# fi
#done
end script
'';
}];
}