Refactor environment variables to configuration and registration (#90)

Close #21.

Refactor environment variables to configuration file (config.yaml) and registration file (.runner).

The old environment variables are still supported, but warning logs will be printed.

Like:

```text
$ GITEA_DEBUG=true ./act_runner -c config.yaml daemon
INFO[0000] Starting runner daemon
WARN[0000] env GITEA_DEBUG has been ignored because config file is used

$ GITEA_DEBUG=true ./act_runner daemon
INFO[0000] Starting runner daemon
WARN[0000] env GITEA_DEBUG will be deprecated, please use config file instead
```

Reviewed-on: https://gitea.com/gitea/act_runner/pulls/90
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
Jason Song
2023-04-02 22:41:48 +08:00
parent 8eea12dd78
commit 7e7096e60b
20 changed files with 393 additions and 280 deletions

View File

@ -42,14 +42,15 @@ type Handler struct {
outboundIP string
}
func NewHandler() (*Handler, error) {
func NewHandler(dir, outboundIP string, port uint16) (*Handler, error) {
h := &Handler{}
dir := "" // TODO: make the dir configurable if necessary
if home, err := os.UserHomeDir(); err != nil {
return nil, err
} else {
dir = filepath.Join(home, ".cache/actcache")
if dir == "" {
if home, err := os.UserHomeDir(); err != nil {
return nil, err
} else {
dir = filepath.Join(home, ".cache", "actcache")
}
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, err
@ -70,7 +71,9 @@ func NewHandler() (*Handler, error) {
}
h.storage = storage
if ip, err := getOutboundIP(); err != nil {
if outboundIP != "" {
h.outboundIP = outboundIP
} else if ip, err := getOutboundIP(); err != nil {
return nil, err
} else {
h.outboundIP = ip.String()
@ -102,8 +105,7 @@ func NewHandler() (*Handler, error) {
h.gcCache()
// TODO: make the port configurable if necessary
listener, err := net.Listen("tcp", ":0") // random available port
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) // listen on all interfaces
if err != nil {
return nil, err
}