Ejemplo n.º 1
0
        private void LoadProductList()
        {
            // Find all products in the staging area that can have instances.
            string[] productDirs = Directory.GetDirectories(_stagingAreaPath);

            // Clear Productlist
            _productList.Clear();

            // Check if any directories was found
            if (productDirs.GetUpperBound(0) >= 0)
            {
                // Loop through list of directories and find the correct product directories.
                foreach (string productDir in productDirs)
                {
                    // Read the product.xml file from the path
                    Product product = SerializeHandler.GetProduct(productDir);

                    if (product != null)
                    {
                        // Check type of product
                        if (product.ProductType == ProductType.Standard)
                        {
                            _productList.CreateOrUpdate(product, productDir);
                        }
                    }
                }
            }
        }
        public void CreateOrUpdate(Product product, string productDir)
        {
            ProductStandard standardProduct = ProductStandard.Create(product);

            // Set InstallPath
            standardProduct.InstallPath = productDir;

            // Find all product versions
            try
            {
                string[] versionDirs = Directory.GetDirectories(Path.Combine(standardProduct.InstallPath, Constants.SubPathToVersions));

                // Check if any versions was found
                if (versionDirs.GetUpperBound(0) >= 0)
                {
                    // Loop through all directories
                    foreach (string versionDir in versionDirs)
                    {
                        // Finds the last directory part (X.X.X.X)
                        string directoryName = Path.GetFileName(versionDir);

                        // Check if directory name is valid
                        if (VerifyString.IsVersion(directoryName))
                        {
                            // Try to find the .manifest file (should only be one) that is
                            // the application manifest.
                            string[] foundFiles = Directory.GetFiles(versionDir, "*.manifest");

                            // Only one file should be found!
                            if (foundFiles.GetUpperBound(0) == 0)
                            {
                                standardProduct.Versions.Add(new VersionWrapper(directoryName, versionDir, foundFiles[0]));
                            }
                        }
                    }

                    // Sort versions in latest version first order
                    standardProduct.Versions.Sort(new VersionWrapperComparer());
                    standardProduct.Versions.Reverse();
                }
            }
            catch
            {
                standardProduct.Versions.Clear();
            }

            // Only add product if there are versions installed
            if (standardProduct.Versions.Count > 0)
            {
                // Try to read the Product data from the directory
                standardProduct.SetProductData(SerializeHandler.GetProductStandardData(standardProduct.InstallPath));

                // Check if the product already exist
                if (Exists(standardProduct.ProductId))
                {
                    // Remove the existing product
                    Remove(Get(standardProduct.ProductId));
                }

                // Add product to the list
                Add(standardProduct);
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        private void CheckZipfile(ZipFile zip, string extractDir, out string version, out Product product)
        {
            ZipEntry productXMLentry = null;
            bool     manifestFound   = false;

            version = string.Empty;

            // First loop and try to find the version directory
            foreach (ZipEntry entry in zip.Entries)
            {
                // Check if product.xml file
                if (entry.FileName.ToUpper() == SerializeHandler.ProductFilename.ToUpper())
                {
                    productXMLentry = entry;
                    continue;
                }

                if (entry.IsDirectory)
                {
                    // Check that the "root" directory is a versionnumber
                    string[] directories = entry.FileName.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                    if (directories.Length > 0)
                    {
                        // The first part of the path should always be the version number
                        if (VersionHandler.IsVersion(directories[0]))
                        {
                            // Fetch the version number
                            if (directories.Length == 1)
                            {
                                if (string.IsNullOrEmpty(version))
                                {
                                    version = directories[0];
                                }
                                else
                                {
                                    // More than one version directory was found. Error!
                                    throw new Exception("The zip file contains more than one version directory.");
                                }
                            }
                        }
                        else
                        {
                            // It's not a version directory.
                            throw new Exception("The zip file contains directories other than the version directory.");
                        }
                    }
                    else
                    {
                        // Split generated no result
                        throw new Exception("The zip file contains invalid directory entries.");
                    }

                    continue;
                }
            }

            if (string.IsNullOrEmpty(version))
            {
                // The version wasn't found
                throw new Exception("The zip file does not contain a version directory.");
            }

            if (productXMLentry == null)
            {
                throw new Exception(string.Format("The zip file is missing the file {0}.", SerializeHandler.ProductFilename));
            }

            // Read the Product.xml
            productXMLentry.Extract(extractDir);

            // Read the product.xml file from the path
            product = SerializeHandler.GetProduct(extractDir);

            // Throw an exception if the product couldn't be read.
            if (product == null)
            {
                throw new Exception(string.Format("The {0} file couldn't be read.", productXMLentry.FileName));
            }

            // Do additional controls of standard products.
            if (product.ProductType == ProductType.Standard)
            {
                // Check the files in the zipfile
                foreach (ZipEntry entry in zip.Entries)
                {
                    // Ignore directories since we checked them above
                    if (entry.IsDirectory)
                    {
                        continue;
                    }

                    // Check if product.xml file
                    if (entry.FileName.ToUpper() == SerializeHandler.ProductFilename.ToUpper())
                    {
                        continue;
                    }

                    // Check if it's a deploy file
                    if (Path.GetExtension(entry.FileName).ToUpper() == Constants.ValidZIPVersionDirExtension_Deploy.ToUpper())
                    {
                        continue;
                    }

                    // Check if it's a manifest file
                    if (Path.GetExtension(entry.FileName).ToUpper() == Constants.ValidZIPVersionDirExtension_Manifest.ToUpper())
                    {
                        if (!manifestFound)
                        {
                            manifestFound = true;
                        }
                        else
                        {
                            // More than one manifest file found in zip file! Error!
                            throw new Exception("The zip file contains more than one manifest file.");
                        }

                        continue;
                    }

                    // We should not reach this line since then we have found an unknown file
                    // that shouldn't be in the archive.
                    throw new Exception("The zip file contains one or more unknown files.");
                }
            }
        }