Exemplo n.º 1
0
        public override void DoWork()
        {
            // Input parameters:
            //  - FILE_PATH
            //  - FTP_SERVER
            //  - FTP_USERNAME
            //  - FTP_PASSWORD
            //  - FTP_FOLDER

            // get input parameters
            string filePath    = (string)TaskManager.TaskParameters["FILE_PATH"];
            string ftpServer   = (string)TaskManager.TaskParameters["FTP_SERVER"];
            string ftpUsername = (string)TaskManager.TaskParameters["FTP_USERNAME"];
            string ftpPassword = (string)TaskManager.TaskParameters["FTP_PASSWORD"];
            string ftpFolder   = (string)TaskManager.TaskParameters["FTP_FOLDER"];

            // check input parameters
            if (String.IsNullOrEmpty(filePath))
            {
                TaskManager.WriteWarning("Specify 'File' task parameter");
                return;
            }

            if (String.IsNullOrEmpty(ftpServer))
            {
                TaskManager.WriteWarning("Specify 'FTP Server' task parameter");
                return;
            }

            // substitute parameters
            DateTime d    = DateTime.Now;
            string   date = d.ToString("yyyyMMdd");
            string   time = d.ToString("HHmm");

            filePath = Utils.ReplaceStringVariable(filePath, "date", date);
            filePath = Utils.ReplaceStringVariable(filePath, "time", time);

            // build FTP command file
            StringBuilder sb     = new StringBuilder();
            StringWriter  writer = new StringWriter(sb);

            // FTP server
            writer.WriteLine("open " + ftpServer);

            // check if anonymous mode
            if (String.IsNullOrEmpty(ftpUsername))
            {
                ftpUsername = "******";
                ftpPassword = "******";
            }

            // FTP username/password
            writer.WriteLine(ftpUsername);
            writer.WriteLine(ftpPassword);

            // check if we need to change remote folder
            if (!String.IsNullOrEmpty(ftpFolder))
            {
                writer.WriteLine("cd " + ftpFolder.Replace("\\", "/"));
            }

            // file to send
            writer.WriteLine("binary");
            writer.WriteLine("put " + FilesController.GetFullPackagePath(TaskManager.PackageId, filePath));

            // bye
            writer.WriteLine("bye");

            string cmdBatch = sb.ToString();

            // create temp file in user space
            string cmdPath     = Utils.GetRandomString(10) + ".txt";
            string fullCmdPath = FilesController.GetFullPackagePath(TaskManager.PackageId, cmdPath);

            // upload batch
            FilesController.UpdateFileBinaryContent(TaskManager.PackageId, cmdPath, Encoding.UTF8.GetBytes(cmdBatch));

            // execute system command
            // load OS service
            int serviceId = PackageController.GetPackageServiceId(TaskManager.PackageId, ResourceGroups.Os);

            // load service
            ServiceInfo service = ServerController.GetServiceInfo(serviceId);

            if (service == null)
            {
                return;
            }

            WindowsServer winServer = new WindowsServer();

            ServiceProviderProxy.ServerInit(winServer, service.ServerId);
            TaskManager.Write(winServer.ExecuteSystemCommand("ftp.exe", "-s:" + fullCmdPath));

            // delete batch file
            FilesController.DeleteFiles(TaskManager.PackageId, new string[] { cmdPath });
        }
Exemplo n.º 2
0
 public int UpdateFileBinaryContent(int packageId, string path, byte[] content)
 {
     return(FilesController.UpdateFileBinaryContent(packageId, path, content));
 }
Exemplo n.º 3
0
        private int RunScenario(string scenarioPath, InstallationInfo inst, bool throwExceptions)
        {
            // load XML document
            XmlDocument docScenario = new XmlDocument();

            docScenario.Load(scenarioPath);

            // go through "check" section
            XmlNode nodeCheck = docScenario.SelectSingleNode("//check");

            if (nodeCheck != null)
            {
                foreach (XmlNode nodeStep in nodeCheck.ChildNodes)
                {
                    if (nodeStep.Name == "fileExists")
                    {
                        /*
                         * // check if the specified file exists
                         * string fileName = nodeStep.Attributes["path"].Value;
                         * fileName = ExpandVariables(fileName, inst);
                         * if (fileName.StartsWith("\\"))
                         * {
                         *  fileName = fileName.Substring(1);
                         * }
                         * //get full path to instal folder
                         * PackageInfo package = PackageController.GetPackage(inst.PackageId);
                         * string fullPath = Path.Combine(GetFullPathToInstallFolder(package.UserId), fileName);
                         * if (os.FileExists(fullPath))
                         *  return BusinessErrorCodes.ERROR_WEB_INSTALLER_TARGET_WEBSITE_UNSUITABLE;
                         */
                    }
                    else if (nodeStep.Name == "sql")
                    {
                        string cmdText = nodeStep.InnerText;
                        cmdText = ExpandVariables(cmdText, inst);

                        DataSet dsResults = sql.ExecuteSqlQuery(inst.DatabaseName, cmdText);
                        if (dsResults.Tables[0].Rows.Count > 0)
                        {
                            return(BusinessErrorCodes.ERROR_WEB_INSTALLER_TARGET_DATABASE_UNSUITABLE);
                        }
                    }
                }
            }

            // go through "commands" section
            XmlNode nodeCommands = docScenario.SelectSingleNode("//commands");

            if (nodeCommands != null)
            {
                foreach (XmlNode nodeCommand in nodeCommands.ChildNodes)
                {
                    if (nodeCommand.Name == "processFile")
                    {
                        // process remote file
                        string fileName = nodeCommand.Attributes["path"].Value;
                        fileName = ExpandVariables(fileName, inst);

                        byte[] fileBinaryContent = FilesController.GetFileBinaryContent(inst.PackageId, fileName);
                        if (fileBinaryContent == null)
                        {
                            throw new Exception("Could not process scenario file: " + fileName);
                        }

                        string fileContent = Encoding.UTF8.GetString(fileBinaryContent);
                        fileContent = ExpandVariables(fileContent, inst);

                        FilesController.UpdateFileBinaryContent(inst.PackageId, fileName,
                                                                Encoding.UTF8.GetBytes(fileContent));
                    }
                    else if (nodeCommand.Name == "runSql")
                    {
                        string cmdText = nodeCommand.InnerText;
                        if (nodeCommand.Attributes["path"] != null)
                        {
                            // load SQL from file
                            string sqlPath = Path.Combine(app.Folder, nodeCommand.Attributes["path"].Value);

                            if (!File.Exists(sqlPath))
                            {
                                continue;
                            }

                            StreamReader reader = new StreamReader(sqlPath);
                            cmdText = reader.ReadToEnd();
                            reader.Close();
                        }

                        bool run = true;
                        if (nodeCommand.Attributes["dependsOnProperty"] != null)
                        {
                            string[] propNames = nodeCommand.Attributes["dependsOnProperty"].Value.Split(',');
                            foreach (string propName in propNames)
                            {
                                if (inst[propName.Trim()] == null)
                                {
                                    run = false;
                                    break;
                                }
                            }
                        }

                        if (run)
                        {
                            try
                            {
                                cmdText = ExpandVariables(cmdText, inst);
                                sql.ExecuteSqlNonQuerySafe(inst.DatabaseName, inst.Username, inst.Password, cmdText);
                            }
                            catch (Exception ex)
                            {
                                if (throwExceptions)
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                }
            }
            return(0);
        }