Пример #1
0
        public override void Start()
        {
            logger("Start to linux Service Start :" + this.args.AppName);


            LinuxServiceHelper.ServiceRun(this.args.AppName, this.args.TempPhysicalPath, this.args.UseOfflineHtm, this.args.ApplicationPoolName, this.logger);

            var runSuccess = false;

            logger("Wait 5Senconds to Check service state :" + retryTimes);
            Thread.Sleep(5000);

            CopyHelper.RunCommand($"sudo systemctl status {this.args.AppName}", null, (msg) =>
            {
                if (!string.IsNullOrEmpty(msg))
                {
                    this.logger.Invoke(msg);
                    var msg1 = msg.ToLower();
                    if (msg1.Contains("activating (start)"))
                    {
                        runSuccess = true;
                    }
                    else if (msg1.Contains("active:") && (msg1.Contains("running")))
                    {
                        runSuccess = true;
                    }
                }
            });

            if (!runSuccess)
            {
                throw new Exception("Start service Fail ");
            }
        }
Пример #2
0
        private void CheckDocker(GetVersionVm request)
        {
            if (string.IsNullOrEmpty(request.Name))
            {
                WriteError("service name required!");
                return;
            }

            if (!string.IsNullOrEmpty(request.Mac) && !Setting.CheckIsInWhiteMacList(request.Mac))
            {
                WriteError($"macAddress:[{request.Mac}] invalid");
                return;
            }

            var serviceName = request.Name.Trim();
            var service     = LinuxServiceHelper.GetLinuxService(serviceName);

            if (!string.IsNullOrEmpty(service.Item1))
            {
                WriteError(service.Item1);
                return;
            }

            CheckExistResult result = new CheckExistResult {
                WebSiteName = serviceName, Success = !string.IsNullOrEmpty(service.Item2)
            };

            WriteSuccess(result);
        }
Пример #3
0
        public override string DeployExcutor(FormHandler.FormItem fileItem)
        {
            var projectPath = Path.Combine(Setting.PublishLinuxPathFolder, _serviceName);

            _projectPublishFolder = Path.Combine(projectPath, !string.IsNullOrEmpty(_dateTimeFolderName) ? _dateTimeFolderName : DateTime.Now.ToString("yyyyMMddHHmmss"));
            EnsureProjectFolder(projectPath);
            EnsureProjectFolder(_projectPublishFolder);

            try
            {
                var filePath = Path.Combine(_projectPublishFolder, fileItem.FileName);
                using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(fileItem.FileBody, 0, fileItem.FileBody.Length);
                }


                if (!File.Exists(filePath))
                {
                    return("publish file save fail");
                }
#if NETCORE
                if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Log("linux agent version ==>" + AntDeployAgentWindows.Version.VERSION);
                }
                else
                {
                    Log("netcore agent version ==>" + AntDeployAgentWindows.Version.VERSION);
                }
#else
                Log("netframework agent version ==>" + AntDeployAgentWindows.Version.VERSION);
#endif
                Log("upload success ==>" + filePath);
                //解压
                try
                {
                    Log("start unzip file");
                    ZipFile.ExtractToDirectory(filePath, _projectPublishFolder);
                }
                catch (Exception ex)
                {
                    return("unzip publish file error:" + ex.Message);
                }

                Log("unzip success ==>" + _projectPublishFolder);

                var deployFolder = Path.Combine(_projectPublishFolder, "publish");

                if (!Directory.Exists(deployFolder))
                {
                    if (Directory.Exists(_projectPublishFolder))
                    {
                        var temp           = new DirectoryInfo(_projectPublishFolder);
                        var tempFolderList = temp.GetDirectories();
                        if (tempFolderList.Length == 1)
                        {
                            deployFolder = tempFolderList.First().FullName;
                        }
                    }
                }

                if (!Directory.Exists(deployFolder))
                {
                    return("unzip publish file error,Path not found:" + deployFolder);
                }

                //查找  Service 里面是否存在该服务
                var service = LinuxServiceHelper.GetLinuxService(this._serviceName);
                if (!string.IsNullOrEmpty(service.Item1))
                {
                    //运行命令出错了
                    return(service.Item1);
                }
                if (string.IsNullOrEmpty(service.Item2))//没有找到该服务的workingFolder 可能是service描述文件内容不对,可能是服务不存在
                {
                    Log($"systemctlService : {_serviceName} not found,start to create!");

                    //创建发布目录
                    var firstDeployFolder = string.IsNullOrEmpty(_physicalPath) ? Path.Combine(projectPath, "deploy") : _physicalPath;
                    EnsureProjectFolder(firstDeployFolder);
                    if (Directory.Exists(firstDeployFolder))
                    {
                        Log($"deploy folder create success : {firstDeployFolder} ");
                    }
                    else
                    {
                        return($"DeployFolder : {firstDeployFolder} create error!");
                    }

                    //复制文件到发布目录
                    CopyHelper.ProcessXcopy(deployFolder, firstDeployFolder, Log);

                    Log($"copy files success from [{deployFolder}] to [{firstDeployFolder}]");

                    //linux service
                    var execFullPath = Path.Combine(firstDeployFolder, _serviceExecName);
                    if (!File.Exists(execFullPath))
                    {
                        //try { Directory.Delete(firstDeployFolder, true); } catch (Exception) { }
                        return($"systemctl service exec file not found : {execFullPath}");
                    }

                    //安装服务
                    Log($"start to install systemctl service");
                    Log($"service name:{_serviceName}");
                    Log($"service path:{execFullPath}");
                    Log($"service description:{_serviceDescription ?? string.Empty}");

                    try
                    {
                        var err = LinuxServiceHelper.CreateServiceFileAndRun(this._serviceName, firstDeployFolder, _serviceExecName,
                                                                             (_serviceDescription ?? string.Empty), _env, execFullPath,
                                                                             string.IsNullOrEmpty(_serviceStartType) || _serviceStartType.Equals("Auto"), _notify, Log);
                        if (!string.IsNullOrEmpty(err))
                        {
                            return(err);
                        }
                        //检查是否成功了?
                        var runSuccess = false;

                        Thread.Sleep(5000);

                        CopyHelper.RunCommand($"sudo systemctl status {this._serviceName}", null, (msg) =>
                        {
                            if (!string.IsNullOrEmpty(msg))
                            {
                                this.Log(msg);
                                var msg1 = msg.ToLower();
                                if (msg1.Contains("activating (start)"))
                                {
                                    runSuccess = true;
                                }
                                else if (msg1.Contains("active:") && msg1.Contains("running"))
                                {
                                    runSuccess = true;
                                }
                            }
                        });

                        if (!runSuccess)
                        {
                            return($"install linux service fail");
                        }

                        Log($"install systemctl service success");
                        return(string.Empty);
                    }
                    catch (Exception e2)
                    {
                        return($"install linux service fail:" + e2.Message);
                    }
                }

                var projectLocationFolder = service.Item2;

                try
                {
                    if (!Directory.Exists(projectLocationFolder))
                    {
                        //如果目录不存在 那么就重新建立
                        EnsureProjectFolder(projectLocationFolder);
                    }
                }
                catch (Exception)
                {
                    return("ServiceFolder is not correct ===> " + projectLocationFolder);
                }

                if (string.IsNullOrEmpty(projectLocationFolder))
                {
                    return("ServiceFolder is not correct ===> " + projectLocationFolder);
                }

                var fullExcutePath = Path.Combine(projectLocationFolder, _serviceExecName);
                if (!File.Exists(fullExcutePath))
                {
                    return($"systemctl service exec file not found : {fullExcutePath}");
                }

                //保证有service描述文件 等后面实际要用到
                LinuxServiceHelper.CreateServiceFile(this._serviceName, projectLocationFolder, _serviceExecName,
                                                     (_serviceDescription ?? string.Empty), _env, this._notify, Log);

                Arguments args = new Arguments
                {
                    DeployType          = "Linux",
                    BackupFolder        = Setting.BackUpLinuxPathFolder,
                    AppName             = _serviceName,
                    AppFolder           = projectLocationFolder,
                    TempPhysicalPath    = Path.Combine(projectLocationFolder, $"{_serviceName}.service"),//服务文件描述
                    DeployFolder        = deployFolder,
                    ApplicationPoolName = fullExcutePath,
                    BackUpIgnoreList    = this._backUpIgnoreList,
                    UseOfflineHtm       = string.IsNullOrEmpty(_serviceStartType) || _serviceStartType.Equals("Auto"),
                    NoBackup            = !Setting.NeedBackUp,
                    Site1 = _env
                };

                Log("Start to deploy linux Service:");
                Log("ServiceName ===>" + _serviceName);
                Log("ServiceFolder ===> " + projectLocationFolder);

                if (_isNoStopWebSite)
                {
                    args.NoStop  = true;
                    args.NoStart = true;
                }

                var ops = new OperationsLinux(args, Log);

                try
                {
                    ops.Execute();

                    try
                    {
                        //如果是增量的话 要把复制过来
                        if (_isIncrement)
                        {
                            Log("Increment deploy start to backup...");
                            //projectLocation.Item1 转到 increment 的目录
                            var incrementFolder = Path.Combine(_projectPublishFolder, "increment");
                            EnsureProjectFolder(incrementFolder);

                            if (this._backUpIgnoreList != null && this._backUpIgnoreList.Any())
                            {
                                var excludFrom = Path.Combine(_projectPublishFolder, "exclude.log");
                                File.WriteAllLines(excludFrom, this._backUpIgnoreList);
                                //存到文件里面去 要指定排除
                                CopyHelper.ProcessXcopy(projectLocationFolder, incrementFolder, excludFrom, Log);
                            }
                            else
                            {
                                CopyHelper.ProcessXcopy(projectLocationFolder, incrementFolder, Log);
                            }

                            Log("Increment deploy backup success...");
                        }
                    }
                    catch (Exception ex3)
                    {
                        Log("Increment deploy folder backup fail:" + ex3.Message);
                    }

                    Log("Deploy linux service Execute Success");
                }
                catch (Exception ex)
                {
                    try
                    {
                        //ops.Rollback();

                        return($"publish to linux service err:{ex.Message}");
                    }
                    catch (Exception ex2)
                    {
                        return($"publish to linux service err:{ex.Message},rollback fail:{ex2.Message}");
                    }
                }
                return(string.Empty);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Пример #4
0
        public override string RollBack()
        {
            try
            {
                var projectPath = Path.Combine(Setting.PublishLinuxPathFolder, _serviceName);
                _projectPublishFolder = Path.Combine(projectPath, _dateTimeFolderName);
                if (!Directory.Exists(_projectPublishFolder))
                {
                    return("rollback folder not found:" + _projectPublishFolder);
                }

#if NETCORE
                if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Log("linux agent version ==>" + AntDeployAgentWindows.Version.VERSION);
                }
                else
                {
                    Log("netcore agent version ==>" + AntDeployAgentWindows.Version.VERSION);
                }
#else
                Log("netframework agent version ==>" + AntDeployAgentWindows.Version.VERSION);
#endif
                var deployFolder = Path.Combine(_projectPublishFolder, "publish");

                if (!Directory.Exists(deployFolder))
                {
                    if (Directory.Exists(_projectPublishFolder))
                    {
                        var temp           = new DirectoryInfo(_projectPublishFolder);
                        var tempFolderList = temp.GetDirectories();
                        if (tempFolderList.Length == 1)
                        {
                            deployFolder = tempFolderList.First().FullName;
                        }
                    }
                }

                var incrementFolder = Path.Combine(_projectPublishFolder, "increment");
                if (Directory.Exists(incrementFolder))
                {
                    deployFolder = incrementFolder;
                }

                if (!Directory.Exists(deployFolder))
                {
                    return("rollback folder not found:" + deployFolder);
                }

                Log("rollback from folder ==>" + deployFolder);

                var service = LinuxServiceHelper.GetLinuxService(this._serviceName);
                if (!string.IsNullOrEmpty(service.Item1))
                {
                    //运行命令出错了
                    return(service.Item1);
                }

                var projectLocationFolder = service.Item2;
                if (string.IsNullOrEmpty(projectLocationFolder))
                {
                    return($"can not find executable folder of service:{_serviceName}");
                }

                if (!Directory.Exists(projectLocationFolder))
                {
                    //如果目录不存在 那么就重新建立
                    return($"can not find executable folder of service:{_serviceName}");
                }

                if (string.IsNullOrEmpty(service.Item3))
                {
                    return($"can not find executable path of service:{_serviceName}");
                }
                if (!File.Exists(service.Item3))
                {
                    return($"can not find executable path of service:{_serviceName}");
                }

                Log("Start to rollback Linux Service:");
                Log("ServiceName ===>" + _serviceName);
                Log("ServiceFolder ===> " + projectLocationFolder);

                Arguments args = new Arguments
                {
                    DeployType          = "Linux",
                    BackupFolder        = Setting.BackUpLinuxPathFolder,
                    AppName             = _serviceName,
                    AppFolder           = projectLocationFolder,
                    DeployFolder        = deployFolder,
                    ApplicationPoolName = service.Item3,//执行文件
                    NoBackup            = true,
                };
                var ops = new OperationsLinux(args, Log);
                try
                {
                    ops.Execute();
                    SaveCurrentVersion(new DirectoryInfo(deployFolder).Parent.FullName);
                    Log("Rollback Linux Service Execute Success");
                }
                catch (Exception ex)
                {
                    try
                    {
                        return($"Rollback to Linux Service err:{ex.Message}");
                    }
                    catch (Exception ex2)
                    {
                        return($"Rollback to Linux Service err:{ex.Message}, fail:{ex2.Message}");
                    }
                }
                return(string.Empty);
            }
            catch (Exception ex1)
            {
                return(ex1.Message);
            }
        }