Skip to content

Commit

Permalink
Clean up of configurators
Browse files Browse the repository at this point in the history
  • Loading branch information
icetan committed Sep 5, 2019
1 parent cefb41c commit d0c97f4
Show file tree
Hide file tree
Showing 22 changed files with 250 additions and 181 deletions.
9 changes: 7 additions & 2 deletions bin/terranix
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ _init() {
# CLI commands

init() {
mkdir -p .terranix
cat > .terranix/input-transform.nix <<EOF
{ lib, ... }: with builtins; with lib; let
requiredKeys = [ "name" "ip" "authorized_keys" ];
Expand All @@ -197,16 +198,20 @@ EOF
}

initFromJSON() {
[ -f ./terranix-input.json ] || { echo >&2 "Error: couldn't find input JSON at $PWD/terranix-input.json"; return 6; }
[ -f ./terranix-input.json ] \
|| { echo >&2 "Error: couldn't find input JSON at $PWD/terranix-input.json"; return 6; }
mkdir -p .terranix
cat > .terranix/input-transform.nix <<EOF
{ lib, ... }: lib.importJSON ../terranix-input.json;
EOF
_init
}

state() {
[ -f ./.terranix/input-transform.nix ] \
|| { echo >&2 "Error: couldn't find input transform, try running: terranix init"; return 6; }
nix-instantiate --eval-only --strict --json \
-E 'with import <nixpkgs> {};import .terranix/input-transform.nix {inherit lib;}' \
-E 'with import <nixpkgs> {};import ./.terranix/input-transform.nix {inherit lib;}' \
|| { echo >&2 "Error: input transform failed"; return 6; }
}

Expand Down
4 changes: 2 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let
terranix = {
stdenv, makeWrapper, lib, shellcheck,
glibcLocales, coreutils, jq, openssh, nix, git
glibcLocales, coreutils, gzip, gnutar, jq, openssh, nix, git
}: stdenv.mkDerivation rec {
name = "terranix-${version}";
version = "0.0.0";
Expand All @@ -14,7 +14,7 @@ let
buildPhase = "true";
installPhase = let
path = lib.makeBinPath [
coreutils jq openssh nix git
coreutils gzip gnutar jq openssh nix git
];
locales = lib.optionalString (glibcLocales != null)
"--set LOCALE_ARCHIVE \"${glibcLocales}\"/lib/locale/locale-archive";
Expand Down
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Init Terraform plugins and apply plan:

```sh
terraform init
terraform apply -auto-approve
terraform apply
```

Initialize TerraNix using Terraform state and push config to instances:
Expand All @@ -45,5 +45,5 @@ terranix push
Check result by browsing to web server:

```sh
xdg-open http://$(jq -r '."nixos-1".ip' < terranix-state.json)
xdg-open http://$(terranix state | jq -r '."node-1".ip')
```
1 change: 0 additions & 1 deletion examples/aws/aws_nixos/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ resource "aws_key_pair" "terranix" {

resource "aws_instance" "nixos" {
ami = "ami-0022b8ea9efde5de4" #nixos
#ami = "ami-0cddf7994f6cbae36" # ubuntu 16.04
instance_type = "t2.micro"
key_name = "${aws_key_pair.terranix.key_name}"
security_groups = ["${var.sg}"]
Expand Down
6 changes: 3 additions & 3 deletions examples/aws/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ provider "aws" {
region = "eu-central-1"
}

resource "aws_security_group" "nixos" {
name = "allow-all-sg"
resource "aws_security_group" "default" {
name = "allow-ssh-http-sg"

ingress {
cidr_blocks = [ "0.0.0.0/0" ]
Expand All @@ -31,6 +31,6 @@ resource "aws_security_group" "nixos" {
module "aws-1" {
source = "./aws_nixos"
name = "aws-1"
sg = "${aws_security_group.nixos.name}"
sg = "${aws_security_group.default.name}"
authorized_keys = [ "${file("${path.module}/../ssh_key.pub")}" ]
}
25 changes: 1 addition & 24 deletions examples/aws/terranix.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
let
httpServer = content: { pkgs, ... }: {
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.nginx = {
enable = true;
virtualHosts.localhost = {
locations."/" = {
root = pkgs.runCommand "hej" {} ''
mkdir -p $out
printf %s '${content}' > $out/index.html
'';
};
};
};
};
in {
"aws-1" = { config, ... }: {
require = [
(httpServer ''
hej hej Im nixos-1 : ${config.terranix.aws-1.ip}
'')
];
};
}
import ../network-1-node.nix
30 changes: 30 additions & 0 deletions examples/common.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
http-server = content: { pkgs, ... }: {
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.nginx = {
enable = true;
virtualHosts.localhost = {
locations."/" = {
root = pkgs.runCommand "http-server-content" {} ''
mkdir -p $out
cat > $out/index.html <<EOF
<pre>
${content}
</pre>
EOF
'';
};
};
};
};

info = with builtins; cfg:
concatStringsSep "" (map (x: ''
${x.name}
==============
provider: ${x.provider}
ip: ${x.ip}
'') (attrValues cfg)
);
}
25 changes: 1 addition & 24 deletions examples/do/terranix.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
let
httpServer = content: { pkgs, ... }: {
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.nginx = {
enable = true;
virtualHosts.localhost = {
locations."/" = {
root = pkgs.runCommand "hej" {} ''
mkdir -p $out
printf %s '${content}' > $out/index.html
'';
};
};
};
};
in {
"do-1" = { config, ... }: {
require = [
(httpServer ''
hej hej Im nixos-1 : ${config.terranix.do-1.ip}
'')
];
};
}
import ../network-1-node.nix
32 changes: 32 additions & 0 deletions examples/hcloud/hcloud_nixos/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
variable "name" {}
variable "authorized_keys" {
type = "list"
}

resource "hcloud_ssh_key" "default" {
name = "Some SSH key"
public_key = "${var.authorized_keys[0]}"
}

resource "hcloud_server" "ubuntu" {
name = "terranix-${var.name}"
server_type = "cx11"
image = "ubuntu-18.04"
ssh_keys = ["${hcloud_ssh_key.default.id}"]
}

output "provider" {
value = "hcloud"
}

output "name" {
value = "${var.name}"
}

output "ip" {
value = "${hcloud_server.ubuntu.ipv4_address}"
}

output "authorized_keys" {
value = ["${var.authorized_keys[0]}"]
}
11 changes: 11 additions & 0 deletions examples/hcloud/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "hcloud_token" {}

provider "hcloud" {
token = "${var.hcloud_token}"
}

module "node-1" {
source = "./hcloud_nixos"
name = "node-1"
authorized_keys = [ "${file("${path.module}/../ssh_key.pub")}" ]
}
1 change: 1 addition & 0 deletions examples/hcloud/terranix.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ../network-1-node.nix
6 changes: 0 additions & 6 deletions examples/libvirt/libvirt_nixos/conf.nix

This file was deleted.

8 changes: 4 additions & 4 deletions examples/libvirt/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ provider "libvirt" {
uri = "qemu:///system"
}

module "nixos-1" {
module "node-1" {
source = "./libvirt_nixos"
name = "nixos-1"
name = "node-1"
authorized_keys = [ "${file("${path.module}/../ssh_key.pub")}" ]
}

module "nixos-2" {
module "node-2" {
source = "./libvirt_nixos"
name = "nixos-2"
name = "node-2"
authorized_keys = [ "${file("${path.module}/../ssh_key.pub")}" ]
}
27 changes: 1 addition & 26 deletions examples/libvirt/terranix.nix
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
let
# libvirt specific configuration
virtlib = ./libvirt_nixos/conf.nix;

httpServer = content: { pkgs, ... }: {
networking.firewall.allowedTCPPorts = [ 80 443 ];
services.nginx = {
enable = true;
virtualHosts.localhost = {
locations."/" = {
root = pkgs.runCommand "hej" {} ''
mkdir -p $out
printf %s '${content}' > $out/index.html
'';
};
};
};
};
in {
"nixos-1" = { config, ... }: { require = [ virtlib (httpServer ''
hej hej Im nixos-1 : ${config.terranix.nixos-1.ip}
'') ]; };
"nixos-2" = { config, ... }: { require = [ virtlib (httpServer ''
hello my name is nixos-2 and my IP is ${config.terranix.nixos-2.ip}
'') ]; };
}
import ../network-2-nodes.nix
10 changes: 10 additions & 0 deletions examples/network-1-node.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let
common = import ./common.nix;
in {
"node-1" = { config, ... }: {
require = [ (common.http-server ''
hej hej I'm node-1
${common.info config.terranix}
'') ];
};
}
16 changes: 16 additions & 0 deletions examples/network-2-nodes.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let
common = import ./common.nix;
in {
"node-1" = { config, ... }: {
require = [ (common.http-server ''
hej hej I'm node-1
${common.info config.terranix}
'') ];
};
"node-2" = { config, ... }: {
require = [ (common.http-server ''
hello my name is node-2
${common.info config.terranix}
'') ];
};
}
3 changes: 1 addition & 2 deletions examples/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

let
pkgs = (import nixpkgs {});
inherit (pkgs.lib.strings) concatStringsSep;

terranix = import ./.. { inherit pkgs; };
terraform = pkgs.terraform_0_11.withPlugins (p: with p; [
libvirt template digitalocean aws
libvirt template digitalocean aws hcloud
]);
in
pkgs.mkShell rec {
Expand Down
Loading

0 comments on commit d0c97f4

Please sign in to comment.