#region Disclaimer / License
// Copyright (C) 2011, Jackie Ng, jumpinjackie@gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Globalization;
using System.IO;
namespace InstantSetup.Core
{
public enum ApiType
{
Php,
Java,
DotNet
}
public delegate void FormatLineWriter(string msg, params object [] args);
///
/// Base configuration setup process
///
public abstract class AbstractSetupConfigurationProcess
{
protected AbstractSetupConfigurationProcess()
{
this.EnablePhp = true;
this.WriteMentorDictionaryPath = false;
this.DefaultViewer = ApiType.Php;
//Port numbers specified not to intentionally clash with an existing
//MGOS installation
this.ServerAdminPort = 2820;
this.ServerClientPort = 2821;
this.ServerSitePort = 2822;
}
public virtual void WriteSummary(FormatLineWriter writer)
{
writer("Backup config files: {0}", this.BackupConfigFiles);
writer("Backup config path: {0}", this.BackupConfigPath);
writer("Batch file output dir: {0}", this.BatchFileOutputDirectory);
writer("Source directory: {0}", this.BuildOutputPath);
writer("CS-Map directory: {0}", this.CsMapDictionaryDir);
writer("Default Viewer: {0}", this.DefaultViewer);
writer("Enable PHP: {0}", this.EnablePhp);
writer("Install Services: {0}", this.InstallServices);
writer("64-bit MapGuide: {0}", this.Is64BitMapGuide);
writer("MapGuide Service Name: {0}", this.MapGuideServiceName);
writer("Server Admin Port: {0}", this.ServerAdminPort);
writer("Server Client Port: {0}", this.ServerClientPort);
writer("Server Site Port: {0}", this.ServerSitePort);
writer("Server root dir: {0}", this.ServerRootDir);
writer("Server bin dir: {0}", this.ServerBinDir);
writer("Virtual Directory: {0}", this.VirtualDirectoryName);
writer("Map Agent directory: {0}", this.WebTierMapAgentDir);
writer("PHP directory: {0}", this.WebTierPhpDir);
writer("Web tier public dir: {0}", this.WebTierPublicDir);
writer("Web tier root dir: {0}", this.WebTierRootDir);
writer("Write MENTOR_DICTIONARY_PATH: {0}", this.WriteMentorDictionaryPath);
}
///
/// Gets or sets whether the installation of MapGuide to be configured is 64-bit
///
public bool Is64BitMapGuide { get; set; }
public bool WriteMentorDictionaryPath { get; set; }
public string BatchFileOutputDirectory { get; set; }
public string MapGuideServiceName { get; set; }
public bool InstallServices { get; set; }
public string BackupConfigPath { get; set; }
public bool BackupConfigFiles { get; set; }
///
/// Gets or sets the build output path where the result of build.bat
/// in the MapGuide Source root copies files to
///
public string BuildOutputPath { get; set; }
///
/// The server bin directory
///
public virtual string ServerBinDir
{
get
{
return Path.Combine(ServerRootDir, "bin");
}
}
///
/// The server tier root directory
///
public virtual string ServerRootDir
{
get { return Path.Combine(BuildOutputPath, "Server"); }
}
///
/// The CS-Map Dictionary directory
///
public virtual string CsMapDictionaryDir
{
get { return Path.Combine(BuildOutputPath, "CS-Map\\Dictionaries"); }
}
///
/// The www directory of the web tier
///
public virtual string WebTierPublicDir
{
get { return Path.Combine(WebTierRootDir, "www"); }
}
///
/// The web tier root directory
///
public virtual string WebTierRootDir
{
get
{
return Path.Combine(BuildOutputPath, "Web");
}
}
///
/// The mapagent directory of the web tier
///
public virtual string WebTierMapAgentDir
{
get { return Path.Combine(WebTierPublicDir, "mapagent"); }
}
///
/// The PHP web tier directory
///
public virtual string WebTierPhpDir
{
get
{
return Path.Combine(WebTierRootDir, "Php");
}
}
public bool EnablePhp { get; set; }
///
/// The name of the mapguide virtual directory to create
///
public string VirtualDirectoryName { get; set; }
///
/// TCP/IP number for client operations
///
public int ServerClientPort { get; set; }
///
/// TCP/IP number for admin operations
///
public int ServerAdminPort { get; set; }
///
/// TCP/IP number for port operations
///
public int ServerSitePort { get; set; }
///
/// The default viewer that mapviewerajax will resolve to
///
public ApiType DefaultViewer { get; set; }
private IniFile _serverConfig;
private IniFile _webConfig;
private IniFile _phpConfig;
public void Execute(FormatLineWriter writer)
{
ValidateConfigSettings(writer);
if (this.BackupConfigFiles)
{
writer("Backing up existing configuration files");
if (string.IsNullOrEmpty(this.BackupConfigPath))
throw new InvalidOperationException("Please specify a backup path for config files");
else
{
if (!Directory.Exists(this.BackupConfigPath))
Directory.CreateDirectory(this.BackupConfigPath);
DoBackupConfigFiles(writer);
}
}
if (!Directory.Exists(this.BatchFileOutputDirectory))
Directory.CreateDirectory(this.BatchFileOutputDirectory);
//Process serverconfig.ini
string serverConfigIni = Path.Combine(this.ServerBinDir, "serverconfig.ini");
if (!File.Exists(serverConfigIni))
throw new InvalidOperationException("Could not find expected file: " + serverConfigIni);
string webConfigIni = Path.Combine(this.WebTierPublicDir, "webconfig.ini");
if (!File.Exists(webConfigIni))
throw new InvalidOperationException("Could not find expected file: " + webConfigIni);
string phpIni = Path.Combine(this.WebTierPhpDir, "php.ini");
if (!File.Exists(phpIni))
throw new InvalidOperationException("Could not find expected file: " + phpIni);
_serverConfig = new IniFile(serverConfigIni);
_webConfig = new IniFile(webConfigIni);
_phpConfig = new IniFile(phpIni);
SetServerConfigProperties(writer);
SetWebConfigProperties(writer);
SetPhpConfigProperties(writer);
ConfigureWebServer(writer);
if (this.InstallServices)
{
throw new NotImplementedException("Service installation not implemented");
}
else
{
if (this.WriteMentorDictionaryPath)
{
writer("Writing mgserver.bat");
//Write server batch file
string serverText = string.Format(Properties.Resources.SERVER, this.CsMapDictionaryDir, this.ServerBinDir);
File.WriteAllText(Path.Combine(this.BatchFileOutputDirectory, "mgserver.bat"), serverText);
writer("Writing mgserverinstall.bat");
//Write service install batch file
serverText = string.Format(Properties.Resources.SERVER_INSTALL, this.CsMapDictionaryDir, this.ServerBinDir, this.MapGuideServiceName);
File.WriteAllText(Path.Combine(this.BatchFileOutputDirectory, "mgserverinstall.bat"), serverText);
writer("Writing mgserveruninstall.bat");
//Write service uninstall batch file
serverText = string.Format(Properties.Resources.SERVER_UNINSTALL, this.CsMapDictionaryDir, this.ServerBinDir, this.MapGuideServiceName);
File.WriteAllText(Path.Combine(this.BatchFileOutputDirectory, "mgserveruninstall.bat"), serverText);
}
else //Post RFC 122
{
writer("Writing mgserver.bat");
//Write server batch file
string serverText = string.Format(Properties.Resources.SERVER_NO_MENTOR, this.ServerBinDir);
File.WriteAllText(Path.Combine(this.BatchFileOutputDirectory, "mgserver.bat"), serverText);
writer("Writing mgserverinstall.bat");
//Write service install batch file
serverText = string.Format(Properties.Resources.SERVER_INSTALL_NO_MENTOR, this.ServerBinDir, this.MapGuideServiceName);
File.WriteAllText(Path.Combine(this.BatchFileOutputDirectory, "mgserverinstall.bat"), serverText);
writer("Writing mgserveruninstall.bat");
//Write service uninstall batch file
serverText = string.Format(Properties.Resources.SERVER_UNINSTALL_NO_MENTOR, this.ServerBinDir, this.MapGuideServiceName);
File.WriteAllText(Path.Combine(this.BatchFileOutputDirectory, "mgserveruninstall.bat"), serverText);
}
WriteAdditionalBatchFiles(writer);
}
}
protected abstract void WriteAdditionalBatchFiles(FormatLineWriter writer);
protected abstract void ValidateConfigSettings(FormatLineWriter writer);
protected abstract void ConfigureWebServer(FormatLineWriter writer);
private void SetPhpConfigProperties(FormatLineWriter writer)
{
_phpConfig.IniWriteValue("PHP", "extension_dir", Path.Combine(this.WebTierPhpDir, "ext"));
_phpConfig.IniWriteValue("Session", "session.save_path", Path.Combine(this.WebTierRootDir, "Temp"));
var tempDir = Path.Combine(this.WebTierRootDir, "Temp");
_phpConfig.IniWriteValue("PHP", "sys_temp_dir", tempDir);
}
private void SetWebConfigProperties(FormatLineWriter writer)
{
//Port numbers
_webConfig.IniWriteValue("AdministrativeConnectionProperties", "Port", this.ServerAdminPort.ToString(CultureInfo.InvariantCulture));
_webConfig.IniWriteValue("ClientConnectionProperties", "Port", this.ServerClientPort.ToString(CultureInfo.InvariantCulture));
_webConfig.IniWriteValue("SiteConnectionProperties","Port",this.ServerSitePort.ToString(CultureInfo.InvariantCulture));
_webConfig.IniWriteValue("GeneralProperties", "ResourcesPath", Path.Combine(this.WebTierMapAgentDir, "Resources"));
var tempDir = Path.Combine(this.WebTierRootDir, "Temp");
_webConfig.IniWriteValue("GeneralProperties", "TempPath", tempDir);
var logsDir = Path.Combine(this.WebTierRootDir, "Logs");
_webConfig.IniWriteValue("GeneralProperties", "LogsPath", logsDir);
if (!Directory.Exists(tempDir))
Directory.CreateDirectory(tempDir);
//Post RFC 122
if (!this.WriteMentorDictionaryPath)
_webConfig.IniWriteValue("GeneralProperties", "MentorDictionaryPath", this.CsMapDictionaryDir);
_webConfig.IniWriteValue("SiteConnectionProperties", "IpAddress", "127.0.0.1");
_webConfig.IniWriteValue("WebApplicationProperties", "TemplateRootFolder", Path.Combine(this.WebTierPublicDir, "fusion\\templates\\mapguide"));
_webConfig.IniWriteValue("WebApplicationProperties", "WidgetInfoFolder", Path.Combine(this.WebTierPublicDir, "fusion\\widgets\\widgetinfo"));
_webConfig.IniWriteValue("WebApplicationProperties", "ContainerInfoFolder", Path.Combine(this.WebTierPublicDir, "fusion\\containerinfo"));
}
private void SetServerConfigProperties(FormatLineWriter writer)
{
//Port numbers
_serverConfig.IniWriteValue("AdministrativeConnectionProperties", "Port", this.ServerAdminPort.ToString(CultureInfo.InvariantCulture));
_serverConfig.IniWriteValue("ClientConnectionProperties", "Port", this.ServerClientPort.ToString(CultureInfo.InvariantCulture));
_serverConfig.IniWriteValue("SiteConnectionProperties", "Port", this.ServerSitePort.ToString(CultureInfo.InvariantCulture));
_serverConfig.IniWriteValue("GeneralProperties", "LogsPath", Path.Combine(ServerRootDir, "Logs"));
var tempDir = Path.Combine(ServerRootDir, "Temp");
_serverConfig.IniWriteValue("GeneralProperties", "TempPath", tempDir);
_serverConfig.IniWriteValue("GeneralProperties", "ResourcesPath", Path.Combine(ServerRootDir, "Resources"));
_serverConfig.IniWriteValue("GeneralProperties", "WfsDocumentPath", Path.Combine(ServerRootDir, "Wfs"));
_serverConfig.IniWriteValue("GeneralProperties", "WmsDocumentPath", Path.Combine(ServerRootDir, "Wms"));
_serverConfig.IniWriteValue("GeneralProperties", "FdoPath", Path.Combine(ServerRootDir, "Fdo"));
if (!Directory.Exists(tempDir))
Directory.CreateDirectory(tempDir);
//Post RFC 122
if (!this.WriteMentorDictionaryPath)
_serverConfig.IniWriteValue("GeneralProperties", "MentorDictionaryPath", this.CsMapDictionaryDir);
_serverConfig.IniWriteValue("ResourceServiceProperties", "PackagesPath", Path.Combine(ServerRootDir, "Packages"));
_serverConfig.IniWriteValue("ResourceServiceProperties", "LibraryRepositoryPath", Path.Combine(ServerRootDir, "Repositories\\Library"));
_serverConfig.IniWriteValue("ResourceServiceProperties", "LibraryResourceDataFilePath", Path.Combine(ServerRootDir, "Repositories\\DataFiles"));
_serverConfig.IniWriteValue("ResourceServiceProperties", "SessionRepositoryPath", Path.Combine(ServerRootDir, "Repositories\\Session"));
_serverConfig.IniWriteValue("ResourceServiceProperties", "SessionResourceDataFilePath", Path.Combine(ServerRootDir, "Repositories\\Session\\DataFiles"));
_serverConfig.IniWriteValue("ResourceServiceProperties", "SiteRepositoryPath", Path.Combine(ServerRootDir, "Repositories\\Site"));
_serverConfig.IniWriteValue("TileServiceProperties", "TileCachePath", Path.Combine(ServerRootDir, "Repositories\\TileCache"));
_serverConfig.IniWriteValue("ResourceServiceProperties", "ResourceSchemaFilePath", Path.Combine(ServerRootDir, "Schema"));
}
protected abstract void DoBackupConfigFiles(FormatLineWriter writer);
}
}