daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Ordering Services
  2. #
  3. # Given a set of services, make them run one at a time in a specific
  4. # order, on a timer.
  5. let
  6.   lib = import <nixpkgs/lib>;
  7.   servicePrefix = "ordering-test-";
  8.  
  9.   mkService = name: {
  10.     name = "${servicePrefix}${name}";
  11.     value = {
  12.       description = "Ordering test named ${name}";
  13.       script = ''
  14.         sleep 10
  15.       '';
  16.       serviceConfig = {
  17.         Type = "oneshot";
  18.         RemainAfterExit = false;
  19.       };
  20.       unitConfig = {
  21.         After = [];
  22.       };
  23.     };
  24.   };
  25.  
  26.   nameList = [ "a" "b" "c" "d" "e" ];
  27.   services = builtins.map mkService nameList;
  28.  
  29.   # Given a list of systemd service, give each one an After
  30.   # attribute, so they start in a specific order. The returned
  31.   # list can be converted in to a systemd.services attrset with
  32.   # `lib.listToAttrs`.
  33.   #
  34.   # Example:
  35.   #
  36.   #  mkOrderedChain [
  37.   #    { name = "foo"; value = { script = "true"; }; }
  38.   #    { name = "bar"; value = { script = "true"; }; }
  39.   #  ]
  40.   #
  41.   # => [
  42.   #  {
  43.   #    name = "foo";
  44.   #    value = {
  45.   #      script = "true";
  46.   #      unitConfig = { After = []; };
  47.   #    };
  48.   #  }
  49.   #  {
  50.   #    name = "bar";
  51.   #    value = {
  52.   #      script = "true";
  53.   #      unitConfig = { After = [ "bar" ]; };
  54.   #    };
  55.   #  }
  56.   #
  57.   mkOrderedChain = jobs: let
  58.     unitConfigFrom = job: job.unitConfig or {};
  59.     afterFrom = job: (unitConfigFrom job).After or [];
  60.     previousFrom = collector:
  61.       if collector ? previous
  62.       then [collector.previous]
  63.       else [];
  64.  
  65.       ordered = builtins.foldl'
  66.         (collector: item: {
  67.           services = collector.services
  68.           ++ [{
  69.             inherit (item) name;
  70.             value = item.value // {
  71.               unitConfig = (unitConfigFrom item.value) //
  72.               {
  73.                 After = (afterFrom item.value) ++
  74.                   (previousFrom collector);
  75.               };
  76.             };
  77.           }];
  78.           previous = item.name;
  79.         })
  80.         { services = []; }
  81.         jobs;
  82.     in ordered.services;
  83. in {
  84.   systemd.services = (lib.listToAttrs (mkOrderedChain services)) // {
  85.     "${servicePrefix}all" = {
  86.       description = "Start all ordered tests";
  87.       unitConfig = {
  88.         After = builtins.map
  89.           (service: "${service.name}.service")
  90.           services;
  91.         Wants = builtins.map
  92.           (service: "${service.name}.service")
  93.           services;
  94.       };
  95.       script = "true";
  96.     };
  97.   };
  98.  
  99.   systemd.timers."${servicePrefix}all" = {
  100.     description = "Start all ordered tests";
  101.     wantedBy = [ "timers.target" ];
  102.     timerConfig = {
  103.       OnUnitInactiveSec = 30;
  104.       OnBootSec = 900;
  105.       AccuracySec = 10;
  106.       Unit = "${servicePrefix}all.service";
  107.     };
  108.   };
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top