Skip to content

Commit

Permalink
Merge decentralized cpud to cpud (#237)
Browse files Browse the repository at this point in the history
* mv init.go to init_linux.go

Signed-off-by: Ronald G Minnich <[email protected]>

* remove decpud/init.go

Signed-off-by: Ronald G Minnich <[email protected]>

* move main_darwin.go to cpud

Signed-off-by: Ronald G Minnich <[email protected]>

* add information about mdns to cpuddo

Signed-off-by: Ronald G Minnich <[email protected]>

* add mdns tests

Signed-off-by: Ronald G Minnich <[email protected]>

* remove remnants of decpud

it is now integrated and enabled with build tags.

Signed-off-by: Ronald G Minnich <[email protected]>

* cpud: add mdns server code

intended to be enabled by build tags.

Signed-off-by: Ronald G Minnich <[email protected]>

* cpuddoc: add docs for ds switches

Signed-off-by: Ronald G Minnich <[email protected]>

* cpud: add code for mdns

Signed-off-by: Ronald G Minnich <[email protected]>

* add tests

Signed-off-by: Ronald G Minnich <[email protected]>

* cpud/serve_mdns_test.go: remove duplicate

there are no mDNS tests, turns out

Signed-off-by: Ronald G Minnich <[email protected]>

* cpud: mv main.go to main_linux.go

Signed-off-by: Ronald G Minnich <[email protected]>

* cpud :add modifier, which greatly reduces the extra code needed for mDNS

Signed-off-by: Ronald G Minnich <[email protected]>

* remove decpud from Makefile

Signed-off-by: Ronald G Minnich <[email protected]>

* fix Dockerfile

Signed-off-by: Ronald G Minnich <[email protected]>

* cpud: don't defer Unregister

We'll need to figure out how to do this properly.

With this change, the mDNS service *almost* works.
cpud side (note that we can run more than one cpud.
So on this machine we are using sidecore to get in, then running
cpud with port 17011. Nice huh?)

root@192:~/go/src/github.com/u-root/cpu/cmds/decpu# ./decpu -d -sp 17011 -key ~/.ssh/cpu_rsa
1970/01/01 01:12:12 Advertising w/dnssd map[]
1970/01/01 01:12:12 starting dns-sd server
1970/01/01 01:12:12 Advertising: ._ncpu._tcp.local.

client:
./decpu  -sp 17011 -key ~/.ssh/cpu_rsa date

Note: it seems to be ignoring the key switch in cpud.

Signed-off-by: Ronald G Minnich <[email protected]>

---------

Signed-off-by: Ronald G Minnich <[email protected]>
  • Loading branch information
rminnich authored Aug 21, 2023
1 parent 04899d3 commit d39e9f4
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 542 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN mkdir -p /app/lib64
RUN CGO_ENABLED=0 go build -o cpud ./cmds/cpud/.
RUN CGO_ENABLED=0 go build -o cpu ./cmds/cpu/.
RUN CGO_ENABLED=0 go build -o decpu ./cmds/decpu/.
RUN CGO_ENABLED=0 go build -o decpud ./cmds/decpud/.
RUN CGO_ENABLED=0 go build -o decpud -tags mDNS ./cmds/cpud/.
RUN CGO_ENABLED=0 GOBIN=`pwd` go install github.com/u-root/u-root/cmds/core/date
RUN CGO_ENABLED=0 GOBIN=`pwd` go install github.com/u-root/u-root/cmds/core/cat

Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ else
-CGO_ENABLED=0 go build -o bin/decpu ./cmds/decpu/.
ifeq ($(OS),Linux)
-CGO_ENABLED=0 go build -o bin/cpud ./cmds/cpud/.
-CGO_ENABLED=0 go build -o bin/decpud ./cmds/decpud/.
-CGO_ENABLED=0 go build -tags mDNS -o bin/decpud ./cmds/cpud/.
endif
endif

Expand All @@ -31,7 +31,6 @@ else
-(cd bin && CGO_ENABLED=0 gox -osarch=$(CPU_OSARCH) ../cmds/cpu/.)
-(cd bin && CGO_ENABLED=0 gox -osarch=$(CPUD_OSARCH) ../cmds/cpud/.)
-(cd bin && CGO_ENABLED=0 gox -osarch=$(CPU_OSARCH) ../cmds/decpu/.)
-(cd bin && CGO_ENABLED=0 gox -osarch=$(CPUD_OSARCH) ../cmds/decpud/.)
endif

test:
Expand Down
64 changes: 39 additions & 25 deletions cmds/cpud/cpuddoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,45 @@
//
// Options:
//
// -d enable debug prints
// -dbg9p
// show 9p io
// -hostkey string
// host key file
// -key string
// key file (default "$HOME/.ssh/cpu_rsa")
// -network string
// network to use (default "tcp")
// -p string
// port to use (default "17010")
// -port9p string
// port9p # on remote machine for 9p mount
// -remote
// Indicates we are the remote side of the cpu session
// -srv string
// what server to run (default none; use internal)
//
// For registering with a controller
// -register netaddr
// netaddr in Go style (host:port) to Dial and send 'ok' to
//
// -registerTO
// timeout in time.Duration format (e.g. 25s, 1m, etc.)
// for connection to succeed.
// -d enable debug prints
// -dbg9p
// show 9p io
// -hostkey string
// host key file
// -key string
// key file (default "$HOME/.ssh/cpu_rsa")
// -network string
// network to use (default "tcp")
// -p string
// port to use (default "17010")
// -port9p string
// port9p # on remote machine for 9p mount
// -remote
// Indicates we are the remote side of the cpu session
// -srv string
// what server to run (default none; use internal)
//
// For registering with a controller
// -register netaddr
// netaddr in Go style (host:port) to Dial and send 'ok' to
//
// -registerTO
// timeout in time.Duration format (e.g. 25s, 1m, etc.)
// for connection to succeed.
//
// If it is build with mDNS tags, the following switches are enabled.
// - dsEnabled (default true)
// advertise service using DNSSD
// - dsInstance
// DNSSD instance name (default $HOSTNAME-cpud)
// - dsDomain
// DNSSD domain (default local)
// - dsService
// DNSSD Service Type (default "_ncpu._tcp")
// - dsInterface
// DNSSD Interface
// - dsTxt
// Additional string key-value pair meta-data for host
//
// cpud is the daemon side of a cpu session.
// In the original Plan 9 implementation, cpu was a command that contained
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions cmds/decpud/main_darwin.go → cmds/cpud/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin
// +build darwin

package main

import (
Expand Down
File renamed without changes.
70 changes: 70 additions & 0 deletions cmds/cpud/main_mdns_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2018-2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build mDNS

package main

import (
"flag"
"fmt"
"log"
"strconv"

// We use this ssh because it implements port redirection.
// It can not, however, unpack password-protected keys yet.
"github.com/gliderlabs/ssh"
"github.com/u-root/cpu/ds"
)

var (
dsEnabled = flag.Bool("dnssd", true, "advertise service using DNSSD")
dsInstance = flag.String("dsInstance", "", "DNSSD instance name")
dsDomain = flag.String("dsDomain", "local", "DNSSD domain")
dsService = flag.String("dsService", "_ncpu._tcp", "DNSSD Service Type")
dsInterface = flag.String("dsInterface", "", "DNSSD Interface")
dsTxtStr = flag.String("dsTxt", "", "DNSSD key-value pair string parameterizing advertisement")
dsTxt map[string]string
)

func init() {
modifiers = append(modifiers, &modifier{f: servemDNS, name: "mDNS"})
}

type handleWrapper struct {
handle func(s ssh.Session)
}

func (w *handleWrapper) handler(s ssh.Session) {
ds.Tenant(1)
w.handle(s)
ds.Tenant(-1)
}

// servemDNS wraps an existing ssh session with an mDNS instance.
func servemDNS(s *ssh.Server) error {
if *debug {
ds.Verbose(log.Printf)
}
dsTxt = ds.ParseKv(*dsTxtStr)

v("Advertising w/dnssd %q", dsTxt)
p, err := strconv.Atoi(*port)
if err != nil {
return fmt.Errorf("Could not parse port: %s, %w", *port, err)
}

err = ds.Register(*dsInstance, *dsDomain, *dsService, *dsInterface, p, dsTxt)
if err != nil {
return fmt.Errorf("Could not advertise with dns-sd: %w", err)
}
// defer ds.Unregister()

wrap := &handleWrapper{
handle: s.Handler,
}
s.Handler = wrap.handler

return nil
}
29 changes: 29 additions & 0 deletions cmds/cpud/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ import (

const any = math.MaxUint32

type modifier struct {
name string
f func(*ssh.Server) error
}

func (m *modifier) String() string {
return fmt.Sprintf("%s", m.name)
}

var (
// modifiers are called once the ssh server is set up and before s.Serve() in serve()
// is called.
// There is only one current use, namely, mDNS.
// Once we better understand how to structure modifiers it may be time for another
// package.
// Before the modifier runs, the ssh server is ready for operation. The question remains:
// what if a modifier fails? For now, code will log errors but continue.
// This makes debugging failed modifiers far easier.
// Therefore, it is the responsibility of modifiers not to break the ssh server.
// They should ensure correctness before commiting changes.
modifiers []*modifier
)

// hang hangs for a VERY long time.
// This aids diagnosis, else you lose all messages in the
// kernel panic as init exits.
Expand Down Expand Up @@ -163,6 +186,12 @@ func serve(cpud string) error {
}
}()

for _, m := range modifiers {
if err := m.f(s); err != nil {
log.Printf("Error %v from modifier %s", err, m)
}
}

if err := s.Serve(ln); err != ssh.ErrServerClosed {
log.Printf("s.Daemon(): %v != %v", err, ssh.ErrServerClosed)
hang()
Expand Down
6 changes: 6 additions & 0 deletions cmds/cpud/serve_mdns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build mDNS
// +build mDNS

package main

// Add tests here for mDNS
1 change: 0 additions & 1 deletion cmds/decpud/.gitignore

This file was deleted.

118 changes: 0 additions & 118 deletions cmds/decpud/cpuddoc.go

This file was deleted.

Loading

0 comments on commit d39e9f4

Please sign in to comment.