aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/lxc-build
blob: 9e6b5eb13f420497224fee40888bfe73564652f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env runhaskell

import           System.Console.GetOpt
import           System.Environment
import           System.Exit
import           System.Process

version :: Fractional p => p
version = 0.01

help :: [Char]
help = unlines
  [ ""
  , "NAME"
  , ""
  , "   lxc-build"
  , ""
  , "SYNOPSIS"
  , ""
  , "   Replace a container of the same name with the new build"
  , ""
  , "       lxc-build --replace --name rockylinux rockylinux8.dockerfile"
  , ""
  , "DESCRIPTION"
  , ""
  , "   Builds a lxc container from a Dockerfile"
  , ""
  , "COMMANDS"
  , ""
  , "   -h, -help,        --help        Shows this help menu"
  , "   -r, -replace,     --replace     Replace container with new build"
  , "   -v, -version,     --version     Prints program version"
  ]

display = putStrLn

main = do
  arguments <- getArgs
  case arguments of
    ["--replace", "--name", name, dockerfile] -> do
      callCommand ("\\lxc-destroy --force --name " ++ name ++ " |& \\true")
      callCommand
        (  "\\lxc-create --name "
        ++ name
        ++ " --template=none && \\mkdir ~/.local/share/lxc/"
        ++ name
        ++ "/rootfs"
        )
      callCommand ("\\docker build --file " ++ dockerfile)
      callCommand
        (  "id=$(\\docker run --detach \"$(\\docker build --file "
        ++ dockerfile
        ++ " | tail --lines=1)\" /bin/true) && \\docker export \"$id\" | \\tar --extract --directory ~/.local/share/lxc/"
        ++ name
        ++ "/rootfs && \\docker container rm \"$id\""
        )
      callCommand
        (  "\\printf 'doas chown --recursive 200000:200000 ~/.local/share/lxc/"
        ++ name
        ++ "/rootfs\n'"
        )
      callCommand
        (  "\\doas chown --recursive 200000:200000 ~/.local/share/lxc/"
        ++ name
        ++ "/rootfs"
        )
      callCommand
        (  "\\printf 'lxc.uts.name = "
        ++ name
        ++ "\n' >> ~/.local/share/lxc/"
        ++ name
        ++ "/config"
        )
      callCommand
        (  "\\printf \"lxc.rootfs.path = dir:$HOME/.local/share/lxc/"
        ++ name
        ++ "/rootfs\n\" >> ~/.local/share/lxc/"
        ++ name
        ++ "/config"
        )
      callCommand ("\\lxc-start --name " ++ name)
    _ -> do
      die help