-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
233 lines (200 loc) · 10.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using AlexPilotti.FTPS.Client;
using AlexPilotti.FTPS.Common;
using Assimilated.DMVRU.Properties;
using Assimilated.DMVRU.Util;
namespace Assimilated.DMVRU
{
class Program
{
private static int stepCounter = 0;
static void Main(string[] commandLineArgs)
{
Helpers.PrintIntro();
Init(commandLineArgs);
SetupWorkingDirectory();
try
{
using (var client = new FTPSClient())
{
// connect to server
Helpers.PrintStep("Connecting to server.");
client.Connect(RuntimeConfig.Host,
new NetworkCredential(RuntimeConfig.UserName, RuntimeConfig.Password),
ESSLSupportMode.ClearText);
Helpers.PrintResult(client.WelcomeMessage);
// verify siteroot exists
try
{
Helpers.PrintStep("Verifying SiteRoot exists.");
client.SetCurrentDirectory(RuntimeConfig.SiteRoot);
}
catch (FTPException)
{
Helpers.PrintResult("SiteRoot '{0}' does not exists on server.", RuntimeConfig.SiteRoot);
Helpers.PrintResult("Cannot continue.");
CleanUp();
Environment.Exit(1);
}
// verify sitedirectory exists
try
{
Helpers.PrintStep("Verifying existing Drupal installations 'sites' directory exists.");
client.SetCurrentDirectory("/");
client.SetCurrentDirectory(RuntimeConfig.SitesDirectory);
}
catch (FTPException)
{
Helpers.PrintResult("Sites directory '{0}' does not exists on server.", RuntimeConfig.SitesDirectory);
Helpers.PrintResult("Cannot continue.");
CleanUp();
Environment.Exit(1);
}
client.SetCurrentDirectory("/");
// create backup of existing config files
// e.g. .htaccess, robots.txt.
try
{
Helpers.PrintStep("Creating backup folder.");
client.MakeDir(RuntimeConfig.BackupDirectory);
}
catch (FTPCommandException exc)
{
Console.WriteLine(" Backup directory already exists, existing content will be overridden.");
Console.Write(" Press [Enter] or [Y] to continue, press [N] to abort. ");
if (!Helpers.ShouldContinue())
{
CleanUp();
Console.WriteLine();
Console.WriteLine("Existing!");
Environment.Exit(0);
}
}
Helpers.PrintStep("Backing up (customized) files.");
foreach (var file in RuntimeConfig.CustomizedFiles)
{
var source = RuntimeConfig.SiteRoot + "/" + file;
var target = RuntimeConfig.BackupDirectory + "/" + file;
client.MoveFile(source, target);
}
// upload maintenance files
Helpers.PrintStep("Uploading 'Maintenance' .htaccess and .php files to server.");
client.PutFile(RuntimeConfig.HTAccessFile.FullName, RuntimeConfig.SiteRoot + "/.htaccess");
client.PutFile(RuntimeConfig.MaintenanceFile.FullName, RuntimeConfig.SiteRoot + "/maintenance.php");
// upload drupal upgrade package
Helpers.PrintStep("Uploading Drupal upgrade.");
Console.Write(" Uploading ");
var spinner = ConsoleSpinner.Create();
spinner.Start();
client.PutFiles(RuntimeConfig.UpgradeDirectory.FullName, "", "*", EPatternStyle.Wildcard, true, null);
spinner.Stop();
Console.WriteLine();
// assumes name of drupal upgrade file is the same as the catalog inside it
var remoteUpgradeDir = RuntimeConfig.UpgradeDirectory.GetDirectories().First().Name;
var remoteSitesUpgradeDir = remoteUpgradeDir + "/sites";
// delete sites folder in upgrade
Helpers.PrintStep("Deleting 'sites' folder in Drupal upgrade directory ({0}).", remoteSitesUpgradeDir);
Helpers.RecrusiveDelete(client, remoteUpgradeDir + "/sites");
// move sites folder from website to upgrade
Helpers.PrintStep("Move '{0}' folder from siteroot to upgrade folder.", RuntimeConfig.SitesDirectory);
client.RenameFile(RuntimeConfig.SitesDirectory, remoteSitesUpgradeDir);
Helpers.PrintStep("Restore backed up files?");
Console.Write(" Press [Enter] or [Y] to continue, press [N] to abort. ");
if (Helpers.ShouldContinue())
{
Helpers.PrintStep("Restoring backed up files.");
foreach (var file in RuntimeConfig.CustomizedFiles)
{
var source = RuntimeConfig.BackupDirectory + "/" + file;
var target = remoteUpgradeDir + "/" + file;
client.MoveFile(source, target);
}
}
// rename website to website-old
var backupName = RuntimeConfig.BackupDirectory + "/" + string.Format("drupal-old-{0}", DateTime.UtcNow.ToString("yyyy-mm-dd"));
Helpers.PrintStep("Rename {0} to '{1}'.", RuntimeConfig.SiteRoot, backupName);
client.RenameFile(RuntimeConfig.SiteRoot, backupName);
// rename upgrade to website
Helpers.PrintStep("Rename upgrade folder to siteroot.");
client.RenameFile(remoteUpgradeDir, RuntimeConfig.SiteRoot);
Console.WriteLine();
Console.WriteLine("DONE!");
Console.WriteLine();
Console.WriteLine("Remember to reapply changes rebots.txt, .htaccess, and settings.php if the upgrade requires it.");
Console.WriteLine("Feel free to remove the backup directory once done.");
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("An error ocurred!{0}{1}", Environment.NewLine, ex.Message);
}
finally
{
CleanUp();
Environment.Exit(1);
}
}
static void Init(string[] commandLineArgs)
{
var args = new Arguments(commandLineArgs);
if (args["help"] != null)
{
Helpers.PrintHelp();
Environment.Exit(0);
}
// get ftp connection info
if (string.IsNullOrWhiteSpace(args["host"])) Helpers.ExitWithInputError("host");
RuntimeConfig.Host = args["host"];
int port;
RuntimeConfig.Port = !string.IsNullOrWhiteSpace(args["port"]) && int.TryParse(args["port"], out port) ? port : 21;
if (string.IsNullOrWhiteSpace(args["username"])) Helpers.ExitWithInputError("username");
RuntimeConfig.UserName = args["username"];
if (string.IsNullOrWhiteSpace(args["password"])) Helpers.ExitWithInputError("password");
RuntimeConfig.Password = args["password"];
// get directory info
if (string.IsNullOrWhiteSpace(args["siteroot"])) Helpers.ExitWithInputError("siteroot");
RuntimeConfig.SiteRoot = args["siteroot"];
RuntimeConfig.BackupDirectory = string.IsNullOrWhiteSpace(args["backupdirectory"]) ? "backup" : args["backupdirectory"];
// get sitesdirectory
//RuntimeConfig.SitesDirectory = !string.IsNullOrWhiteSpace(args["sitesdirectory"]) ? args["sitesdirectory"] : RuntimeConfig.SiteRoot + "/sites";
RuntimeConfig.SitesDirectory = RuntimeConfig.SiteRoot + "/sites";
// get upgrade file
if (string.IsNullOrWhiteSpace(args["upgradefile"])) Helpers.ExitWithInputError("upgradefile");
if (!File.Exists(args["upgradefile"])) Helpers.ExitWithInputError("upgradefile", "File does not exists.");
RuntimeConfig.UpgradeFile = new FileInfo(args["upgradefile"]);
// create list of customized files
RuntimeConfig.CustomizedFiles = new List<string>() { ".htaccess", "robots.txt" };
}
private static void SetupWorkingDirectory()
{
// create maintenenace files
Helpers.PrintStep("Creating maintenance files");
RuntimeConfig.HTAccessFile = new FileInfo(Path.GetTempFileName());
using (var outfile = File.CreateText(RuntimeConfig.HTAccessFile.FullName))
{
outfile.Write(Resources.HTAccessFile);
}
RuntimeConfig.MaintenanceFile = new FileInfo(Path.GetTempFileName());
using (var outfile = File.CreateText(RuntimeConfig.MaintenanceFile.FullName))
{
outfile.Write(Resources.MaintenancePage);
}
// unzip drupal upgrade
Helpers.PrintStep("Unzipping drupal upgrade");
RuntimeConfig.UpgradeDirectory = new DirectoryInfo(Helpers.GetTemporaryDirectory());
Helpers.Unzip(RuntimeConfig.UpgradeDirectory, RuntimeConfig.UpgradeFile);
}
private static void CleanUp()
{
Directory.SetCurrentDirectory(Path.GetTempPath());
if (RuntimeConfig.HTAccessFile.Exists) RuntimeConfig.HTAccessFile.Delete();
if (RuntimeConfig.MaintenanceFile.Exists) RuntimeConfig.MaintenanceFile.Delete();
if (RuntimeConfig.UpgradeDirectory.Exists) RuntimeConfig.UpgradeDirectory.Delete(true);
}
}
}