From 848221fc0ae7c3242881889d38fca32a49e20b46 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Wed, 27 Sep 2017 14:14:23 -0400 Subject: [PATCH] Adds a function to recreate static directories --- TGCommandLine/AdminCommands.cs | 23 ++++++++++++++-- TGControlPanel/Main.Designer.cs | 13 ++++++++++ TGControlPanel/Program.cs | 10 +++++++ TGControlPanel/ServerPage.cs | 18 ++++++++++--- TGControlPanel/StaticPage.cs | 22 ++++++++++++++++ TGServerService/Administration.cs | 39 ++++++++++++++++++++++++++++ TGServerService/Repository.cs | 37 ++++++++++++++------------ TGServiceInterface/Administration.cs | 7 +++++ 8 files changed, 148 insertions(+), 21 deletions(-) diff --git a/TGCommandLine/AdminCommands.cs b/TGCommandLine/AdminCommands.cs index 7822c9dd4c..132ee6b395 100644 --- a/TGCommandLine/AdminCommands.cs +++ b/TGCommandLine/AdminCommands.cs @@ -9,14 +9,14 @@ namespace TGCommandLine public AdminCommand() { Keyword = "admin"; - Children = new Command[] { new AdminViewGroupCommand(), new AdminSetGroupCommand(), new AdminClearGroupCommand(), new AdminViewPortCommand(), new AdminSetPortCommand(), new AdminMoveServerCommand() }; + Children = new Command[] { new AdminViewGroupCommand(), new AdminSetGroupCommand(), new AdminClearGroupCommand(), new AdminViewPortCommand(), new AdminSetPortCommand(), new AdminMoveServerCommand(), new AdminRecreateStaticCommand() }; } public override string GetHelpText() { return "Manage server service authentication"; } } - + class AdminMoveServerCommand : Command { public AdminMoveServerCommand() @@ -42,6 +42,25 @@ namespace TGCommandLine return "Move the server installation (BYOND, Repo, Game) to a new location. Nothing else may be running for this task to complete"; } } + class AdminRecreateStaticCommand : Command + { + public AdminRecreateStaticCommand() + { + Keyword = "recreate-static-directory"; + } + + protected override ExitCode Run(IList parameters) + { + var res = Server.GetComponent().RecreateStaticFolder(); + OutputProc(res ?? "Success"); + return res == null ? ExitCode.Normal : ExitCode.ServerError; + } + + public override string GetHelpText() + { + return "Backup the current static directory and repopulate it from the TGS3.json in the repository"; + } + } class AdminSetPortCommand : Command { diff --git a/TGControlPanel/Main.Designer.cs b/TGControlPanel/Main.Designer.cs index acd77ebb86..9e679c9776 100644 --- a/TGControlPanel/Main.Designer.cs +++ b/TGControlPanel/Main.Designer.cs @@ -154,6 +154,7 @@ this.StaticFileCreateButton = new System.Windows.Forms.Button(); this.label6 = new System.Windows.Forms.Label(); this.StaticFileListBox = new System.Windows.Forms.ListBox(); + this.RecreateStaticButton = new System.Windows.Forms.Button(); this.ChatPanel.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.ChatPortSelector)).BeginInit(); this.ChatProviderSelectorPanel.SuspendLayout(); @@ -1611,6 +1612,7 @@ // StaticPanel // this.StaticPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(39)))), ((int)(((byte)(40)))), ((int)(((byte)(34))))); + this.StaticPanel.Controls.Add(this.RecreateStaticButton); this.StaticPanel.Controls.Add(this.StaticFileDownloadButton); this.StaticPanel.Controls.Add(this.StaticFilesRefreshButton); this.StaticPanel.Controls.Add(this.StaticFileUploadButton); @@ -1725,6 +1727,16 @@ this.StaticFileListBox.TabIndex = 0; this.StaticFileListBox.SelectedIndexChanged += new System.EventHandler(this.StaticFileListBox_SelectedIndexChanged); // + // RecreateStaticButton + // + this.RecreateStaticButton.Location = new System.Drawing.Point(471, 6); + this.RecreateStaticButton.Name = "RecreateStaticButton"; + this.RecreateStaticButton.Size = new System.Drawing.Size(144, 22); + this.RecreateStaticButton.TabIndex = 33; + this.RecreateStaticButton.Text = "Recreate Static Directory"; + this.RecreateStaticButton.UseVisualStyleBackColor = true; + this.RecreateStaticButton.Click += new System.EventHandler(this.RecreateStaticButton_Click); + // // Main // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -1887,5 +1899,6 @@ private System.Windows.Forms.Button TGSJsonUpdate; private System.Windows.Forms.Button BYONDRefreshButton; private System.Windows.Forms.Button ServerPageRefreshButton; + private System.Windows.Forms.Button RecreateStaticButton; } } diff --git a/TGControlPanel/Program.cs b/TGControlPanel/Program.cs index 280b4f0d54..9542f8d9d6 100644 --- a/TGControlPanel/Program.cs +++ b/TGControlPanel/Program.cs @@ -44,6 +44,16 @@ namespace TGControlPanel return true; } + public static bool CheckAdminWithWarning() + { + if (!Server.AuthenticateAdmin()) + { + MessageBox.Show("Only system administrators may use this command!"); + return false; + } + return true; + } + public static void ServiceDisconnectException(Exception e) { MessageBox.Show("An unhandled exception occurred. This usually means we lost connection to the service. Error" + e.ToString()); diff --git a/TGControlPanel/ServerPage.cs b/TGControlPanel/ServerPage.cs index c4cf091de4..b6315f0a39 100644 --- a/TGControlPanel/ServerPage.cs +++ b/TGControlPanel/ServerPage.cs @@ -21,10 +21,14 @@ namespace TGControlPanel string updateError; bool updatingFields = false; - string DDStatusString = null; void InitServerPage() { LoadServerPage(); + if (!Server.AuthenticateAdmin()) + { + ServerPathTextbox.Enabled = false; + ServerPathTextbox.ReadOnly = true; + } FullUpdateWorker.RunWorkerCompleted += FullUpdateWorker_RunWorkerCompleted; ServerPathTextbox.LostFocus += ServerPathTextbox_LostFocus; ServerPathTextbox.KeyDown += ServerPathTextbox_KeyDown; @@ -77,9 +81,10 @@ namespace TGControlPanel void UpdateServerPath() { - if (!Server.AuthenticateAdmin()) + if (!Program.CheckAdminWithWarning()) { - MessageBox.Show("Only system administrators may move the server installation"); + ServerPathTextbox.Enabled = false; + ServerPathTextbox.ReadOnly = true; return; } if (updatingFields || ServerPathTextbox.Text.Trim() == Server.GetComponent().ServerDirectory()) @@ -87,6 +92,13 @@ namespace TGControlPanel var DialogResult = MessageBox.Show("This will move the entire server installation.", "Confim", MessageBoxButtons.YesNo); if (DialogResult != DialogResult.Yes) return; + + if (!Program.CheckAdminWithWarning()) + { + ServerPathTextbox.Enabled = false; + ServerPathTextbox.ReadOnly = true; + return; + } MessageBox.Show(Server.GetComponent().MoveServer(ServerPathTextbox.Text) ?? "Success!"); } diff --git a/TGControlPanel/StaticPage.cs b/TGControlPanel/StaticPage.cs index 192f9a8635..6e867f0967 100644 --- a/TGControlPanel/StaticPage.cs +++ b/TGControlPanel/StaticPage.cs @@ -22,6 +22,8 @@ namespace TGControlPanel void InitStaticPage() { + if(!Server.AuthenticateAdmin()) + RecreateStaticButton.Visible = false; BuildFileList(); } @@ -317,5 +319,25 @@ namespace TGControlPanel } } } + + private void RecreateStaticButton_Click(object sender, EventArgs e) + { + if (!Program.CheckAdminWithWarning()) + { + RecreateStaticButton.Visible = false; + return; + } + if (MessageBox.Show("This will rename the current static directory to a backup and recreate it. Continue?", "Confirm", MessageBoxButtons.YesNo) != DialogResult.Yes) + return; + if (!Program.CheckAdminWithWarning()) + { + RecreateStaticButton.Visible = false; + return; + } + var res = Server.GetComponent().RecreateStaticFolder(); + if (res != null) + MessageBox.Show(res); + BuildFileList(); + } } } diff --git a/TGServerService/Administration.cs b/TGServerService/Administration.cs index a9a24818fd..cc43d818e9 100644 --- a/TGServerService/Administration.cs +++ b/TGServerService/Administration.cs @@ -239,5 +239,44 @@ namespace TGServerService return e.ToString(); } } + public string RecreateStaticFolder() + { + if (!Monitor.TryEnter(RepoLock)) + return "Repo locked!"; + try + { + if (!Monitor.TryEnter(watchdogLock)) + return "Watchdog locked!"; + try + { + if (!Monitor.TryEnter(configLock)) + return "Static dir locked!"; + try + { + if (currentStatus != TGDreamDaemonStatus.Offline) + return "Watchdog running!"; + BackupAndDeleteStaticDirectory(); + InitialConfigureRepository(); + } + finally + { + Monitor.Exit(configLock); + } + } + finally + { + Monitor.Exit(watchdogLock); + } + } + catch(Exception e) + { + return e.ToString(); + } + finally + { + Monitor.Exit(RepoLock); + } + return null; + } } } diff --git a/TGServerService/Repository.cs b/TGServerService/Repository.cs index c52193723b..460e8050b4 100644 --- a/TGServerService/Repository.cs +++ b/TGServerService/Repository.cs @@ -249,22 +249,7 @@ namespace TGServerService DeletePRList(); lock (configLock) { - if (Directory.Exists(StaticDirs)) - { - int count = 1; - - string path = Path.GetDirectoryName(StaticBackupDir); - string newFullPath = StaticBackupDir; - - while (File.Exists(newFullPath) || Directory.Exists(newFullPath)) - { - string tempDirName = string.Format("{0}({1})", StaticBackupDir, count++); - newFullPath = Path.Combine(path, tempDirName); - } - - Program.CopyDirectory(StaticDirs, newFullPath); - } - Program.DeleteDirectory(StaticDirs); + BackupAndDeleteStaticDirectory(); } var Opts = new CloneOptions() @@ -309,6 +294,26 @@ namespace TGServerService } } + void BackupAndDeleteStaticDirectory() + { + if (Directory.Exists(StaticDirs)) + { + int count = 1; + + string path = Path.GetDirectoryName(StaticBackupDir); + string newFullPath = StaticBackupDir; + + while (File.Exists(newFullPath) || Directory.Exists(newFullPath)) + { + string tempDirName = string.Format("{0}({1})", StaticBackupDir, count++); + newFullPath = Path.Combine(path, tempDirName); + } + + Program.CopyDirectory(StaticDirs, newFullPath); + } + Program.DeleteDirectory(StaticDirs); + } + public string UpdateTGS3Json() { try diff --git a/TGServiceInterface/Administration.cs b/TGServiceInterface/Administration.cs index d5f211a7f4..fbb7964003 100644 --- a/TGServiceInterface/Administration.cs +++ b/TGServiceInterface/Administration.cs @@ -46,5 +46,12 @@ namespace TGServiceInterface /// null on success, error message on failure [OperationContract] string MoveServer(string new_location); + + /// + /// Renames the current static folder to a backup name and recreates is from the current repo using TGS3.json + /// + /// null on success, error message on failure + [OperationContract] + string RecreateStaticFolder(); } }