Example #1
0
    protected void btnExtractSite_Click(object sender, EventArgs e)
    {
        //check all the packages in this site
        //Read the extractor sp
        //execute the sp and save to the respective file
        //make zip of the packages
        //extract site json file
        //create temporary folder and then make zip of all the file

        ///declaring paths
        string destZip             = "temptemplatezip";
        string tempPackageFilePath = Server.MapPath(@"~\temptemplatefiles");
        string tempZipFolderPath   = Server.MapPath(@"~\" + destZip);
        string fullpath            = Server.MapPath(@"~\ContentderPackages");

        //create temporary folder for file collection
        if (Directory.Exists(tempPackageFilePath))
        {
            Directory.Delete(tempPackageFilePath, true);
        }
        Directory.CreateDirectory(tempPackageFilePath);

        WebBuilderController objController = new WebBuilderController();

        if (Directory.Exists(fullpath))
        {
            string[]           directories     = Directory.GetDirectories(fullpath);
            int                directoryLength = directories.Length;
            List <EasyPackage> objPackage      = new List <EasyPackage>();
            if (directoryLength > 0)
            {
                for (int i = 0; i < directoryLength; i++)
                {
                    EasyPackage   objPack = new EasyPackage();
                    DirectoryInfo dir     = new DirectoryInfo(directories[i]);
                    Dictionary <string, string> paramKeysValues = new Dictionary <string, string>();
                    objPack.Name = dir.Name;
                    string xmlPath = directories[i] + "\\package.xml";
                    if (File.Exists(xmlPath))
                    {
                        XmlDocument doc  = SageFrame.Templating.xmlparser.XmlHelper.LoadXMLDocument(xmlPath);
                        XmlNode     node = doc.SelectSingleNode("package/datasql/sql");
                        if (node != null)
                        {
                            objPack.Sql = node.InnerText;
                        }
                        node = doc.SelectSingleNode("package/datasql/storeprocedure");
                        if (node != null)
                        {
                            objPack.Storeprocedure = node.InnerText;
                        }
                        node = doc.SelectSingleNode("package/datasql/params");
                        if (node != null)
                        {
                            XmlNodeList xnList = doc.SelectNodes("package/datasql/params/param");
                            foreach (XmlNode xn in xnList)
                            {
                                string key   = xn["key"].InnerText;
                                string value = xn["value"].InnerText;
                                if (key.Trim() != string.Empty)
                                {
                                    paramKeysValues.Add(key.Trim(), value.Trim());
                                }
                            }
                        }
                        objPack.ParamkeyAndValue = paramKeysValues;
                        objPackage.Add(objPack);
                    }
                }
                objPackage = objController.ExtractPackageData(objPackage);

                //writing the data in respective sql file
                foreach (EasyPackage item in objPackage)
                {
                    string spSavePath = Server.MapPath(@"~\ContentderPackages\" + item.Name + "\\sql\\" + item.Sql + ".sql");
                    if (!File.Exists(spSavePath))
                    {
                        File.Create(spSavePath);
                    }
                    using (StreamWriter writetext = new StreamWriter(spSavePath))
                    {
                        writetext.WriteLine(item.Result);
                    }
                }

                //copy packages to temp folder
                DirectoryInfo tempDir    = new DirectoryInfo(tempPackageFilePath + "\\ContentderPackages");
                DirectoryInfo packageDir = new DirectoryInfo(fullpath);
                IOHelper.CopyDirectory(packageDir, tempDir);
            }
        }
        //read json from database for site
        WebbuilderSite objWebsite = new WebbuilderSite();

        objWebsite         = objController.ExtractSite(userModuleID, GetCurrentCulture(), GetHostURL());
        objWebsite.Culture = GetCurrentCulture();
        string jsonFile = JsonConvert.SerializeObject(objWebsite);

        //write extracted site json to file and save inside temporary folder
        string saveJson = tempPackageFilePath + "\\theme.json";

        using (StreamWriter writetext = new StreamWriter(saveJson))
        {
            writetext.WriteLine(jsonFile);
        }
        //Create zip folder
        if (!Directory.Exists(tempZipFolderPath))
        {
            Directory.CreateDirectory(tempZipFolderPath);
        }

        //zip files inside the zip folder
        tempPackageFilePath += "\\";
        tempZipFolderPath   += "\\Contentder.zip";
        destZip             += "/Contentder.zip";
        ZipUtil.ZipFiles(tempPackageFilePath, tempZipFolderPath, string.Empty);
        if (destZip != string.Empty)
        {
            //download zip
            //Response.Redirect(GetHostURL() + "/" + destZip);
            ////delete the temporary package and zip folder
            //IOHelper.DeleteDirectory(tempPackageFilePath);
            //IOHelper.DeleteDirectory(tempZipFolderPath);
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=Contentder.zip");
            Response.ContentType = "application/x-zip-compressed";
            Response.WriteFile(tempZipFolderPath);
            IOHelper.DeleteDirectory(tempPackageFilePath);
            IOHelper.DeleteDirectory(tempZipFolderPath);
            Response.Flush();
            Response.End();
        }
    }
        private void processTaskScript(String scriptProcessingLocation, Task task, TaskScript taskScript)
        {
            _log.Debug("Determining task type");

            IProcessor processor = null;

            switch (taskScript.Type)
            {
            case ScriptType.Git:
                processor = new GitProcessor(scriptProcessingLocation, taskScript);
                break;

            case ScriptType.CommandLine:
                processor = new CommandLineProcessor(scriptProcessingLocation, _nodeLocation, taskScript);
                break;

            case ScriptType.Node:
                processor = new NodeProcessor(scriptProcessingLocation, _nodeLocation, taskScript);
                break;

            case ScriptType.Grunt:
                processor = new GruntProcessor(scriptProcessingLocation, _nodeLocation, taskScript);
                break;

            case ScriptType.PhantomJS:
                processor = new PhantomJSProcessor(scriptProcessingLocation, _phantomJSLocation, taskScript);
                break;
            }

            String processResults = String.Empty;

            if (processor != null)
            {
                _log.Debug("Starting to process task");

                if (!Directory.Exists(scriptProcessingLocation))
                {
                    Directory.CreateDirectory(scriptProcessingLocation);
                }

                _log.Debug("Checking for any required node packages");

                foreach (String requiredPackage in processor.GetRequiredPackages())
                {
                    PackageCache packageCache = _packageCacheDataAccess.Get(task.Project.ID, requiredPackage);

                    _log.Debug("Package required: " + requiredPackage);

                    String packageCacheLocation  = _packageCacheLocation.TrimEnd(new char[] { '\\' }) + "\\" + task.Project.ID + "\\" + requiredPackage;
                    String targetPackageLocation = scriptProcessingLocation.TrimEnd(new char[] { '\\' }) + "\\node_modules\\" + requiredPackage;

                    _log.Debug("Package cache location: " + packageCacheLocation);
                    _log.Debug("target package location: " + targetPackageLocation);

                    if ((packageCache == null) ||
                        ((packageCache != null) && (!packageCache.Store)) ||
                        ((packageCache != null) && (packageCache.Store) && (!Directory.Exists(packageCacheLocation))))
                    {
                        _log.Debug("Cached package does not exist so importing");

                        processResults += CommandWindowHelper.ProcessCommand(scriptProcessingLocation, _nodeLocation, 5, "npm install " + requiredPackage) + Environment.NewLine;
                    }
                    else
                    {
                        _log.Debug("Cached package exists so attempting to pull into place");

                        Boolean packageImportSuccessful = IOHelper.CopyDirectory(packageCacheLocation, targetPackageLocation);

                        _log.Debug("Check if the cach package copy was successfull");

                        if (!packageImportSuccessful)
                        {
                            _log.Debug("Cache package copy failed so pulling " + requiredPackage + " from npm");

                            processResults += CommandWindowHelper.ProcessCommand(scriptProcessingLocation, _nodeLocation, 5, "npm install " + requiredPackage) + Environment.NewLine;
                        }
                    }

                    _log.Debug("Checking that we now have the target package");

                    if (Directory.Exists(targetPackageLocation))
                    {
                        _log.Debug("We do have the package so copy it into cache");

                        Boolean copySuccessful = true;

                        if ((packageCache == null) ||
                            ((packageCache != null) && (packageCache.Store) && (!Directory.Exists(packageCacheLocation))))
                        {
                            copySuccessful = IOHelper.CopyDirectory(targetPackageLocation, packageCacheLocation);
                        }

                        if ((packageCache == null) && (copySuccessful))
                        {
                            packageCache            = new PackageCache();
                            packageCache.ExternalId = System.Guid.NewGuid().ToString();
                            packageCache.Name       = requiredPackage;
                            packageCache.Version    = IOHelper.GetPackageVersion(targetPackageLocation);
                            packageCache.Store      = true;
                            packageCache.Project    = task.Project;

                            packageCache = _packageCacheDataAccess.Insert(packageCache);
                        }
                    }
                }

                processResults += processor.Process();
            }

            _taskScriptDataAccess.UpdateTaskScriptLog(taskScript.ID, processResults);
        }
        private void PackageInstall(string packageDirPath, string tempPackageFilePath)
        {
            EasyPackage   objPack = new EasyPackage();
            DirectoryInfo dir     = new DirectoryInfo(packageDirPath);

            objPack.Name = dir.Name;
            string xmlPath = packageDirPath + "\\package.xml";

            if (File.Exists(xmlPath))
            {
                XmlDocument doc  = SageFrame.Templating.xmlparser.XmlHelper.LoadXMLDocument(xmlPath);
                XmlNode     node = doc.SelectSingleNode("package/files");
                if (node != null && node.Attributes["destpath"] != null && node.Attributes["srcpath"] != null)
                {
                    string destFolderPath = node.Attributes["destpath"].Value;
                    string srcFolderPath  = node.Attributes["srcpath"].Value;
                    destFolderPath = HttpContext.Current.Server.MapPath(@"~\" + destFolderPath);
                    if (Directory.Exists(destFolderPath))
                    {
                        Directory.Delete(destFolderPath, true);
                    }
                    while (Directory.Exists(destFolderPath))
                    {
                        Thread.Sleep(1);
                    }
                    IOHelper.CopyDirectory(new DirectoryInfo(packageDirPath + "\\" + srcFolderPath), new DirectoryInfo(destFolderPath));
                    //string srcFilPath = tempPackageFilePath + "\\" + objPack.Name;
                    //if (!Directory.Exists(filePath))
                    //    Directory.CreateDirectory(filePath);
                    //XmlNodeList nodeList = doc.SelectNodes("package/files/file");
                    //foreach (XmlNode item in nodeList)
                    //{
                    //    string resFilePath = item.Attributes["path"].Value;
                    //    File.Copy(srcFilPath + "\\" + resFilePath, filePath + "\\" + resFilePath, true);
                    //}
                }

                //get all the dll from zip folder and
                if (Directory.Exists(packageDirPath + "\\dll"))
                {
                    string[] dlls = Directory.GetFiles(packageDirPath + "\\dll\\", "*.dll");
                    foreach (string dll in dlls)
                    {
                        FileInfo dllInfo  = new FileInfo(dll);
                        string   destFile = dllInfo.Name;
                        string   destDll  = HttpContext.Current.Server.MapPath(@"\\bin\\" + destFile);
                        string   srcDll   = tempPackageFilePath + "\\" + objPack.Name + "\\dll\\" + destFile;
                        File.Copy(srcDll, destDll, true);
                    }
                }
                //Read data sql and replace siteID with current siteID

                XmlNode sqlnode         = doc.SelectSingleNode("package/datasql/sql");
                string  sqlDateFileName = string.Empty;
                if (sqlnode != null)
                {
                    sqlDateFileName = sqlnode.InnerText.Trim();
                }

                string   sqlFilePath = tempPackageFilePath + "\\" + objPack.Name + "\\sql";
                string[] sqlFiles    = Directory.GetFiles(sqlFilePath);
                int      sqlLength   = sqlFiles.Length;
                for (int j = 0; j < sqlLength; j++)
                {
                    string fileName = Path.GetFileNameWithoutExtension(sqlFiles[j]);
                    if (fileName.Trim() == sqlDateFileName)
                    {
                        string replacedata   = "%SiteID%";
                        string replacesiteID = "0";
                        ReplaceTextAndRunSQLScript(sqlFiles[j], replacedata, replacesiteID, SystemSetting.SageFrameConnectionString);
                    }
                    //else
                    //{
                    //    RunGivenSQLScript(sqlFiles[j], SystemSetting.SageFrameConnectionString);
                    //}
                }
                //copy files to contentderpackage
                string contenderPackageath = HttpContext.Current.Server.MapPath(@"~\ContentderPackages\");
                if (!Directory.Exists(contenderPackageath))
                {
                    Directory.CreateDirectory(contenderPackageath);
                }
                string fullpath = contenderPackageath + objPack.Name;
                if (Directory.Exists(fullpath))
                {
                    Directory.Delete(fullpath, true);
                }

                //The while loop below and thread sleep is because the delete operation in WINDOWS is delaying
                //Directory.Delete calls the Windows API function RemoveDirectory. The observed behavior is documented:
                //The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed.
                while (Directory.Exists(fullpath))
                {
                    Thread.Sleep(1);
                }
                Directory.Move(packageDirPath, fullpath);
            }
        }
Example #4
0
        /// <summary>
        /// 执行
        /// </summary>
        public override RunCommandResult Execute()
        {
            string taskid        = CommandQueue.taskid.ToString();
            string taskversionid = CommandQueue.taskversionid.ToString();

            try
            {
                var taskruntimeinfo = TaskPoolManager.CreateInstance().Get(taskid.ToString());
                if (taskruntimeinfo != null)
                {
                    ShowCommandLog("任务已在运行中");
                    UploadStatus(taskid, taskversionid, TaskScheduleStatus.Scheduling, "");
                    return(new RunCommandResult()
                    {
                        ExecuteStatus = ExecuteStatus.ExecuteFailed, Message = "任务已在运行中"
                    });
                }
                taskruntimeinfo          = new NodeTaskRunTimeInfo();
                taskruntimeinfo.TaskLock = new TaskLock();
                //读取任务版本信息
                var taskversionreq = new LoadTaskVersionRequest()
                {
                    TaskId = CommandQueue.taskid, TaskVersionId = CommandQueue.taskversionid, Source = Source.Node
                };
                var r = NodeProxy.PostToServer <LoadTaskVersionResponse, LoadTaskVersionRequest>(ProxyUrl.TaskVersionDetail_Url, taskversionreq);
                if (r.Status != ResponesStatus.Success)
                {
                    ShowCommandLog("获取任务版本号详情失败,请求Url:" + ProxyUrl.TaskVersionDetail_Url + ",请求参数:" + JsonConvert.SerializeObject(taskversionreq) + ",返回参数:" + JsonConvert.SerializeObject(r));
                    return(new RunCommandResult()
                    {
                        ExecuteStatus = ExecuteStatus.ExecuteFailed, Message = "获取任务版本号详情失败"
                    });
                }
                taskruntimeinfo.TaskVersionModel = r.Data.TaskVersionDetail;
                taskruntimeinfo.TaskModel        = r.Data.TaskDetail;
                ShowCommandLog("开始创建缓存目录,域安装目录,拷贝共享程序集...");
                string filelocalcachepath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalNodeConfig.TaskDllCompressFileCacheDir + @"\" + taskid + @"\" + r.Data.TaskVersionDetail.version + @"\" + r.Data.TaskVersionDetail.zipfilename;
                string domaininstallpath  = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalNodeConfig.TaskDllDir + "\\" + taskid + "\\";
                // string domaininstallmainclassdllpath = domaininstallpath + @"\" + taskruntimeinfo.TaskModel.taskclassname;
                string taskshareddlldir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalNodeConfig.TaskSharedDllsDir;
                //通知节点TaskProvider任务执行
                IOHelper.CreateDirectory(filelocalcachepath);
                IOHelper.CreateDirectory(domaininstallpath);
                File.WriteAllBytes(filelocalcachepath, taskruntimeinfo.TaskVersionModel.zipfile);
                CompressHelper.UnCompress(filelocalcachepath, domaininstallpath);
                IOHelper.CopyDirectory(taskshareddlldir, domaininstallpath);//拷贝共享程序集到域安装路径
                ShowCommandLog("目录操作完成,拷贝共享程序集完成,开始创建任务的AppDomain域");
                try
                {
                    var          dllpath = Path.Combine(domaininstallpath, taskruntimeinfo.TaskVersionModel.zipfilename + ".dll");
                    AbstractTask dllTask = new AppDomainLoaderHelper <AbstractTask>().CreateDomain(dllpath.Replace(".rar", "").Replace("zip", ""), taskruntimeinfo.TaskModel.taskclassname, out taskruntimeinfo.Domain);
                    dllTask.SetAlarmList(r.Data.AlarmEmailList, r.Data.AlarmMobileList);//设置任务报警信息
                    tb_task        cloneTaskModel        = taskruntimeinfo.TaskModel.CloneObj <tb_task>();
                    tb_taskversion cloneTaskVersionModel = taskruntimeinfo.TaskVersionModel.CloneObj <tb_taskversion>();
                    dllTask.TaskDetail        = cloneTaskModel;
                    dllTask.TaskVersionDetail = cloneTaskVersionModel;
                    dllTask.AppConfig         = new TaskAppConfigInfo();

                    //CommandParams cmdparams= JsonConvert.DeserializeObject<CommandParams>(CommandQueue.commandparams);
                    //if (!string.IsNullOrEmpty(cmdparams.TaskParams))
                    //{
                    //    dllTask.AppConfig = JsonConvert.DeserializeObject<TaskAppConfigInfo>(cmdparams.TaskParams);
                    //}
                    //else
                    //{
                    if (!string.IsNullOrEmpty(taskruntimeinfo.TaskVersionModel.taskparams))
                    {
                        dllTask.AppConfig = JsonConvert.DeserializeObject <TaskAppConfigInfo>(taskruntimeinfo.TaskVersionModel.taskparams);
                    }
                    //}
                    taskruntimeinfo.DllTask = dllTask;
                    string nextruntime = "2099-12-30";
                    TaskPoolManager.CreateInstance().Add(taskid.ToString(), taskruntimeinfo, ref nextruntime);
                    ShowCommandLog("加载AppDomain域成功,开始添加到任务池等待执行");
                    //上报任务执行日志,并更新调度状态为调度中
                    UploadStatus(taskid, taskversionid, TaskScheduleStatus.Scheduling, nextruntime);
                    ShowCommandLog("添加到任务池成功,开启任务成功");
                    return(new RunCommandResult()
                    {
                        ExecuteStatus = ExecuteStatus.ExecuteSucess
                    });
                }
                catch (Exception ex)
                {
                    ShowCommandLog("加载任务应用程序域异常,异常信息:" + JsonConvert.SerializeObject(ex));
                    UploadStatus(taskid, taskversionid, TaskScheduleStatus.StopSchedule);
                    return(new RunCommandResult()
                    {
                        ExecuteStatus = ExecuteStatus.ExecuteException, Message = ex.Message, Ex = ex
                    });
                }
            }
            catch (Exception ex)
            {
                ShowCommandLog("开启任务命令异常,异常信息:" + JsonConvert.SerializeObject(ex));
                UploadStatus(taskid, taskversionid, TaskScheduleStatus.StopSchedule);
                return(new RunCommandResult()
                {
                    ExecuteStatus = ExecuteStatus.ExecuteException, Message = ex.Message, Ex = ex
                });
            }
        }