Semaphore/util/config.go

299 lines
7.0 KiB
Go
Raw Normal View History

2016-01-05 00:32:53 +01:00
package util
import (
"encoding/base64"
2016-01-05 00:32:53 +01:00
"encoding/json"
"flag"
"fmt"
2016-03-16 22:49:43 +01:00
"os"
2016-04-24 20:11:43 +02:00
"path"
2016-03-16 22:49:43 +01:00
2016-01-05 00:32:53 +01:00
"github.com/bugsnag/bugsnag-go"
"github.com/gin-gonic/gin"
"github.com/gorilla/securecookie"
"golang.org/x/crypto/bcrypt"
2016-01-05 00:32:53 +01:00
)
var Cookie *securecookie.SecureCookie
2016-01-05 00:32:53 +01:00
var Migration bool
2016-04-18 02:58:29 +02:00
var InteractiveSetup bool
2016-04-26 20:18:28 +02:00
var Upgrade bool
2016-01-05 00:32:53 +01:00
type mySQLConfig struct {
Hostname string `json:"host"`
Username string `json:"user"`
Password string `json:"pass"`
DbName string `json:"name"`
}
type configType struct {
MySQL mySQLConfig `json:"mysql"`
// Format `:port_num` eg, :3000
Port string `json:"port"`
BugsnagKey string `json:"bugsnag_key"`
// semaphore stores projects here
TmpPath string `json:"tmp_path"`
// cookie hashing & encryption
CookieHash string `json:"cookie_hash"`
CookieEncryption string `json:"cookie_encryption"`
2017-02-22 09:46:42 +01:00
//email alerting
EmailAlert bool `json:"email_alert"`
2017-02-22 09:46:42 +01:00
EmailSender string `json:"email_sender"`
EmailHost string `json:"email_host"`
EmailPort string `json:"email_port"`
//web host
WebHost string `json:"web_host"`
//ldap settings
LdapEnable bool `json:"ldap_enable"`
LdapBindDN string `json:"ldap_binddn"`
LdapBindPassword string `json:"ldap_bindpassword"`
LdapServer string `json:"ldap_server"`
LdapNeedTLS bool `json:"ldap_needtls"`
LdapSearchDN string `json:"ldap_searchdn"`
LdapSearchFilter string `json:"ldap_searchfilter"`
2016-01-05 00:32:53 +01:00
}
var Config *configType
func NewConfig() *configType {
return &configType{}
}
2016-01-05 00:32:53 +01:00
func init() {
2016-04-18 02:58:29 +02:00
flag.BoolVar(&InteractiveSetup, "setup", false, "perform interactive setup")
2016-01-05 00:32:53 +01:00
flag.BoolVar(&Migration, "migrate", false, "execute migrations")
2016-04-26 20:18:28 +02:00
flag.BoolVar(&Upgrade, "upgrade", false, "upgrade semaphore")
2016-01-05 00:32:53 +01:00
path := flag.String("config", "", "config path")
2016-03-19 00:23:03 +01:00
var pwd string
flag.StringVar(&pwd, "hash", "", "generate hash of given password")
2016-04-11 12:30:31 +02:00
var printConfig bool
flag.BoolVar(&printConfig, "printConfig", false, "print example configuration")
2016-01-05 00:32:53 +01:00
flag.Parse()
2016-04-11 12:30:31 +02:00
if printConfig {
cfg := &configType{
2016-04-11 12:30:31 +02:00
MySQL: mySQLConfig{
Hostname: "127.0.0.1:3306",
Username: "root",
DbName: "semaphore",
},
Port: ":3000",
TmpPath: "/tmp/semaphore",
}
cfg.GenerateCookieSecrets()
b, _ := json.MarshalIndent(cfg, "", "\t")
2016-04-11 12:30:31 +02:00
fmt.Println(string(b))
os.Exit(0)
}
2016-03-19 00:23:03 +01:00
if len(pwd) > 0 {
password, _ := bcrypt.GenerateFromPassword([]byte(pwd), 11)
fmt.Println("Generated password: ", string(password))
os.Exit(0)
}
2016-01-05 00:32:53 +01:00
if path != nil && len(*path) > 0 {
// load
file, err := os.Open(*path)
if err != nil {
panic(err)
}
if err := json.NewDecoder(file).Decode(&Config); err != nil {
fmt.Println("Could not decode configuration!")
panic(err)
}
} else {
configFile, err := Asset("config.json")
if err != nil {
fmt.Println("Cannot Find configuration! Use -c parameter to point to a JSON file generated by -setup.\n\n Hint: have you run `-setup` ?")
2016-01-05 00:32:53 +01:00
os.Exit(1)
}
if err := json.Unmarshal(configFile, &Config); err != nil {
fmt.Println("Could not decode configuration!")
panic(err)
}
}
if len(os.Getenv("PORT")) > 0 {
Config.Port = ":" + os.Getenv("PORT")
}
if len(Config.Port) == 0 {
Config.Port = ":3000"
}
if len(Config.TmpPath) == 0 {
Config.TmpPath = "/tmp/semaphore"
}
var encryption []byte
encryption = nil
hash, _ := base64.StdEncoding.DecodeString(Config.CookieHash)
if len(Config.CookieEncryption) > 0 {
encryption, _ = base64.StdEncoding.DecodeString(Config.CookieEncryption)
}
Cookie = securecookie.New(hash, encryption)
2016-01-05 00:32:53 +01:00
stage := ""
if gin.Mode() == "release" {
stage = "production"
} else {
stage = "development"
}
bugsnag.Configure(bugsnag.Configuration{
APIKey: Config.BugsnagKey,
ReleaseStage: stage,
NotifyReleaseStages: []string{"production"},
AppVersion: Version,
2016-03-16 22:49:43 +01:00
ProjectPackages: []string{"github.com/ansible-semaphore/semaphore/**"},
2016-01-05 00:32:53 +01:00
})
}
func (conf *configType) GenerateCookieSecrets() {
hash := securecookie.GenerateRandomKey(32)
encryption := securecookie.GenerateRandomKey(32)
2016-01-05 00:32:53 +01:00
conf.CookieHash = base64.StdEncoding.EncodeToString(hash)
conf.CookieEncryption = base64.StdEncoding.EncodeToString(encryption)
2016-01-05 00:32:53 +01:00
}
func (conf *configType) Scan() {
2016-04-30 09:52:33 +02:00
fmt.Print(" > DB Hostname (default 127.0.0.1:3306): ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&conf.MySQL.Hostname)
2016-04-24 20:11:43 +02:00
if len(conf.MySQL.Hostname) == 0 {
conf.MySQL.Hostname = "127.0.0.1:3306"
}
2016-04-18 02:58:29 +02:00
2016-04-30 09:52:33 +02:00
fmt.Print(" > DB User (default root): ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&conf.MySQL.Username)
2016-04-24 20:11:43 +02:00
if len(conf.MySQL.Username) == 0 {
conf.MySQL.Username = "root"
}
2016-04-18 02:58:29 +02:00
2016-04-30 09:52:33 +02:00
fmt.Print(" > DB Password: ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&conf.MySQL.Password)
2016-04-30 09:52:33 +02:00
fmt.Print(" > DB Name (default semaphore): ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&conf.MySQL.DbName)
2016-04-24 20:11:43 +02:00
if len(conf.MySQL.DbName) == 0 {
conf.MySQL.DbName = "semaphore"
}
2016-04-18 02:58:29 +02:00
2016-04-30 09:52:33 +02:00
fmt.Print(" > Playbook path: ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&conf.TmpPath)
2016-04-24 20:11:43 +02:00
if len(conf.TmpPath) == 0 {
conf.TmpPath = "/tmp/semaphore"
}
conf.TmpPath = path.Clean(conf.TmpPath)
var alertanswer string
2017-03-13 03:30:48 +01:00
fmt.Print(" > Enable email alerts (y/n, default n): ")
fmt.Scanln(&alertanswer)
if alertanswer == "yes" || alertanswer == "y" {
conf.EmailAlert = true
2017-03-27 07:08:41 +02:00
fmt.Print(" > Mail server host (default localhost389): ")
fmt.Scanln(&conf.EmailHost)
if len(conf.EmailHost) == 0 {
conf.EmailHost = "localhost"
}
fmt.Print(" > Mail server port (default 25): ")
fmt.Scanln(&conf.EmailPort)
if len(conf.EmailPort) == 0 {
conf.EmailPort = "25"
}
fmt.Print(" > Mail sender address (default semaphore@localhost): ")
fmt.Scanln(&conf.EmailSender)
if len(conf.EmailSender) == 0 {
conf.EmailSender = "semaphore@localhost"
}
fmt.Print(" > Web root URL (default http://localhost:8010/): ")
fmt.Scanln(&conf.WebHost)
if len(conf.WebHost) == 0 {
conf.WebHost = "http://localhost:8010/"
}
} else {
conf.EmailAlert = false
}
2017-03-27 07:08:41 +02:00
var LdapAnswer string
fmt.Print(" > Enable LDAP authentificaton (y/n, default n): ")
fmt.Scanln(&LdapAnswer)
if LdapAnswer == "yes" || LdapAnswer == "y" {
conf.LdapEnable = true
fmt.Print(" > LDAP server host (default localhost:389): ")
fmt.Scanln(&conf.LdapServer)
if len(conf.LdapServer) == 0 {
conf.LdapServer = "localhost:389"
}
var LdapTLSAnswer string
fmt.Print(" > Enable LDAP TLS connection (y/n, default n): ")
fmt.Scanln(&LdapTLSAnswer)
if LdapTLSAnswer == "yes" || LdapTLSAnswer == "y" {
conf.LdapNeedTLS = true
} else {
conf.LdapNeedTLS = false
}
fmt.Print(" > LDAP DN for bind (default cn=user,ou=users,dc=example): ")
fmt.Scanln(&conf.LdapBindDN)
if len(conf.LdapBindDN) == 0 {
conf.LdapBindDN = "cn=user,ou=users,dc=example"
}
fmt.Print(" > Password for LDAP bind user (default pa55w0rd): ")
fmt.Scanln(&conf.LdapBindPassword)
if len(conf.LdapBindPassword) == 0 {
conf.LdapBindPassword = "pa55w0rd"
}
fmt.Print(" > LDAP DN for user search (default ou=users,dc=example): ")
fmt.Scanln(&conf.LdapSearchDN)
if len(conf.LdapSearchDN) == 0 {
conf.LdapSearchDN = "ou=users,dc=example"
}
fmt.Print(" > LDAP search filter (default (uid=%s)): ")
fmt.Scanln(&conf.LdapSearchFilter)
if len(conf.LdapSearchFilter) == 0 {
conf.LdapSearchFilter = "(uid=%s)"
}
} else {
conf.LdapEnable = false
}
2016-04-18 02:58:29 +02:00
}