(req request.NginxConfigFileUpdate)
| 133 | } |
| 134 | |
| 135 | func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error { |
| 136 | fileOp := files.NewFileOp() |
| 137 | nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | filePath := path.Join(global.Dir.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "nginx.conf") |
| 142 | if req.Backup { |
| 143 | backupPath := path.Join(path.Dir(filePath), "bak") |
| 144 | if !fileOp.Stat(backupPath) { |
| 145 | if err := fileOp.CreateDir(backupPath, constant.DirPerm); err != nil { |
| 146 | return err |
| 147 | } |
| 148 | } |
| 149 | newFile := path.Join(backupPath, "nginx.bak"+"-"+time.Now().Format("2006-01-02-15-04-05")) |
| 150 | if err := fileOp.Copy(filePath, backupPath); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | if err := fileOp.Rename(path.Join(backupPath, "nginx.conf"), newFile); err != nil { |
| 154 | return err |
| 155 | } |
| 156 | } |
| 157 | oldContent, err := os.ReadFile(filePath) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), constant.DirPerm); err != nil { |
| 162 | return err |
| 163 | } |
| 164 | if status, err := checkContainerStatus(nginxInstall.ContainerName); err == nil && status != "running" { |
| 165 | if out, err := compose.DownAndUp(nginxInstall.GetComposePath()); err != nil { |
| 166 | _ = fileOp.SaveFile(filePath, string(oldContent), constant.DirPerm) |
| 167 | return fmt.Errorf("nginx restart failed: %v", out) |
| 168 | } else { |
| 169 | return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName) |
| 170 | } |
| 171 | } |
| 172 | return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName) |
| 173 | } |
| 174 | |
| 175 | func (n NginxService) Build(req request.NginxBuildReq) error { |
| 176 | nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) |
nothing calls this directly
no test coverage detected