2021-08-25 22:12:19 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-25 22:36:04 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/ansible-semaphore/semaphore/api"
|
|
|
|
"github.com/ansible-semaphore/semaphore/api/sockets"
|
|
|
|
"github.com/ansible-semaphore/semaphore/api/tasks"
|
2021-08-25 22:12:19 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db/factory"
|
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2021-08-25 22:36:04 +02:00
|
|
|
"github.com/gorilla/context"
|
|
|
|
"github.com/gorilla/handlers"
|
2021-08-25 22:12:19 +02:00
|
|
|
"github.com/spf13/cobra"
|
2021-08-25 22:36:04 +02:00
|
|
|
"net/http"
|
2021-08-25 22:12:19 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configPath string
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "semaphore",
|
|
|
|
Short: "Ansible Semaphore is a beautiful web UI for Ansible",
|
2021-08-27 10:50:26 +02:00
|
|
|
Long: `Ansible Semaphore is a beautiful web UI for Ansible.
|
|
|
|
Source code is available at https://github.com/ansible-semaphore/semaphore.
|
|
|
|
Complete documentation is available at https://ansible-semaphore.com.`,
|
2021-08-25 22:12:19 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) == 0 && configPath == "" {
|
|
|
|
_ = cmd.Help()
|
|
|
|
os.Exit(0)
|
|
|
|
} else {
|
|
|
|
serviceCmd.Run(cmd, args)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
2021-08-25 22:36:04 +02:00
|
|
|
args := os.Args[1:]
|
|
|
|
if len(args) == 2 && args[0] == "-config" {
|
|
|
|
configPath = args[1]
|
|
|
|
runService()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:12:19 +02:00
|
|
|
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "Configuration file path")
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:36:04 +02:00
|
|
|
func runService() {
|
|
|
|
store := createStore()
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
fmt.Printf("Semaphore %v\n", util.Version)
|
|
|
|
fmt.Printf("Interface %v\n", util.Config.Interface)
|
|
|
|
fmt.Printf("Port %v\n", util.Config.Port)
|
|
|
|
|
|
|
|
go sockets.StartWS()
|
|
|
|
go tasks.StartRunner()
|
|
|
|
|
|
|
|
route := api.Route()
|
|
|
|
|
|
|
|
route.Use(func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
context.Set(r, "store", store)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var router http.Handler = route
|
|
|
|
|
|
|
|
router = handlers.ProxyHeaders(router)
|
|
|
|
http.Handle("/", router)
|
|
|
|
|
|
|
|
fmt.Println("Server is running")
|
|
|
|
|
|
|
|
err := http.ListenAndServe(util.Config.Interface+util.Config.Port, cropTrailingSlashMiddleware(router))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:12:19 +02:00
|
|
|
func createStore() db.Store {
|
|
|
|
util.ConfigInit(configPath)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case util.Config.MySQL.IsPresent():
|
|
|
|
fmt.Printf("MySQL %v@%v %v\n", util.Config.MySQL.Username, util.Config.MySQL.Hostname, util.Config.MySQL.DbName)
|
|
|
|
case util.Config.BoltDb.IsPresent():
|
|
|
|
fmt.Printf("BoltDB %v\n", util.Config.BoltDb.Hostname)
|
|
|
|
case util.Config.Postgres.IsPresent():
|
|
|
|
fmt.Printf("Postgres %v@%v %v\n", util.Config.Postgres.Username, util.Config.Postgres.Hostname, util.Config.Postgres.DbName)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("database configuration not found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Tmp Path (projects home) %v\n", util.Config.TmpPath)
|
|
|
|
|
|
|
|
store := factory.CreateStore()
|
|
|
|
|
|
|
|
if err := store.Connect(); err != nil {
|
|
|
|
fmt.Println("\n Have you run `semaphore setup`?")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.Migrate(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return store
|
|
|
|
}
|