2020-09-28 07:26:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/flate"
|
2020-09-29 10:40:09 +00:00
|
|
|
"encoding/json"
|
2020-09-28 07:26:16 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-10-21 03:59:10 +00:00
|
|
|
"path/filepath"
|
2020-10-09 08:34:46 +00:00
|
|
|
"strings"
|
2020-09-28 07:55:23 +00:00
|
|
|
"time"
|
2020-09-28 07:26:16 +00:00
|
|
|
|
2020-10-23 21:56:49 +00:00
|
|
|
"git.rootprojects.org/root/gitdeploy/assets/examples"
|
2020-10-24 00:03:15 +00:00
|
|
|
"git.rootprojects.org/root/gitdeploy/assets/public"
|
2020-11-21 12:41:01 +00:00
|
|
|
"git.rootprojects.org/root/gitdeploy/internal/api"
|
2020-12-01 00:48:09 +00:00
|
|
|
"git.rootprojects.org/root/gitdeploy/internal/log"
|
2020-10-09 09:22:06 +00:00
|
|
|
"git.rootprojects.org/root/gitdeploy/internal/options"
|
|
|
|
"git.rootprojects.org/root/gitdeploy/internal/webhooks"
|
2020-10-24 00:03:15 +00:00
|
|
|
"git.rootprojects.org/root/vfscopy"
|
2020-09-28 09:56:51 +00:00
|
|
|
|
2020-09-28 07:26:16 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
2020-11-21 12:41:01 +00:00
|
|
|
|
2020-09-28 09:56:51 +00:00
|
|
|
_ "github.com/joho/godotenv/autoload"
|
2020-09-28 07:26:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
name = "gitdeploy"
|
|
|
|
version = "0.0.0"
|
|
|
|
date = "0001-01-01T00:00:00Z"
|
|
|
|
commit = "0000000"
|
|
|
|
)
|
|
|
|
|
|
|
|
func usage() {
|
2020-10-20 23:05:01 +00:00
|
|
|
fmt.Println(ver())
|
2020-09-28 07:26:16 +00:00
|
|
|
fmt.Println("")
|
2020-10-20 23:05:01 +00:00
|
|
|
fmt.Printf("Use '%s help <command>'\n", name)
|
2020-09-28 07:26:16 +00:00
|
|
|
fmt.Println(" init")
|
|
|
|
fmt.Println(" run")
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:05:01 +00:00
|
|
|
func ver() string {
|
2020-10-20 23:35:35 +00:00
|
|
|
return fmt.Sprintf("%s v%s (%s) %s", name, version, commit[:7], date)
|
2020-09-28 07:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 23:39:05 +00:00
|
|
|
var runOpts *options.ServerConfig
|
2020-09-28 07:26:16 +00:00
|
|
|
var runFlags *flag.FlagSet
|
|
|
|
var initFlags *flag.FlagSet
|
2020-10-19 19:21:50 +00:00
|
|
|
var promotions []string
|
|
|
|
var promotionList string
|
|
|
|
var defaultPromotionList = "production,staging,master"
|
2020-10-21 03:59:10 +00:00
|
|
|
var oldScripts string
|
2020-09-28 07:26:16 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-09-28 23:39:05 +00:00
|
|
|
runOpts = options.Server
|
2020-10-21 05:54:33 +00:00
|
|
|
|
2020-09-28 23:39:05 +00:00
|
|
|
initFlags = options.InitFlags
|
2020-10-21 05:54:33 +00:00
|
|
|
_ = initFlags.Bool("TODO", false, "init will eventually copy default assets into a local directory")
|
|
|
|
|
|
|
|
runFlags = options.ServerFlags
|
2020-10-09 08:15:39 +00:00
|
|
|
runFlags.StringVar(&runOpts.Addr, "listen", "", "the address and port on which to listen (default :4483)")
|
2020-09-28 23:39:05 +00:00
|
|
|
runFlags.BoolVar(&runOpts.TrustProxy, "trust-proxy", false, "trust X-Forwarded-For header")
|
2020-10-21 03:59:10 +00:00
|
|
|
runFlags.StringVar(&runOpts.RepoList, "trust-repos", "",
|
2020-10-21 05:54:33 +00:00
|
|
|
"list of repos (ex: 'github.com/org/repo', or '*' for all) for which to run '.gitdeploy/deploy.sh'")
|
2020-09-28 23:39:05 +00:00
|
|
|
runFlags.BoolVar(&runOpts.Compress, "compress", true, "enable compression for text,html,js,css,etc")
|
2020-09-29 03:15:30 +00:00
|
|
|
runFlags.StringVar(
|
|
|
|
&runOpts.ServePath, "serve-path", "",
|
|
|
|
"path to serve, falls back to built-in web app")
|
|
|
|
runFlags.StringVar(
|
2020-10-21 03:59:10 +00:00
|
|
|
&oldScripts, "exec", "",
|
|
|
|
"old alias for --scripts")
|
|
|
|
runFlags.StringVar(
|
2020-10-21 05:54:33 +00:00
|
|
|
&runOpts.ScriptsPath, "scripts", "",
|
2020-09-29 10:40:09 +00:00
|
|
|
"path to ./scripts/{deploy.sh,promote.sh,etc}")
|
|
|
|
//"path to bash script to run with git info as arguments")
|
2020-10-19 19:21:50 +00:00
|
|
|
runFlags.StringVar(&promotionList, "promotions", "",
|
|
|
|
"a list of promotable branches in descending order (default '"+defaultPromotionList+"')")
|
2020-09-28 07:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-10-20 23:05:01 +00:00
|
|
|
// Support [--]version and -V
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
if "version" == strings.TrimLeft(os.Args[1], "-") || "-V" == os.Args[1] {
|
|
|
|
fmt.Println(ver())
|
|
|
|
os.Exit(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:26:16 +00:00
|
|
|
args := os.Args[:]
|
|
|
|
if 1 == len(args) {
|
|
|
|
// "run" should be the default
|
|
|
|
args = append(args, "run")
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:05:01 +00:00
|
|
|
if "help" == strings.TrimLeft(args[1], "-") {
|
2020-09-28 07:26:16 +00:00
|
|
|
// top-level help
|
|
|
|
if 2 == len(args) {
|
|
|
|
usage()
|
|
|
|
os.Exit(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// move help to subcommand argument
|
|
|
|
self := args[0]
|
|
|
|
args = append([]string{self}, args[2:]...)
|
|
|
|
args = append(args, "--help")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch args[1] {
|
|
|
|
case "version":
|
2020-10-20 23:05:01 +00:00
|
|
|
fmt.Println(ver())
|
2020-09-28 07:26:16 +00:00
|
|
|
os.Exit(0)
|
|
|
|
return
|
|
|
|
case "init":
|
2020-09-29 08:08:35 +00:00
|
|
|
_ = initFlags.Parse(args[2:])
|
2020-10-24 00:03:15 +00:00
|
|
|
gdInit()
|
2020-10-21 05:54:33 +00:00
|
|
|
os.Exit(0)
|
|
|
|
return
|
2020-09-28 07:26:16 +00:00
|
|
|
case "run":
|
2020-09-29 08:08:35 +00:00
|
|
|
_ = runFlags.Parse(args[2:])
|
2020-10-21 05:54:33 +00:00
|
|
|
if "" == runOpts.ScriptsPath {
|
2020-10-21 03:59:10 +00:00
|
|
|
if "" != oldScripts {
|
|
|
|
fmt.Fprintf(os.Stderr, "--exec is deprecated and will be removed. Please use --scripts instead.\n")
|
2020-10-21 05:54:33 +00:00
|
|
|
runOpts.ScriptsPath = oldScripts
|
2020-10-21 03:59:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 05:54:33 +00:00
|
|
|
if "" == runOpts.ScriptsPath {
|
|
|
|
runOpts.ScriptsPath = "./scripts"
|
2020-10-21 03:59:10 +00:00
|
|
|
pathname, _ := filepath.Abs("./scripts")
|
|
|
|
if info, _ := os.Stat("./scripts/deploy.sh"); nil == info || !info.Mode().IsRegular() {
|
|
|
|
fmt.Printf(
|
|
|
|
"%q not found.\nPlease provide --scripts ./scripts/ as a path where \"deploy.sh\" can be found\n",
|
|
|
|
pathname,
|
|
|
|
)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-09-29 03:15:30 +00:00
|
|
|
}
|
2020-10-09 08:15:39 +00:00
|
|
|
if 0 == len(runOpts.Addr) {
|
|
|
|
runOpts.Addr = os.Getenv("LISTEN")
|
|
|
|
}
|
2020-10-24 00:03:15 +00:00
|
|
|
if 0 == len(runOpts.Addr) {
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if len(port) > 0 {
|
|
|
|
runOpts.Addr = "localhost:" + port
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 08:15:39 +00:00
|
|
|
if 0 == len(runOpts.Addr) {
|
2020-10-21 03:59:10 +00:00
|
|
|
runOpts.Addr = "localhost:4483"
|
2020-10-09 08:15:39 +00:00
|
|
|
}
|
|
|
|
if 0 == len(runOpts.Addr) {
|
|
|
|
fmt.Printf("--listen <[addr]:port> is a required flag")
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2020-10-21 03:59:10 +00:00
|
|
|
if 0 == len(runOpts.RepoList) {
|
2020-10-21 04:44:31 +00:00
|
|
|
runOpts.RepoList = os.Getenv("TRUST_REPOS")
|
|
|
|
}
|
|
|
|
if len(runOpts.RepoList) > 0 {
|
|
|
|
runOpts.RepoList = strings.ReplaceAll(runOpts.RepoList, ",", " ")
|
|
|
|
runOpts.RepoList = strings.ReplaceAll(runOpts.RepoList, " ", " ")
|
2020-10-26 20:12:43 +00:00
|
|
|
runOpts.RepoList = strings.ToLower(runOpts.RepoList)
|
2020-10-21 03:59:10 +00:00
|
|
|
}
|
2020-11-21 12:41:01 +00:00
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
log.Printf("WORK_DIR=%s", cwd)
|
|
|
|
log.Printf("TRUST_REPOS=%s", runOpts.RepoList)
|
2020-10-19 19:21:50 +00:00
|
|
|
if 0 == len(promotionList) {
|
|
|
|
promotionList = os.Getenv("PROMOTIONS")
|
|
|
|
}
|
|
|
|
if 0 == len(promotionList) {
|
|
|
|
promotionList = defaultPromotionList
|
|
|
|
}
|
2020-11-21 12:41:01 +00:00
|
|
|
runOpts.Promotions = strings.Fields(
|
2020-10-19 19:21:50 +00:00
|
|
|
strings.ReplaceAll(promotionList, ",", " "),
|
|
|
|
)
|
|
|
|
|
2020-09-28 23:39:05 +00:00
|
|
|
webhooks.MustRegisterAll()
|
2020-09-28 07:26:16 +00:00
|
|
|
serve()
|
|
|
|
default:
|
|
|
|
usage()
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 00:03:15 +00:00
|
|
|
func gdInit() {
|
|
|
|
vfs := vfscopy.NewVFS(examples.Assets)
|
|
|
|
_, err := os.Open("scripts")
|
|
|
|
fmt.Println("Initiazing ...")
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
fmt.Fprintf(os.Stderr, " skip: ./scripts already exists\n")
|
|
|
|
} else {
|
|
|
|
if err := vfscopy.CopyAll(vfs, ".", "./scripts", vfscopy.Options{
|
|
|
|
AddPermission: os.FileMode(0600),
|
|
|
|
Skip: func(path string) (bool, error) {
|
|
|
|
if strings.HasSuffix(path, "/dotenv") {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ := vfs.Open(path)
|
|
|
|
fi, _ := f.Stat()
|
|
|
|
if !fi.IsDir() {
|
|
|
|
fmt.Println(" copy: scripts/" + path)
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
}); nil != err {
|
|
|
|
fmt.Fprintf(os.Stderr, "error initializing ./scripts directory: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = os.Open(".env")
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
fmt.Fprintf(os.Stderr, " skip: ./.env already exists\n")
|
|
|
|
} else {
|
|
|
|
if err := vfscopy.CopyAll(vfs, "dotenv", ".env", vfscopy.Options{
|
|
|
|
AddPermission: os.FileMode(0600),
|
|
|
|
}); nil != err {
|
|
|
|
fmt.Fprintf(os.Stderr, "error initializing ./.env file: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = os.Chmod(".env", 0600)
|
|
|
|
fmt.Println(" copy: .env")
|
|
|
|
}
|
|
|
|
fmt.Println("Done.")
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:26:16 +00:00
|
|
|
func serve() {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
// A good base middleware stack
|
2020-09-28 23:39:05 +00:00
|
|
|
if runOpts.TrustProxy {
|
2020-09-28 07:26:16 +00:00
|
|
|
r.Use(middleware.RealIP)
|
|
|
|
}
|
2020-09-28 23:39:05 +00:00
|
|
|
if runOpts.Compress {
|
2020-09-28 07:26:16 +00:00
|
|
|
r.Use(middleware.Compress(flate.DefaultCompression))
|
|
|
|
}
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|
r.Use(middleware.Recoverer)
|
2020-09-28 09:43:30 +00:00
|
|
|
|
2020-10-20 23:05:01 +00:00
|
|
|
r.Get("/version", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(append([]byte(ver()), '\n'))
|
|
|
|
})
|
|
|
|
r.Get("/api/version", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
b, _ := json.MarshalIndent(struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Date string `json:"date"`
|
|
|
|
Commit string `json:"commit"`
|
|
|
|
}{
|
|
|
|
Name: name,
|
|
|
|
Version: version,
|
|
|
|
Date: date,
|
|
|
|
Commit: commit,
|
|
|
|
}, "", " ")
|
|
|
|
w.Write(append(b, '\n'))
|
|
|
|
})
|
2020-11-21 12:41:01 +00:00
|
|
|
api.Route(r, runOpts)
|
2020-09-29 10:40:09 +00:00
|
|
|
|
2020-11-21 12:41:01 +00:00
|
|
|
var staticHandler http.HandlerFunc
|
|
|
|
pub := http.FileServer(public.Assets)
|
2020-09-29 10:40:09 +00:00
|
|
|
|
2020-11-21 12:41:01 +00:00
|
|
|
if len(runOpts.ServePath) > 0 {
|
|
|
|
// try the user-provided directory first, then fallback to the built-in
|
|
|
|
devFS := http.Dir(runOpts.ServePath)
|
|
|
|
dev := http.FileServer(devFS)
|
|
|
|
staticHandler = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if _, err := devFS.Open(r.URL.Path); nil != err {
|
|
|
|
pub.ServeHTTP(w, r)
|
2020-09-29 10:40:09 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-21 12:41:01 +00:00
|
|
|
dev.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
staticHandler = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pub.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 07:55:23 +00:00
|
|
|
r.Get("/*", staticHandler)
|
|
|
|
|
2020-09-28 23:39:05 +00:00
|
|
|
fmt.Println("Listening for http (with reasonable timeouts) on", runOpts.Addr)
|
2020-09-28 07:55:23 +00:00
|
|
|
srv := &http.Server{
|
2020-09-28 23:39:05 +00:00
|
|
|
Addr: runOpts.Addr,
|
2020-09-28 07:55:23 +00:00
|
|
|
Handler: r,
|
|
|
|
ReadHeaderTimeout: 2 * time.Second,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 20 * time.Second,
|
|
|
|
MaxHeaderBytes: 1024 * 1024, // 1MiB
|
|
|
|
}
|
|
|
|
if err := srv.ListenAndServe(); nil != err {
|
2020-09-28 07:26:16 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|