private void DeployUpdate(HttpContext context)
        {
            context.Response.Write("<Errors>");

            // Get the source instance name from
            // the http request's parameters.
            string instanceName = context.Request.Params["Instance"];

            InstanceVersion toVersion = new InstanceVersion(context.Request.Params["Version"]);

            string instanceRoot = ConfigurationManager.AppSettings["InstanceRoot"];

            InstanceCollection instances = new InstanceCollection(instanceRoot);

            if (!instances.Instances.ContainsKey(instanceName))
            {
                return;
            }

            InstanceVersion version = new InstanceVersion(instances.Instances[instanceName].Version);

            version += 1;

            while (version.ToInt() <= toVersion.ToInt())
            {
                string directory = Path.Combine(
                    context.Request.PhysicalApplicationPath,
                    "App_Data",
                    "SoftwareUpdate",
                    version.ToString()
                    );

                if (!Directory.Exists(directory))
                {
                    continue;
                }

                string fileArchive = Path.Combine(
                    directory,
                    "Files.zip"
                    );

                if (File.Exists(fileArchive))
                {
                    try
                    {
                        string tempDirectory = Path.Combine(
                            Path.GetTempPath(),
                            Guid.NewGuid().ToString()
                            );

                        Directory.CreateDirectory(tempDirectory);

                        System.IO.Compression.ZipFile.ExtractToDirectory(
                            fileArchive,
                            tempDirectory
                            );

                        DeployDirectory(tempDirectory, Path.Combine(
                                            instanceRoot,
                                            instanceName
                                            ), version.ToString());
                    }
                    catch (Exception ex)
                    {
                        context.Response.Write(string.Format(
                                                   "<Error Type=\"Software\" Version=\"{1}\">{0}</Error>",
                                                   ex.ToString(),
                                                   version.ToString()
                                                   ));
                    }
                }

                string databaseScripts = Path.Combine(
                    directory,
                    "DATABASE"
                    );

                if (!Directory.Exists(databaseScripts))
                {
                    version += 1;
                    continue;
                }

                Client[] clients = instances.Instances[instanceName].GetClients();

                foreach (Client client in clients)
                {
                    foreach (string script in Directory.GetFiles(databaseScripts))
                    {
                        try
                        {
                            ExecuteQuery(client.Database, File.ReadAllText(script));
                        }
                        catch (Exception ex)
                        {
                            context.Response.Write(string.Format(
                                                       "<Error Type=\"Database\" Version=\"{1}\" Client=\"{2}\">{0}</Error>",
                                                       ex.ToString(),
                                                       version.ToString(),
                                                       client.Name
                                                       ));
                        }
                    }
                }

                version += 1;
            }

            context.Response.Write("</Errors>");
        }
Пример #2
0
        private void GetDeploymentDetails(HttpContext context)
        {
            // Get the name of the instance where to deploy to.
            string instance = context.Request.Params["Instance"];

            InstanceCollection instances = new InstanceCollection();

            if (!instances.Instances.ContainsKey(instance))
            {
                return;
            }

            InstanceVersion instanceVerion = new InstanceVersion(
                instances.Instances[instance].Version
                );

            // Create a new string builder that
            // contains the result JSON string.
            StringBuilder result = new StringBuilder();

            result.Append("{");

            result.Append(string.Format(
                              "\"FromVersion\": \"{0}\",",
                              instances.Instances[instance].Version
                              ));

            result.Append(string.Format(
                              "\"Errors\": [",
                              instances.Instances[instance].Version
                              ));

            bool hasErrors = false;

            foreach (string server in instances.Instances[instance].Servers)
            {
                if (instances.Servers.Items.ContainsKey(server) == false ||
                    instances.Servers.Items[server].State == ServerState.Offline)
                {
                    hasErrors = true;

                    result.Append(string.Format("\"{0}\",", string.Format(Global.LanguageManager.GetText(
                                                                              "SoftwareUpdateError_ServerOffline"),
                                                                          instances.Servers.Items.ContainsKey(server) ?
                                                                          instances.Servers.Items[server].Description :
                                                                          server
                                                                          )));
                }
            }

            if (hasErrors)
            {
                result = result.Remove(result.Length - 1, 1);

                result.Append("]}");

                context.Response.Write(result.ToString());

                return;
            }

            result.Append("],");

            result.Append("\"AvailableUpdates\": [");

            ServiceLink service = new ServiceLink(string.Format(
                                                      "http://{0}:8080/Handler.ashx",
                                                      instances.Instances[instance].Servers[0]
                                                      ));

            XmlDocument document = new XmlDocument();

            document.LoadXml(service.Request(new string[] {
                "Method=GetAvailableUpdates"
            }));

            foreach (XmlNode xmlNode in document.DocumentElement.SelectNodes("Update"))
            {
                InstanceVersion version = new InstanceVersion(
                    xmlNode.Attributes["Version"].Value
                    );

                if (version.ToInt() <= instanceVerion.ToInt())
                {
                    continue;
                }

                result.Append(string.Format(
                                  "\"{0}\",",
                                  (version).ToString()
                                  ));
            }

            if (result.ToString().EndsWith(","))
            {
                result = result.Remove(result.Length - 1, 1);
            }

            result.Append("]");

            result.Append("}");

            context.Response.Write(result.ToString());
        }