Skip to content

Commit

Permalink
Merge d37b103 into 0a2019a
Browse files Browse the repository at this point in the history
  • Loading branch information
rgooch committed Oct 20, 2019
2 parents 0a2019a + d37b103 commit df85731
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 30 deletions.
3 changes: 2 additions & 1 deletion cmd/installer/configureLocalNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"time"

"github.com/Symantec/Dominator/lib/fsutil"
"github.com/Symantec/Dominator/lib/json"
"github.com/Symantec/Dominator/lib/log"
libnet "github.com/Symantec/Dominator/lib/net"
Expand Down Expand Up @@ -147,7 +148,7 @@ func loadTftpFiles(tftpServer net.IP, logger log.DebugLogger) error {
if err != nil {
return err
}
if err := os.MkdirAll(*tftpDirectory, dirPerms); err != nil {
if err := os.MkdirAll(*tftpDirectory, fsutil.DirPerms); err != nil {
return err
}
for _, name := range tftpFiles {
Expand Down
3 changes: 2 additions & 1 deletion cmd/installer/configureNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"

"github.com/Symantec/Dominator/lib/fsutil"
"github.com/Symantec/Dominator/lib/log"
libnet "github.com/Symantec/Dominator/lib/net"
"github.com/Symantec/Dominator/lib/net/configurator"
Expand All @@ -31,7 +32,7 @@ func configureNetwork(machineInfo fm_proto.GetMachineInfoResponse,
interfaces map[string]net.Interface, logger log.DebugLogger) error {
hostname := strings.Split(machineInfo.Machine.Hostname, ".")[0]
err := ioutil.WriteFile(filepath.Join(*mountPoint, "etc", "hostname"),
[]byte(hostname+"\n"), filePerms)
[]byte(hostname+"\n"), fsutil.PublicFilePerms)
if err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions cmd/installer/configureStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ import (
)

const (
dirPerms = syscall.S_IRWXU | syscall.S_IRGRP | syscall.S_IXGRP |
syscall.S_IROTH | syscall.S_IXOTH
filePerms = syscall.S_IRUSR | syscall.S_IWUSR | syscall.S_IRGRP |
syscall.S_IROTH
keyFile = "/etc/crypt.key"
)

Expand All @@ -67,7 +63,7 @@ func configureBootDrive(cpuSharer cpusharer.CpuSharer, drive *driveType,
drive.discarded = true
logger.Printf("discarded %s in %s\n",
drive.devpath, format.Duration(time.Since(startTime)))
} else {
} else { // Erase old partition.
if err := eraseStart(drive.devpath, logger); err != nil {
return err
}
Expand Down Expand Up @@ -579,6 +575,8 @@ func remapDevice(device, target string) string {
}

func unmountStorage(logger log.DebugLogger) error {
syscall.Sync()
time.Sleep(time.Millisecond * 100)
file, err := os.Open("/proc/mounts")
if err != nil {
return err
Expand Down Expand Up @@ -614,6 +612,7 @@ func unmountStorage(logger log.DebugLogger) error {
if !unmountedMainMountPoint {
return errors.New("did not find main mount point to unmount")
}
syscall.Sync()
return nil
}

Expand Down Expand Up @@ -653,15 +652,19 @@ func (drive driveType) makeFileSystem(cpuSharer cpusharer.CpuSharer,
device, target, fstype string, mkfsMutex *sync.Mutex, data bool,
logger log.DebugLogger) error {
label := target
erase := true
if label == "/" {
label = "rootfs"
if drive.discarded {
erase = false
}
} else {
if err := drive.cryptSetup(cpuSharer, device, logger); err != nil {
return err
}
device = filepath.Join("/dev/mapper", filepath.Base(device))
}
if !drive.discarded {
if erase {
if err := eraseStart(device, logger); err != nil {
return err
}
Expand Down
61 changes: 48 additions & 13 deletions cmd/installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
stdlog "log"
"os"
"path/filepath"
"sync"
"syscall"
"time"

Expand All @@ -23,6 +24,10 @@ import (

const logfile = "/var/log/installer/latest"

type flusher interface {
Flush() error
}

var (
dryRun = flag.Bool("dryRun", ifUnprivileged(),
"If true, do not make changes")
Expand All @@ -47,13 +52,15 @@ var (
"Mount point for temporary (tmpfs) root file-system")
)

func copyLogs() error {
func copyLogs(logFlusher flusher) error {
logFlusher.Flush()
logdir := filepath.Join(*mountPoint, "var", "log", "installer")
return fsutil.CopyFile(filepath.Join(logdir, "log"), logfile, filePerms)
return fsutil.CopyFile(filepath.Join(logdir, "log"), logfile,
fsutil.PublicFilePerms)
}

func createLogger() (*logbuf.LogBuffer, log.DebugLogger) {
os.MkdirAll("/var/log/installer", dirPerms)
os.MkdirAll("/var/log/installer", fsutil.DirPerms)
options := logbuf.GetStandardOptions()
options.AlsoLogToStderr = true
logBuffer := logbuf.NewWithOptions(options)
Expand All @@ -69,7 +76,7 @@ func ifUnprivileged() bool {
return false
}

func install(logger log.DebugLogger) error {
func install(logFlusher flusher, logger log.DebugLogger) error {
machineInfo, interfaces, err := configureLocalNetwork(logger)
if err != nil {
return err
Expand All @@ -85,17 +92,46 @@ func install(logger log.DebugLogger) error {
return err
}
}
if err := copyLogs(); err != nil {
if err := copyLogs(logFlusher); err != nil {
return fmt.Errorf("error copying logs: %s", err)
}
syscall.Sync()
time.Sleep(time.Second)
if err := unmountStorage(logger); err != nil {
return fmt.Errorf("error unmounting: %s", err)
}
return nil
}

func printAndWait(initialTimeoutString, waitTimeoutString string,
waitGroup *sync.WaitGroup, logger log.Logger) {
initialTimeout, _ := time.ParseDuration(initialTimeoutString)
if initialTimeout < time.Second {
initialTimeout = time.Second
initialTimeoutString = "1s"
}
logger.Printf("waiting %s before rebooting\n", initialTimeoutString)
time.Sleep(initialTimeout - time.Second)
waitChannel := make(chan struct{})
go func() {
waitGroup.Wait()
waitChannel <- struct{}{}
}()
timer := time.NewTimer(time.Second)
select {
case <-timer.C:
case <-waitChannel:
return
}
logger.Printf(
"waiting %s for remote shells to terminate before rebooting\n",
waitTimeoutString)
waitTimeout, _ := time.ParseDuration(waitTimeoutString)
timer.Reset(waitTimeout)
select {
case <-timer.C:
case <-waitChannel:
}
}

func main() {
if err := loadflags.LoadForDaemon("installer"); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -110,18 +146,17 @@ func main() {
if err := setupserver.SetupTls(); err != nil {
logger.Println(err)
}
if newLogger, err := startServer(*portNum, logger); err != nil {
waitGroup := &sync.WaitGroup{}
if newLogger, err := startServer(*portNum, waitGroup, logger); err != nil {
logger.Printf("cannot start server: %s\n", err)
} else {
logger = newLogger
}
if err := install(logger); err != nil {
if err := install(logBuffer, logger); err != nil {
logger.Println(err)
logger.Println("waiting 5m before rebooting")
time.Sleep(time.Minute * 5)
printAndWait("5m", "1h", waitGroup, logger)
} else {
logger.Println("waiting 5s before rebooting")
time.Sleep(time.Second * 5)
printAndWait("5s", "5m", waitGroup, logger)
}
syscall.Sync()
if err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions cmd/installer/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/Symantec/Dominator/lib/format"
"github.com/Symantec/Dominator/lib/fsutil"
"github.com/Symantec/Dominator/lib/hash"
"github.com/Symantec/Dominator/lib/log"
"github.com/Symantec/Dominator/lib/objectcache"
Expand Down Expand Up @@ -187,7 +188,7 @@ func (cache *objectsCache) getNextObject(hashVal hash.Hash,
}
hashName := filepath.Join(*objectsDirectory,
objectcache.HashToFilename(hashVal))
if err := os.MkdirAll(filepath.Dir(hashName), dirPerms); err != nil {
if err := os.MkdirAll(filepath.Dir(hashName), fsutil.DirPerms); err != nil {
return err
}
defer reader.Close()
Expand Down Expand Up @@ -223,7 +224,8 @@ func (cache *objectsCache) handleFile(filename string, copy bool,
}
hashName := filepath.Join(*objectsDirectory,
objectcache.HashToFilename(hashVal))
if err := os.MkdirAll(filepath.Dir(hashName), dirPerms); err != nil {
err := os.MkdirAll(filepath.Dir(hashName), fsutil.DirPerms)
if err != nil {
return err
}
if copy {
Expand Down Expand Up @@ -281,7 +283,7 @@ func (cache *objectsCache) scanCache(topDir, subpath string) error {

func (cache *objectsCache) scanRoot(
requiredObjects map[hash.Hash]uint64) error {
if err := os.Mkdir(*objectsDirectory, dirPerms); err != nil {
if err := os.Mkdir(*objectsDirectory, fsutil.DirPerms); err != nil {
return err
}
err := wsyscall.Mount("none", *objectsDirectory, "tmpfs", 0, "")
Expand Down
16 changes: 10 additions & 6 deletions cmd/installer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ type state struct {
}

type srpcType struct {
logger log.DebugLogger
mutex sync.RWMutex
connections map[*srpc.Conn]struct{}
remoteShellWaitGroup *sync.WaitGroup
logger log.DebugLogger
mutex sync.RWMutex
connections map[*srpc.Conn]struct{}
}

var htmlWriters []HtmlWriter

func startServer(portNum uint,
func startServer(portNum uint, remoteShellWaitGroup *sync.WaitGroup,
logger log.DebugLogger) (log.DebugLogger, error) {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", portNum))
if err != nil {
Expand All @@ -46,8 +47,9 @@ func startServer(portNum uint,
myState := state{logger}
html.HandleFunc("/", myState.statusHandler)
srpcObj := &srpcType{
logger: logger,
connections: make(map[*srpc.Conn]struct{}),
remoteShellWaitGroup: remoteShellWaitGroup,
logger: logger,
connections: make(map[*srpc.Conn]struct{}),
}
if err := srpc.RegisterName("Installer", srpcObj); err != nil {
logger.Printf("error registering SRPC receiver: %s\n", err)
Expand Down Expand Up @@ -96,6 +98,8 @@ func (s state) writeDashboard(writer io.Writer) {

func (t *srpcType) Shell(conn *srpc.Conn) error {
t.logger.Println("starting shell on SRPC connection")
t.remoteShellWaitGroup.Add(1)
defer t.remoteShellWaitGroup.Done()
pty, tty, err := openPty()
if err != nil {
return err
Expand Down

0 comments on commit df85731

Please sign in to comment.