[2612] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2612] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[2513] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
| 27 | using System.IO;
|
---|
[2517] | 28 | using System.ComponentModel;
|
---|
[2688] | 29 | using System.Reflection;
|
---|
[2922] | 30 | using ICSharpCode.SharpZipLib.Zip;
|
---|
[3006] | 31 | using System.ServiceModel;
|
---|
[2513] | 32 |
|
---|
| 33 | namespace HeuristicLab.PluginInfrastructure.Advanced {
|
---|
[2517] | 34 | internal class InstallationManager {
|
---|
[2513] | 35 |
|
---|
[2517] | 36 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreUpdatePlugin;
|
---|
| 37 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreRemovePlugin;
|
---|
| 38 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreInstallPlugin;
|
---|
| 39 |
|
---|
| 40 | internal event EventHandler<PluginInfrastructureEventArgs> PluginUpdated;
|
---|
| 41 | internal event EventHandler<PluginInfrastructureEventArgs> PluginRemoved;
|
---|
| 42 | internal event EventHandler<PluginInfrastructureEventArgs> PluginInstalled;
|
---|
| 43 |
|
---|
[2513] | 44 | private string pluginDir;
|
---|
[2517] | 45 | public InstallationManager(string pluginDir) {
|
---|
| 46 | this.pluginDir = pluginDir;
|
---|
[2513] | 47 | }
|
---|
| 48 |
|
---|
[2922] | 49 | //public IEnumerable<string> Show(IEnumerable<string> pluginNames) {
|
---|
| 50 | // foreach (PluginDescription desc in GetPluginDescriptions(pluginNames)) {
|
---|
| 51 | // yield return GetInformation(desc);
|
---|
| 52 | // }
|
---|
| 53 | //}
|
---|
[2513] | 54 |
|
---|
[2922] | 55 | //internal string GetInformation(string pluginName) {
|
---|
| 56 | // return GetInformation(GetPluginDescription(pluginName));
|
---|
| 57 | //}
|
---|
[2517] | 58 |
|
---|
[2922] | 59 | //private string GetInformation(PluginDescription desc) {
|
---|
| 60 | // StringBuilder builder = new StringBuilder();
|
---|
| 61 | // builder.Append("Name: ").AppendLine(desc.Name);
|
---|
| 62 | // builder.Append("Version: ").AppendLine(desc.Version.ToString());
|
---|
| 63 | // builder.AppendLine("Description:").AppendLine(desc.Description);
|
---|
| 64 | // if (!string.IsNullOrEmpty(desc.ContactName)) {
|
---|
| 65 | // builder.Append("Contact: ").Append(desc.ContactName).Append(", ").AppendLine(desc.ContactEmail);
|
---|
| 66 | // }
|
---|
| 67 | // builder.AppendLine("This plugin is " + desc.PluginState.ToString().ToLowerInvariant() + ".");
|
---|
| 68 | // builder.AppendLine("Files: ");
|
---|
| 69 | // foreach (var file in desc.Files) {
|
---|
| 70 | // builder.AppendLine(file.Type + " " + file.Name);
|
---|
| 71 | // }
|
---|
| 72 | // builder.AppendLine().AppendLine("Directly depends on:");
|
---|
| 73 | // if (desc.Dependencies.Count() == 0) builder.AppendLine("None");
|
---|
| 74 | // foreach (var dependency in desc.Dependencies) {
|
---|
| 75 | // builder.AppendLine(dependency.Name + " " + dependency.Version);
|
---|
| 76 | // }
|
---|
| 77 | // builder.AppendLine().AppendFormat("Plugins directly dependent on {0}:", desc.Name).AppendLine();
|
---|
| 78 | // var dependents = from x in pluginManager.Plugins
|
---|
| 79 | // where x.Dependencies.Contains(desc)
|
---|
| 80 | // select x;
|
---|
| 81 | // if (dependents.Count() == 0) builder.AppendLine("None");
|
---|
| 82 | // foreach (var dependent in dependents) {
|
---|
| 83 | // builder.AppendLine(dependent.Name + " " + dependent.Version);
|
---|
| 84 | // }
|
---|
| 85 | // builder.AppendLine();
|
---|
| 86 | // if (desc.PluginState == PluginState.Disabled) {
|
---|
| 87 | // builder.AppendLine(DetermineProblem(desc));
|
---|
| 88 | // }
|
---|
[2513] | 89 |
|
---|
[2922] | 90 | // return builder.ToString();
|
---|
| 91 | //}
|
---|
[2513] | 92 |
|
---|
[2922] | 93 | //private static string DetermineProblem(PluginDescription desc) {
|
---|
| 94 | // // either any file is missing
|
---|
| 95 | // StringBuilder builder = new StringBuilder();
|
---|
| 96 | // builder.AppendLine("Problem report:");
|
---|
| 97 | // builder.AppendLine(desc.LoadingErrorInformation);
|
---|
| 98 | // return builder.ToString();
|
---|
| 99 | //}
|
---|
[2517] | 100 |
|
---|
[2922] | 101 | //private PluginDescription GetPluginDescription(string pluginName) {
|
---|
| 102 | // var exactMatch = from pluginDesc in pluginManager.Plugins
|
---|
| 103 | // where string.Equals(pluginName, pluginDesc.Name, StringComparison.InvariantCultureIgnoreCase)
|
---|
| 104 | // select pluginDesc;
|
---|
| 105 | // var inexactMatch = from pluginDesc in pluginManager.Plugins
|
---|
| 106 | // where MatchPluginNameInexact(pluginName, pluginDesc.Name)
|
---|
| 107 | // select pluginDesc;
|
---|
| 108 | // return exactMatch.Count() > 0 ? exactMatch.Single() : inexactMatch.First();
|
---|
| 109 | //}
|
---|
[2517] | 110 |
|
---|
[2922] | 111 | //private IEnumerable<PluginDescription> GetPluginDescriptions(IEnumerable<string> pluginNames) {
|
---|
| 112 | // return from pluginName in pluginNames
|
---|
| 113 | // select GetPluginDescription(pluginName);
|
---|
| 114 | //}
|
---|
| 115 |
|
---|
| 116 | //private static bool MatchPluginNameInexact(string similarName, string actualName) {
|
---|
| 117 | // return
|
---|
| 118 | // // Core-3.2 == HeuristicLab.Core-3.2
|
---|
| 119 | // actualName.Equals("HeuristicLab." + similarName, StringComparison.InvariantCultureIgnoreCase) ||
|
---|
| 120 | // // HeuristicLab.Core == HeuristicLab.Core-3.2 (this should be save because we checked for exact matches first)
|
---|
| 121 | // (Math.Abs(actualName.Length - similarName.Length) <= 4 && actualName.StartsWith(similarName, StringComparison.InvariantCultureIgnoreCase)) ||
|
---|
| 122 | // // Core == HeuristicLab.Core-3.2
|
---|
| 123 | // (Math.Abs(actualName.Length - similarName.Length) <= 17 && actualName.StartsWith("HeuristicLab." + similarName, StringComparison.InvariantCultureIgnoreCase));
|
---|
| 124 | //}
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | /// <summary>
|
---|
| 128 | /// Retrieves a list of plugins available at the remote server
|
---|
| 129 | /// </summary>
|
---|
| 130 | /// <returns></returns>
|
---|
[3006] | 131 | public IEnumerable<IPluginDescription> GetRemotePluginList() {
|
---|
| 132 | var client = DeploymentService.UpdateClientFactory.CreateClient();
|
---|
| 133 | try {
|
---|
| 134 | List<IPluginDescription> plugins = new List<IPluginDescription>(client.GetPlugins());
|
---|
| 135 | client.Close();
|
---|
| 136 | return plugins;
|
---|
[2922] | 137 | }
|
---|
[3006] | 138 | catch (FaultException) {
|
---|
| 139 | client.Abort();
|
---|
| 140 | return new IPluginDescription[] { };
|
---|
| 141 | }
|
---|
[2517] | 142 | }
|
---|
| 143 |
|
---|
[2922] | 144 | /// <summary>
|
---|
| 145 | /// Retrieves the list of products available at the remote server
|
---|
| 146 | /// </summary>
|
---|
| 147 | /// <returns></returns>
|
---|
[3006] | 148 | public IEnumerable<DeploymentService.ProductDescription> GetRemoteProductList() {
|
---|
| 149 | var client = DeploymentService.UpdateClientFactory.CreateClient();
|
---|
| 150 | try {
|
---|
| 151 | List<DeploymentService.ProductDescription> products = new List<DeploymentService.ProductDescription>(client.GetProducts());
|
---|
| 152 | client.Close();
|
---|
| 153 | return products;
|
---|
[2922] | 154 | }
|
---|
[3006] | 155 | catch (FaultException) {
|
---|
| 156 | client.Abort();
|
---|
| 157 | return new DeploymentService.ProductDescription[] { };
|
---|
| 158 | }
|
---|
[2513] | 159 | }
|
---|
| 160 |
|
---|
[2922] | 161 | /// <summary>
|
---|
| 162 | /// Installs plugins from remote server
|
---|
| 163 | /// </summary>
|
---|
[3092] | 164 | /// <param name="plugins"></param>
|
---|
[3006] | 165 | public void Install(IEnumerable<IPluginDescription> plugins) {
|
---|
| 166 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
| 167 | OnPreInstall(args);
|
---|
| 168 | if (!args.Cancel) {
|
---|
| 169 | var client = DeploymentService.UpdateClientFactory.CreateClient();
|
---|
| 170 | try {
|
---|
| 171 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
| 172 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
| 173 | Unpack(zippedPackage);
|
---|
| 174 | OnInstalled(new PluginInfrastructureEventArgs(plugin));
|
---|
| 175 | }
|
---|
| 176 | client.Close();
|
---|
[2922] | 177 | }
|
---|
[3006] | 178 | catch (FaultException) {
|
---|
| 179 | client.Abort();
|
---|
| 180 | }
|
---|
[2922] | 181 | }
|
---|
[2517] | 182 | }
|
---|
| 183 |
|
---|
[2922] | 184 | /// <summary>
|
---|
| 185 | /// Updates plugins from remote server
|
---|
| 186 | /// </summary>
|
---|
[3092] | 187 | /// <param name="plugins"></param>
|
---|
[3006] | 188 | public void Update(IEnumerable<IPluginDescription> plugins) {
|
---|
| 189 | PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
[2922] | 190 | OnPreUpdate(args);
|
---|
| 191 | if (!args.Cancel) {
|
---|
[3006] | 192 | var client = DeploymentService.UpdateClientFactory.CreateClient();
|
---|
| 193 | try {
|
---|
[2922] | 194 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
| 195 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
| 196 | Unpack(zippedPackage);
|
---|
| 197 | OnUpdated(new PluginInfrastructureEventArgs(plugin));
|
---|
| 198 | }
|
---|
[3006] | 199 | client.Close();
|
---|
[2922] | 200 | }
|
---|
[3006] | 201 | catch (FaultException) {
|
---|
| 202 | client.Abort();
|
---|
| 203 | }
|
---|
[2922] | 204 | }
|
---|
[2513] | 205 | }
|
---|
| 206 |
|
---|
[2922] | 207 | /// <summary>
|
---|
| 208 | /// Deletes all plugin files from local installation
|
---|
| 209 | /// </summary>
|
---|
[3092] | 210 | /// <param name="plugins"></param>
|
---|
[2922] | 211 | public void Remove(IEnumerable<IPluginDescription> plugins) {
|
---|
| 212 | var fileNames = from pluginToDelete in plugins
|
---|
[2688] | 213 | from file in pluginToDelete.Files
|
---|
| 214 | select Path.Combine(pluginDir, file.Name);
|
---|
[3006] | 215 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
[2517] | 216 | OnPreDelete(args);
|
---|
| 217 | if (!args.Cancel) {
|
---|
| 218 | foreach (string fileName in fileNames) {
|
---|
[2922] | 219 | File.Delete(fileName);
|
---|
| 220 | OnDeleted(new PluginInfrastructureEventArgs(fileName));
|
---|
[2513] | 221 | }
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[2922] | 225 | private void Unpack(byte[] zippedPackage) {
|
---|
| 226 | using (ZipInputStream s = new ZipInputStream(new MemoryStream(zippedPackage))) {
|
---|
| 227 | ZipEntry theEntry;
|
---|
| 228 | string tmpEntry = String.Empty;
|
---|
| 229 | while ((theEntry = s.GetNextEntry()) != null) {
|
---|
| 230 | string directoryName = pluginDir;
|
---|
| 231 | string fileName = Path.GetFileName(theEntry.Name);
|
---|
| 232 | // create directory
|
---|
| 233 | if (directoryName != "") {
|
---|
| 234 | Directory.CreateDirectory(directoryName);
|
---|
[2527] | 235 | }
|
---|
[2922] | 236 | if (fileName != String.Empty) {
|
---|
| 237 | string fullPath = Path.Combine(directoryName, fileName);
|
---|
| 238 | string fullDirPath = Path.GetDirectoryName(fullPath);
|
---|
| 239 | if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
|
---|
| 240 | FileStream streamWriter = File.Create(fullPath);
|
---|
| 241 | int size = 2048;
|
---|
| 242 | byte[] data = new byte[2048];
|
---|
| 243 | while (true) {
|
---|
| 244 | size = s.Read(data, 0, data.Length);
|
---|
| 245 | if (size > 0) {
|
---|
| 246 | streamWriter.Write(data, 0, size);
|
---|
| 247 | } else {
|
---|
| 248 | break;
|
---|
| 249 | }
|
---|
[2527] | 250 | }
|
---|
[2922] | 251 | streamWriter.Close();
|
---|
[2527] | 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
[2513] | 255 | }
|
---|
[2517] | 256 |
|
---|
[2527] | 257 | private void OnPreUpdate(PluginInfrastructureCancelEventArgs args) {
|
---|
| 258 | if (PreUpdatePlugin != null) PreUpdatePlugin(this, args);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | private void OnUpdated(PluginInfrastructureEventArgs args) {
|
---|
| 262 | if (PluginUpdated != null) PluginUpdated(this, args);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[2517] | 265 | private void OnPreDelete(PluginInfrastructureCancelEventArgs args) {
|
---|
| 266 | if (PreRemovePlugin != null) PreRemovePlugin(this, args);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | private void OnDeleted(PluginInfrastructureEventArgs args) {
|
---|
| 270 | if (PluginRemoved != null) PluginRemoved(this, args);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | private void OnPreInstall(PluginInfrastructureCancelEventArgs args) {
|
---|
| 274 | if (PreInstallPlugin != null) PreInstallPlugin(this, args);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | private void OnInstalled(PluginInfrastructureEventArgs args) {
|
---|
| 278 | if (PluginInstalled != null) PluginInstalled(this, args);
|
---|
| 279 | }
|
---|
[2513] | 280 | }
|
---|
| 281 | }
|
---|