Semaphore/cli/cmd/root.go

113 lines
2.6 KiB
Go
Raw Normal View History

2021-08-25 22:12:19 +02:00
package cmd
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/api"
"github.com/ansible-semaphore/semaphore/api/sockets"
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/services/schedules"
"github.com/ansible-semaphore/semaphore/services/tasks"
2021-08-25 22:12:19 +02:00
"github.com/ansible-semaphore/semaphore/util"
"github.com/gorilla/context"
"github.com/gorilla/handlers"
2021-08-25 22:12:19 +02:00
"github.com/spf13/cobra"
"net/http"
2021-08-25 22:12:19 +02:00
"os"
2023-09-20 10:22:26 +02:00
"strings"
2021-08-25 22:12:19 +02:00
)
var configPath string
var rootCmd = &cobra.Command{
Use: "semaphore",
Short: "Ansible Semaphore is a beautiful web UI for Ansible",
2021-12-18 14:16:34 +01:00
Long: `Ansible Semaphore is a beautiful web UI for Ansible.
2021-08-27 10:50:26 +02:00
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) {
_ = cmd.Help()
os.Exit(0)
2021-08-25 22:12:19 +02:00
},
}
func Execute() {
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "Configuration file path")
2021-08-25 22:12:19 +02:00
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func runService() {
store := createStore("root")
taskPool := tasks.CreateTaskPool(store)
schedulePool := schedules.CreateSchedulePool(store, &taskPool)
defer schedulePool.Destroy()
2021-08-28 18:24:54 +02:00
util.Config.PrintDbInfo()
2023-09-20 10:22:26 +02:00
port := util.Config.Port
if !strings.HasPrefix(port, ":") {
port = ":" + port
}
fmt.Printf("Tmp Path (projects home) %v\n", util.Config.TmpPath)
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 schedulePool.Run()
go taskPool.Run()
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)
context.Set(r, "schedule_pool", schedulePool)
context.Set(r, "task_pool", &taskPool)
next.ServeHTTP(w, r)
})
})
var router http.Handler = route
router = handlers.ProxyHeaders(router)
http.Handle("/", router)
fmt.Println("Server is running")
if store.PermanentConnection() {
defer store.Close("root")
} else {
store.Close("root")
}
2023-09-20 10:22:26 +02:00
err := http.ListenAndServe(util.Config.Interface+port, cropTrailingSlashMiddleware(router))
if err != nil {
log.Panic(err)
}
}
func createStore(token string) db.Store {
2021-08-25 22:12:19 +02:00
util.ConfigInit(configPath)
store := factory.CreateStore()
store.Connect(token)
err := db.Migrate(store)
2021-12-18 14:16:34 +01:00
if err != nil {
panic(err)
}
2021-08-25 22:12:19 +02:00
return store
2021-12-18 14:16:34 +01:00
}