public void Import(string kitFilePath, string instanceToUpdate) { string tempDir; string version = string.Empty; Product product = null; bool oldVersion = false; // Create the name for the temporary directory RaiseStatusChange("Creating temporary directory", 0, 0, -1); do { tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); }while (Directory.Exists(tempDir)); try { // Create the temporary directory Directory.CreateDirectory(tempDir); RaiseStatusChange("Extracting kit", 0, 0, -1); // Open the archive using (ZipFile zip = ZipFile.Read(kitFilePath)) { // Do checks with the zipfile and reads the Product file try { CheckZipfile(zip, tempDir, out version, out product); } catch (Exception ex) { throw new Exception(string.Format("The selected zip file doesn't contain a valid product.\n{0}", ex.Message), ex); } if (product.ProductType == ProductType.Standard) { // Check if trying to import an older version than the latest // version installed for the product. if (_productList.Exists(product.ProductId) && VersionHandler.VersionStringCompare(_productList.Get(product.ProductId).LatestVersion, version) == 1) { oldVersion = true; } } // Set eventhandler to do something useful. zip.ExtractProgress += new EventHandler <ExtractProgressEventArgs>(zip_ExtractProgress); // If no errors was found during the check then continue with extracting // all files from the archive. zip.ExtractAll(tempDir, true); // Remove event again zip.ExtractProgress -= zip_ExtractProgress; // Remove readonly attribute on the extracted files since it only creates problems. FileHandler.RemoveReadOnlyAttribute(tempDir, true); } if (product.ProductType == ProductType.Standard) { RaiseStatusChange("Creating/Updating product and version", 0, 0, -1); string installDir; string versionInstallDir; bool productExist = _productList.Exists(product.ProductId); // Check if product exist if (productExist) { installDir = _productList.Get(product.ProductId).InstallPath; } else { installDir = FileHandler.GetNewInstallDirectory(product.ProductName, _stagingAreaPath); } // Versions install directory versionInstallDir = Path.Combine(installDir, Constants.SubPathToVersions); // Copy the version directory from temp to install directory if (!FileHandler.CopyDirectory(Path.Combine(tempDir, version), Path.Combine(versionInstallDir, version))) { throw new Exception("Unable to copy the new version to the staging area."); } // Copy the product file // Only do this if the version is newer than the LatestVersion or new parameters // for newer versions will be removed etc. if (!oldVersion) { try { FileHandler.CopyFileForced(Path.Combine(tempDir, SerializeHandler.ProductFilename), installDir); } catch { throw new Exception(string.Format("Error copying the file {0} to the staging area.", SerializeHandler.ProductFilename)); } } // Now create or update the ProductList _productList.CreateOrUpdate(product, installDir); //Update manifest to force download even if same version foreach (Instance instance in _productList.Get(product.ProductId).Instances) { if (string.IsNullOrEmpty(instanceToUpdate)) { if (VersionHandler.VersionStringCompare(instance.ProductVersion, version) == 0) { RaiseStatusChange("Updating instance using same product version: " + instance.Name, 0, 0, -1); UpdateDeployManifest(_productList.Get(product.ProductId), instance, true, null); } } else { if (instance.Name == instanceToUpdate) { bool changed = false; RaiseStatusChange("Updating instance: " + instance.Name, 0, 0, -1); if (instance.ProductVersion != version) { instance.ProductVersion = version; instance.VersionPath = Path.Combine(versionInstallDir, version); instance.ApplicationManifestFile = Directory.GetFiles(instance.VersionPath, "*.manifest")[0]; changed = true; } UpdateDeployManifest(_productList.Get(product.ProductId), instance, true, null); if (changed) { SerializeHandler.SaveProductStandardData(_productList.Get(product.ProductId).GetProductData(), _productList.Get(product.ProductId).InstallPath); } } } } WebpageHandler.UpdateWebpage(_config, _productList); } } finally { // Clean the temp dir Directory.Delete(tempDir, true); } }
private void UpdateDeployManifest(ProductStandard product, Instance instance, bool onlyUpdate, string certificatePassword) { // Deploy Manifestfile string deployManifestFile = Path.Combine(product.InstallPath, instance.DeployManifestFile); // Check if only update of existing file is necessary if (!onlyUpdate) { // Temp filename to use string tempTemplateFile = Path.Combine(Path.GetTempPath(), Constants.DeployManifestTemplateTemporaryName); // Get the template file from the assembly and overwrite the template in temp directory if (!FileHandler.GetFileFromAssembly(tempTemplateFile, Assembly.Load("Imi.SupplyChain.Deployment"), Constants.DeployManifestTemplateResource)) { throw new Exception("Could not copy the deployment manifest to the temporary directory."); } // Move and rename the template to the correct place. try { File.Move(tempTemplateFile, deployManifestFile); } catch { throw new Exception("Could not move the deployment manifest."); } } string providerURL = "http://" + _config.GetWebserverName() + "/" + product.VirtualDirectoryName + "/" + instance.Name + ".application"; // Build the arguments for updating the deploy template with mage.exe string argument = string.Format("-Update \"{0}\" " + "-AppManifest \"{1}\" " + "-Name \"{2}\" " + "-Version {3} " + "-p x86 " + "-CertFile \"{4}\"{5} " + "-ProviderUrl \"{6}\" ", deployManifestFile, instance.ApplicationManifestFile, product.ProductName, instance.ProductVersion, _certificateFile, !string.IsNullOrEmpty(certificatePassword) ? string.Format(" -Password {0}", certificatePassword) : string.Empty, providerURL); Regex regEx = new Regex(Constants.DeployManifestSuccessfulPattern); string result = RunConsoleApp.Run(Path.Combine(_executablePath, Constants.MageExeFilename), argument); // Check if result was successful if (!regEx.Match(result).Success) { if (!onlyUpdate) { // Remove the unsigned manifest file. // Only if we're not updating an existing working manifest File.Delete(deployManifestFile); } if (result.Contains("The specified network password is not correct.")) { throw new Exception("Invalid password."); } else { throw new Exception(string.Format("Error when running mage.exe.\n\nArgument:\n\t{0}\n\nResult:\n\t{1}", argument, result)); } } }