Skip to content

Commit

Permalink
Fixup other platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
abourget committed May 9, 2017
1 parent 13a8815 commit 281540b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 54 deletions.
12 changes: 12 additions & 0 deletions build-release.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#!/bin/bash


echo ""
echo "Building for Linux"
echo ""
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o secrets-bridge-linux-amd64

echo ""
echo "Building for Windows"
echo ""
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -v -o secrets-bridge-windows-amd64.exe

echo ""
echo "Building for Darwin"
echo ""
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -o secrets-bridge-darwin-amd64
55 changes: 1 addition & 54 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import (
"log"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"

"github.com/abourget/secrets-bridge/pkg/agentfwd"
"github.com/abourget/secrets-bridge/pkg/bridge"
"github.com/abourget/secrets-bridge/pkg/secrets"
daemon "github.com/sevlyar/go-daemon"
"github.com/spf13/cobra"
"golang.org/x/net/websocket"
)
Expand Down Expand Up @@ -57,44 +54,6 @@ func init() {
serveCmd.Flags().BoolVarP(&insecureMode, "insecure", "", false, "Do not check client certificate for incoming connections")
}

func serveDaemonized(cmd *cobra.Command, args []string) {
if daemonize == "" {
serve(cmd, args)
return
}

ctx := daemon.Context{
LogFileName: daemonize,
}
child, err := ctx.Reborn()
if err != nil {
log.Fatalln("Error setting up daemon:", err)
}

if child == nil {
log.Printf("Will daemonize upon successful socket listening. pid=%d\n", os.Getpid())

serve(cmd, args)
// WARN: with all those `Fatalln` in `serve`, it's possible
// `ctx.Release()` won't get called.
ctx.Release()
return
}

log.Printf("Setting up secrets-bridge daemon, pid=%d\n", child.Pid)

// Parent, waiting for Listen signal, then exits.
childReady := make(chan os.Signal, 5)
signal.Notify(childReady, syscall.SIGUSR1)
select {
case <-childReady:
log.Println("Setup successfully.")
return
case <-time.After(5 * time.Second):
log.Fatalln("Failed. Timed out")
}
}

func serve(cmd *cobra.Command, args []string) {
confFile := bridgeConfFilenameWithDefault()
var b *bridge.Bridge
Expand Down Expand Up @@ -243,19 +202,7 @@ func serve(cmd *cobra.Command, args []string) {
}()
}

if daemonize != "" {
time.Sleep(100 * time.Millisecond)
proc, err := os.FindProcess(os.Getppid())
if err != nil {
log.Fatalln("Couldn't find parent process id:", err)
}

log.Println("Daemonizing")

if err := proc.Signal(syscall.SIGUSR1); err != nil {
log.Fatalln("couldn't send SIGUSR1 signal to parent:", err)
}
}
detachFromParent()

<-done
}
Expand Down
66 changes: 66 additions & 0 deletions cmd/serve_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"log"
"os"
"os/signal"
"syscall"
"time"

daemon "github.com/sevlyar/go-daemon"
"github.com/spf13/cobra"
)

func serveDaemonized(cmd *cobra.Command, args []string) {
if daemonize == "" {
serve(cmd, args)
return
}

ctx := daemon.Context{
LogFileName: daemonize,
}
child, err := ctx.Reborn()
if err != nil {
log.Fatalln("Error setting up daemon:", err)
}

if child == nil {
log.Printf("Will daemonize upon successful socket listening. pid=%d\n", os.Getpid())

serve(cmd, args)
// WARN: with all those `Fatalln` in `serve`, it's possible
// `ctx.Release()` won't get called.
ctx.Release()
return
}

log.Printf("Setting up secrets-bridge daemon, pid=%d\n", child.Pid)

// Parent, waiting for Listen signal, then exits.
childReady := make(chan os.Signal, 5)
signal.Notify(childReady, syscall.SIGUSR1)
select {
case <-childReady:
log.Println("Setup successfully.")
return
case <-time.After(5 * time.Second):
log.Fatalln("Failed. Timed out")
}
}

func detachFromParent() {
if daemonize != "" {
time.Sleep(100 * time.Millisecond)
proc, err := os.FindProcess(os.Getppid())
if err != nil {
log.Fatalln("Couldn't find parent process id:", err)
}

log.Println("Daemonizing")

if err := proc.Signal(syscall.SIGUSR1); err != nil {
log.Fatalln("couldn't send SIGUSR1 signal to parent:", err)
}
}
}
20 changes: 20 additions & 0 deletions cmd/serve_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// +build !linux

package cmd

import (
"log"

"github.com/spf13/cobra"
)

func serveDaemonized(cmd *cobra.Command, args []string) {
if daemonize == "" {
serve(cmd, args)
return
} else {
log.Fatalln("Daemonization not support on this platform.")
}
}

func detachFromParent() {}

0 comments on commit 281540b

Please sign in to comment.