]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/linux/DeployLxcContainer.java
8c20be5f981dc79cdd54753cff6384cde6e9ff00
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / lib / linux / DeployLxcContainer.java
1 package org.argeo.slc.lib.linux;
2
3 import java.io.File;
4
5 import javax.security.auth.callback.CallbackHandler;
6
7 import org.argeo.slc.SlcException;
8 import org.argeo.slc.core.execution.tasks.SystemCall;
9 import org.springframework.core.io.ClassPathResource;
10 import org.springframework.core.io.Resource;
11
12 /** Deploy and initialize an LXC container. */
13 public class DeployLxcContainer implements Runnable {
14
15 private String chroot;
16
17 private Resource hostScript = new ClassPathResource(
18 "/org/argeo/slc/lib/linux/lxc-init-host.sh", getClass()
19 .getClassLoader());
20 private Resource guestScript = new ClassPathResource(
21 "/org/argeo/slc/lib/linux/lxc-init-guest.sh", getClass()
22 .getClassLoader());;
23
24 private CallbackHandler callbackHandler;
25
26 private Integer ram = 1024;
27 private Integer vcpu = 2;
28
29 @Override
30 public void run() {
31 if (chroot == null || chroot.trim().equals(""))
32 throw new SlcException("A chroot directory must be defined");
33
34 File chrootDir = new File(chroot);
35 chrootDir.mkdirs();
36
37 ScriptCall hostCall = new ScriptCall(hostScript);
38 hostCall.setLogCommand(true);
39 hostCall.arg(chroot);
40 // hostCall.getEnvironmentVariables().put("CHROOT", chroot);
41 hostCall.setSudo("");
42 hostCall.setCallbackHandler(callbackHandler);
43 hostCall.run();
44
45 ScriptCall guestCall = new ScriptCall(guestScript);
46 guestCall.setLogCommand(true);
47 guestCall.setSudo("");
48 guestCall.setCallbackHandler(callbackHandler);
49 guestCall.setChroot(chroot);
50 guestCall.run();
51
52 SystemCall virtInstall = new SystemCall(
53 "virt-install --connect lxc:/// --name " + chrootDir.getName()
54 + " --ram " + ram + " --vcpu " + vcpu
55 + " --filesystem " + chrootDir.getAbsolutePath()
56 + ",/ --noautoconsole");
57 virtInstall.setLogCommand(true);
58 virtInstall.setSudo("");
59 virtInstall.run();
60 }
61
62 public void setChroot(String chroot) {
63 this.chroot = chroot;
64 }
65
66 public void setHostScript(Resource hostScript) {
67 this.hostScript = hostScript;
68 }
69
70 public void setGuestScript(Resource guestScript) {
71 this.guestScript = guestScript;
72 }
73
74 public void setCallbackHandler(CallbackHandler callbackHandler) {
75 this.callbackHandler = callbackHandler;
76 }
77
78 public void setRam(Integer ram) {
79 this.ram = ram;
80 }
81
82 public void setVcpu(Integer vcpu) {
83 this.vcpu = vcpu;
84 }
85
86 }