The main tasks of
the scripts are to create folders inside of the future application, to
assign necessary permissions to them, to generate configuration files,
to create the structure for the used databases and to fill them with
initial data.
Reading and
checking the standard input parameters are necessary procedures inside
of every script. In case the checkup fails, the error is returned, and
the corresponding message on this error is displayed for a Plesk
user.
The example of vbs script that replaces configuration file parameters by those indicated by the user in the forms when installing the application:
Dim FSO
' shell object
Dim Shell
' input parameters
Dim Params
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Shell = WScript.CreateObject("WScript.Shell")
Set Params = CreateObject("Scripting.Dictionary")
' Reading parameters
ReadParams
' Checkup
CheckParams
' Processing. Here the internal parameters are set, such as a path to the configuration file
ParseParams
' Generating config
GenerateConfig
' Replacing some dump values by the values set by the user in the forms
EditMysqlFile
' Filling the database and managing files
ConfigureBaseAndRemoveFiles
Set Params = Nothing
Set FSO=Nothing
Set Shell=Nothing
WScript.Quit(err.number)
' Reading standard input parameters
Sub ReadParams
Params.RemoveAll
Do While Not WScript.StdIn.AtEndOfStream
Param = Trim(WScript.StdIn.ReadLine)
Pos = InStr(Param, "=")
Name = Mid(Param, 1, Pos - 1)
Value = Mid(Param, Pos + 1)
Params.Add Name, Value
End Sub
' An auxiliary function for debugging
Sub PrintParams(OutputFileName)
Dim Output
Set Output = FSO.CreateTextFile(OutputFileName, True)
Dim Keys, Items
Keys = Params.Keys()
Items = Params.Items()
For I = 0 To Params.Count - 1
Output.WriteLine Keys(I) & "=" & Items(I)
Next
Output.Close
End Sub
' Checking the external parameters and adding the internal ones that will be installed in ParseParams
Sub CheckParams
Dim Name, Names
' necessary parameters
Names = Array(_
"vhost_path","domain_name","install_prefix","ssl_target_directory",_
"dbname","dbuser","dbpasswd", "admin_name", "admin_passwd", _
"application_url")
For Each Name In Names
If Not Params.Exists(Name) Or Len(Params.Item(Name)) = 0 Then
WScript.Echo "No " & Name & " parameter specified for application"
WScript.Quit(1)
End If
Next
' extra parameters
Names = Array(_
"documents_directory","proto","root_d","config_file","MYSQL_BIN_D","mysql_schema_file")
For Each Name In Names
If Not Params.Exists(Name) Then
Params.Add Name, ""
End If
Next
End Sub
' Processing. Here the internal parameters are set, such as a path to the configuration file, mysql path, path to the root directory of the application, etc.
Sub ParseParams
If Params.Item("ssl_target_directory") = "true" Then
Params.Item("documents_directory") = "httpsdocs"
Params.Item("proto") = "https://"
Else
Params.Item("documents_directory") = "httpdocs"
Params.Item("proto") = "http://"
End If
Params.Item("root_d")=Params.Item("vhost_path")&"\"&Params.Item("documents_directory")&"\"&Params.Item("install_prefix")
Params.Item("config_file")=Params.Item("root_d")&"\include\config.inc.php"
Params.Item("MYSQL_BIN_D")=FSO.GetFolder(Shell.RegRead("HKLM\SOFTWARE\Plesk\PSA Config\Config\MYSQL_BIN_D")).ShortPath
Params.Item("mysql_schema_file")=Params.Item("root_d")&"\install\sql\dump.sql"
End Sub
Sub BackupFile(PathName)
If FSO.FileExists(PathName) And Not FSO.FileExists(PathName& ".orig") Then
FSO.MoveFile PathName, PathName & ".orig"
End If
End Sub
' A configuration file can be formed from scratch or by changing some parameters in it, as in this example. In the latter case, the new file is created, the source configuration file is then replaced by
Sub GenerateConfig
Dim Source
Dim Dest
Dim retstring
Dim FileName
FileName =Params.Item("config_file")
Set Dest=FSO.CreateTextFile(Params.Item("config_file")&".copy", True)
Set Source=FSO.OpenTextFile(Params.Item("config_file"), 1, False)
Do While Source.AtEndOfStream <>true
retstring = Source.ReadLine
retstring=Replace(retstring,"_dbname_",Params.Item("dbname"))
retstring=Replace(retstring,"_dbuser_",Params.Item("dbuser"))
retstring=Replace(retstring,"_dbpasswd_",Params.Item("dbpasswd"))
Dest.WriteLine retstring
Dest.Close
Source.Close
End Sub
'Also the new file is formed for replacing the source dump
Sub EditMysqlFile
Dim Source
Dim Dest
Dim retstring
Set Dest=FSO.CreateTextFile(Params.Item("mysql_schema_file")&".copy", True)
Set Source=FSO.OpenTextFile(Params.Item("mysql_schema_file"), 1, False)
Do While Source.AtEndOfStream <>true
retstring = Source.ReadLine
retstring=Replace(retstring,"_admin_name_",Params.Item("admin_name"))
retstring=Replace(retstring,"_admin_passwd_",Params.Item("admin_passwd"))
retstring=Replace(retstring,"_install_prefix_",Params.Item("install_prefix"))
Dest.WriteLine retstring
Dest.Close
Source.Close
End Sub
'Filling a database, replacing a configuration file by the corrected GenerateConfig, removing unnecessary copies of the dump and config
Sub ConfigureBaseAndRemoveFiles
Shell.Run "cmd /c "" """&Params.Item("MYSQL_BIN_D")&"\mysql.exe"" -u"&Params.Item("dbuser")&" -p"&Params.Item("dbpasswd")&" "&Params.Item("dbname")&" < """&Params.Item("mysql_schema_file")&".copy"" """ ,1, true
FSO.CopyFile Params.Item("config_file")&".copy" , Params.Item("config_file")
FSO.DeleteFile Params.Item("mysql_schema_file")&".copy"
FSO.DeleteFile Params.Item("config_file")&".copy"
End Sub
In cases when special permissions should be set for the folders so they could function properly, such as, for example, writing permissions, the preinstall script is used. It creates the necessary folders and sets permissions for an Internet user. To create this script on VBS, some auxiliary functions,described in the Security.inc file, should be added to it. Here is the example of their usage:
If it's necessary to set permissions for certain files of the application, it should be done in the postinstall script by using the same code from the Security.inc file.
Any files executed under Windows OS can act as scripts - .bat, .pl , exe, etc. The secure script execution is guaranteed due to the fact that it is executed with the permissions of the domain owner, and, accordingly, the script cannot perform anything that is not allowed for the domain owner. Thus, its actions are determined by the permissions set at its creation. But if the script does not have enough permissions for the described actions, no error is returned, as this situation is considered equal to the lack of the script in the application installation package. In this case, the installation is considered successful, but the actions, described in the script are ignored, making thus debugging more difficult!