MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / UpdateConfigFile

Method UpdateConfigFile

agent/app/service/nginx.go:135–173  ·  view source on GitHub ↗
(req request.NginxConfigFileUpdate)

Source from the content-addressed store, hash-verified

133}
134
135func (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
175func (n NginxService) Build(req request.NginxBuildReq) error {
176 nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)

Callers

nothing calls this directly

Calls 11

StatMethod · 0.95
CreateDirMethod · 0.95
CopyMethod · 0.95
RenameMethod · 0.95
WriteFileMethod · 0.95
SaveFileMethod · 0.95
getAppInstallByKeyFunction · 0.85
checkContainerStatusFunction · 0.85
nginxCheckAndReloadFunction · 0.85
FormatMethod · 0.45
GetComposePathMethod · 0.45

Tested by

no test coverage detected