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