|
22 | 22 | overlays = [ self.overlays.default ];
|
23 | 23 | };
|
24 | 24 | system = "x86_64-linux";
|
25 |
| - terraform = |
26 |
| - pkgs.terraform.withPlugins (p: [ p.null p.external p.libvirt ]); |
27 |
| - terraformConfiguration = terranix.lib.terranixConfiguration { |
28 |
| - inherit system pkgs; |
29 |
| - modules = [ ./provision.nix ]; |
30 |
| - }; |
| 25 | + terraform = pkgs.terraform.withPlugins |
| 26 | + (p: [ p.null p.external p.libvirt p.google p.azurerm ]); |
| 27 | + |
| 28 | + genConfig = env: |
| 29 | + terranix.lib.terranixConfiguration { |
| 30 | + inherit system pkgs; |
| 31 | + modules = [ ./provision ./env/${env}/config.nix ]; |
| 32 | + }; |
| 33 | + |
| 34 | + localConfig = genConfig "local"; |
| 35 | + gcpConfig = genConfig "gcp"; |
31 | 36 | in {
|
32 | 37 | # overlay
|
33 | 38 | overlays = import ./overlays;
|
|
47 | 52 |
|
48 | 53 | # Apps
|
49 | 54 | apps.${system} = {
|
| 55 | + |
50 | 56 | # nix run ".#apply"
|
51 |
| - apply = { |
| 57 | + # defaults to local |
| 58 | + apply = self.apps.${system}.apply-local; |
| 59 | + |
| 60 | + # nix run ".#apply-local" |
| 61 | + apply-local = { |
52 | 62 | type = "app";
|
53 |
| - program = toString (pkgs.writers.writeBash "apply" '' |
54 |
| - set -euo pipefail |
55 |
| - if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi |
56 |
| - cp ${terraformConfiguration} config.tf.json \ |
57 |
| - && ${terraform}/bin/terraform init \ |
58 |
| - && ${terraform}/bin/terraform apply \ |
59 |
| - && ${terraform}/bin/terraform output -json > output.json |
| 63 | + program = toString (pkgs.writers.writeBash "apply-local" '' |
| 64 | + scripts/terranix-apply.sh "local" ${localConfig} |
| 65 | + ''); |
| 66 | + }; |
| 67 | + |
| 68 | + # nix run ".#apply-gcp" |
| 69 | + apply-gcp = { |
| 70 | + type = "app"; |
| 71 | + program = toString (pkgs.writers.writeBash "apply-gcp" '' |
| 72 | + scripts/terranix-apply.sh "gcp" ${gcpConfig} |
60 | 73 | '');
|
61 | 74 | };
|
| 75 | + |
62 | 76 | # nix run ".#local-vault"
|
63 | 77 | local-vault = {
|
64 | 78 | type = "app";
|
65 | 79 | program = toString (pkgs.writers.writeBash "local-vault" ''
|
66 | 80 | set -euo pipefail
|
67 |
| -
|
| 81 | + cd arion/ |
68 | 82 | arion up
|
69 | 83 | '');
|
70 | 84 | };
|
|
74 | 88 | type = "app";
|
75 | 89 | program = toString (pkgs.writers.writeBash "local-k8s" ''
|
76 | 90 | set -euo pipefail
|
77 |
| - scripts/local-k8s.sh |
78 |
| - scripts/configre.sh |
| 91 | + scripts/k8s/local-k8s.sh |
| 92 | + scripts/k8s/configre.sh |
79 | 93 | '');
|
80 | 94 | };
|
81 | 95 |
|
82 | 96 | # nix run ".#destroy"
|
83 |
| - destroy = { |
| 97 | + # defaults to local |
| 98 | + destroy = self.apps.${system}.destroy-local; |
| 99 | + |
| 100 | + # nix run ".#destroy-local" |
| 101 | + destroy-local = { |
84 | 102 | type = "app";
|
85 |
| - program = toString (pkgs.writers.writeBash "destroy" '' |
86 |
| - set -euo pipefail |
87 |
| - if [[ -e config.tf.json ]]; then rm -f config.tf.json; fi |
88 |
| - cp ${terraformConfiguration} config.tf.json \ |
89 |
| - && ${terraform}/bin/terraform init \ |
90 |
| - && ${terraform}/bin/terraform destroy \ |
91 |
| - && rm -f output.json |
| 103 | + program = toString (pkgs.writers.writeBash "destroy-local" '' |
| 104 | + scripts/terranix-destroy.sh "local" ${localConfig} |
| 105 | + ''); |
| 106 | + }; |
| 107 | + |
| 108 | + # nix run ".#destroy-gcp |
| 109 | + destroy-gcp = { |
| 110 | + type = "app"; |
| 111 | + program = toString (pkgs.writers.writeBash "destroy-gcp" '' |
| 112 | + scripts/terranix-destroy.sh "gcp" ${gcpConfig} |
92 | 113 | '');
|
93 | 114 | };
|
| 115 | + |
94 | 116 | # nix run ".#clean-ssh"
|
95 | 117 | clean-ssh = {
|
96 | 118 | type = "app";
|
97 | 119 | program = toString (pkgs.writers.writeBash "clean-ssh" ''
|
98 | 120 | set -euo pipefail
|
99 |
| - for ip in $(${pkgs.jq}/bin/jq -r '.[].value' output.json); do |
| 121 | + for ip in $(${pkgs.jq}/bin/jq -r '.[].value.ip.pub' ./env/*/output.json); do |
100 | 122 | ssh-keygen -R "$ip"
|
101 | 123 | done
|
102 | 124 | '');
|
|
118 | 140 | colmena = let
|
119 | 141 | # read attributes from ouput.json gerated by `nix run .#apply`
|
120 | 142 | inherit (builtins) fromJSON readFile foldl' attrNames;
|
121 |
| - output = fromJSON (readFile ./output.json); |
122 | 143 | keys = import ./keys; # datacenter: {...}
|
| 144 | + genOutput = env: |
| 145 | + let output = fromJSON (readFile ./env/${env}/output.json); |
| 146 | + in foldl' (a: b: a // b) { } (map (name: |
| 147 | + let host = output.${name}.value; |
| 148 | + in { |
| 149 | + # generate hosts by name prefix |
| 150 | + ${name} = { |
| 151 | + deployment = { |
| 152 | + tags = [ env ]; |
| 153 | + targetHost = host.ip.pub; |
| 154 | + keys = import ./keys; |
| 155 | + }; |
| 156 | + }; |
| 157 | + }) (attrNames output)); |
123 | 158 | in {
|
124 | 159 | meta = { nixpkgs = pkgs; };
|
125 | 160 | defaults = import ./deploys/consul;
|
126 |
| - } // foldl' (a: b: a // b) { } (map (name: { |
127 |
| - # generate hosts by name prefix |
128 |
| - ${name} = { |
129 |
| - deployment = { |
130 |
| - targetHost = output.${name}.value; |
131 |
| - keys = import ./keys; |
132 |
| - }; |
133 |
| - }; |
134 |
| - }) (attrNames output)); |
| 161 | + } // (genOutput "local") // (genOutput "gcp"); |
135 | 162 | };
|
136 | 163 | }
|
0 commit comments