Refactor triton-docker-env.sh, don't hardcode domains and dcs

This commit is contained in:
jhonas 2022-03-03 00:34:18 +01:00
parent 0bb8a49c78
commit 8ff3f099b4
Signed by: teutat3s
GPG key ID: 924889A86D0B0FEB
8 changed files with 75 additions and 39 deletions

View file

@ -15,7 +15,6 @@
`nix`):
https://nixos.wiki/wiki/Flakes#Installing_flakes
2. Get this nix flake template, it will be setup in a new directory
`./tritonshell` (feel free to adjust this):
```
@ -23,8 +22,13 @@ nix flake new --template "git+https://git.greenbaum.cloud/dev/tritonshell?ref=ma
cd tritonshell
```
3. Adjust `./flake.nix` to match domains and data centers of your Triton Data
Center setup. Look for the `CUSTOMIZE` comment.
```
vim ./flake.nix
```
3. Ready to go:
4. Ready to go:
```
nix develop
```
@ -36,5 +40,4 @@ can do so: just edit `flake.nix` and look for `extraDevshellPkgs`. Packages
added here will be pulled from the `nixos-unstable` channel, you can [search
for available packages here](https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=hello).
TODO: add `nix-direnv`

View file

@ -6,11 +6,11 @@
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1644227066,
"narHash": "sha256-FHcFZtpZEWnUh62xlyY3jfXAXHzJNEDLDzLsJxn+ve0=",
"lastModified": 1646194241,
"narHash": "sha256-ahPiM14XXa2CQwzqHlGJNpplER2HGFx3DfLbBhIeubI=",
"owner": "numtide",
"repo": "devshell",
"rev": "7033f64dd9ef8d9d8644c5030c73913351d2b660",
"rev": "20d50fc6adf77fd8a652fc824c6e282d7737b85d",
"type": "github"
},
"original": {

View file

@ -11,7 +11,24 @@
# Internal utility package with shell function and env vars helper.
# These get source'd in devshell.bash.extra when starting tritonshell
packages.triton-utils = import ./pkgs/triton-utils.nix { inherit nixpkgs; inherit system; };
packages = {
triton-utils = import ./pkgs/triton-utils.nix { inherit nixpkgs system; };
triton-docker-env =
let
# CUSTOMIZE:
# variables used to set triton env vars in tritonshell
# adjust to suit your Triton Data Center setup
# take a look at ./pkgs/triton-docker-env-shell.nix to see how these get used
cnsBaseDomain = "greenbaum.zone";
dataCenters = [ "cgn-1" "lev-1" ];
mantaDomain = "eu-central.manta.greenbaum.cloud";
tritonApiDomain = "api.greenbaum.cloud";
in
import ./pkgs/triton-docker-env.nix {
inherit nixpkgs system cnsBaseDomain dataCenters mantaDomain
tritonApiDomain;
};
};
devShell =
let

View file

@ -0,0 +1,30 @@
{ pkgs, cnsBaseDomain, dataCenters, mantaDomain, tritonApiDomain, ... }:
with pkgs.nodePackages;
''
# script to set the docker, triton, manta and CNS env vars for the current
# triton profile
# set triton and docker host environment variables
eval "$(triton env)"
# get the user's UUID
triton_account_uuid="$(triton account get --json | json id)"
# set the CNS (container name service) base for auto-generated DNS records
# in public and private networks
# note, this makes assumptions that only work if you configured the nix
# variables in flake.nix "CUSTOMIZE" section according to your Triton Data
# Center setup
for dc in ${pkgs.lib.concatStringsSep " " dataCenters}; do
if env | grep -q -E "SDC_URL=https://''${dc}.${tritonApiDomain}"; then
export \
TRITON_CNS_SEARCH_DOMAIN_PUBLIC="''${triton_account_uuid}.''${dc}.${cnsBaseDomain}" \
TRITON_CNS_SEARCH_DOMAIN_PRIVATE="''${triton_account_uuid}.''${dc}.int.${cnsBaseDomain}" \
TRITON_DC=''$dc
fi
done
export MANTA_URL=https://${mantaDomain}
export MANTA_USER=$SDC_ACCOUNT
export MANTA_KEY_ID=$SDC_KEY_ID
''

View file

@ -0,0 +1,11 @@
{ nixpkgs, system, cnsBaseDomain, dataCenters, mantaDomain, tritonApiDomain, ... }:
let
pkgs = import nixpkgs {
inherit system;
};
in
pkgs.writeShellApplication {
name = "triton-docker-env.sh";
runtimeInputs = with pkgs.nodePackages; [ triton json ];
text = import ./triton-docker-env-shell.nix { inherit pkgs cnsBaseDomain dataCenters mantaDomain tritonApiDomain; };
}

View file

@ -6,14 +6,13 @@ let
in
pkgs.stdenv.mkDerivation {
pname = "triton-utils";
version = "0.0.2";
version = "0.0.3";
src = pkgs.lib.cleanSource ./.;
installPhase = ''
mkdir -p $out/bin $out/share/certs
cd ./utils
cp ./cacert-2022-02-01.pem $out/share/certs
cp ./triton-docker.env.sh $out/bin
cp ./ttp.sh $out/bin
cp ./unset-env.sh $out/bin
'';

View file

@ -1,26 +0,0 @@
#!/usr/bin/env bash
# script to set the docker, triton, manta and CNS env vars for the current triton profile
# set triton and docker host environment variables
eval "$(triton env)"
# get the user's UUID
triton_account_uuid="$(triton account get --json | json id)"
# set the CNS (container name service) base for auto-generated DNS records
# in public and private networks
# note, this makes assumptions that only work with Greenbaum's public cloud
if env | grep -q -E "SDC_URL=https://api.greenbaum.cloud|SDC_URL=https://cgn-1.api.greenbaum.cloud"; then
export TRITON_CNS_SEARCH_DOMAIN_PUBLIC="${triton_account_uuid}.cgn-1.greenbaum.zone"
export TRITON_CNS_SEARCH_DOMAIN_PRIVATE="${triton_account_uuid}.cgn-1.int.greenbaum.zone"
export TRITON_DC=cgn-1
fi
if env | grep -q "SDC_URL=https://lev-1.api.greenbaum.cloud"; then
export TRITON_CNS_SEARCH_DOMAIN_PUBLIC="${triton_account_uuid}.lev-1.greenbaum.zone"
export TRITON_CNS_SEARCH_DOMAIN_PRIVATE="${triton_account_uuid}.lev-1.int.greenbaum.zone"
export TRITON_DC=lev-1
fi
export MANTA_URL=https://eu-central.manta.greenbaum.cloud
export MANTA_USER=$SDC_ACCOUNT
export MANTA_KEY_ID=$SDC_KEY_ID

View file

@ -55,10 +55,12 @@ pkgs.devshell.mkShell {
pkgs.bash-completion
# use docker-compose version 1.26.2 for best triton API compatibility
pkgs20-09.docker-compose
# used in ./utils/triton-docker.env.sh
pkgs.nodePackages.json
pkgs.bunyan-rs
# shell scripts and utilities to set and unset triton environment variables
self.packages.${system}.triton-utils
self.packages.${system}.triton-docker-env
# useful for working with JSON data
pkgs.jq
pkgs.bunyan-rs
] ++ extraDevshellPkgs;
env = [
@ -84,7 +86,7 @@ pkgs.devshell.mkShell {
bash = {
extra = ''
source ${self.packages.${system}.triton-utils}/bin/triton-docker.env.sh
source ${self.packages.${system}.triton-docker-env}/bin/triton-docker-env.sh
source ${self.packages.${system}.triton-utils}/bin/ttp.sh
if [ "$(uname)" == "Darwin" ]; then
source $DEVSHELL_DIR/share/bash-completion/bash_completion