Skip to content

Commit

Permalink
Merged 'pre_master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Mlynarczyk committed Apr 29, 2012
1 parent 9100dee commit 19689d6
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 111 deletions.
4 changes: 2 additions & 2 deletions VMAT/Controllers/VirtualMachineController.cs
Expand Up @@ -78,7 +78,7 @@ public ActionResult Create(VirtualMachineFormViewModel vmForm)
return RedirectToAction("Index");
}
}
//TODO: Add comments to what this does
//TODO: Add comments to what this does
var projectName = new SelectList(vmRepo.GetAllProjects(),
"ProjectName", "ProjectName");

Expand Down Expand Up @@ -110,7 +110,7 @@ public ActionResult Edit(VirtualMachineFormViewModel vm)
{
if (ModelState.IsValid)
{
// save changes (removed)
// save changes (removed)
return RedirectToAction("Index");
}

Expand Down
223 changes: 114 additions & 109 deletions VMAT/Models/VirtualMachineRepository.cs
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using VMAT.Services;
using Vestris.VMWareLib;
using System.Net;

namespace VMAT.Models
{
Expand All @@ -21,10 +22,10 @@ public VirtualMachineRepository(DataEntities db)
dataDB.VirtualMachines == null || dataDB.VirtualMachines.Count() <= 0)
InitializeDataContext();
}
public VirtualMachineRepository(bool skip)
{
}
public VirtualMachineRepository(bool skip)
{
}
private void InitializeDataContext()
{
var registeredImages = RegisteredVirtualMachineService.GetRegisteredVMImagePaths();
Expand Down Expand Up @@ -215,53 +216,53 @@ public void UndoScheduleArchiveVirtualMachine(int id)
dataDB.SaveChanges();
}

public void CreatePendingVirtualMachine(VirtualMachine vm)
{
long freeSpace = 0;
long fileSize = 0;
long currentlyPendingSize = 0;
int fudgeFactor = (int)Math.Pow(2, 12); //windows has 4 kilobyte partitions, so a given file is at least 4 kilobytes on disk, while this just gives us the base size
//try
//{
string[] a = Directory.GetFiles(vm.ImagePathName);
foreach (string name in a)
{
FileInfo info = new FileInfo(name);
fileSize += info.Length + fudgeFactor;
}
foreach (VirtualMachine pvm in GetAllPendingVirtualMachines())
{
a = Directory.GetFiles(pvm.ImagePathName);
foreach (string name in a)
{
FileInfo info = new FileInfo(name);
currentlyPendingSize += info.Length + fudgeFactor;
}
}
DriveInfo dI = new DriveInfo("Z:");
freeSpace = dI.AvailableFreeSpace;
//}
//catch (IOException ex)
//{
// //drive not ready or exist
// throw new IOException("Drive not ready or does not exist");
//}
//catch (ArgumentException ex)
//{
// //drive does not exist
// throw new
//}
if (freeSpace > fileSize + currentlyPendingSize)
{
dataDB.VirtualMachines.Add(vm);
dataDB.SaveChanges();
}
else
{
//error message
throw new Exception("Not enough free space");
}
}
public void CreatePendingVirtualMachine(VirtualMachine vm)
{
long freeSpace = 0;
long fileSize = 0;
long currentlyPendingSize = 0;
int fudgeFactor = (int)Math.Pow(2, 12); //windows has 4 kilobyte partitions, so a given file is at least 4 kilobytes on disk, while this just gives us the base size
//try
//{
string[] a = Directory.GetFiles(vm.ImagePathName);
foreach (string name in a)
{
FileInfo info = new FileInfo(name);
fileSize += info.Length + fudgeFactor;
}
foreach (VirtualMachine pvm in GetAllPendingVirtualMachines())
{
a = Directory.GetFiles(pvm.ImagePathName);
foreach (string name in a)
{
FileInfo info = new FileInfo(name);
currentlyPendingSize += info.Length + fudgeFactor;
}
}
DriveInfo dI = new DriveInfo("Z:");
freeSpace = dI.AvailableFreeSpace;
//}
//catch (IOException ex)
//{
// //drive not ready or exist
// throw new IOException("Drive not ready or does not exist");
//}
//catch (ArgumentException ex)
//{
// //drive does not exist
// throw new
//}
if (freeSpace > fileSize + currentlyPendingSize)
{
dataDB.VirtualMachines.Add(vm);
dataDB.SaveChanges();
}
else
{
//error message
throw new Exception("Not enough free space");
}
}

public int ToggleVMStatus(int id)
{
Expand All @@ -278,67 +279,71 @@ public int ToggleVMStatus(int id)

public string GetNextAvailableIP()
{
List<string> ipList = new List<string>();
ipList = dataDB.VirtualMachines.Select(v => v.IP).
ToList<string>();
List<string> ipList = new List<string>();
ipList = dataDB.VirtualMachines.Select(v => v.IP).
ToList<string>();

ipList.AddRange(dataDB.VirtualMachines.Select(v => v.IP).ToList<string>());

return GetNextAvailableIP(ipList);
}

public string GetNextAvailableIP(List<string> ipList )
{
bool[] ipUsed = new bool[256];
//TODO: get the correct HostConfiguration
ConfigurationRepository configRepo = new ConfigurationRepository();
HostConfiguration config = configRepo.GetHostConfiguration();

//remove low and high end IP's from being available
IPAddress minIp = IPAddress.Parse(config.MinIP);
IPAddress maxIp = IPAddress.Parse(config.MaxIP);
int min, max;
ipUsed[0] = true;
bool canMin = int.TryParse(config.MinIP.Substring(config.MinIP.LastIndexOf('.')), out min);
bool canMax = int.TryParse(config.MaxIP.Substring(config.MaxIP.LastIndexOf('.')), out max);
if (canMin)
{
for (int i = 0; i < min; i++)
{
ipUsed[i] = true;
}
}
if(canMax)
{
for (int i = 255; i > max; i--)
{
ipUsed[i] = true;
}
}

ipList.AddRange(dataDB.VirtualMachines.Select(v => v.IP).ToList<string>());
foreach (var ip in ipList)
{
try
{
string longIP = ip;

int ipTail = int.Parse(longIP.Substring(longIP.LastIndexOf('.') + 1));
ipUsed[ipTail] = true;
}
catch (NullReferenceException)
{
// Ignore if a stored IP address is NULL
}
catch (FormatException)
{
// Ignore if a stored IP address is invalid
}
}

for (int index = 0; index < ipUsed.Length; index++)
{
if (!ipUsed[index])
return "192.168.1." + index.ToString();
}

return GetNextAvailableIP(ipList);
return null;
}
public string GetNextAvailableIP(List<string> ipList )
{
bool[] ipUsed = new bool[256];
//TODO: get the correct HostConfiguration
ConfigurationRepository configRepo = new ConfigurationRepository();
HostConfiguration config = configRepo.GetHostConfiguration();
//remove low and high end IP's from being available
int min, max;
ipUsed[0] = true;
bool canMin = int.TryParse(config.MinIP.Substring(config.MinIP.LastIndexOf('.')), out min);
bool canMax = int.TryParse(config.MaxIP.Substring(config.MaxIP.LastIndexOf('.')), out max);
if (canMin)
{
for (int i = 0; i < min; i++)
{
ipUsed[i] = true;
}
}
if(canMax)
{
for (int i = 255; i > max; i--)
{
ipUsed[i] = true;
}
}

foreach (var ip in ipList)
{
try
{
string longIP = ip;

int ipTail = int.Parse(longIP.Substring(longIP.LastIndexOf('.') + 1));
ipUsed[ipTail] = true;
}
catch (NullReferenceException)
{
// Ignore if a stored IP address is NULL
}
catch (FormatException)
{
// Ignore if a stored IP address is invalid
}
}

for (int index = 0; index < ipUsed.Length; index++)
{
if (!ipUsed[index])
return "192.168.1." + index.ToString();
}

return null;
}

public void CreateSnapshot(VirtualMachine vm, string name, string description)
{
Expand Down

0 comments on commit 19689d6

Please sign in to comment.