/// <summary> /// Creates a file share if one doesn't already exist. /// It may also perform some modifications in the file share if needed. /// </summary> private void CreateFileShare() { _fileSharePath = _ticket.FileSharePath; // Create a share for the system. It will be used for test documents and other // shared resources used by the system. if (SharedDrive.IsShared(_ticket.FileShareName)) { // If there is already a share in place, then find it's path _fileSharePath = WindowsManagementInstrumentation .GetFileShares(Environment.MachineName) .First(x => x.Key.Equals(_ticket.FileShareName)) .Value; SystemTrace.Instance.Debug("Share location: {0}".FormatWith(_fileSharePath)); } else { try { // Create the file share path if it doesn't already exist if (!Directory.Exists(_fileSharePath)) { Directory.CreateDirectory(_fileSharePath); } SystemTrace.Instance.Debug("Share will be created on {0}".FormatWith(_fileSharePath)); SystemTrace.Instance.Debug("Share name: {0}".FormatWith(_ticket.FileShareName)); string desc = "Solution Test Bench System Share"; SharedDrive drive = new SharedDrive(_ticket.FileShareName); // Create NT Level access control for the shared directory DirectorySecurity accessControl = Directory.GetAccessControl(_fileSharePath); IdentityReference everybody = new SecurityIdentifier(WellKnownSidType.WorldSid, null); FileSystemAccessRule accessRule = new FileSystemAccessRule ( everybody, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow ); accessControl.AddAccessRule(accessRule); Directory.SetAccessControl(_fileSharePath, accessControl); // Now create the share, and then add user rights for Everyone. FileSystemRights[] rights = new FileSystemRights[] { FileSystemRights.FullControl }; var result = drive.Create(_fileSharePath, desc, "Everyone", rights); if (result == SharedDrive.ShareResult.Success) { // If the share was successfully created, then you need to explicitly // add user rights to the share as well. drive.SetUserRights("Everyone", rights); } else { SendError($"Unable to create shared folder on {_fileSharePath} : {result}"); } } catch (Exception ex) { SendError($"Unable to create shared folder on {_fileSharePath}: {ex.Message}", ex); } } // Read in all the shared folder paths from the resource file. Create a subdirectory // for each of these locations in the shared folder space. StringReader reader = new StringReader(new EmbeddedResource("HP.SolutionTest.FileShare").Read("FolderList.txt")); string subdirectory = reader.ReadLine(); while (subdirectory != null) { string path = Path.Combine(_fileSharePath, subdirectory); if (!Directory.Exists(path)) { SystemTrace.Instance.Debug("Created file share location: {0}".FormatWith(path)); Directory.CreateDirectory(path); } subdirectory = reader.ReadLine(); } // In case of an STB upgrade, if the file share contains a folder named 'TestLibrary\Documents\JPEG', // move all its contents to the 'TestLibrary\Documents\Image' folder, and then delete the 'JPEG' folder. string jpegPath = Path.Combine(_fileSharePath, @"TestLibrary\Documents\JPEG"); string imageFilePath = Path.Combine(_fileSharePath, @"TestLibrary\Documents\Image"); try { if (Directory.Exists(jpegPath) && Directory.Exists(imageFilePath)) { foreach (var file in new DirectoryInfo(jpegPath).GetFiles()) { file.MoveTo($@"{imageFilePath}\{file.Name}"); } SystemTrace.Instance.Debug($"Files in share location \'{jpegPath}\' moved to \'{imageFilePath}\'"); Directory.Delete(jpegPath); SystemTrace.Instance.Debug("Deleted file share location: {0}".FormatWith(jpegPath)); } } catch (Exception ex) { SendError($"Unable to rename shared folder on {jpegPath}: {ex.Message}", ex); } // For any initial test documents provided, save those to the appropriate shared folder location. EmbeddedResource resource = new EmbeddedResource("HP.SolutionTest.FileShare.Content"); foreach (var name in resource.SubNames) { SystemTrace.Instance.Debug("Saving resource: {0}".FormatWith(name)); // Convert the sub name into a file path (ie replace '.' with '\') string filePath = Path.Combine(_fileSharePath, Path.Combine(name.Split('.'))); // Fix the last '.' before the file extension that was converted into a '\' above. int index = filePath.LastIndexOf('\\'); filePath = string.Concat(filePath.Select((c, i) => i == index ? '.' : c)); SystemTrace.Instance.Debug("FilePath: {0}".FormatWith(filePath)); string directory = Path.GetDirectoryName(filePath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); SystemTrace.Instance.Debug("Created file share folder: {0}".FormatWith(directory)); } resource.Save(filePath, name); } }
private bool CreateDatabases() { SystemTrace.Instance.Debug("Creating databases..."); string creationSQL = string.Empty; //Cache this in case we need to roll back. StringBuilder sqlText = new StringBuilder(); // Create tables, insert data, etc. for each database using (SqlTextAdapter adapter = new SqlTextAdapter(Environment.MachineName, "Master")) { try { UpdateStatus("Creating Solution Test Bench database instances..."); EmbeddedResource stream = new EmbeddedResource("HP.SolutionTest.DatabaseCreation"); creationSQL = stream.Read("create_DatabaseInstances.sql"); sqlText.Append(creationSQL); sqlText.Replace("{DATABASE_PATH}", _ticket.DatabaseFilesPath); if (!ExecuteSql(sqlText.ToString(), adapter, false)) { return(false); } UpdateStatus("Creating Solution Test Bench database users..."); if (!ExecuteSql(stream.Read("create_DatabaseUsers.sql"), adapter, true)) { return(false); } UpdateStatus("Creating Solution Test Bench primary configuration database schema..."); if (!ExecuteSql(stream.Read("create_SchemaEnterpriseTest.sql"), adapter, true)) { return(false); } UpdateStatus("Creating Solution Test Bench asset inventory database schema..."); if (!ExecuteSql(stream.Read("create_SchemaAssetInventory.sql"), adapter, true)) { return(false); } UpdateStatus("Creating Solution Test Bench test document database schema..."); if (!ExecuteSql(stream.Read("create_SchemaTestDocumentLibrary.sql"), adapter, true)) { return(false); } UpdateStatus("Creating Solution Test Bench data log database schema..."); if (!ExecuteSql(stream.Read("create_SchemaScalableTestDatalog.sql"), adapter, true)) { return(false); } UpdateStatus("Populating Solution Test Bench databases with base configuration data..."); sqlText.Clear(); sqlText.Append(stream.Read("ins_BaseConfigurationData.sql")); sqlText.Replace("{FILE_SHARE}", _ticket.FileShareName); sqlText.Replace("{SERVER_ADDRESS}", _ticket.ServerHostname); sqlText.Replace("{ADMIN_USER}", _ticket.AdminUserName); sqlText.Replace("{ADMIN_DOMAIN}", _ticket.AdminDomain); sqlText.Replace("{ORGANIZATION}", _ticket.OrganizationName); sqlText.Replace("{ADMIN_EMAIL}", _ticket.AdminEmail); sqlText.Replace("{DNS_DOMAIN}", _dnsDomainName); sqlText.Replace("{AD_DOMAIN}", Environment.UserDomainName); if (!ExecuteSql(sqlText.ToString(), adapter, true)) { return(false); } Version version = new Version(stream.Read("SchemaVersion.txt")); SystemTrace.Instance.Debug($"Schema version: {version}"); UpdateStatus("Updating Solution Test Bench database to initial version"); sqlText.Clear(); sqlText.Append(stream.Read("ins_SchemaVersion.sql")); sqlText = sqlText.Replace("{0}", version.ToString()); if (!ExecuteSql(sqlText.ToString(), adapter, true)) { return(false); } // Update the ticket to the current version _ticket.CurrentVersion = version; } catch (Exception ex) { SendError(ex); DropDatabaseInstances(GetSTBDatabaseNames(ref creationSQL), adapter); return(false); } } return(true); }