about summary refs log tree commit diff
path: root/nixpkgs/pkgs/applications/networking/cluster/nomad-autoscaler/default.nix
blob: d32445c6455c36cd102bcfe2a85ebad7344311ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{ lib, fetchFromGitHub, buildGoModule, buildEnv }:

let
  package = buildGoModule rec {
    pname = "nomad-autoscaler";
    version = "0.3.6";

    outputs = [
      "out"
      "bin"
      "aws_asg"
      "azure_vmss"
      "datadog"
      "fixed_value"
      "gce_mig"
      "nomad_apm"
      "nomad_target"
      "pass_through"
      "prometheus"
      "target_value"
      "threshold"
    ];

    src = fetchFromGitHub {
      owner = "hashicorp";
      repo = "nomad-autoscaler";
      rev = "v${version}";
      sha256 = "sha256-fK5GsszNhz/WP0zVk2lOfU/gwYijdQa5qhNYO33RhXc=";
    };

    vendorHash = "sha256-Duzjpl011mj/SNoX/jQGMXwqUHPDz7iIMygRmK1vC3Q=";

    buildPhase = ''
      runHook preBuild
      make build plugins
      runHook postBuild
    '';

    # tries to pull tests from network, and fails silently anyway
    doCheck = false;

    installPhase = ''
      runHook preInstall
      mkdir -p $bin/bin $out/bin
      mv bin/nomad-autoscaler $bin/bin
      ln -s $bin/bin/nomad-autoscaler $out/bin/nomad-autoscaler

      for d in $(getAllOutputNames); do
        mkdir -p ''${!d}/share
      done
      rmdir $bin/share

      # have out contain all of the plugins
      for plugin in bin/plugins/*; do
        cp "$plugin" $out/share/
      done

      # populate the outputs as individual plugins
      # can't think of a more generic way to handle this
      # bash doesn't allow for dashes '-' to be in a variable name
      # this means that the output names will need to differ slightly from the binary
      mv bin/plugins/aws-asg $aws_asg/share/
      mv bin/plugins/azure-vmss $azure_vmss/share/
      mv bin/plugins/datadog $datadog/share/
      mv bin/plugins/fixed-value $fixed_value/share/
      mv bin/plugins/gce-mig $gce_mig/share/
      mv bin/plugins/nomad-apm $nomad_apm/share/
      mv bin/plugins/nomad-target $nomad_target/share/
      mv bin/plugins/pass-through $pass_through/share/
      mv bin/plugins/prometheus $prometheus/share/
      mv bin/plugins/target-value $target_value/share/
      mv bin/plugins/threshold $threshold/share/
      runHook postInstall
    '';

    # make toggle-able, so that overrided versions can disable this check if
    # they want newer versions of the plugins without having to modify
    # the output logic
    doInstallCheck = true;
    installCheckPhase = ''
      rmdir bin/plugins || {
        echo "Not all plugins were extracted"
        echo "Please move the following to their related output: $(ls bin/plugins)"
        exit 1
      }
    '';

    passthru = {
      inherit plugins withPlugins;
    };

    meta = with lib; {
      description = "Autoscaling daemon for Nomad";
      mainProgram = "nomad-autoscaler";
      homepage = "https://github.com/hashicorp/nomad-autoscaler";
      license = licenses.mpl20;
      maintainers = with maintainers; [ jonringer ];
    };
  };

  plugins = let
      plugins = builtins.filter (n: !(lib.elem n [ "out" "bin" ])) package.outputs;
    in lib.genAttrs plugins (output: package.${output});

  # Intended to be used as: (nomad-autoscaler.withPlugins (ps: [ ps.aws_asg ps.nomad_target ])
  withPlugins = f: buildEnv {
    name = "nomad-autoscaler-env";
    paths = [ package.bin ] ++ f plugins;
  };
in
  package