/** * Main Constructor Takes a ServiceInstance object created by the login splash * * @param ServiceInstance si * @see LoginSplash */ public CommandEngine(ServiceInstance si, Main main) { super(si); this.main = main; vmTemplates = new ArrayList<Template>(); vmRootEntities = new ArrayList<VirtualMachineExt>(); vmFolders = new ArrayList<FolderExt>(); networks = new ArrayList<Network>(); try { Folder rootFolder = si.getRootFolder(); ManagedEntity[] datacenters = rootFolder.getChildEntity(); for (int i = 0; i < datacenters.length; i++) { if (datacenters[i] instanceof Datacenter && datacenters[i].getName().equalsIgnoreCase(DATACENTER)) { dc = (Datacenter) datacenters[i]; } } // Set the template directory and the project directory for (ManagedEntity each : dc.getVmFolder().getChildEntity()) { if (each instanceof Folder && each.getName().equalsIgnoreCase(TEMPLATE_FOLDER)) { templateDir = (Folder) each; } else if (each instanceof Folder && each.getName().equalsIgnoreCase(ROOT_FOLDER)) { rootDir = (Folder) each; } } // Set the target datastore for new VM deployments for (ManagedEntity each : dc.getDatastoreFolder().getChildEntity()) { if (each instanceof Datastore && each.getName().equalsIgnoreCase(TARGET_DATASTORE)) { targetDatastore = (Datastore) each; } } // Get Networks - only happens once per load for (ManagedEntity each : dc.getNetworkFolder().getChildEntity()) { if (each instanceof Network) { networks.add(((Network) each)); } } refresh(); } catch (RemoteException e) { disconnect(); LOG.printStackTrace(e); } catch (Exception e) { LOG.printStackTrace(e); } }
/** Exports the Last Deployment to an html file Called by the GuiMain in an action event */ public void exportLastDeployment(ArrayList<DeployedVirtualMachine> deployed, Component parent) { if (deployed.size() != 0) { FileSystemView fsv = FileSystemView.getFileSystemView(); JFileChooser chooser = new JFileChooser(fsv.getRoots()[0]); chooser.addChoosableFileFilter(new ReportGeneratorFileFilter()); chooser.setSelectedFile(new File("report.html")); int choice = chooser.showSaveDialog(parent); if (choice == JFileChooser.APPROVE_OPTION) { try { LOG.write("Creating report..."); new ReportGenerator().makeReport(chooser.getSelectedFile(), deployed, this); LOG.write("Report saved at " + chooser.getSelectedFile().getAbsolutePath()); } catch (Exception e) { LOG.write("Error in generating the report"); LOG.printStackTrace(e); } } } else { JOptionPane.showMessageDialog( main, "No deployment data exists. \nUse the Deployment Wizard to start a " + "new deployment before creating a deployment report.", "Could not create report", JOptionPane.ERROR_MESSAGE); } }
/** * Gathers the data from each VirtualMachine object Creates an Object[][] which is filled with * data in a primary for/each loop. <br> * <strong>Important: </strong>refresh() should be called to ensure this information is valid * * @return the table of information from each virtual machine */ public Object[][] gatherTableData() { Object[][] data = null; try { data = new Object[vmRootEntities.size()][COLUMN_HEADINGS.length]; int row = 0; for (VirtualMachineExt each : vmRootEntities) { data[row][0] = each.getIcon(); data[row][1] = each.getName(); data[row][2] = each.getGuestOS(); data[row][3] = each.getTeam(); data[row][4] = each.getPowerStatus(); data[row][5] = each.getCpuUsage(); data[row][6] = each.getMemUsage(); data[row][7] = each.getHost(); data[row][8] = each.getIpAddr(); data[row][9] = each.getHostName(); data[row][10] = each.getDescription(); row++; } } catch (Exception e) { LOG.write("Error getting data to update VM table", true); LOG.printStackTrace(e); } return data; }
/** * Logs off from the service instance <br> * Should be called when the program exists to make a clean exit from vSphere */ public void logoff() { try { int choice = JOptionPane.showConfirmDialog( main, "Logoff and return to the login screen?", "Logoff VASE Deploy?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (choice == JOptionPane.YES_OPTION) { saveSettings(); si.getServerConnection().logout(); } } catch (RuntimeException e) { disconnect(); } catch (Exception e) { LOG.printStackTrace(e); } finally { main.worker.halt(); main.dispose(); new LoginSplash(); } }
/** * Refreshes the following attributes: * * <ul> * <li>VirtualMachine[] vmRootEntities - containing all virtual machines in the rootDir * <li>VirtualMachine[] vmTemplates - containing all templates in the templateDir * <li>ManagedEntity[] dcFolders - all folders in the vmFolder in the datacenter <br> * <strong>Note: </strong>This method should be called first in the refresh thread */ public synchronized void refresh() { try { // Refresh VirtualMachines if (rootDir == null) { boolean found = false; ManagedEntity[] folders = dc.getVmFolder().getChildEntity(); for (ManagedEntity each : folders) { if (each.getName().equals(ROOT_FOLDER) && each instanceof Folder) { rootDir = (Folder) each; found = true; } } if (!found) { LOG.write( "Error: Could not find root directory. Check the deploy.conf file and ensure " + "the root project directory exists"); } } else { ManagedEntity[] theseVMs = new InventoryNavigator(rootDir).searchManagedEntities("VirtualMachine"); vmRootEntities.clear(); for (int i = 0; i < theseVMs.length; i++) { vmRootEntities.add(new VirtualMachineExt((VirtualMachine) theseVMs[i])); } } // Refresh Templates if (templateDir == null) { boolean found = false; ManagedEntity[] folders = dc.getVmFolder().getChildEntity(); for (ManagedEntity each : folders) { if (each.getName().equals(TEMPLATE_FOLDER)) { templateDir = (Folder) each; found = true; } } if (!found) { LOG.write( "Error: Could not find template directory. Check the deploy.conf file and ensure " + "the template directory exists"); } } else { ManagedEntity[] theseVMs = new InventoryNavigator(templateDir).searchManagedEntities("VirtualMachine"); vmTemplates.clear(); for (int i = 0; i < theseVMs.length; i++) { vmTemplates.add(new Template((VirtualMachine) theseVMs[i])); } } // Refresh Folder Listing ManagedEntity[] dcFolders = dc.getVmFolder().getChildEntity(); vmFolders.clear(); for (int i = 0; i < dcFolders.length; i++) { if (dcFolders[i] instanceof Folder) vmFolders.add(new FolderExt((Folder) dcFolders[i])); } } catch (NullPointerException e) { LOG.write("Could not gather object data", true); } catch (ManagedObjectNotFound e) { LOG.write("Error: Could not locate VM in datacenter", true); } catch (RuntimeException e) { LOG.write("Error: Runtime error in refreshing datacenter list", true); disconnect(); } catch (RemoteException e) { disconnect(); } catch (Exception e) { LOG.write("Error in Refreshing data: " + e.getMessage(), true); LOG.printStackTrace(e); } }