示例#1
0
        public void Save(string filename)
        {
            try
            {
                XmlDocument doc      = XMLHelper.CreateXmlDocument("Topology");
                XmlNode     rootNode = doc.LastChild;

                #region Parameter Section
                XmlNode paramsNode = XMLHelper.CreateChildNode(rootNode, "Parameters");
                foreach (OctopusLib.Parameter param in ParameterCollection)
                {
                    XmlNode paramNode = XMLHelper.CreateChildNode(paramsNode, "Parameter");
                    XMLHelper.CreateAttribute(paramNode, "Name", param.Name);
                    XMLHelper.CreateAttribute(paramNode, "Value", param.IsEncrypted ? Encryption.Encrypt(param.Value, _machineId) : param.Value);
                    if (param.IsEncrypted)
                    {
                        XMLHelper.CreateAttribute(paramNode, "IsEncrypted", param.IsEncrypted.ToString());
                    }
                }
                #endregion

                #region Machine Section
                XmlNode machinesNode = XMLHelper.CreateChildNode(rootNode, "Machines");
                foreach (OctopusLib.Machine machine in MachineCollection)
                {
                    XmlNode machineNode = XMLHelper.CreateChildNode(machinesNode, "Machine");
                    XMLHelper.CreateAttribute(machineNode, "Name", machine.Name);
                    XMLHelper.CreateAttribute(machineNode, "IP", machine.IP);
                    XMLHelper.CreateAttribute(machineNode, "Username", machine.Username);
                    XMLHelper.CreateAttribute(machineNode, "Password", Regex.IsMatch(machine.Password, @"{%(.+?)%}") ? machine.Password : Encryption.Encrypt(machine.Password, _machineId));
                    XMLHelper.CreateAttribute(machineNode, "Domain", machine.Domain);
                    XMLHelper.CreateAttribute(machineNode, "Architecture", machine.Architecture.ToString());
                    XMLHelper.CreateAttribute(machineNode, "IsLinux", machine.IsLinux.ToString());
                }
                #endregion

                #region Action Section
                XmlNode actionsNode = XMLHelper.CreateChildNode(rootNode, "Actions");
                foreach (OctopusLib.Action action in ActionCollection)
                {
                    XmlNode actionNode = XMLHelper.CreateChildNode(actionsNode, "Action");
                    XMLHelper.CreateAttribute(actionNode, "Name", action.Name);
                    foreach (OctopusLib.Command command in action.ActionCommands.OrderBy(o => o.Sequence))
                    {
                        XmlNode cmdNode = XMLHelper.CreateChildNode(actionNode, "Command");
                        XMLHelper.CreateAttribute(cmdNode, "Type", command.CommandType.ToString());
                        XMLHelper.CreateAttribute(cmdNode, "Try", command.Try.ToString());
                        XMLHelper.CreateAttribute(cmdNode, "IsEnabled", command.IsEnabled.ToString());
                        XMLHelper.CreateAttribute(cmdNode, "RetryTimes", command.RetryTimes.ToString());
                        XMLHelper.CreateAttribute(cmdNode, "RetryIntervalSeconds", command.RetryIntervalSeconds.ToString());
                        switch (command.CommandType)
                        {
                        case RunCommandType.Copy:
                            XMLHelper.CreateAttribute(cmdNode, "Direction", ((OctopusLib.CopyCommand)command).CopyDirection.ToString());
                            if (!string.IsNullOrEmpty(((OctopusLib.CopyCommand)command).CopySourceFiles))
                            {
                                XMLHelper.CreateAttribute(cmdNode, "SourceFiles", ((OctopusLib.CopyCommand)command).CopySourceFiles.ToString());
                            }
                            if (!string.IsNullOrEmpty(((OctopusLib.CopyCommand)command).CopySourceDir))
                            {
                                XMLHelper.CreateAttribute(cmdNode, "SourceDir", ((OctopusLib.CopyCommand)command).CopySourceDir.ToString());
                            }
                            XMLHelper.CreateAttribute(cmdNode, "TargetDir", ((OctopusLib.CopyCommand)command).CopyTargetDir.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "Force", ((OctopusLib.CopyCommand)command).IsForce.ToString());
                            break;

                        case RunCommandType.Remote:
                            XMLHelper.CreateAttribute(cmdNode, "ExpectedResult", ((OctopusLib.RemoteCommand)command).ExpectedResult.ToString());
                            if (!string.IsNullOrEmpty(((OctopusLib.RemoteCommand)command).WorkingDirectory))
                            {
                                XMLHelper.CreateAttribute(cmdNode, "WorkingDirectory", ((OctopusLib.RemoteCommand)command).WorkingDirectory.ToString());
                            }
                            XMLHelper.CreateAttribute(cmdNode, "Reboot", ((OctopusLib.RemoteCommand)command).IsRebootRequired.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "Interactive", ((OctopusLib.RemoteCommand)command).IsUIInteractive.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "NotLoadProfile", ((OctopusLib.RemoteCommand)command).IsNotLoadProfile.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "RunAsSystemAccount", ((OctopusLib.RemoteCommand)command).IsRunAsSystemAccount.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "RunAsLimittedUser", ((OctopusLib.RemoteCommand)command).IsRunAsLimittedUser.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "Terminate", ((OctopusLib.RemoteCommand)command).IsNotWaitForTerminate.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "Architecture", ((OctopusLib.RemoteCommand)command).Architecture.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "TimeOutSeconds", ((OctopusLib.RemoteCommand)command).TimeOutSeconds.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "RemoteRunAsUsername", string.IsNullOrEmpty(((OctopusLib.RemoteCommand)command).RemoteRunAsUsername) ? string.Empty : ((OctopusLib.RemoteCommand)command).RemoteRunAsUsername.ToString());
                            XMLHelper.CreateAttribute(cmdNode,
                                                      "RemoteRunAsPassword",
                                                      string.IsNullOrEmpty(((OctopusLib.RemoteCommand)command).RemoteRunAsPassword) ? string.Empty : Regex.IsMatch(((OctopusLib.RemoteCommand)command).RemoteRunAsPassword.ToString(), @"{%(.+?)%}") ?
                                                      ((OctopusLib.RemoteCommand)command).RemoteRunAsPassword.ToString() : Encryption.Encrypt(((OctopusLib.RemoteCommand)command).RemoteRunAsPassword.ToString(), _machineId));
                            XMLHelper.CreateAttribute(cmdNode, "OutputParameter", string.IsNullOrEmpty(((OctopusLib.RemoteCommand)command).OutputParameter) ? string.Empty : ((OctopusLib.RemoteCommand)command).OutputParameter.ToString());
                            cmdNode.InnerXml = System.Net.WebUtility.HtmlEncode(command.CommandText);
                            break;

                        case RunCommandType.Local:
                            XMLHelper.CreateAttribute(cmdNode, "IsSystemCommand", ((OctopusLib.LocalCommand)command).IsSystemCommand.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "ExpectedResult", ((OctopusLib.LocalCommand)command).ExpectedResult.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "Architecture", ((OctopusLib.LocalCommand)command).Architecture.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "TimeOutSeconds", ((OctopusLib.LocalCommand)command).TimeOutSeconds.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "OutputParameter", string.IsNullOrEmpty(((OctopusLib.LocalCommand)command).OutputParameter) ? string.Empty : ((OctopusLib.LocalCommand)command).OutputParameter.ToString());
                            cmdNode.InnerXml = System.Net.WebUtility.HtmlEncode(command.CommandText);
                            break;

                        case RunCommandType.LinuxSSH:
                            XMLHelper.CreateAttribute(cmdNode, "ExpectedResult", ((OctopusLib.LinuxSSHCommand)command).ExpectedResult == null ? string.Empty : ((OctopusLib.LinuxSSHCommand)command).ExpectedResult.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "ExpectedPrompt", ((OctopusLib.LinuxSSHCommand)command).ExpectedPrompt == null ? string.Empty : ((OctopusLib.LinuxSSHCommand)command).ExpectedPrompt.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "SSHType", ((OctopusLib.LinuxSSHCommand)command).SshType.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "TimeOutSeconds", ((OctopusLib.LinuxSSHCommand)command).TimeOutSeconds.ToString());
                            XMLHelper.CreateAttribute(cmdNode, "Reboot", ((OctopusLib.LinuxSSHCommand)command).IsRebootRequired.ToString());
                            if (!string.IsNullOrEmpty(command.CommandText))
                            {
                                cmdNode.InnerXml = System.Net.WebUtility.HtmlEncode(command.CommandText);
                            }
                            break;
                        }
                    }
                }
                #endregion

                #region Task Section
                XmlNode runsNode = XMLHelper.CreateChildNode(rootNode, "Tasks");
                foreach (OctopusLib.Task task in TaskCollection.OrderBy(o => o.Sequence))
                {
                    XmlNode runNode = XMLHelper.CreateChildNode(runsNode, "Task");
                    XMLHelper.CreateAttribute(runNode, "Name", task.Name);
                    XMLHelper.CreateAttribute(runNode, "Sequence", task.Sequence.ToString());
                    XMLHelper.CreateAttribute(runNode, "IsEnabled", task.IsEnabled.ToString());

                    StringBuilder msb = new StringBuilder();
                    foreach (OctopusLib.Machine machine in task.Machines)
                    {
                        if (msb.Length == 0)
                        {
                            msb.Append(string.Format("{0}", machine.Name));
                        }
                        else
                        {
                            msb.Append(string.Format(",{0}", machine.Name));
                        }
                    }
                    XMLHelper.CreateAttribute(runNode, "Machine", msb.ToString());
                    foreach (OctopusLib.TaskAction taskAction in task.TaskActionCollection.OrderBy(o => o.Sequence))
                    {
                        XmlNode taskActionNode = XMLHelper.CreateChildNode(runNode, "TaskAction");
                        taskActionNode.InnerText = taskAction.Name;
                        if (taskAction.Machine != null)
                        {
                            XMLHelper.CreateAttribute(taskActionNode, "Machine", taskAction.Machine.Name.ToString());
                        }
                        XMLHelper.CreateAttribute(taskActionNode, "IsEnabled", taskAction.IsEnabled.ToString());
                        XMLHelper.CreateAttribute(taskActionNode, "Fixed", taskAction.IsFixed.ToString());
                    }
                }
                #endregion

                doc.Save(filename);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }