From eb55ada1153655e464ff8a0a046fba935150dff1 Mon Sep 17 00:00:00 2001 From: Skyler Turner Date: Mon, 21 Feb 2022 15:49:41 +0000 Subject: [PATCH] Add work over the holidays --- containerManager/config/busybox/1.nix | 22 ++++++++++++ containerManager/config/mini-test/11.nix | 12 +++++++ containerManager/config/mini-test/12.nix | 12 +++++++ containerManager/config/mini-test/13.nix | 13 +++++++ containerManager/config/mini-test/14.nix | 12 +++++++ containerManager/config/mini-test/15.nix | 13 +++++++ containerManager/config/mini-test/16.nix | 23 ++++++++++++ containerManager/config/mini-test/17.nix | 18 ++++++++++ containerManager/config/mini-test/18.nix | 24 +++++++++++++ containerManager/config/mini-test/19.nix | 45 ++++++++++++++++++++++++ containerManager/main.go | 27 +++++++++----- main.go | 3 ++ restAPI/main.go | 11 ++++++ 13 files changed, 227 insertions(+), 8 deletions(-) create mode 100644 containerManager/config/busybox/1.nix create mode 100644 containerManager/config/mini-test/11.nix create mode 100644 containerManager/config/mini-test/12.nix create mode 100644 containerManager/config/mini-test/13.nix create mode 100644 containerManager/config/mini-test/14.nix create mode 100644 containerManager/config/mini-test/15.nix create mode 100644 containerManager/config/mini-test/16.nix create mode 100644 containerManager/config/mini-test/17.nix create mode 100644 containerManager/config/mini-test/18.nix create mode 100644 containerManager/config/mini-test/19.nix diff --git a/containerManager/config/busybox/1.nix b/containerManager/config/busybox/1.nix new file mode 100644 index 0000000..c4ac814 --- /dev/null +++ b/containerManager/config/busybox/1.nix @@ -0,0 +1,22 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: +pkgs.dockerTools.buildImage { + name = "discord-bot-runner"; + config = { + Cmd = [ "sh" ]; + WorkingDir = "/root"; + }; + + runAsRoot = '' + #!${pkgsLinux.busybox}/bin/sh + mkdir /etc + echo "$(hostname)" > /etc/hostname + echo "127.0.0.1 localhost" >> /etc/hosts + echo "127.0.0.2 $(cat /etc/hostname)" >> /etc/hosts + echo "nameserver 1.1.1.1" >> /etc/resolv.conf + echo "nameserver 1.0.0.1" >> /etc/resolv.conf + ''; + + contents = [ pkgsLinux.busybox ]; +} diff --git a/containerManager/config/mini-test/11.nix b/containerManager/config/mini-test/11.nix new file mode 100644 index 0000000..9fe5c47 --- /dev/null +++ b/containerManager/config/mini-test/11.nix @@ -0,0 +1,12 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "ping-google"; + config = { + Cmd = [ "${pkgsLinux.iputils}/bin/ping" "google.com" ]; + }; + + contents = with pkgsLinux; [ iputils ]; +} diff --git a/containerManager/config/mini-test/12.nix b/containerManager/config/mini-test/12.nix new file mode 100644 index 0000000..9fe5c47 --- /dev/null +++ b/containerManager/config/mini-test/12.nix @@ -0,0 +1,12 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "ping-google"; + config = { + Cmd = [ "${pkgsLinux.iputils}/bin/ping" "google.com" ]; + }; + + contents = with pkgsLinux; [ iputils ]; +} diff --git a/containerManager/config/mini-test/13.nix b/containerManager/config/mini-test/13.nix new file mode 100644 index 0000000..297e315 --- /dev/null +++ b/containerManager/config/mini-test/13.nix @@ -0,0 +1,13 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "ip-route"; + config = { + Cmd = [ "${pkgsLinux.iproute2}/bin/ip" "route" ]; + services.resolved.enable = true; + }; + + contents = with pkgsLinux; [ iproute2 ]; +} diff --git a/containerManager/config/mini-test/14.nix b/containerManager/config/mini-test/14.nix new file mode 100644 index 0000000..2b1c671 --- /dev/null +++ b/containerManager/config/mini-test/14.nix @@ -0,0 +1,12 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "dig-google"; + config = { + Cmd = [ "${pkgsLinux.dig}/bin/dig" "@1.1.1.1" "google.com" ]; + }; + + contents = with pkgsLinux; [ dig ]; +} diff --git a/containerManager/config/mini-test/15.nix b/containerManager/config/mini-test/15.nix new file mode 100644 index 0000000..ce57849 --- /dev/null +++ b/containerManager/config/mini-test/15.nix @@ -0,0 +1,13 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "dig-google"; + config = { + Cmd = [ "${pkgsLinux.coreutils}/bin/cat" "/etc/resolv.conf" ]; + + }; + + contents = with pkgsLinux; [ coreutils ]; +} diff --git a/containerManager/config/mini-test/16.nix b/containerManager/config/mini-test/16.nix new file mode 100644 index 0000000..7c61c2b --- /dev/null +++ b/containerManager/config/mini-test/16.nix @@ -0,0 +1,23 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.ociTools.buildContainer { + args = [ + (with pkgs; + writeScript "run.sh" '' + #!${bash}/bin/bash + exec ${bash}/bin/bash + '').outPath + ]; + + mounts = { + "/data" = { + type = "none"; + source = "/var/lib/mydata"; + options = [ "bind" ]; + }; + }; + + readonly = false; +} \ No newline at end of file diff --git a/containerManager/config/mini-test/17.nix b/containerManager/config/mini-test/17.nix new file mode 100644 index 0000000..44975c7 --- /dev/null +++ b/containerManager/config/mini-test/17.nix @@ -0,0 +1,18 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "ping-1.1.1.1-and-google"; + config = { + Cmd = [ + "sh" + "-c" + '' + ${pkgsLinux.iputils}/bin/ping -c 3 1.1.1.1 && + ${pkgsLinux.iputils}/bin/ping -c 3 google.com + '']; + }; + + contents = with pkgsLinux; [ iputils busybox ]; +} \ No newline at end of file diff --git a/containerManager/config/mini-test/18.nix b/containerManager/config/mini-test/18.nix new file mode 100644 index 0000000..37eefab --- /dev/null +++ b/containerManager/config/mini-test/18.nix @@ -0,0 +1,24 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "ping-1.1.1.1-and-google"; + config = { + Cmd = [ + "sh" + "-c" + '' + echo "$(hostname)" > /etc/hostname && + echo "127.0.0.1 localhost" >> /etc/hosts && + echo "127.0.0.2 $(cat /etc/hostname)" >> /etc/hosts && + echo "nameserver 1.1.1.1" >> /etc/resolv.conf && + echo "nameserver 1.0.0.1" >> /etc/resolv.conf && + ${pkgsLinux.dig}/bin/dig @1.1.1.1 google.com && + ${pkgsLinux.iputils}/bin/ping -c 3 1.1.1.1 && + ${pkgsLinux.iputils}/bin/ping -c 3 google.com + '']; + }; + + contents = with pkgsLinux; [ iputils busybox dig ]; +} \ No newline at end of file diff --git a/containerManager/config/mini-test/19.nix b/containerManager/config/mini-test/19.nix new file mode 100644 index 0000000..b850d0a --- /dev/null +++ b/containerManager/config/mini-test/19.nix @@ -0,0 +1,45 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: +let + repo = pkgsLinux.stdenv.mkDerivation { + name = "repo"; + src = pkgs.fetchFromGitHub { + owner = "Minion3665"; + repo = "container"; + rev = "production"; + sha256 = "sha256-wGvftnTv+79lfnPpKeOSIr44pCqHEW02XVOxGpnXqaM="; + }; + + buildPhase = "echo 'No build phase'"; + installPhase = '' + mkdir $out/src -p + cp $src/* $out/src -r + ''; + }; + DISCORD_TOKEN = (import /home/minion/Private/create-machine-programmers-discord-token.nix {}).token; +in pkgs.dockerTools.buildImage { + name = "discord-bot-runner"; + config = { + Env = [ + "DISCORD_TOKEN=${DISCORD_TOKEN}" + "PATH=${pkgsLinux.busybox}/bin:${pkgsLinux.nodejs-17_x}/bin" + ]; + Entrypoint = [ "${pkgsLinux.nodejs-17_x}/bin/npm" ]; + Cmd = [ "run" "container" ]; + WorkingDir = "${repo}/src"; + + }; + + runAsRoot = '' + #!${pkgsLinux.busybox}/bin/sh + mkdir /etc + echo "$(hostname)" > /etc/hostname + echo "127.0.0.1 localhost" >> /etc/hosts + echo "127.0.0.2 $(cat /etc/hostname)" >> /etc/hosts + echo "nameserver 1.1.1.1" >> /etc/resolv.conf + echo "nameserver 1.0.0.1" >> /etc/resolv.conf + ''; + + contents = [ pkgsLinux.busybox repo ]; +} diff --git a/containerManager/main.go b/containerManager/main.go index ce9a232..5741f67 100644 --- a/containerManager/main.go +++ b/containerManager/main.go @@ -14,9 +14,9 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "sort" "strconv" - "strings" "time" ) @@ -38,10 +38,12 @@ func createContainer(options Options) { } func getLatestConfig(id string) string { - var files []string + var files []int64 // Get all the files in the config directory - root := "./containerManager/config" + root := "./containerManager/config/" + id + fileToIDRegex := regexp.MustCompile(`^(?:.*/)?([0-9]+)\.nix$`) + err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -49,8 +51,16 @@ func getLatestConfig(id string) string { if info.IsDir() { return nil } - if filepath.Ext(path) == ".nix" { - files = append(files, path) + // Find the file name by the following regex: + // ^(?:.*\/)?([0-9]+)\.nix$ + // The group is the config ID, if there is a match the ID should be parsed as an int and appended to the files array + match := fileToIDRegex.FindStringSubmatch(path) + if match != nil { + id, err := strconv.ParseInt(match[1], 10, 64) + if err != nil { + return err + } + files = append(files, id) } return nil }) @@ -60,6 +70,7 @@ func getLatestConfig(id string) string { // Sort the files by name sort.Slice(files, func(i, j int) bool { + // Find the file's name return files[i] < files[j] }) @@ -68,7 +79,7 @@ func getLatestConfig(id string) string { // Return the latest file // We return without the extension, as we will add it later - return strings.TrimSuffix(filepath.Base(latest), filepath.Ext(latest)) + return strconv.FormatInt(latest, 10) } func GetVersion(id string, version string) string { @@ -238,11 +249,11 @@ func RunContainer(id string, version string) error { netPath := fmt.Sprintf("/proc/%d/ns/net", task.Pid()) netId := id + "-" + timestamp - /*defer func(network cni.CNI, ctx context.Context, id string, path string, opts ...cni.NamespaceOpts) { + defer func(network cni.CNI, ctx context.Context, id string, path string, opts ...cni.NamespaceOpts) { if err := network.Remove(ctx, id, path, opts...); err != nil { panic(err) } }(network, ctx, netId, netPath) // This isn't needed, as the container is deleted when the task is deleted - */ + // The above comment is a lie net, err := network.Setup(ctx, netId, netPath) if err != nil { diff --git a/main.go b/main.go index 04f58b0..6c4d36b 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "clicksminuteper.net/process-manager/containerManager" + "clicksminuteper.net/process-manager/restAPI" "log" ) @@ -19,4 +20,6 @@ func main() { return } log.Println("Ran container successfully") + + restAPI.Run() } diff --git a/restAPI/main.go b/restAPI/main.go index 846b7e3..317d243 100644 --- a/restAPI/main.go +++ b/restAPI/main.go @@ -46,6 +46,17 @@ func BuildContainer(c *gin.Context) { c.JSON(200, containerBuildResponse{ID: 0}) } +func BuildContainerFromNixFile(c *gin.Context) { + // Log the request + log.Println("BuildContainer called with: " + c.Request.URL.String()) + + uid := "1234" + name := "test" + containerManager.BuildContainer(uid + ":" + name, "@latest") + + c.JSON(200, containerBuildResponse{ID: 0}) +} + func StartContainer(c *gin.Context) { // Log the request log.Println("StartContainer called with: " + c.Request.URL.String())