Introduce LXC container deployment task
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Jan 2014 12:19:00 +0000 (12:19 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Jan 2014 12:19:00 +0000 (12:19 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@6763 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/linux/DeployLxcContainer.java [new file with mode: 0644]

diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/linux/DeployLxcContainer.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/linux/DeployLxcContainer.java
new file mode 100644 (file)
index 0000000..8c20be5
--- /dev/null
@@ -0,0 +1,86 @@
+package org.argeo.slc.lib.linux;
+
+import java.io.File;
+
+import javax.security.auth.callback.CallbackHandler;
+
+import org.argeo.slc.SlcException;
+import org.argeo.slc.core.execution.tasks.SystemCall;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+/** Deploy and initialize an LXC container. */
+public class DeployLxcContainer implements Runnable {
+
+       private String chroot;
+
+       private Resource hostScript = new ClassPathResource(
+                       "/org/argeo/slc/lib/linux/lxc-init-host.sh", getClass()
+                                       .getClassLoader());
+       private Resource guestScript = new ClassPathResource(
+                       "/org/argeo/slc/lib/linux/lxc-init-guest.sh", getClass()
+                                       .getClassLoader());;
+
+       private CallbackHandler callbackHandler;
+
+       private Integer ram = 1024;
+       private Integer vcpu = 2;
+
+       @Override
+       public void run() {
+               if (chroot == null || chroot.trim().equals(""))
+                       throw new SlcException("A chroot directory must be defined");
+
+               File chrootDir = new File(chroot);
+               chrootDir.mkdirs();
+
+               ScriptCall hostCall = new ScriptCall(hostScript);
+               hostCall.setLogCommand(true);
+               hostCall.arg(chroot);
+               // hostCall.getEnvironmentVariables().put("CHROOT", chroot);
+               hostCall.setSudo("");
+               hostCall.setCallbackHandler(callbackHandler);
+               hostCall.run();
+
+               ScriptCall guestCall = new ScriptCall(guestScript);
+               guestCall.setLogCommand(true);
+               guestCall.setSudo("");
+               guestCall.setCallbackHandler(callbackHandler);
+               guestCall.setChroot(chroot);
+               guestCall.run();
+
+               SystemCall virtInstall = new SystemCall(
+                               "virt-install --connect lxc:/// --name " + chrootDir.getName()
+                                               + " --ram " + ram + " --vcpu " + vcpu
+                                               + " --filesystem " + chrootDir.getAbsolutePath()
+                                               + ",/ --noautoconsole");
+               virtInstall.setLogCommand(true);
+               virtInstall.setSudo("");
+               virtInstall.run();
+       }
+
+       public void setChroot(String chroot) {
+               this.chroot = chroot;
+       }
+
+       public void setHostScript(Resource hostScript) {
+               this.hostScript = hostScript;
+       }
+
+       public void setGuestScript(Resource guestScript) {
+               this.guestScript = guestScript;
+       }
+
+       public void setCallbackHandler(CallbackHandler callbackHandler) {
+               this.callbackHandler = callbackHandler;
+       }
+
+       public void setRam(Integer ram) {
+               this.ram = ram;
+       }
+
+       public void setVcpu(Integer vcpu) {
+               this.vcpu = vcpu;
+       }
+
+}