
com.github.jjYBdx4IL.utils.vmmgmt.VMData Maven / Gradle / Ivy
/*
* Copyright (C) 2016 jjYBdx4IL (https://github.com/jjYBdx4IL)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jjYBdx4IL.utils.vmmgmt;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freemarker.template.TemplateException;
/**
* @author jjYBdx4IL
*/
public class VMData {
private static final Logger log = LoggerFactory.getLogger(VMData.class);
public static final int SSH_MIN_PORT = 5000;
public static final int SSH_MAX_PORT = 64000;
public static final String VM_NAME_REGEX = "^[a-z][a-z0-9-]*$";
public static final Pattern VM_NAME_PATTERN = Pattern.compile(VM_NAME_REGEX, Pattern.CASE_INSENSITIVE);
private boolean autoDestroy = true;
private File workDir = null;
private OS os = null;
private String name = null;
private int ramMB = 1024;
private DiskImage diskImage = null;
private final List pathMappings = new ArrayList<>();
private int nvcpus = 0;
private int sshForwardPort = 0;
private URI installKernelURI = null;
private URI installInitrdURI = null;
private URI installIsoURI = null;
private VMControl.SSHResources sshResources = null;
private Random r = new Random();
public VMData() {
// do some sensible working directory auto-configuration when running under maven:
String basedir = System.getProperty("basedir");
if (basedir != null && !basedir.isEmpty()) {
workDir = new File(basedir, "target" + File.separator + getClass().getPackage().getName());
}
}
public String getDomainCreateXML(BootMode bootMode) throws IOException, TemplateException {
String xmlFileName = null;
switch (bootMode) {
case INSTALLATION:
xmlFileName = "LibvirtInstallDomainTemplate.xml";
break;
case REGULAR:
xmlFileName = "LibvirtRunDomainTemplate.xml";
break;
}
return XMLWriter.createDomainCreateXML(xmlFileName, getXmlTplData());
}
public File getKernel() {
return new File(getWorkDir(), "kernel");
}
public File getInitRD() {
return new File(getWorkDir(), "initrd");
}
public File getInstallationISO() {
return new File(getWorkDir(), "install.iso");
}
public Map getXmlTplData() {
Map tplData = new HashMap<>();
tplData.put("name", getName());
tplData.put("title", "title");
tplData.put("vmlinuzFile", getKernel().getAbsolutePath());
tplData.put("initrdFile", getInitRD().getAbsolutePath());
tplData.put("cmdline", "ks=file:///ks.cfg");
tplData.put("ram", Integer.toString(getRamMB()));
tplData.put("diskImg", getDiskImage().getImage().getAbsolutePath());
tplData.put("isoImg", getInstallationISO().getAbsolutePath());
tplData.put("sharedFolders", getSharedFolders());
if (getNvcpus() < 1) {
setNvcpus((int) new Integer(Runtime.getRuntime().availableProcessors()));
}
tplData.put("nvcpus", Integer.toString(getNvcpus()));
tplData.put("sshFwdPort", Integer.toString(getSshForwardPort()));
tplData.put("keymap", "");
return tplData;
}
protected List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy