use of com.emc.storageos.networkcontroller.SSHSession in project coprhd-controller by CoprHD.
the class ImageServerControllerImpl method verifyImageServer.
/**
* The following is expected to exist on the image server:
* TFTPBOOT directory
* pxelinux.0 binary
* python
* Everything else if doesn't exist, will be pushed.
*/
private boolean verifyImageServer(ComputeImageServer imageServer) {
log.info("verifyImageServer: {}", imageServer.getImageServerIp());
boolean imageServerVerified = false;
if (!isImageServerValid(imageServer)) {
imageServerErrorMsg = "Image server settings are not valid, can't verify the server";
log.warn(imageServerErrorMsg);
return imageServerVerified;
}
ImageServerDialog d = null;
try {
SSHSession session = new SSHSession();
session.connect(imageServer.getImageServerIp(), imageServer.getSshPort(), imageServer.getImageServerUser(), imageServer.getImageServerPassword());
d = new ImageServerDialog(session, imageServer.getSshTimeoutMs());
d.init();
if (!d.directoryExists(imageServer.getTftpBootDir())) {
throw ImageServerControllerException.exceptions.imageServerNotSetup("tftpboot directory does not exist");
}
if (!d.fileExists(imageServer.getTftpBootDir() + PXELINUX_0_FILE)) {
throw ImageServerControllerException.exceptions.imageServerNotSetup("pxelinux.0 binary does not exist");
}
if (!d.fileExists("/usr/bin/python")) {
throw ImageServerControllerException.exceptions.imageServerNotSetup("python not found");
}
// check is.properties file if upgrade is needed
// perform upgrade if file is not there, or version property is not
// found or not valid or less then IMAGE_SERVER_VERSION
boolean upgradeRequired = false;
if (!d.fileExists(imageServer.getTftpBootDir() + "is.properties")) {
upgradeRequired = true;
} else {
String s = d.readFile(imageServer.getTftpBootDir() + "is.properties");
Properties p = ImageServerUtils.stringToProperties(s);
if (p.getProperty("version") == null) {
upgradeRequired = true;
} else {
try {
int version = Integer.parseInt(p.getProperty("version"));
if (version < IMAGE_SERVER_VERSION) {
upgradeRequired = true;
}
} catch (NumberFormatException e) {
upgradeRequired = true;
}
}
}
log.info("image server upgrade required: {}", upgradeRequired);
if (!d.directoryExists(imageServer.getTftpBootDir() + PXELINUX_CFG_DIR)) {
log.info("pxelinux.cfg does not exist, will create it");
d.mkdir(imageServer.getTftpBootDir() + PXELINUX_CFG_DIR);
}
if (!StringUtils.isBlank(imageServer.getImageDir()) && !d.directoryExists(imageServer.getTftpBootDir() + imageServer.getImageDir())) {
log.info("image directory does not exist, will create it");
d.mkdir(imageServer.getTftpBootDir() + imageServer.getImageDir());
}
if (upgradeRequired || !d.fileExists(imageServer.getTftpBootDir() + PXELINUX_CFG_DIR + DEFAULT_FILE)) {
log.info("creating pxelinux.cfg/default");
String content = ImageServerUtils.getResourceAsString("imageserver/default");
d.writeFile(imageServer.getTftpBootDir() + PXELINUX_CFG_DIR + DEFAULT_FILE, content);
}
if (!d.directoryExists(imageServer.getTftpBootDir() + HTTP_DIR)) {
log.info("http does not exist, will create it");
d.mkdir(imageServer.getTftpBootDir() + HTTP_DIR);
}
if (upgradeRequired || !d.fileExists(imageServer.getTftpBootDir() + HTTP_DIR + SERVER_PY_FILE)) {
log.info("creating server.py");
String content = ImageServerUtils.getResourceAsString("imageserver/server.py");
StringBuilder script = new StringBuilder(content);
ImageServerUtils.replaceAll(script, "{http.port}", imageServer.getImageServerHttpPort());
d.writeFile(imageServer.getTftpBootDir() + HTTP_DIR + SERVER_PY_FILE, script.toString());
d.chmodFile("744", imageServer.getTftpBootDir() + HTTP_DIR + SERVER_PY_FILE);
}
String pid = d.getServerPid(imageServer.getImageServerHttpPort());
if (upgradeRequired && pid != null) {
// if update required and server is running, kill it
log.info("{} is running as pid: {}, kill it", SERVER_PY_FILE, pid);
d.kill(pid);
pid = null;
}
if (pid == null) {
log.info("{} is not running, will attempt to start it", SERVER_PY_FILE);
d.cd(imageServer.getTftpBootDir() + HTTP_DIR);
d.nohup(String.format("python %s", SERVER_PY_FILE));
}
if (upgradeRequired || !d.fileExists(imageServer.getTftpBootDir() + HTTP_DIR + WGET_FILE)) {
log.info("creating wget wrapper script");
String content = ImageServerUtils.getResourceAsString("imageserver/wget");
d.writeFile(imageServer.getTftpBootDir() + HTTP_DIR + WGET_FILE, content);
}
if (!d.directoryExists(imageServer.getTftpBootDir() + HTTP_KICKSTART_DIR)) {
log.info("http/ks does not exist, will create it");
d.mkdir(imageServer.getTftpBootDir() + HTTP_KICKSTART_DIR);
}
if (!d.directoryExists(imageServer.getTftpBootDir() + HTTP_FIRSTBOOT_DIR)) {
log.info("http/fb does not exist, will create it");
d.mkdir(imageServer.getTftpBootDir() + HTTP_FIRSTBOOT_DIR);
}
if (!d.directoryExists(imageServer.getTftpBootDir() + HTTP_SUCCESS_DIR)) {
log.info("http/success does not exist, will create it");
d.mkdir(imageServer.getTftpBootDir() + HTTP_SUCCESS_DIR);
}
if (!d.directoryExists(imageServer.getTftpBootDir() + HTTP_FAILURE_DIR)) {
log.info("http/failure does not exist, will create it");
d.mkdir(imageServer.getTftpBootDir() + HTTP_FAILURE_DIR);
}
// save is.properties
if (upgradeRequired) {
log.info("saving is.properties");
d.writeFile(imageServer.getTftpBootDir() + "is.properties", "version=" + IMAGE_SERVER_VERSION + "\nhttp_port=" + imageServer.getImageServerHttpPort());
}
log.info("image server setup was successfully verified");
imageServerVerified = true;
imageServerErrorMsg = null;
} catch (Exception e) {
log.error("Unexpected exception during image server verification: " + e.getMessage(), e);
imageServerErrorMsg = e.getMessage();
} finally {
if (d != null && d.isConnected()) {
d.close();
}
}
return imageServerVerified;
}
use of com.emc.storageos.networkcontroller.SSHSession in project coprhd-controller by CoprHD.
the class ImageServerControllerImpl method importImageMethod.
/**
* Method to import an image
*
* @param ciId {@link URI} computeImage URI
* @param imageServer {@link ComputeImageServer} imageServer instance
* @param opName operation Name
* @param stepId {@link String} step Id
*/
public void importImageMethod(URI ciId, ComputeImageServer imageServer, String opName, String stepId) {
log.info("importImageMethod importing image {} on to imageServer {}", ciId, imageServer.getId());
ImageServerDialog d = null;
ComputeImage ci = null;
try {
WorkflowStepCompleter.stepExecuting(stepId);
ci = dbClient.queryObject(ComputeImage.class, ciId);
SSHSession session = new SSHSession();
session.connect(imageServer.getImageServerIp(), imageServer.getSshPort(), imageServer.getImageServerUser(), imageServer.getImageServerPassword());
d = new ImageServerDialog(session, imageServer.getSshTimeoutMs());
importImage(imageServer, ci, d);
WorkflowStepCompleter.stepSucceded(stepId);
} catch (InternalException e) {
log.error("Exception importing image: " + e.getMessage(), e);
updateFailedImages(imageServer.getId(), ci);
WorkflowStepCompleter.stepFailed(stepId, e);
} catch (Exception e) {
log.error("Unexpected exception importing image: " + e.getMessage(), e);
String operationName = opName;
if (null == operationName) {
operationName = ResourceOperationTypeEnum.IMPORT_IMAGE.getName();
}
updateFailedImages(imageServer.getId(), ci);
WorkflowStepCompleter.stepFailed(stepId, ImageServerControllerException.exceptions.unexpectedException(operationName, e));
} finally {
try {
if (d != null && d.isConnected()) {
d.close();
}
} catch (Exception e) {
log.error(FAILED_TO_CLOSE_STR, e);
}
}
}
use of com.emc.storageos.networkcontroller.SSHSession in project coprhd-controller by CoprHD.
the class ImageServerControllerImpl method preOsInstallImageServerCheck.
/**
* Performs preOs install check on the imageServer
* @param jobId {@link URI} job id
* @param stepId {@link String} step id
*/
public void preOsInstallImageServerCheck(URI jobId, String stepId) {
log.info("preOsInstallImageServerCheck {} ", jobId);
ImageServerDialog d = null;
try {
WorkflowStepCompleter.stepExecuting(stepId);
ComputeImageJob job = dbClient.queryObject(ComputeImageJob.class, jobId);
ComputeImage img = dbClient.queryObject(ComputeImage.class, job.getComputeImageId());
ComputeImageServer imageServer = dbClient.queryObject(ComputeImageServer.class, job.getComputeImageServerId());
SSHSession session = new SSHSession();
session.connect(imageServer.getImageServerIp(), imageServer.getSshPort(), imageServer.getImageServerUser(), imageServer.getImageServerPassword());
d = new ImageServerDialog(session, imageServer.getSshTimeoutMs());
d.init();
log.info("connected to image server");
log.info("verify the image is still there");
if (!d.directoryExists(imageServer.getTftpBootDir() + img.getPathToDirectory())) {
log.error("the image is missing");
throw ImageServerControllerException.exceptions.computeImageIsMissing(img.getPathToDirectory());
}
String pid = d.getServerPid("67");
if (pid == null) {
// dhcp down
throw ImageServerControllerException.exceptions.dhcpServerNotRunning();
}
pid = d.getServerPid("69");
if (pid == null) {
// tftp down
throw ImageServerControllerException.exceptions.tftpServerNotRunning();
}
log.info("make sure the python server is running");
pid = d.getServerPid(imageServer.getImageServerHttpPort());
if (pid == null) {
log.warn("python server is not running, attempt to start it");
d.cd(imageServer.getTftpBootDir() + HTTP_DIR);
d.nohup(String.format("python %s", SERVER_PY_FILE));
pid = d.getServerPid(imageServer.getImageServerHttpPort());
if (pid == null) {
throw ImageServerControllerException.exceptions.httpPythonServerNotRunning();
}
}
WorkflowStepCompleter.stepSucceded(stepId);
} catch (InternalException e) {
log.error("Exception during image server check pre os install: " + e.getMessage(), e);
WorkflowStepCompleter.stepFailed(stepId, e);
} catch (Exception e) {
log.error("Unexpected exception during image server check pre os install: " + e.getMessage(), e);
String opName = ResourceOperationTypeEnum.INSTALL_OPERATING_SYSTEM.getName();
WorkflowStepCompleter.stepFailed(stepId, ImageServerControllerException.exceptions.unexpectedException(opName, e));
} finally {
try {
if (d != null && d.isConnected()) {
d.close();
}
} catch (Exception e) {
log.error(FAILED_TO_CLOSE_STR, e);
}
}
}
use of com.emc.storageos.networkcontroller.SSHSession in project coprhd-controller by CoprHD.
the class ImageServerDialog method main.
public static void main(String[] args) {
ImageServerDialog d = null;
try {
ImageServerConf imageServerConf = new ImageServerConf();
imageServerConf.setImageServerIp("<IP>");
imageServerConf.setImageServerUser("root");
imageServerConf.setImageServerPassword("<password>");
imageServerConf.setTftpbootDir("/opt/tftpboot/");
imageServerConf.setImageServerSecondIp("<IP>");
imageServerConf.setImageServerHttpPort("44491");
imageServerConf.setImageDir("images");
SSHSession session = new SSHSession();
session.connect(imageServerConf.getImageServerIp(), imageServerConf.getSshPort(), imageServerConf.getImageServerUser(), imageServerConf.getImageServerPassword());
d = new ImageServerDialog(session, imageServerConf.getSshTimeoutMs());
d.init();
d.cd("/tmp");
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
if (d != null && d.isConnected()) {
d.close();
}
}
System.out.println("exit");
System.exit(0);
}
use of com.emc.storageos.networkcontroller.SSHSession in project coprhd-controller by CoprHD.
the class OsInstallStatusPoller method pollImageServer.
private void pollImageServer() {
log.debug("pollImageServer");
List<URI> ids = dbClient.queryByType(ComputeImageServer.class, true);
for (URI imageServerId : ids) {
ComputeImageServer imageServer = dbClient.queryObject(ComputeImageServer.class, imageServerId);
ImageServerDialog d = null;
String[] successes = null;
String[] failures = null;
try {
SSHSession session = new SSHSession();
session.connect(imageServer.getImageServerIp(), imageServer.getSshPort(), imageServer.getImageServerUser(), imageServer.getImageServerPassword());
d = new ImageServerDialog(session, imageServer.getSshTimeoutMs());
d.init();
successes = d.lsDir(imageServer.getTftpBootDir() + HTTP_SUCCESS_DIR);
failures = d.lsDir(imageServer.getTftpBootDir() + HTTP_FAILURE_DIR);
log.info("successes: {}; failures: {}", Arrays.asList(successes), Arrays.asList(failures));
if (successes != null) {
for (String sessionId : successes) {
sessionStatusMap.put(sessionId, OsInstallStatus.SUCCESS);
}
}
if (failures != null) {
for (String sessionId : failures) {
sessionStatusMap.put(sessionId, OsInstallStatus.FAILURE);
}
}
} catch (Exception e) {
log.error("Unexpected exception when polling image server: " + e.getMessage(), e);
} finally {
try {
if (d != null && d.isConnected()) {
d.close();
}
} catch (Exception e) {
log.error("failed to close image server dialog", e);
}
}
}
}
Aggregations