Skip to content

Commit 7f176c6

Browse files
committed
Adds the autostart feature
Closes #11
1 parent 419dd3d commit 7f176c6

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ rpm: $(APP_OUT)
5050

5151
install: $(APP_OUT)
5252
install $(APP_OUT) $(DESTDIR)/usr/bin
53-
install -g 0 -o 0 -m 0644 lxc-wrapper.1.gz /usr/share/man/man1/
53+
install -g 0 -o 0 -m 0644 dist/root/usr/share/man/man1/lxc-wrapper.1.gz /usr/share/man/man1/
5454

5555
bin:
5656
@mkdir bin

src/cli.lisp

-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ Commands:
9696
(format t "
9797
Overridable variables and default values for all commands (must be BEFORE the command):
9898
--default-shell=/bin/bash
99-
--debug
10099
101100
"))
102101

src/lxc-wrapper.lisp

+19
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@
133133
(format t " done.~%")
134134
(init-lxc cli-name *hosts-file*))))
135135

136+
(defcommand autostart (name args)
137+
"autostart NAME
138+
Toggles the autostart status of the container named NAME
139+
140+
Overridable variables and default values (must be BEFORE the command):
141+
--lxc-default-folder=/var/lib/lxc/
142+
--lxc-config=config"
143+
(declare (ignore args))
144+
(let* ((cli-name (adapt-arg name))
145+
(lxc-path (merge-pathnames *lxc-config*
146+
(merge-pathnames
147+
(concatenate 'string cli-name "/")
148+
*lxc-default-folder*)))
149+
(config-content (alexandria:read-file-into-string
150+
lxc-path)))
151+
(if (lxc-config-has-autostart config-content)
152+
(toggle-autostart-value lxc-path config-content)
153+
(add-autostart-line lxc-path))))
154+
136155
(defun adapt-arg (name)
137156
"Adapts an argument to string"
138157
(when (symbolp name)

src/lxc.lisp

+28
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,31 @@
8181
(cl-ppcre:scan-to-strings "\\n\\s*lxc\\.utsname\\s*=\\s*(\\w+)" config)
8282
(declare (ignore match))
8383
(elt name 0)))
84+
85+
(defun lxc-config-has-autostart (content)
86+
"Finds out if a content holds the autostart line"
87+
(cl-ppcre:scan "lxc\\.start\\.auto" content))
88+
89+
(defun toggle-autostart-value (file content)
90+
"Toggles the autostart value"
91+
(let ((value (parse-integer
92+
(elt
93+
(multiple-value-bind (match val)
94+
(cl-ppcre:scan-to-strings "lxc\\.start\\.auto\\s*=\\s*(\\d+)"
95+
content)
96+
(declare (ignore match))
97+
val)
98+
0))))
99+
(alexandria:write-string-into-file
100+
(cl-ppcre:regex-replace "lxc\\.start\\.auto\\s*=\\s*\\d+"
101+
content
102+
(format nil
103+
"lxc.start.auto = ~A"
104+
(if (= value 0) 1 0)))
105+
file
106+
:if-exists :overwrite)))
107+
108+
(defun add-autostart-line (file)
109+
"Adds the autostart line in the file"
110+
(with-open-file (f file :direction :output :if-exists :append)
111+
(format f "lxc.start.auto = 1")))

0 commit comments

Comments
 (0)