1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| package main
import ( "bytes" "errors" "flag" "fmt" "net/http" "os" "os/exec" "os/signal" "strings" "syscall" "time" )
const ( bash = "/bin/bash" script = "/root/luowen/bloggem/bloggem.sh" logfileName = "app.log" httpEntry = "/path/to/deploy.json" )
var ( logfile *os.File mode string )
func init() { flag.StringVar(&mode, "mode", "http", "run console or http mode.") flag.Parse() }
func main() { defer closelogfile() openlogfile() switch strings.ToLower(mode) { case "http": runHttpMode() case "console": runConsoleMode() default: flag.PrintDefaults() } }
func runConsoleMode() string {
logsave("info", "console run start.") var buf = &bytes.Buffer{} cmd := exec.Cmd{ Path: bash, Args: []string{"-c", script}, Stdout: buf, Stderr: buf, } err := cmd.Run() if err != nil { fmt.Println(err) return err.Error() } result := buf.String() fmt.Println(result) logsave("info", result) logsave("info", "console run completed.") return result }
func runHttpMode() { logsave("info", "open logfile: app.log success!") go httpListen() logsave("info", "http server listen: 20000 success!") sigChan := make(chan os.Signal) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGHUP, syscall.SIGKILL) sig := <-sigChan logsave("info", "receive terminal signal:", sig) }
func httpListen() { mux := http.NewServeMux() mux.HandleFunc(httpEntry, deploy) _ = http.ListenAndServe(":20000", mux) }
func deploy(w http.ResponseWriter, r *http.Request) { result := runConsoleMode() _, _ = w.Write([]byte(result)) }
func openlogfile() { logfile, _ = os.OpenFile(logfileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm) }
func closelogfile() { if logfile != nil { _ = logfile.Close() } }
func logsave(typ string, logmesg ...interface{}) { if logfile == nil { panic(errors.New("logfile don't open yet!")) } fmtMesg := fmt.Sprintf("[%s]:%s: %v\n", typ, time.Now().Format("2006-01-02 15:04:05"), logmesg) fmt.Print(fmtMesg) _, _ = logfile.WriteString(fmtMesg); }
|