Skip to content
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

Add cmd to allow generalised mapping of environment variables to Gitea Ini #7287

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
94426b6
Add cmd to allow generalised mapping of environment variables to Gite…
zeripath Jun 24, 2019
890d539
spelling mistake
zeripath Jun 24, 2019
51338a1
Use full path to gitea
zeripath Jun 24, 2019
64c15c2
Move to use double underscore as a separator
zeripath Jun 24, 2019
f73377b
Add encoding to the environment strings
zeripath Jun 26, 2019
730e300
Remove unused separator
zeripath Jun 26, 2019
e0ca836
Add Documentation
zeripath Jun 26, 2019
30a289f
Merge branch 'master' into environment-to-ini
zeripath Jun 26, 2019
f7d10ea
Remove unused args struct
zeripath Jun 26, 2019
4ae3b4d
Merge branch 'environment-to-ini' of github.com:zeripath/gitea into e…
zeripath Jun 26, 2019
aba65bf
Merge branch 'master' into environment-to-ini
zeripath Jul 7, 2019
80b54d1
Merge branch 'master' into environment-to-ini
zeripath Aug 16, 2019
fd82c14
Merge branch 'master' into environment-to-ini
zeripath Aug 16, 2019
fc52e23
Merge branch 'master' into environment-to-ini
zeripath Aug 17, 2019
3f5e1ba
Provide example as per @silverwind
zeripath Aug 17, 2019
aa42fb8
Merge branch 'master' into environment-to-ini
zeripath Oct 9, 2019
019e124
Fix Unknwon to unknwon in cmd/environment_to_ini
zeripath Oct 9, 2019
f157062
Remove Prefix before passing to DecodeSectionKey
zeripath Oct 9, 2019
8a5a3b8
Merge branch 'master' into environment-to-ini
zeripath Oct 14, 2019
bd44265
Merge branch 'master' into environment-to-ini
sapk Oct 19, 2019
cbd9404
Merge branch 'master' into environment-to-ini
zeripath Dec 23, 2019
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
92 changes: 92 additions & 0 deletions cmd/environment_to_ini.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package cmd

import (
"os"
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"github.com/Unknwon/com"
"github.com/urfave/cli"
ini "gopkg.in/ini.v1"
)

// EnvironmentPrefix environment variables prefixed with this represent ini values to write
const EnvironmentPrefix = "GITEA:"
zeripath marked this conversation as resolved.
Show resolved Hide resolved

// Separator is the character that will separate section from key name
const Separator = ":"
zeripath marked this conversation as resolved.
Show resolved Hide resolved

// CmdEnvironmentToIni represents the command to use a provided environment to update the configuration ini
var CmdEnvironmentToIni = cli.Command{
Name: "environment-to-ini",
Usage: "Use provided environment to update configuration ini",
Action: runEnvironmentToIni,
Flags: []cli.Flag{
cli.StringFlag{
Name: "out, o",
Value: "",
Usage: "Destination file to write to",
},
},
}

func runEnvironmentToIni(c *cli.Context) error {
cfg := ini.Empty()
if com.IsFile(setting.CustomConf) {
if err := cfg.Append(setting.CustomConf); err != nil {
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err)
}
} else {
log.Warn("Custom config '%s' not found, ignore this if you're running first time", setting.CustomConf)
}
cfg.NameMapper = ini.AllCapsUnderscore

for _, kv := range os.Environ() {
idx := strings.IndexByte(kv, '=')
if idx < 0 {
continue
}
eKey := kv[:idx]
value := kv[idx+1:]
if !strings.HasPrefix(eKey, EnvironmentPrefix) {
continue
}
parts := strings.Split(eKey, Separator)
if len(parts) != 3 {
continue
}
sectionName := parts[1]
keyName := parts[2]
if len(sectionName) == 0 || len(keyName) == 0 {
continue
}
section, err := cfg.GetSection(sectionName)
if err != nil {
section, err = cfg.NewSection(sectionName)
if err != nil {
log.Error("Error creating section: %s : %v", sectionName, err)
continue
}
}
key := section.Key(keyName)
if key == nil {
key, err = section.NewKey(keyName, value)
if err != nil {
log.Error("Error creating key: %s in section: %s with value: %s : %v", keyName, sectionName, value, err)
continue
}
}
key.SetValue(value)
}
destination := c.String("out")
if len(destination) == 0 {
destination = setting.CustomConf
}
err := cfg.SaveTo(destination)
return err
}
2 changes: 2 additions & 0 deletions docker/root/etc/s6/gitea/setup
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ if [ ! -f ${GITEA_CUSTOM}/conf/app.ini ]; then
SECRET_KEY=${SECRET_KEY:-""} \
envsubst < /etc/templates/app.ini > ${GITEA_CUSTOM}/conf/app.ini
Copy link
Member

@sapk sapk Oct 19, 2019

Choose a reason for hiding this comment

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

Can't we drop the template and for example do ?

SECRET_KEY=${SECRET_KEY:-""}
GITEA__security__SECRET_KEY=${GITEA__security__SECRET_KEY:-"$SECRET_KEY"}

(maybe they can get in one line)
The goal could be to deprecate the old variables and use the new generic only later ?
So that ${GITEA_CUSTOM}/conf/app.ini is only write once and drop template app.ini


/app/gitea/gitea environment-to-ini -c ${GITEA_CUSTOM}/conf/app.ini

chown ${USER}:git ${GITEA_CUSTOM}/conf/app.ini
fi

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ arguments - which can alternatively be run by running the subcommand web.`
cmd.CmdMigrate,
cmd.CmdKeys,
cmd.CmdConvert,
cmd.CmdEnvironmentToIni,
}
// Now adjust these commands to add our global configuration options

Expand Down