func Run(conf *Config) error { log.Debug("Running git hook") builderKey, err := builderconf.GetBuilderKey() if err != nil { return err } s3Client, err := storage.GetClient(conf.StorageRegion) if err != nil { return fmt.Errorf("configuring S3 client (%s)", err) } kubeClient, err := client.NewInCluster() if err != nil { return fmt.Errorf("couldn't reach the api server (%s)", err) } scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { line := scanner.Text() oldRev, newRev, refName, err := readLine(line) if err != nil { return fmt.Errorf("reading STDIN (%s)", err) } log.Debug("read [%s,%s,%s]", oldRev, newRev, refName) if err := receive(conf, builderKey, newRev); err != nil { return err } // if we're processing a receive-pack on an existing repo, run a build if strings.HasPrefix(conf.SSHOriginalCommand, "git-receive-pack") { if err := build(conf, s3Client, kubeClient, builderKey, newRev); err != nil { return err } } } if err := scanner.Err(); err != nil { return err } return nil }
func main() { if os.Getenv("DEBUG") == "true" { pkglog.DefaultLogger.SetDebug(true) cookoolog.Level = cookoolog.LogDebug log.Printf("Running in debug mode") } app := cli.NewApp() app.Commands = []cli.Command{ { Name: "server", Aliases: []string{"srv"}, Usage: "Run the git server", Action: func(c *cli.Context) { cnf := new(sshd.Config) if err := conf.EnvConfig(serverConfAppName, cnf); err != nil { pkglog.Err("getting config for %s [%s]", serverConfAppName, err) os.Exit(1) } circ := sshd.NewCircuit() s3Client, err := storage.GetClient(cnf.HealthSrvTestStorageRegion) if err != nil { log.Printf("Error getting s3 client (%s)", err) os.Exit(1) } kubeClient, err := kcl.NewInCluster() if err != nil { log.Printf("Error getting kubernetes client [%s]", err) os.Exit(1) } log.Printf("Starting health check server on port %d", cnf.HealthSrvPort) healthSrvCh := make(chan error) go func() { if err := healthsrv.Start(cnf.HealthSrvPort, kubeClient.Namespaces(), s3Client, circ); err != nil { healthSrvCh <- err } }() log.Printf("Starting SSH server on %s:%d", cnf.SSHHostIP, cnf.SSHHostPort) sshCh := make(chan int) go func() { sshCh <- pkg.RunBuilder(cnf.SSHHostIP, cnf.SSHHostPort, circ) }() select { case err := <-healthSrvCh: log.Printf("Error running health server (%s)", err) os.Exit(1) case i := <-sshCh: log.Printf("Unexpected SSH server stop with code %d", i) os.Exit(i) } }, }, { Name: "git-receive", Aliases: []string{"gr"}, Usage: "Run the git-receive hook", Action: func(c *cli.Context) { cnf := new(gitreceive.Config) if err := conf.EnvConfig(gitReceiveConfAppName, cnf); err != nil { log.Printf("Error getting config for %s [%s]", gitReceiveConfAppName, err) os.Exit(1) } cnf.CheckDurations() if err := gitreceive.Run(cnf); err != nil { log.Printf("Error running git receive hook [%s]", err) os.Exit(1) } }, }, } app.Run(os.Args) }