Skip to content

Commit

Permalink
Add work over the holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
Minion3665 committed Feb 21, 2022
1 parent 5309aaf commit eb55ada
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 8 deletions.
22 changes: 22 additions & 0 deletions containerManager/config/busybox/1.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { 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 ];
}
12 changes: 12 additions & 0 deletions containerManager/config/mini-test/11.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { system = "x86_64-linux"; }
}:

pkgs.dockerTools.buildLayeredImage {
name = "ping-google";
config = {
Cmd = [ "${pkgsLinux.iputils}/bin/ping" "google.com" ];
};

contents = with pkgsLinux; [ iputils ];
}
12 changes: 12 additions & 0 deletions containerManager/config/mini-test/12.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { system = "x86_64-linux"; }
}:

pkgs.dockerTools.buildLayeredImage {
name = "ping-google";
config = {
Cmd = [ "${pkgsLinux.iputils}/bin/ping" "google.com" ];
};

contents = with pkgsLinux; [ iputils ];
}
13 changes: 13 additions & 0 deletions containerManager/config/mini-test/13.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { 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 ];
}
12 changes: 12 additions & 0 deletions containerManager/config/mini-test/14.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { 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 ];
}
13 changes: 13 additions & 0 deletions containerManager/config/mini-test/15.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { system = "x86_64-linux"; }
}:

pkgs.dockerTools.buildLayeredImage {
name = "dig-google";
config = {
Cmd = [ "${pkgsLinux.coreutils}/bin/cat" "/etc/resolv.conf" ];

};

contents = with pkgsLinux; [ coreutils ];
}
23 changes: 23 additions & 0 deletions containerManager/config/mini-test/16.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { 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;
}
18 changes: 18 additions & 0 deletions containerManager/config/mini-test/17.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { 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 ];
}
24 changes: 24 additions & 0 deletions containerManager/config/mini-test/18.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { 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 ];
}
45 changes: 45 additions & 0 deletions containerManager/config/mini-test/19.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { 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 ];
}
27 changes: 19 additions & 8 deletions containerManager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
)

Expand All @@ -38,19 +38,29 @@ 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
}
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
})
Expand All @@ -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]
})

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"clicksminuteper.net/process-manager/containerManager"
"clicksminuteper.net/process-manager/restAPI"
"log"
)

Expand All @@ -19,4 +20,6 @@ func main() {
return
}
log.Println("Ran container successfully")

restAPI.Run()
}
11 changes: 11 additions & 0 deletions restAPI/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit eb55ada

Please sign in to comment.