<commit_before>;;;; lxc-wrapper.lisp

(in-package #:lxc-wrapper)

(defvar *lxc-folder* "~/lxc/")

;;; "lxc-wrapper" goes here. Hacks and glory await!

(defmacro run (&body command)
  "Runs a command using sudo. LXC requires sudo.
To avoid having an awkward API (i.e. passing a list),
defining this as a macro."
  (let ((stream (gensym)))
    `(with-output-to-string (,stream)
       (external-program:run
	 "sudo"
	 (list ,@command)
	 :output ,stream
	 :environment '(("$PATH" . "/usr/bin")))
       ,stream)))

(defun init-lxc (name)
  "Initializes the LXC after creating it. It means:
- Giving it a static IP
- Adding the static IP to the host's /etc/hosts
- Making a symlink to the rootfs somewhere")

(defun create (name &key base template)
  "Creates an LXC"
  (if base
      (create-clone base name)
      (create-base name template)))

(defun create-clone (base name)
  "Creates a clone of another LXC"
  (let ((cli-base (adapt-arg base))
	(cli-name (adapt-arg name)))
    (run
      "lxc-clone"
      "--orig" cli-base
      "--new" cli-name)
    (init-lxc cli-name)))

(defun create-base (name template)
  "Creates an LXC from no base"
  (let ((cli-name (adapt-arg name))
	(cli-template (adapt-arg template)))
    (run
      "lxc-create"
      "--name" cli-name<commit_msg>Adds the boilerplate for init-lxc<commit_after>;;;; lxc-wrapper.lisp

(in-package #:lxc-wrapper)

(defvar *lxc-default-folder* "/var/lib/lxc/")
(defvar *lxc-folder* "~/lxc/")
(defvar *hosts-file* "/etc/hosts")
(defvar *lxc-host-extension* ".lxc")

;;; "lxc-wrapper" goes here. Hacks and glory await!

(defmacro run (&body command)
  "Runs a command using sudo. LXC requires sudo.
To avoid having an awkward API (i.e. passing a list),
defining this as a macro."
  (let ((stream (gensym)))
    `(with-output-to-string (,stream)
       (external-program:run
	 "sudo"
	 (list ,@command)
	 :output ,stream
	 :environment '(("$PATH" . "/usr/bin")))
       ,stream)))

(defun init-lxc (name)
  "Initializes the LXC after creating it. It means:
- Giving it a static IP
- Adding the static IP to the host's /etc/hosts
- Making a symlink to the rootfs somewhere"
  (let ((ip (next-ip)))
    (add-ip *hosts-file* ip (concatenate 'string name "." *lxc-host-extension*))
    (make-symlink
     (concatenate 'string *lxc-default-folder* name "/rootfs")
     (concatenate 'string *lxc-folder* name))))

(defun next-ip ()
  "Finds the next available IP for the LXC")

(defun add-ip (file ip extension)
  "Adds the ip:extension pair to the hosts file")

(defun make-symlink (base end)
  "Makes a symlink from end to base"
  (external-program:run
   "ln"
   '("-s" base end)))

(defun create (name &key base template)
  "Creates an LXC"
  (if base
      (create-clone base name)
      (create-base name template)))

(defun create-clone (base name)
  "Creates a clone of another LXC"
  (let ((cli-base (adapt-arg base))
	(cli-name (adapt-arg name)))
    (run
      "lxc-clone"
      "--orig" cli-base
      "--new" cli-name)
    (init-lxc cli-name)))

(defun create-base (name template)
  "Creates an LXC from no base"
  (let ((cli-name (adapt-arg name))
	(cli-template (adapt-arg template)))
    (run
      "lxc-create"
      "--name" cli-name
{'fraction_non_alphanumeric': 0.15497349547864048, 'fraction_numerical': 0.0, 'mean_word_length': 3.16083009079118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2079979', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1163, 'n_words': 371}