diff --git a/containerManager/config/mini-test/10.nix b/containerManager/config/mini-test/10.nix new file mode 100644 index 0000000..9fe5c47 --- /dev/null +++ b/containerManager/config/mini-test/10.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/8.nix b/containerManager/config/mini-test/8.nix new file mode 100644 index 0000000..5c3a892 --- /dev/null +++ b/containerManager/config/mini-test/8.nix @@ -0,0 +1,35 @@ +{ 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"; + + }; + + contents = [ pkgsLinux.python3 pkgsLinux.busybox repo ]; +} diff --git a/containerManager/config/mini-test/9.nix b/containerManager/config/mini-test/9.nix new file mode 100644 index 0000000..e6241bf --- /dev/null +++ b/containerManager/config/mini-test/9.nix @@ -0,0 +1,12 @@ +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: + +pkgs.dockerTools.buildLayeredImage { + name = "ping-cloudflare-dns"; + config = { + Cmd = [ "${pkgsLinux.iputils}/bin/ping" "1.1.1.1" ]; + }; + + contents = with pkgsLinux; [ iputils ]; +} diff --git a/containerManager/main.go b/containerManager/main.go index 1a5c51e..ce9a232 100644 --- a/containerManager/main.go +++ b/containerManager/main.go @@ -115,17 +115,19 @@ func BuildContainer(id string, version string) error { func RunContainer(id string, version string) error { log.Println("Ready to run container " + id + " with version " + version) - network, err := cni.New() + network, err := cni.New(cni.WithPluginDir([]string{"/nix/store/84qpsw3nz2zahmz9xxvzbmf8sfdmk771-cni-plugins-1.0.1/bin"})) if err != nil { return err - } + } // See https://github.com/containerd/go-cni/search?q=WithPluginDir - if err := network.Load(cni.WithLoNetwork); err != nil { - return err - } + //if err := network.Load(cni.WithLoNetwork); err != nil { + // return err + //} if err := network.Load(cni.WithConfFile("./containerManager/networking/bridge.json")); err != nil { return err } + // See https://github.com/containernetworking/cni/blob/master/SPEC.md for the format of the config + // Important for nix paths log.Println("Created container network & loaded configuration") @@ -163,9 +165,12 @@ func RunContainer(id string, version string) error { return err } - //defer func(file *os.File) { - // err := file.Close(); if err != nil { panic(err) } - //}(file) + defer func(file *os.File) { + err := file.Close() + if err != nil { + panic(err) + } + }(file) log.Println("Opened container file for reading") @@ -202,18 +207,11 @@ func RunContainer(id string, version string) error { return err } - //defer func(container containerd.Container, ctx context.Context, opts ...containerd.DeleteOpts) { - // if err := container.Delete(ctx, opts...); err != nil { panic(err) } - //}(container, ctx) - - net, err := network.Setup(ctx, id+"-"+timestamp, fmt.Sprintf("/proc/%d/ns/net", os.Getpid())) - if err != nil { - return err - } - // Print out all the interfaces along with their IP addresses - for key, _ := range net.Interfaces { - log.Println(key) - } + defer func(container containerd.Container, ctx context.Context, opts ...containerd.DeleteOpts) { + if err := container.Delete(ctx, opts...); err != nil { + panic(err) + } + }(container, ctx) log.Printf("Successfully loaded %s container\n", container.ID()) @@ -228,13 +226,59 @@ func RunContainer(id string, version string) error { return err } - //defer func(task containerd.Task, ctx context.Context, opts ...containerd.ProcessDeleteOpts) { - // if _, err := task.Delete(ctx, opts...); err != nil { panic(err) } - //}(task, ctx) + defer func(task containerd.Task, ctx context.Context, opts ...containerd.ProcessDeleteOpts) { + if _, err := task.Delete(ctx, opts...); err != nil { + panic(err) + } + }(task, ctx) log.Println("Created run-task") log.Println(task.Metrics(ctx)) + 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) { + 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 + */ + + net, err := network.Setup(ctx, netId, netPath) + if err != nil { + return err + } + + // Print out all the interfaces along with their IP addresses + for key, data := range net.Interfaces { + + var ipText string + var macText string + var sandboxText string + + if len(data.IPConfigs) > 0 { + ipText = "got IP " + data.IPConfigs[0].IP.String() + } else { + ipText = "has no in-container IP" + } + + if data.Mac != "" { + macText = ", MAC address " + data.Mac + } else { + macText = ", no MAC address" + } + + if data.Sandbox != "" { + sandboxText = " and is using path " + data.Sandbox + } else { + sandboxText = " and doesn't have a sandbox" + } + + log.Println("Interface " + key + " " + ipText + macText + sandboxText) + } + + log.Println("Connected the container to networking") + // Run the container! if err := task.Start(ctx); err != nil { return err diff --git a/containerManager/networking/bridge.json b/containerManager/networking/bridge.json index 5a7f4ee..fa5a932 100644 --- a/containerManager/networking/bridge.json +++ b/containerManager/networking/bridge.json @@ -1,5 +1,20 @@ { - "cniVersion": "0.4.4", - "name": "bridge", - "type": "bridge" -} \ No newline at end of file + "cniVersion": "0.3.1", + "name": "clicks-testnet-bridge", + "type": "bridge", + "bridge": "clicks-test0", + "isDefaultGateway": true, + "forceAddress": false, + "ipMasq": true, + "hairpinMode": true, + "ipam": { + "type": "host-local", + "subnet": "10.10.0.0/16" + }, + "dns": { + "nameservers": [ + "1.1.1.1", + "1.0.0.1" + ] + } +}