2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-02 18:07:49 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-01-08 15:24:15 +00:00
|
|
|
"net"
|
2019-01-02 18:07:49 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2019-03-20 14:58:07 +00:00
|
|
|
"strconv"
|
2019-01-02 18:07:49 +00:00
|
|
|
"strings"
|
2019-01-08 15:24:15 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2019-01-02 18:07:49 +00:00
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-01-02 18:07:49 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2020-03-03 09:31:51 +00:00
|
|
|
"storj.io/common/processgroup"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/sync2"
|
2019-01-02 18:07:49 +00:00
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Processes contains list of processes.
|
2019-01-02 18:07:49 +00:00
|
|
|
type Processes struct {
|
2019-03-19 09:10:23 +00:00
|
|
|
Output *PrefixWriter
|
|
|
|
Directory string
|
|
|
|
List []*Process
|
2020-02-11 14:43:53 +00:00
|
|
|
|
|
|
|
MaxStartupWait time.Duration
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:37:16 +01:00
|
|
|
const storjSimMaxLineLen = 10000
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewProcesses returns a group of processes.
|
2019-03-19 09:10:23 +00:00
|
|
|
func NewProcesses(dir string) *Processes {
|
2019-01-02 18:07:49 +00:00
|
|
|
return &Processes{
|
2020-09-10 14:37:16 +01:00
|
|
|
Output: NewPrefixWriter("sim", storjSimMaxLineLen, os.Stdout),
|
2020-02-11 14:43:53 +00:00
|
|
|
Directory: dir,
|
|
|
|
List: nil,
|
|
|
|
MaxStartupWait: time.Minute,
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Exec executes a command on all processes.
|
2019-01-02 18:07:49 +00:00
|
|
|
func (processes *Processes) Exec(ctx context.Context, command string) error {
|
|
|
|
var group errgroup.Group
|
|
|
|
processes.Start(ctx, &group, command)
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Start executes all processes using specified errgroup.Group.
|
2019-01-02 18:07:49 +00:00
|
|
|
func (processes *Processes) Start(ctx context.Context, group *errgroup.Group, command string) {
|
|
|
|
for _, p := range processes.List {
|
|
|
|
process := p
|
|
|
|
group.Go(func() error {
|
|
|
|
return process.Exec(ctx, command)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Env returns environment flags for other nodes.
|
2019-01-02 18:07:49 +00:00
|
|
|
func (processes *Processes) Env() []string {
|
|
|
|
var env []string
|
|
|
|
for _, process := range processes.List {
|
|
|
|
env = append(env, process.Info.Env()...)
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close closes all the processes and their resources.
|
2019-01-02 18:07:49 +00:00
|
|
|
func (processes *Processes) Close() error {
|
2019-01-24 21:04:29 +00:00
|
|
|
var errlist errs.Group
|
2019-01-02 18:07:49 +00:00
|
|
|
for _, process := range processes.List {
|
2019-01-24 21:04:29 +00:00
|
|
|
errlist.Add(process.Close())
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
2019-01-24 21:04:29 +00:00
|
|
|
return errlist.Err()
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Info represents public information about the process.
|
2019-01-08 15:24:15 +00:00
|
|
|
type Info struct {
|
|
|
|
Name string
|
|
|
|
Executable string
|
|
|
|
Address string
|
|
|
|
Directory string
|
|
|
|
ID string
|
2019-03-20 14:58:07 +00:00
|
|
|
Pid int
|
2019-11-11 18:58:08 +00:00
|
|
|
Extra []EnvVar
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// EnvVar represents an environment variable like Key=Value.
|
2019-11-11 18:58:08 +00:00
|
|
|
type EnvVar struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddExtra appends an extra environment variable to the process info.
|
|
|
|
func (info *Info) AddExtra(key, value string) {
|
|
|
|
info.Extra = append(info.Extra, EnvVar{Key: key, Value: value})
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Env returns process flags.
|
2019-01-08 15:24:15 +00:00
|
|
|
func (info *Info) Env() []string {
|
2019-01-02 18:07:49 +00:00
|
|
|
name := strings.ToUpper(info.Name)
|
|
|
|
|
|
|
|
name = strings.Map(func(r rune) rune {
|
|
|
|
switch {
|
|
|
|
case '0' <= r && r <= '9':
|
|
|
|
return r
|
|
|
|
case 'a' <= r && r <= 'z':
|
|
|
|
return r
|
|
|
|
case 'A' <= r && r <= 'Z':
|
|
|
|
return r
|
|
|
|
default:
|
|
|
|
return '_'
|
|
|
|
}
|
|
|
|
}, name)
|
|
|
|
|
|
|
|
var env []string
|
|
|
|
if info.ID != "" {
|
|
|
|
env = append(env, name+"_ID="+info.ID)
|
|
|
|
}
|
|
|
|
if info.Address != "" {
|
|
|
|
env = append(env, name+"_ADDR="+info.Address)
|
|
|
|
}
|
|
|
|
if info.Directory != "" {
|
|
|
|
env = append(env, name+"_DIR="+info.Directory)
|
|
|
|
}
|
2019-03-20 14:58:07 +00:00
|
|
|
if info.Pid != 0 {
|
|
|
|
env = append(env, name+"_PID="+strconv.Itoa(info.Pid))
|
|
|
|
}
|
2019-01-08 15:24:15 +00:00
|
|
|
for _, extra := range info.Extra {
|
2019-11-11 18:58:08 +00:00
|
|
|
env = append(env, name+"_"+strings.ToUpper(extra.Key)+"="+extra.Value)
|
2019-01-08 15:24:15 +00:00
|
|
|
}
|
2019-01-02 18:07:49 +00:00
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Arguments contains arguments based on the main command.
|
2019-01-08 15:24:15 +00:00
|
|
|
type Arguments map[string][]string
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Process is a type for monitoring the process.
|
2019-01-02 18:07:49 +00:00
|
|
|
type Process struct {
|
|
|
|
processes *Processes
|
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
Info
|
2019-01-02 18:07:49 +00:00
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
Delay time.Duration
|
|
|
|
Wait []*sync2.Fence
|
|
|
|
Status struct {
|
|
|
|
Started sync2.Fence
|
|
|
|
Exited sync2.Fence
|
|
|
|
}
|
2019-01-02 18:07:49 +00:00
|
|
|
|
2019-01-29 14:23:30 +00:00
|
|
|
ExecBefore map[string]func(*Process) error
|
|
|
|
Arguments Arguments
|
2019-01-02 18:07:49 +00:00
|
|
|
|
|
|
|
stdout io.Writer
|
|
|
|
stderr io.Writer
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// New creates a process which can be run in the specified directory.
|
2019-01-08 15:24:15 +00:00
|
|
|
func (processes *Processes) New(info Info) *Process {
|
|
|
|
output := processes.Output.Prefixed(info.Name)
|
2019-01-02 18:07:49 +00:00
|
|
|
|
|
|
|
process := &Process{
|
|
|
|
processes: processes,
|
|
|
|
|
2019-01-29 14:23:30 +00:00
|
|
|
Info: info,
|
|
|
|
ExecBefore: map[string]func(*Process) error{},
|
|
|
|
Arguments: Arguments{},
|
2019-01-02 18:07:49 +00:00
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
stdout: output,
|
|
|
|
stderr: output,
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
processes.List = append(processes.List, process)
|
2019-01-08 15:24:15 +00:00
|
|
|
return process
|
|
|
|
}
|
2019-01-02 18:07:49 +00:00
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
// WaitForStart ensures that process will wait on dependency before starting.
|
|
|
|
func (process *Process) WaitForStart(dependency *Process) {
|
|
|
|
process.Wait = append(process.Wait, &dependency.Status.Started)
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 20:09:07 +00:00
|
|
|
// WaitForExited ensures that process will wait on dependency before starting.
|
|
|
|
func (process *Process) WaitForExited(dependency *Process) {
|
|
|
|
process.Wait = append(process.Wait, &dependency.Status.Exited)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Exec runs the process using the arguments for a given command.
|
2019-01-08 15:24:15 +00:00
|
|
|
func (process *Process) Exec(ctx context.Context, command string) (err error) {
|
|
|
|
// ensure that we always release all status fences
|
|
|
|
defer process.Status.Started.Release()
|
|
|
|
defer process.Status.Exited.Release()
|
|
|
|
|
2020-02-11 14:43:53 +00:00
|
|
|
ctx, cancelProcess := context.WithCancel(ctx)
|
|
|
|
defer cancelProcess()
|
|
|
|
|
2019-07-17 19:14:44 +01:00
|
|
|
// wait for dependencies to start
|
|
|
|
for _, fence := range process.Wait {
|
2019-10-28 14:04:31 +00:00
|
|
|
if !fence.Wait(ctx) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2019-07-17 19:14:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// in case we have an explicit delay then sleep
|
|
|
|
if process.Delay > 0 {
|
|
|
|
if !sync2.Sleep(ctx, process.Delay) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if exec, ok := process.ExecBefore[command]; ok {
|
|
|
|
if err := exec(process); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 20:13:38 +01:00
|
|
|
executable := process.Executable
|
|
|
|
|
|
|
|
// use executable inside the directory, if it exists
|
|
|
|
localExecutable := filepath.Join(process.Directory, executable)
|
|
|
|
if _, err := os.Lstat(localExecutable); !os.IsNotExist(err) {
|
|
|
|
executable = localExecutable
|
|
|
|
}
|
|
|
|
|
2019-10-16 21:34:25 +01:00
|
|
|
if _, ok := process.Arguments[command]; !ok {
|
|
|
|
fmt.Fprintf(process.processes.Output, "%s running: %s\n", process.Name, command)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-09 20:13:38 +01:00
|
|
|
cmd := exec.CommandContext(ctx, executable, process.Arguments[command]...)
|
2019-03-19 09:10:23 +00:00
|
|
|
cmd.Dir = process.processes.Directory
|
2019-01-08 15:24:15 +00:00
|
|
|
cmd.Env = append(os.Environ(), "STORJ_LOG_NOTIME=1")
|
|
|
|
|
|
|
|
{ // setup standard output with logging into file
|
|
|
|
outfile, err1 := os.OpenFile(filepath.Join(process.Directory, "stdout.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err1 != nil {
|
|
|
|
return fmt.Errorf("open stdout: %v", err1)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, outfile.Close()) }()
|
|
|
|
|
|
|
|
cmd.Stdout = io.MultiWriter(process.stdout, outfile)
|
|
|
|
}
|
2019-01-02 18:07:49 +00:00
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
{ // setup standard error with logging into file
|
|
|
|
errfile, err2 := os.OpenFile(filepath.Join(process.Directory, "stderr.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err2 != nil {
|
|
|
|
return fmt.Errorf("open stderr: %v", err2)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, errfile.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
cmd.Stderr = io.MultiWriter(process.stderr, errfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that it is part of this process group
|
2019-01-02 18:07:49 +00:00
|
|
|
processgroup.Setup(cmd)
|
|
|
|
|
|
|
|
if printCommands {
|
2019-01-08 15:24:15 +00:00
|
|
|
fmt.Fprintf(process.processes.Output, "%s running: %v\n", process.Name, strings.Join(cmd.Args, " "))
|
|
|
|
defer func() {
|
|
|
|
fmt.Fprintf(process.processes.Output, "%s exited: %v\n", process.Name, err)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the process
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-20 14:58:07 +00:00
|
|
|
process.Info.Pid = cmd.Process.Pid
|
2019-01-08 15:24:15 +00:00
|
|
|
|
2019-01-18 10:36:58 +00:00
|
|
|
if command == "setup" || process.Address == "" {
|
2019-01-08 15:24:15 +00:00
|
|
|
// during setup we aren't starting the addresses, so we can release the dependencies immediately
|
|
|
|
process.Status.Started.Release()
|
2019-01-18 10:36:58 +00:00
|
|
|
} else {
|
2019-01-08 15:24:15 +00:00
|
|
|
// release started when we are able to connect to the process address
|
2020-02-11 14:43:53 +00:00
|
|
|
go func() {
|
|
|
|
defer process.Status.Started.Release()
|
|
|
|
|
|
|
|
err := process.waitForAddress(process.processes.MaxStartupWait)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(process.processes.Output, "failed to wait startup: %v", err)
|
|
|
|
cancelProcess()
|
|
|
|
}
|
|
|
|
}()
|
2019-01-08 15:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for process completion
|
|
|
|
err = cmd.Wait()
|
|
|
|
|
|
|
|
// clear the error if the process was killed
|
|
|
|
if status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok {
|
|
|
|
if status.Signaled() && status.Signal() == os.Kill {
|
|
|
|
err = nil
|
|
|
|
}
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
2019-01-08 15:24:15 +00:00
|
|
|
return err
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 14:43:53 +00:00
|
|
|
// waitForAddress will monitor starting when we are able to start the process.
|
|
|
|
func (process *Process) waitForAddress(maxStartupWait time.Duration) error {
|
|
|
|
start := time.Now()
|
2019-01-08 15:24:15 +00:00
|
|
|
for !process.Status.Started.Released() {
|
|
|
|
if process.tryConnect() {
|
2020-02-11 14:43:53 +00:00
|
|
|
return nil
|
2019-01-08 15:24:15 +00:00
|
|
|
}
|
2020-02-11 14:43:53 +00:00
|
|
|
|
2019-01-08 15:24:15 +00:00
|
|
|
// wait a bit before retrying to reduce load
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2020-02-11 14:43:53 +00:00
|
|
|
|
|
|
|
if time.Since(start) > maxStartupWait {
|
|
|
|
return fmt.Errorf("%s did not start in required time %v", process.Name, maxStartupWait)
|
|
|
|
}
|
2019-01-08 15:24:15 +00:00
|
|
|
}
|
2020-02-11 14:43:53 +00:00
|
|
|
return nil
|
2019-01-02 18:07:49 +00:00
|
|
|
}
|
2019-01-08 15:24:15 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// tryConnect will try to connect to the process public address.
|
2019-01-08 15:24:15 +00:00
|
|
|
func (process *Process) tryConnect() bool {
|
|
|
|
conn, err := net.Dial("tcp", process.Info.Address)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// write empty byte slice to trigger refresh on connection
|
|
|
|
_, _ = conn.Write([]byte{})
|
|
|
|
// ignoring errors, because we only care about being able to connect
|
|
|
|
_ = conn.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close closes process resources.
|
2019-01-08 15:24:15 +00:00
|
|
|
func (process *Process) Close() error { return nil }
|