Skip to content

Commit f6cb942

Browse files
committed
Adds the deploy command
1 parent 7b49602 commit f6cb942

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/cli.lisp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
(defvar *ip-regex* "^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)")
2121
(defvar *lxc-interfaces-file* #p"etc/network/interfaces")
2222
(defvar *lxc-package-extension* ".tar.gz")
23+
(defvar *lxc-config* #p"config")
2324

2425
(defmacro defcommand (name args &body body)
2526
`(progn
@@ -57,6 +58,7 @@
5758
*hosts-file*
5859
*lxc-interfaces-file*
5960
*lxc-package-extension*
61+
*lxc-config*
6062
*default-shell*)
6163
(if command
6264
(funcall (gethash (string-upcase command) *commands*) name (cdr parsed-args))

src/lxc-wrapper.lisp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
(defcommand create (name args)
1414
"Creates an LXC"
1515
(destructuring-bind (&key base template)
16-
(car args)
16+
args
1717
(if base
1818
(create-clone base name)
1919
(create-base name template))))
@@ -22,37 +22,45 @@
2222
"Creates a clone of another LXC"
2323
(let ((cli-base (adapt-arg base))
2424
(cli-name (adapt-arg name)))
25+
(format t "Cloning ~A..." cli-base)
2526
(run
2627
"lxc-clone"
2728
"--orig" cli-base
2829
"--new" cli-name)
30+
(format t " done.~%")
2931
(init-lxc cli-name *hosts-file*)))
3032

3133
(defun create-base (name template)
3234
"Creates an LXC from no base"
3335
(let ((cli-name (adapt-arg name))
3436
(cli-template (adapt-arg template)))
37+
(format t "Creating ~A..." cli-name)
3538
(run
3639
"lxc-create"
3740
"--name" cli-name
3841
"-t" cli-template)
42+
(format t " done.~%")
3943
(init-lxc cli-name *hosts-file*)))
4044

4145
(defcommand start (name &rest args)
4246
"Starts an LXC"
4347
(declare (ignore args))
4448
(let ((cli-name (adapt-arg name)))
49+
(format t "Starting ~A..." cli-name)
4550
(run
4651
"lxc-start"
47-
"--name" cli-name)))
52+
"--name" cli-name)
53+
(format t " done.~%")))
4854

4955
(defcommand stop (name &rest args)
5056
"Stops an LXC"
5157
(declare (ignore args))
5258
(let ((cli-name (adapt-arg name)))
59+
(format t "Stopping ~A..." cli-name)
5360
(run
5461
"lxc-stop"
55-
"--name" cli-name)))
62+
"--name" cli-name)
63+
(format t " done.~%")))
5664

5765
(defcommand ls (&rest args)
5866
"Lists all the LXC"
@@ -65,10 +73,14 @@
6573
"Destroys an LXC and its leftovers"
6674
(declare (ignore args))
6775
(let ((cli-name (adapt-arg name)))
76+
(format t "Destroying ~A..." cli-name)
6877
(run
6978
"lxc-destroy"
7079
"--name" cli-name)
71-
(remove-lxc-leftovers cli-name)))
80+
(format t " done.~%")
81+
(format t "Removing leftovers...")
82+
(remove-lxc-leftovers cli-name)
83+
(format t " done.~%")))
7284

7385
(defcommand package (name &rest args)
7486
"Packages an LXC"
@@ -86,6 +98,23 @@
8698
".")
8799
(format t "Created ~A~%" archive)))
88100

101+
(defcommand deploy (name args)
102+
"Deploys an archive created by lxc-wrapper"
103+
(let* ((cli-name (adapt-arg name))
104+
(lxc-path (merge-pathnames (concatenate 'string cli-name "/")
105+
*lxc-default-folder*)))
106+
(destructuring-bind (&key archive)
107+
args
108+
(run
109+
"mkdir" "-p" lxc-path)
110+
(format t "Deploying ~A..." cli-name)
111+
(run
112+
"tar"
113+
"xf" archive
114+
"-C" lxc-path)
115+
(fix-lxc-config cli-name lxc-path *lxc-config*)
116+
(format t " done.~%")
117+
(init-lxc cli-name *hosts-file*))))
89118
(defun adapt-arg (name)
90119
"Adapts an argument to string"
91120
(when (symbolp name)

src/lxc.lisp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
- Adding the static IP to the host's /etc/hosts
77
- Making a symlink to the rootfs somewhere
88
- Making the container directory readable by all"
9+
(format t "Initializing ~A..." name)
910
(let ((ip (next-ip file))
1011
(lxc-path (merge-pathnames (concatenate 'string name "/")
1112
*lxc-default-folder*)))
1213
(assign-static-ip name ip *lxc-gateway* *default-dns-nameserver*)
1314
(add-ip *hosts-file* ip (concatenate 'string name *lxc-host-extension*))
1415
(make-lxc-symlink (merge-pathnames *lxc-rootfs* lxc-path)
1516
(merge-pathnames name *lxc-folder*))
16-
(fix-permissions lxc-path)))
17+
(fix-permissions lxc-path))
18+
(format t " done.~%"))
1719

1820
(defun fix-permissions (path)
1921
"Makes the folder readable by all"
@@ -36,3 +38,22 @@
3638
"Deletes a file if it exists"
3739
(when (probe-file file)
3840
(delete-file file)))
41+
42+
(defun fix-lxc-config (name lxc-path config)
43+
"Fixes the config of a newly deployed container"
44+
(let* ((config-path (merge-pathnames config lxc-path))
45+
(config-string (alexandria:read-file-into-string config-path))
46+
(base-name (get-base-lxc-name config-string)))
47+
(alexandria:write-string-into-file
48+
(cl-ppcre:regex-replace-all base-name
49+
config-string
50+
name)
51+
config-path
52+
:if-exists :overwrite)))
53+
54+
(defun get-base-lxc-name (config)
55+
"Gets the name of the base lxc"
56+
(multiple-value-bind (match name)
57+
(cl-ppcre:scan-to-strings "\\n\\s*lxc\\.utsname\\s*=\\s*(\\w+)" config)
58+
(declare (ignore match))
59+
(elt name 0)))

0 commit comments

Comments
 (0)