1 { config, pkgs, lib, ... }:
3 cfg = config.services.lifecycled;
5 # TODO: Add the ability to extend this with an rfc 42-like interface.
6 # In the meantime, one can modify the environment (as
7 # long as it's not overriding anything from here) with
8 # systemd.services.lifecycled.serviceConfig.Environment
9 configFile = pkgs.writeText "lifecycled" ''
10 LIFECYCLED_HANDLER=${cfg.handler}
11 ${lib.optionalString (cfg.cloudwatchGroup != null) "LIFECYCLED_CLOUDWATCH_GROUP=${cfg.cloudwatchGroup}"}
12 ${lib.optionalString (cfg.cloudwatchStream != null) "LIFECYCLED_CLOUDWATCH_STREAM=${cfg.cloudwatchStream}"}
13 ${lib.optionalString cfg.debug "LIFECYCLED_DEBUG=${lib.boolToString cfg.debug}"}
14 ${lib.optionalString (cfg.instanceId != null) "LIFECYCLED_INSTANCE_ID=${cfg.instanceId}"}
15 ${lib.optionalString cfg.json "LIFECYCLED_JSON=${lib.boolToString cfg.json}"}
16 ${lib.optionalString cfg.noSpot "LIFECYCLED_NO_SPOT=${lib.boolToString cfg.noSpot}"}
17 ${lib.optionalString (cfg.snsTopic != null) "LIFECYCLED_SNS_TOPIC=${cfg.snsTopic}"}
18 ${lib.optionalString (cfg.awsRegion != null) "AWS_REGION=${cfg.awsRegion}"}
22 meta.maintainers = with lib.maintainers; [ cole-h grahamc ];
25 services.lifecycled = {
26 enable = lib.mkEnableOption "lifecycled, a daemon for responding to AWS AutoScaling Lifecycle Hooks";
29 enable = lib.mkEnableOption "lifecycled-queue-cleaner";
31 frequency = lib.mkOption {
35 How often to trigger the queue cleaner.
37 NOTE: This string should be a valid value for a systemd
38 timer's `OnCalendar` configuration. See
39 {manpage}`systemd.timer(5)`
44 parallel = lib.mkOption {
45 type = lib.types.ints.unsigned;
48 The number of parallel deletes to run.
53 instanceId = lib.mkOption {
54 type = lib.types.nullOr lib.types.str;
57 The instance ID to listen for events for.
61 snsTopic = lib.mkOption {
62 type = lib.types.nullOr lib.types.str;
65 The SNS topic that receives events.
69 noSpot = lib.mkOption {
70 type = lib.types.bool;
73 Disable the spot termination listener.
77 handler = lib.mkOption {
78 type = lib.types.path;
80 The script to invoke to handle events.
85 type = lib.types.bool;
92 cloudwatchGroup = lib.mkOption {
93 type = lib.types.nullOr lib.types.str;
96 Write logs to a specific Cloudwatch Logs group.
100 cloudwatchStream = lib.mkOption {
101 type = lib.types.nullOr lib.types.str;
104 Write logs to a specific Cloudwatch Logs stream. Defaults to the instance ID.
108 debug = lib.mkOption {
109 type = lib.types.bool;
112 Enable debugging information.
116 # XXX: Can be removed if / when
117 # https://github.com/buildkite/lifecycled/pull/91 is merged.
118 awsRegion = lib.mkOption {
119 type = lib.types.nullOr lib.types.str;
122 The region used for accessing AWS services.
128 ### Implementation ###
130 config = lib.mkMerge [
131 (lib.mkIf cfg.enable {
132 environment.etc."lifecycled".source = configFile;
134 systemd.packages = [ pkgs.lifecycled ];
135 systemd.services.lifecycled = {
136 wantedBy = [ "network-online.target" ];
137 restartTriggers = [ configFile ];
141 (lib.mkIf cfg.queueCleaner.enable {
142 systemd.services.lifecycled-queue-cleaner = {
143 description = "Lifecycle Daemon Queue Cleaner";
144 environment = lib.optionalAttrs (cfg.awsRegion != null) { AWS_REGION = cfg.awsRegion; };
147 ExecStart = "${pkgs.lifecycled}/bin/lifecycled-queue-cleaner -parallel ${toString cfg.queueCleaner.parallel}";
151 systemd.timers.lifecycled-queue-cleaner = {
152 description = "Lifecycle Daemon Queue Cleaner Timer";
153 wantedBy = [ "timers.target" ];
154 after = [ "network-online.target" ];
156 Unit = "lifecycled-queue-cleaner.service";
157 OnCalendar = "${cfg.queueCleaner.frequency}";