Semaphore/main.go

164 lines
3.9 KiB
Go
Raw Normal View History

2016-01-05 00:32:53 +01:00
package main
import (
2016-04-18 02:58:29 +02:00
"encoding/json"
2016-01-05 00:32:53 +01:00
"fmt"
2016-04-18 02:58:29 +02:00
"io/ioutil"
"os"
2016-04-24 20:11:43 +02:00
"path"
2016-04-18 02:58:29 +02:00
"strings"
2016-03-16 22:49:43 +01:00
"github.com/ansible-semaphore/semaphore/database"
"github.com/ansible-semaphore/semaphore/migration"
2016-04-18 02:58:29 +02:00
"github.com/ansible-semaphore/semaphore/models"
2016-03-16 22:49:43 +01:00
"github.com/ansible-semaphore/semaphore/routes"
2016-04-02 14:40:07 +02:00
"github.com/ansible-semaphore/semaphore/routes/sockets"
2016-04-04 15:44:34 +02:00
"github.com/ansible-semaphore/semaphore/routes/tasks"
2016-04-26 20:18:28 +02:00
"github.com/ansible-semaphore/semaphore/upgrade"
2016-03-16 22:49:43 +01:00
"github.com/ansible-semaphore/semaphore/util"
2016-01-05 00:32:53 +01:00
"github.com/bugsnag/bugsnag-go"
"github.com/gin-gonic/gin"
2016-04-30 09:52:33 +02:00
"golang.org/x/crypto/bcrypt"
2016-01-05 00:32:53 +01:00
)
func main() {
2016-04-18 02:58:29 +02:00
if util.InteractiveSetup {
os.Exit(doSetup())
}
2016-04-26 20:18:28 +02:00
if util.Upgrade {
if err := upgrade.Upgrade(util.Version); err != nil {
panic(err)
}
os.Exit(0)
}
2016-01-05 00:32:53 +01:00
fmt.Printf("Semaphore %v\n", util.Version)
fmt.Printf("Port %v\n", util.Config.Port)
2016-04-18 02:58:29 +02:00
fmt.Printf("MySQL %v@%v %v\n", util.Config.MySQL.Username, util.Config.MySQL.Hostname, util.Config.MySQL.DbName)
fmt.Printf("Tmp Path (projects home) %v\n", util.Config.TmpPath)
2016-01-05 00:32:53 +01:00
2016-04-18 02:58:29 +02:00
if err := database.Connect(); err != nil {
panic(err)
}
models.SetupDBLink()
2016-01-05 00:32:53 +01:00
defer database.Mysql.Db.Close()
if util.Migration {
fmt.Println("\n Running DB Migrations")
if err := migration.MigrateAll(); err != nil {
panic(err)
}
return
}
2016-04-02 14:40:07 +02:00
go sockets.StartWS()
2016-01-05 00:32:53 +01:00
r := gin.New()
r.Use(gin.Recovery(), recovery, gin.Logger())
routes.Route(r)
2016-05-17 17:18:26 +02:00
go upgrade.CheckUpdate(util.Version)
2016-04-04 15:44:34 +02:00
go tasks.StartRunner()
2016-01-05 00:32:53 +01:00
r.Run(util.Config.Port)
}
func recovery(c *gin.Context) {
defer bugsnag.AutoNotify()
c.Next()
}
2016-04-18 02:58:29 +02:00
func doSetup() int {
fmt.Print(`
2016-04-30 09:52:33 +02:00
Hello! You will now be guided through a setup to:
2016-04-18 02:58:29 +02:00
2016-04-30 09:52:33 +02:00
1. Set up configuration for a MySQL/MariaDB database
2. Set up a path for your playbooks (auto-created)
3. Run database Migrations
4. Set up initial seamphore user & password
2016-04-18 02:58:29 +02:00
`)
var b []byte
setup := util.NewConfig()
2016-04-18 02:58:29 +02:00
for true {
setup.Scan()
2016-04-18 02:58:29 +02:00
var err error
2016-04-30 09:52:33 +02:00
b, err = json.MarshalIndent(&setup, " ", "\t")
2016-04-18 02:58:29 +02:00
if err != nil {
panic(err)
}
2016-04-30 09:52:33 +02:00
fmt.Printf("\n Generated configuration:\n %v\n\n", string(b))
fmt.Print(" > Is this correct? (yes/no): ")
2016-04-18 02:58:29 +02:00
var answer string
fmt.Scanln(&answer)
2016-04-30 09:52:33 +02:00
if answer == "yes" || answer == "y" {
break
2016-04-18 02:58:29 +02:00
}
2016-04-30 09:52:33 +02:00
fmt.Println()
setup = util.NewConfig()
2016-04-18 02:58:29 +02:00
}
setup.GenerateCookieSecrets()
2016-04-30 09:52:33 +02:00
fmt.Printf(" Running: mkdir -p %v..\n", setup.TmpPath)
2016-04-24 20:11:43 +02:00
os.MkdirAll(setup.TmpPath, 0755)
configPath := path.Join(setup.TmpPath, "/semaphore_config.json")
2016-04-30 09:52:33 +02:00
fmt.Printf(" Configuration written to %v..\n", setup.TmpPath)
2016-04-24 20:11:43 +02:00
if err := ioutil.WriteFile(configPath, b, 0644); err != nil {
2016-04-18 02:58:29 +02:00
panic(err)
}
2016-04-30 09:52:33 +02:00
fmt.Println(" Pinging database..")
2016-04-18 02:58:29 +02:00
util.Config = setup
if err := database.Connect(); err != nil {
2016-04-30 09:52:33 +02:00
fmt.Printf("\n Cannot connect to database!\n %v\n", err.Error())
os.Exit(1)
2016-04-18 02:58:29 +02:00
}
2016-04-30 09:52:33 +02:00
fmt.Println("\n Running DB Migrations..")
2016-04-18 02:58:29 +02:00
if err := migration.MigrateAll(); err != nil {
2016-04-30 09:52:33 +02:00
fmt.Printf("\n Database migrations failed!\n %v\n", err.Error())
os.Exit(1)
2016-04-18 02:58:29 +02:00
}
var user models.User
2016-04-30 09:52:33 +02:00
fmt.Print("\n\n > Your name: ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&user.Name)
2016-04-30 09:52:33 +02:00
fmt.Print(" > Username: ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&user.Username)
2016-04-30 09:52:33 +02:00
fmt.Print(" > Email: ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&user.Email)
2016-04-30 09:52:33 +02:00
fmt.Print(" > Password: ")
2016-04-18 02:58:29 +02:00
fmt.Scanln(&user.Password)
pwdHash, _ := bcrypt.GenerateFromPassword([]byte(user.Password), 11)
user.Username = strings.ToLower(user.Username)
user.Email = strings.ToLower(user.Email)
if _, err := database.Mysql.Exec("insert into user set name=?, username=?, email=?, password=?, created=NOW()", user.Name, user.Username, user.Email, pwdHash); err != nil {
2016-04-30 09:52:33 +02:00
fmt.Printf(" Inserting user failed. If you already have a user, you can disregard this error.\n %v\n", err.Error())
os.Exit(1)
2016-04-18 02:58:29 +02:00
}
2016-04-30 09:52:33 +02:00
fmt.Printf("\n You are all setup %v!\n", user.Name)
fmt.Printf(" Re-launch this program pointing to the configuration file\n\n./semaphore -config %v\n\n", configPath)
fmt.Printf(" To run as daemon:\n\nnohup ./semaphore -config %v &\n\n", configPath)
fmt.Println(" Your login is %v or %v.", user.Email, user.Username)
2016-04-18 02:58:29 +02:00
return 0
}