Skip to content

Use correct path to snap binary #17274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/content/doc/advanced/environment-variables.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ For documentation about each of the variables available, refer to the
## Gitea files

- `GITEA_WORK_DIR`: Absolute path of working directory.
- `GITEA_APP_PATH`: Absolute path of gitea binary. (This is usually automatically detected and should therefore be only set in limited circumstances.)
- `GITEA_CUSTOM`: Gitea uses `GITEA_WORK_DIR`/custom folder by default. Use this variable
to change _custom_ directory.
- `GOGS_WORK_DIR`: Deprecated, use `GITEA_WORK_DIR`
Expand All @@ -51,6 +52,8 @@ For documentation about each of the variables available, refer to the
- `USER`: System user that Gitea will run as. Used for some repository access strings.
- `USERNAME`: if no `USER` found, Gitea will use `USERNAME`
- `HOME`: User home directory path. The `USERPROFILE` environment variable is used in Windows.
- `SNAP`: Path to directory that snap uses to store Gitea's configuration. Snap defaults to using `$GITEA_APP_PATH`.
- `SNAP_REVISION`: If Gitea is installed using snap, revision number of snap in use.
Copy link
Contributor

@wxiaoguang wxiaoguang Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SNAP and SNAP_REVISION are not intended to be used by real users. So Maybe they can be written in other sections and warn users do not set these variables manually.


### Only on Windows

Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/installation/from-source.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ using the `LDFLAGS` environment variable for `make`. The appropriate settings ar
- To set the `CustomPath` use `LDFLAGS="-X \"code.gitea.io/gitea/modules/setting.CustomPath=custom-path\""`
- For `CustomConf` you should use `-X \"code.gitea.io/gitea/modules/setting.CustomConf=conf.ini\"`
- For `AppWorkPath` you should use `-X \"code.gitea.io/gitea/modules/setting.AppWorkPath=working-path\"`
- For `AppPath` you should use `-X \"code.gitea.io/gitea/modules/setting.AppPath=/path/to/gitea/binary\"`
- For `StaticRootPath` you should use `-X \"code.gitea.io/gitea/modules/setting.StaticRootPath=static-root-path\"`
- To change the default PID file location use `-X \"code.gitea.io/gitea/modules/setting.PIDFile=/run/gitea.pid\"`

Expand Down
26 changes: 24 additions & 2 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,14 @@ func IsProd() bool {
}

func getAppPath() (string, error) {
var appPath string
appPath := AppPath
if giteaAppPath, ok := os.LookupEnv("GITEA_APP_PATH"); ok {
appPath = giteaAppPath
}
if appPath != "" {
return appPath, nil
}

var err error
if IsWindows && filepath.IsAbs(os.Args[0]) {
appPath = filepath.Clean(os.Args[0])
Expand All @@ -446,9 +453,24 @@ func getAppPath() (string, error) {
if err != nil {
return "", err
}

// Note: we don't use path.Dir here because it does not handle case
// which path starts with two "/" in Windows: "//psf/Home/..."
return strings.ReplaceAll(appPath, "\\", "/"), err
appPath = strings.ReplaceAll(appPath, "\\", "/")

// Fix issue 16209: When running in a snap and repositories are
// created, paths in the hooks include the snap revision. But as new
// revisions come out the older revisions are eventually deleted, and
// the hooks begin to fail.
// The code below replaces the snap revision with "current", which
// is a link to the current snap revision.
snapPath := os.Getenv("SNAP")
if snapPath != "" && strings.HasPrefix(appPath, snapPath) {
revision := os.Getenv("SNAP_REVISION")
appPath = strings.Replace(appPath, revision, "current", 1)
Copy link
Contributor

@wxiaoguang wxiaoguang Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be more careful about the string replacement. Snap document says that the SNAP_REVISION can be something like "x1" (https://snapcraft.io/docs/environment-variables) , a very short string. Maybe there will be other revision strings in future.

So maybe we should just split the appPath and check if the second last field is SNAP_REVISION and replace it, and check the gitea binary existence again. And print a log here to tell users we changed the app path.

Copy link

@OleHusgaard OleHusgaard Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Then the code would be like what I proposed in PR 17157, but with an added check that the second last field is SNAP_REVISION. That would also include a check with exec.LookPath() that the modified path will actually work.

}

return appPath, nil
}

func getWorkPath(appPath string) string {
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ parts:
source: .
stage-packages: [ git, sqlite3, openssh-client ]
build-packages: [ git, libpam0g-dev, libsqlite3-dev]
build-snaps: [ go, node/14/stable ]
build-snaps: [ go, node/16/stable ]
build-environment:
- LDFLAGS: ""
override-pull: |
Expand Down