Skip to content

Commit f382c5e

Browse files
committed
organize and start gcp module
1 parent e9b7bad commit f382c5e

File tree

15 files changed

+271
-232
lines changed

15 files changed

+271
-232
lines changed

.envrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
watch_file "$(pwd)/flake.{lock,nix}"
2+
watch_file "$(pwd)/devShell.nix"
3+
watch_file "$(pwd)/scripts/k8s/local-k8s.sh"
4+
watch_file "$(pwd)/scripts/*.sh"
5+
watch_dir "$(pwd)/overlays"
6+
watch_dir "$(pwd)/nixpkgs"
7+
watch_dir "$(pwd)/lib"
8+
use flake

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ config.tf.json
44
terraform.*
55
.direnv
66
result
7-
.envrc
87
.tmp-arion-*
98
output.json
10-
env/*/image
9+
images/*

deploys/consul/default.nix

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
let
33
inherit (builtins) attrNames attrValues length match elemAt;
44
inherit (pkgs.lib) concatMapStrings mkIf strings filter assertMsg mkForce;
5-
# inherit (pkgs.lib.asserts) ;
5+
inherit (data) datacenter replica;
6+
variables = {
7+
CONSUL_HTTP_ADDR = "http://127.0.0.1:8500";
8+
VAULT_ADDR = "http://10.0.62.1:8200";
9+
VAULT_TOKEN = "root-token";
10+
};
611
ports = {
712
admin = 19000;
813
mesh = 8443; # gateway
@@ -32,14 +37,8 @@ let
3237
datacenter = elemAt name_match 0;
3338
replica = elemAt name_match 1;
3439
};
35-
variables = {
36-
VAULT_ADDR = "http://10.0.62.1:8200";
37-
VAULT_TOKEN = "root-token";
38-
};
39-
inherit (data) datacenter replica;
4040
in {
41-
imports =
42-
[ ../../generators/minimal-libvirt.nix ./gateway.nix ./templates.nix ];
41+
imports = [ ./gateway.nix ./templates.nix ];
4342
networking.hostName = name;
4443
networking.extraHosts = concatMapStrings (hostName: ''
4544
${config.deployment.targetHost} ${hostName}
@@ -140,8 +139,8 @@ in {
140139
wantedBy = [ "consul.service" ];
141140
path = "/etc/consul.d/encryption.hcl";
142141
script = ''
143-
if !systemctl is-active -q consul; then echo consul is down && return; fi
144-
NEW_KEY=$(cut -f2 -d\" </etc/consul.d/gossip.hcl | sed -e '/^$/d')
142+
if ! systemctl is-active -q consul; then echo consul is down && exit 0; fi
143+
NEW_KEY=$(cut -f2 -d\" </etc/consul.d/encryption.hcl | sed -e '/^$/d')
145144
consul keyring -install "$NEW_KEY"
146145
consul keyring -use "$NEW_KEY"
147146
KEYS=$(curl -s "$CONSUL_HTTP_ADDR/v1/operator/keyring")
@@ -160,7 +159,7 @@ in {
160159
};
161160
pki = rec {
162161
script = ''
163-
if !systemctl is-active -q consul; then echo consul is down && return; fi
162+
if ! systemctl is-active -q consul; then echo consul is down && exit 0; fi
164163
consul reload
165164
'';
166165
wantedBy = [ "consul.service" ];

devShell.nix

+63-38
Original file line numberDiff line numberDiff line change
@@ -3,97 +3,122 @@ with pkgs;
33
let
44
inherit (builtins) readFile;
55
inherit (writers) writeBash;
6+
7+
# Build images
68
build = writeScriptBin "build" ''
7-
env=$1
8-
image=$2
9-
nix build --out-link "./env/$env/image" .#$image
9+
set -eu
10+
image=$1
11+
nix build --out-link "./images/$image" .#$image
1012
# add image to cache
11-
git add -Nf env/$env/image
13+
git add -Nf images/$image
1214
'';
1315
build-qcow = writeScriptBin "build-qcow" ''
14-
build local qcow
16+
build qcow
1517
'';
1618
build-gce = writeScriptBin "build-gce" ''
17-
build gcp gce
19+
build gce
1820
'';
21+
22+
# Apply using terraform
1923
apply = writeScriptBin "apply" ''
20-
# defaults to local
21-
nix run .#apply
24+
env=''${1:-libvirt}
25+
# defaults to libvirt
26+
nix run .#apply-$env
2227
'';
23-
apply-local = writeScriptBin "apply-local" ''
24-
nix run .#apply-local
28+
apply-libvirt = writeScriptBin "apply-libvirt" ''
29+
apply libvirt
2530
'';
2631
apply-gcp = writeScriptBin "apply-gcp" ''
27-
nix run .#apply
32+
apply gcp
2833
'';
34+
35+
# Destroy using terraform
2936
destroy = writeScriptBin "destroy" ''
30-
# defaults to local
31-
nix run .#destroy
37+
env=''${1:-libvirt}
38+
# defaults to libvirt
39+
nix run .#destroy-$env
3240
'';
33-
destroy-local = writeScriptBin "destroy-local" ''
34-
nix run .#destroy-local
41+
destroy-libvirt = writeScriptBin "destroy-libvirt" ''
42+
destroy libvirt
3543
'';
3644
destroy-gcp = writeScriptBin "destroy-gcp" ''
37-
nix run .#destroy-gcp
45+
destroy gcp
3846
'';
47+
48+
# Deploy nix using colmena
3949
deploy = writeScriptBin "deploy" ''
40-
nix run .#deploy
50+
env=''${1:-libvirt}
51+
nix run .#deploy-$env
4152
'';
42-
deploy-local = writeScriptBin "deploy-local" ''
43-
nix run .#deploy-local
53+
deploy-libvirt = writeScriptBin "deploy-libvirt" ''
54+
deploy libvirt
4455
'';
4556
deploy-gcp = writeScriptBin "deploy-gcp" ''
46-
nix run .#deploy-gcp
57+
deploy gcp
4758
'';
59+
60+
# Clean SSH authorized keys
4861
clean-ssh = writeScriptBin "clean-ssh" ''
49-
nix run .#clean-ssh
62+
env=''${1:-"libvirt""}
63+
[[ "$env" == all ]] && ./scripts/clean-ssh.sh
64+
nix run .#clean-ssh-$env
5065
'';
51-
clean-ssh-local = writeScriptBin "clean-ssh-local" ''
52-
nix run .#clean-ssh-local
66+
clean-ssh-libvirt = writeScriptBin "clean-ssh-libvirt" ''
67+
nix run .#clean-ssh-libvirt
5368
'';
5469
clean-ssh-gcp = writeScriptBin "clean-ssh-gcp" ''
5570
nix run .#clean-ssh-gcp
5671
'';
72+
73+
# Up and Running local vault using docker-compose by arion
5774
local-vault = writeScriptBin "local-vault" ''
5875
nix run .#local-vault
5976
'';
77+
78+
# Up and running local k8s using k3d
6079
local-k8s = writeScriptBin "local-k8s" ''
6180
nix run .#local-k8s
6281
'';
63-
terranix-apply =
64-
writeBash "terraform-apply" (readFile ./scripts/terranix-apply.sh);
65-
terranix-destroy =
66-
writeBash "terraform-destroy" (readFile ./scripts/terranix-destroy.sh);
82+
6783
in mkShell {
6884
packages = [
69-
# custom
85+
# build images
7086
build
7187
build-qcow
7288
build-gce
73-
minikube
89+
90+
# provision apply
7491
apply
75-
apply-local
92+
apply-libvirt
7693
apply-gcp
77-
destroy-local
78-
destroy-gcp
94+
95+
# provision destroy
7996
destroy
97+
destroy-libvirt
98+
destroy-gcp
99+
100+
# deploy nix
80101
deploy
81-
deploy-local
102+
deploy-libvirt
82103
deploy-gcp
104+
105+
# clean ssh authorized keys
83106
clean-ssh
84-
clean-ssh-local
107+
clean-ssh-libvirt
85108
clean-ssh-gcp
109+
110+
# start local vault
86111
local-vault
112+
113+
# start local k8s
87114
local-k8s
88-
# terranix
89-
# terranix-apply
90-
# terranix-destroy
115+
91116
# pkgs
92117
consul
93118
consul-template
94119
vault
95120
envoy
96-
terraform
121+
terraformWithPlugins
97122
terranix
98123
kube3d
99124
kubernetes-helm

env/gcp/config.nix

+40-41
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
{ config, lib, pkgs, ... }: {
22
provision.gcp = {
3-
networks = {
4-
n1 = {
5-
mode = "nat";
6-
domain = "n1.local";
7-
addresses = [ "10.0.62.0/24" ];
8-
dhcp.enable = true;
9-
dns.enable = true;
10-
};
11-
};
12-
volumes = {
13-
nixos = { source = ./result/nixos.qcow2; };
14-
c1v1 = { source = "nixos"; };
15-
c1v2 = { source = "nixos"; };
16-
c1v3 = { source = "nixos"; };
17-
c2v1 = { source = "nixos"; };
18-
c2v2 = { source = "nixos"; };
19-
c2v3 = { source = "nixos"; };
20-
};
3+
enable = true;
4+
project = "bornlogic-consul";
215
replicas = {
22-
c1r1 = {
23-
interfaces = { n1.addresses = [ "10.0.62.11" ]; };
24-
disks = [ "c1v1" ];
25-
};
26-
c1r2 = {
27-
interfaces = { n1.addresses = [ "10.0.62.12" ]; };
28-
disks = [ "c1v2" ];
29-
};
30-
c1r3 = {
31-
interfaces = { n1.addresses = [ "10.0.62.13" ]; };
32-
disks = [ "c1v3" ];
33-
};
34-
c2r1 = {
35-
interfaces = { n1.addresses = [ "10.0.62.14" ]; };
36-
disks = [ "c2v1" ];
37-
};
38-
c2r2 = {
39-
interfaces = { n1.addresses = [ "10.0.62.15" ]; };
40-
disks = [ "c2v2" ];
41-
};
42-
c2r3 = {
43-
interfaces = { n1.addresses = [ "10.0.62.16" ]; };
44-
disks = [ "c2v3" ];
6+
test = {
7+
458
};
469
};
10+
# networks = {
11+
# n1 = {
12+
# mode = "nat";
13+
# domain = "n1.local";
14+
# addresses = [ "10.0.62.0/24" ];
15+
# dhcp.enable = true;
16+
# dns.enable = true;
17+
# };
18+
# };
19+
# volumes = {
20+
# nixos = {
21+
# source = let
22+
# inherit (builtins) readDir attrNames head;
23+
# base_dir = ./images/gce;
24+
# # get first file of builded generated folder
25+
# filename = (head (attrNames (readDir base_dir)));
26+
# in base_dir + ./${filename};
27+
# };
28+
# c1v1 = { source = "nixos"; };
29+
# c1v2 = { source = "nixos"; };
30+
# c1v3 = { source = "nixos"; };
31+
# };
32+
# replicas = {
33+
# c1r1 = {
34+
# interfaces = { n1.addresses = [ "10.0.62.11" ]; };
35+
# disks = [ "c1v1" ];
36+
# };
37+
# c1r2 = {
38+
# interfaces = { n1.addresses = [ "10.0.62.12" ]; };
39+
# disks = [ "c1v2" ];
40+
# };
41+
# c1r3 = {
42+
# interfaces = { n1.addresses = [ "10.0.62.13" ]; };
43+
# disks = [ "c1v3" ];
44+
# };
45+
# };
4746
};
4847
}

env/local/config.nix env/libvirt/config.nix

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{ config, lib, pkgs, ... }: {
22
provision.libvirt = {
3+
enable = true;
4+
uri = "qemu:///system";
35
networks = {
46
n1 = {
57
mode = "nat";
@@ -10,7 +12,7 @@
1012
};
1113
};
1214
volumes = {
13-
nixos = { source = image/nixos.qcow2; };
15+
nixos = { source = ../../images/qcow/nixos.qcow2; };
1416
c1v1 = { source = "nixos"; };
1517
c1v2 = { source = "nixos"; };
1618
c1v3 = { source = "nixos"; };

env/libvirt/output.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)