diff --git a/cmd/drone-agent/agent.go b/cmd/drone-agent/agent.go index 359538f9..b11745c9 100644 --- a/cmd/drone-agent/agent.go +++ b/cmd/drone-agent/agent.go @@ -6,11 +6,13 @@ import ( "io" "io/ioutil" "log" + "os" "strconv" "sync" "time" "google.golang.org/grpc" + "google.golang.org/grpc/metadata" "github.com/cncd/pipeline/pipeline" "github.com/cncd/pipeline/pipeline/backend" @@ -31,6 +33,11 @@ func loop(c *cli.Context) error { }, } + hostname := c.String("hostname") + if len(hostname) == 0 { + hostname, _ = os.Hostname() + } + // TODO pass version information to grpc server // TODO authenticate to grpc server @@ -44,6 +51,7 @@ func loop(c *cli.Context) error { password: c.String("password"), }), ) + if err != nil { return err } @@ -52,7 +60,10 @@ func loop(c *cli.Context) error { client := rpc.NewGrpcClient(conn) sigterm := abool.New() - ctx := context.Background() + ctx := metadata.NewOutgoingContext( + context.Background(), + metadata.Pairs("hostname", hostname), + ) ctx = interrupt.WithContextFunc(ctx, func() { println("ctrl+c received, terminating process") sigterm.Set() diff --git a/cmd/drone-agent/main.go b/cmd/drone-agent/main.go index a1955f59..b3179463 100644 --- a/cmd/drone-agent/main.go +++ b/cmd/drone-agent/main.go @@ -39,6 +39,10 @@ func main() { Name: "debug", Usage: "start the agent in debug mode", }, + cli.StringFlag{ + EnvVar: "DRONE_HOSTNAME,HOSTNAME", + Name: "hostname", + }, cli.StringFlag{ EnvVar: "DRONE_PLATFORM", Name: "platform", diff --git a/server/rpc.go b/server/rpc.go index c0248505..71055784 100644 --- a/server/rpc.go +++ b/server/rpc.go @@ -10,6 +10,8 @@ import ( oldcontext "golang.org/x/net/context" + "google.golang.org/grpc/metadata" + "github.com/Sirupsen/logrus" "github.com/cncd/logging" "github.com/cncd/pipeline/pipeline/rpc" @@ -207,6 +209,14 @@ func (s *RPC) Upload(c context.Context, id string, file *rpc.File) error { return err } + metadata, ok := metadata.FromContext(c) + if ok { + hostname, ok := metadata["hostname"] + if ok && len(hostname) != 0 { + proc.Machine = hostname[0] + } + } + if file.Mime == "application/json+logs" { return s.store.LogSave( proc, @@ -238,6 +248,13 @@ func (s *RPC) Init(c context.Context, id string, state rpc.State) error { log.Printf("error: cannot find proc with id %d: %s", procID, err) return err } + metadata, ok := metadata.FromContext(c) + if ok { + hostname, ok := metadata["hostname"] + if ok && len(hostname) != 0 { + proc.Machine = hostname[0] + } + } build, err := s.store.GetBuild(proc.BuildID) if err != nil {