Skip to content

Commit

Permalink
Further revision based on @groob comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zwass committed Oct 31, 2017
1 parent f4c7c01 commit 414d2b8
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 110 deletions.
19 changes: 10 additions & 9 deletions autoupdate/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
"path"
"path/filepath"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/fs"
"github.com/kolide/launcher/log"
"github.com/kolide/launcher/osquery"
"github.com/kolide/updater/tuf"
"github.com/pkg/errors"
Expand Down Expand Up @@ -97,7 +98,7 @@ func NewUpdater(binaryPath, rootDirectory string, logger log.Logger, opts ...Upd
func (u *Updater) createLocalTufRepo() error {
// We don't want to overwrite an existing repo as it stores state between installations
if _, err := os.Stat(u.settings.LocalRepoPath); !os.IsNotExist(err) {
u.logger.Debug("msg", "not creating new TUF repositories because they already exist")
level.Debug(u.logger).Log("msg", "not creating new TUF repositories because they already exist")
return nil
}

Expand Down Expand Up @@ -236,35 +237,35 @@ func (u *Updater) Run(opts ...tuf.Option) (stop func(), err error) {
// 3) call the Updater's finalizer method, usually a restart function for the running binary.
func (u *Updater) handler() tuf.NotificationHandler {
return func(stagingPath string, err error) {
u.logger.Debug("msg", "new staged tuf file", "file", stagingPath, "target", u.target, "binary", u.destination)
u.logger.Log("msg", "new staged tuf file", "file", stagingPath, "target", u.target, "binary", u.destination)

if err != nil {
u.logger.Debug("msg", "download failed", "target", u.target, "err", err)
u.logger.Log("msg", "download failed", "target", u.target, "err", err)
return
}

if err := fs.UntarBundle(stagingPath, stagingPath); err != nil {
u.logger.Debug("msg", "untar downloaded target", "binary", u.target, "err", err)
u.logger.Log("msg", "untar downloaded target", "binary", u.target, "err", err)
return
}

binary := filepath.Join(filepath.Dir(stagingPath), filepath.Base(u.destination))
if err := os.Rename(binary, u.destination); err != nil {
u.logger.Debug("msg", "update binary from staging dir", "binary", u.destination, "err", err)
u.logger.Log("msg", "update binary from staging dir", "binary", u.destination, "err", err)
return
}

if err := os.Chmod(u.destination, 0755); err != nil {
u.logger.Debug("msg", "setting +x permissions on binary", "binary", u.destination, "err", err)
u.logger.Log("msg", "setting +x permissions on binary", "binary", u.destination, "err", err)
return
}

if err := u.finalizer(); err != nil {
u.logger.Debug("msg", "calling restart function for updated binary", "binary", u.destination, "err", err)
u.logger.Log("msg", "calling restart function for updated binary", "binary", u.destination, "err", err)
return
}

u.logger.Info("msg", "completed update for binary", "binary", u.destination)
u.logger.Log("msg", "completed update for binary", "binary", u.destination)
}
}

Expand Down
4 changes: 2 additions & 2 deletions autoupdate/autoupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"testing"

"github.com/kolide/launcher/log"
"github.com/go-kit/kit/log"
"github.com/kolide/launcher/osquery"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestNewUpdater(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
gun := fmt.Sprintf("kolide/app")
tt.opts = append(tt.opts, withoutBootstrap())
u, err := NewUpdater("/tmp/app", "/tmp/tuf", log.NewLogger(ioutil.Discard), tt.opts...)
u, err := NewUpdater("/tmp/app", "/tmp/tuf", log.NewNopLogger(), tt.opts...)
require.Nil(t, err)

assert.Equal(t, tt.target, u.target)
Expand Down
12 changes: 6 additions & 6 deletions cmd/launcher/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/kolide/launcher/log"
"github.com/go-kit/kit/log"
"github.com/kolide/launcher/service"
"github.com/stretchr/testify/require"

Expand All @@ -28,7 +28,7 @@ func (m *mockApiServer) RequestEnrollment(ctx context.Context, enrollSecret, hos

func startServer(t *testing.T, conf *tls.Config) func() {
svc := &mockApiServer{}
logger := log.NewLogger(ioutil.Discard)
logger := log.NewNopLogger()
e := service.MakeServerEndpoints(svc)
apiServer := service.NewGRPCServer(e, logger)

Expand Down Expand Up @@ -68,13 +68,13 @@ func TestSwappingCert(t *testing.T) {
pool.AppendCertsFromPEM(pem1)
pool.AppendCertsFromPEM(pem2)

conn, err := dialGRPC("localhost:8443", false, false, log.NewLogger(ioutil.Discard),
conn, err := dialGRPC("localhost:8443", false, false, log.NewNopLogger(),
grpc.WithTransportCredentials(&tlsCreds{credentials.NewTLS(&tls.Config{RootCAs: pool})}),
)
require.Nil(t, err)
defer conn.Close()

client := service.New(conn, log.NewLogger(ioutil.Discard))
client := service.New(conn, log.NewNopLogger())

_, _, err = client.RequestEnrollment(context.Background(), "", "")
require.NotNil(t, err)
Expand Down Expand Up @@ -106,13 +106,13 @@ func TestCertRemainsBad(t *testing.T) {
pool.AppendCertsFromPEM(pem1)
pool.AppendCertsFromPEM(pem2)

conn, err := dialGRPC("localhost:8443", false, false, log.NewLogger(ioutil.Discard),
conn, err := dialGRPC("localhost:8443", false, false, log.NewNopLogger(),
grpc.WithTransportCredentials(&tlsCreds{credentials.NewTLS(&tls.Config{RootCAs: pool})}),
)
require.Nil(t, err)
defer conn.Close()

client := service.New(conn, log.NewLogger(ioutil.Discard))
client := service.New(conn, log.NewNopLogger())

_, _, err = client.RequestEnrollment(context.Background(), "", "")
require.NotNil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/launcher/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net/http"
"time"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/autoupdate"
"github.com/kolide/launcher/log"
"github.com/kolide/updater/tuf"
)

Expand Down
23 changes: 14 additions & 9 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
"time"

"github.com/boltdb/bolt"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/fs"
"github.com/kolide/kit/version"
"github.com/kolide/launcher/autoupdate"
"github.com/kolide/launcher/debug"
"github.com/kolide/launcher/log"
kolidelog "github.com/kolide/launcher/log"
"github.com/kolide/launcher/osquery"
"github.com/kolide/launcher/service"
"github.com/kolide/osquery-go/plugin/config"
Expand All @@ -38,7 +40,7 @@ var (
)

func main() {
logger := log.NewLogger(os.Stderr)
logger := kolidelog.NewLogger(os.Stderr)
opts, err := parseOptions()
if err != nil {
logger.Fatal("err", errors.Wrap(err, "invalid options"))
Expand Down Expand Up @@ -67,12 +69,11 @@ func main() {
logger.Fatal("err", errors.Wrap(err, "creating temporary root directory"))
}
}
logger.Info(
level.Info(logger).Log(
"msg", "using default system root directory",
"path", rootDirectory,
)
}
logger.Log("foo", "bar")

if err := os.MkdirAll(rootDirectory, 0700); err != nil {
logger.Fatal("err", errors.Wrap(err, "creating root directory"))
Expand All @@ -98,7 +99,11 @@ func main() {
}

versionInfo := version.Version()
logger.Info("msg", "started kolide launcher", "version", versionInfo.Version, "build", versionInfo.Revision)
level.Info(logger).Log(
"msg", "started kolide launcher",
"version", versionInfo.Version,
"build", versionInfo.Revision,
)

db, err := bolt.Open(filepath.Join(rootDirectory, "launcher.db"), 0600, nil)
if err != nil {
Expand All @@ -112,7 +117,7 @@ func main() {
}
defer conn.Close()

client := service.New(conn, logger)
client := service.New(conn, level.Debug(logger))

var enrollSecret string
if opts.enrollSecret != "" {
Expand Down Expand Up @@ -229,7 +234,7 @@ func shutdownOsquery(rootdir string) error {
func launcherFinalizer(logger log.Logger, rootDirectory string) func() error {
return func() error {
if err := shutdownOsquery(rootDirectory); err != nil {
logger.Info(
level.Info(logger).Log(
"method", "launcherFinalizer",
"err", err,
)
Expand Down Expand Up @@ -280,7 +285,7 @@ func dialGRPC(
logger log.Logger,
opts ...grpc.DialOption, // Used for overrides in testing
) (*grpc.ClientConn, error) {
logger.Info(
level.Info(logger).Log(
"msg", "dialing grpc server",
"server", serverURL,
"tls_secure", insecureTLS == false,
Expand Down Expand Up @@ -309,7 +314,7 @@ func dialGRPC(
return conn, err
}

func startDebugToggle(logger log.Logger, debug bool) {
func startDebugToggle(logger *kolidelog.Logger, debug bool) {
// Start a loop that will toggle the log level when SIGUSR2 is sent to
// the process.
sigChan := make(chan os.Signal, 1)
Expand Down
15 changes: 8 additions & 7 deletions debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"strings"
"syscall"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/google/uuid"
"github.com/kolide/launcher/log"
"github.com/pkg/errors"
)

Expand All @@ -33,7 +34,7 @@ func AttachDebugHandler(addrPath string, logger log.Logger) {
<-sig
serv, err := startDebugServer(addrPath, logger)
if err != nil {
logger.Info(
level.Info(logger).Log(
"msg", "starting debug server",
"err", err,
)
Expand All @@ -43,14 +44,14 @@ func AttachDebugHandler(addrPath string, logger log.Logger) {
// Stop server on next signal
<-sig
if err := serv.Shutdown(context.Background()); err != nil {
logger.Info(
level.Info(logger).Log(
"msg", "error shutting down debug server",
"err", err,
)
continue
}

logger.Info(
level.Info(logger).Log(
"msg", "shutdown debug server",
)
}
Expand Down Expand Up @@ -80,7 +81,7 @@ func startDebugServer(addrPath string, logger log.Logger) (*http.Server, error)

go func() {
if err := serv.Serve(listener); err != nil && err != http.ErrServerClosed {
logger.Info("msg", "debug server failed", "err", err)
level.Info(logger).Log("msg", "debug server failed", "err", err)
}
}()

Expand All @@ -96,7 +97,7 @@ func startDebugServer(addrPath string, logger log.Logger) (*http.Server, error)
return nil, errors.Wrap(err, "writing debug address")
}

logger.Info(
level.Info(logger).Log(
"msg", "debug server started",
"addr", addr,
)
Expand All @@ -120,7 +121,7 @@ func handler(token string, logger log.Logger) http.HandlerFunc {
case "":
// Index page.
if err := indexTmpl.Execute(w, info); err != nil {
logger.Info(
level.Info(logger).Log(
"msg", "error rendering debug template",
"err", err,
)
Expand Down
8 changes: 4 additions & 4 deletions debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/kolide/launcher/log"
"github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -24,7 +24,7 @@ func TestStartDebugServer(t *testing.T) {
tokenFile, err := ioutil.TempFile("", "kolide_debug_test")
require.Nil(t, err)

serv, err := startDebugServer(tokenFile.Name(), log.NewLogger(ioutil.Discard))
serv, err := startDebugServer(tokenFile.Name(), log.NewNopLogger())
require.Nil(t, err)

url := getDebugURL(t, tokenFile.Name())
Expand All @@ -42,7 +42,7 @@ func TestDebugServerUnauthorized(t *testing.T) {
tokenFile, err := ioutil.TempFile("", "kolide_debug_test")
require.Nil(t, err)

serv, err := startDebugServer(tokenFile.Name(), log.NewLogger(ioutil.Discard))
serv, err := startDebugServer(tokenFile.Name(), log.NewNopLogger())
require.Nil(t, err)

url := getDebugURL(t, tokenFile.Name())
Expand All @@ -60,7 +60,7 @@ func TestAttachDebugHandler(t *testing.T) {
tokenFile, err := ioutil.TempFile("", "kolide_debug_test")
require.Nil(t, err)

AttachDebugHandler(tokenFile.Name(), log.NewLogger(ioutil.Discard))
AttachDebugHandler(tokenFile.Name(), log.NewNopLogger())

// Start server
syscall.Kill(syscall.Getpid(), debugSignal)
Expand Down
Loading

0 comments on commit 414d2b8

Please sign in to comment.