Files
airshipui/pkg/commands/plugins.go
Schiefelbein, Andrew a15a4d87e2 Simplify, refactor and rename to bring in line with CTL
This patch does the following:
1.  Simplify the client websocket message transformation
2.  Move the cmd/airshipui to cmd
3.  move internal to pkg
4.  Clean up the copyright checker
5.  clean up the linting in the makefile

Change-Id: I1381d025e8058cbfba44b58ec3c2ec5c2aa36de5
2020-08-04 14:26:51 -05:00

104 lines
2.6 KiB
Go

// +build !windows
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"sync"
"syscall"
"opendev.org/airship/airshipui/pkg/configs"
"opendev.org/airship/airshipui/pkg/webservice"
)
// ProcessGrpCmd wraps an exec.Cmd and a signal chan
type ProcessGrpCmd struct {
sigChan chan os.Signal
ctx context.Context
waitgrp *sync.WaitGroup
*exec.Cmd
}
// NewProcessGrpCmd creates a new ProcessGrpCmd to monitor a os.Signal chan
func NewProcessGrpCmd(c context.Context, wg *sync.WaitGroup, s chan os.Signal, cmd *exec.Cmd) *ProcessGrpCmd {
return &ProcessGrpCmd{
ctx: c,
waitgrp: wg,
sigChan: s,
Cmd: cmd,
}
}
// Run sets the process group id attribute to ensure all child
// processes started by the cmd will inherit it and will get killed
// with the parent
func (grp *ProcessGrpCmd) Run() error {
grp.Cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if err := grp.Cmd.Start(); err != nil {
grp.waitgrp.Done()
return err
}
go func() {
select {
case <-grp.sigChan:
grp.Terminate()
return
case <-grp.ctx.Done():
grp.Terminate()
return
}
}()
return grp.Cmd.Wait()
}
// RunBinaryWithOptions runs the binary
func RunBinaryWithOptions(ctx context.Context, cmd string, args []string, wg *sync.WaitGroup, s chan os.Signal) {
command := exec.CommandContext(ctx, cmd, args...)
// push executable's stdout / stderr
command.Stdout = os.Stdout
command.Stderr = os.Stderr
monitoredCmd := NewProcessGrpCmd(ctx, wg, s, command)
if err := monitoredCmd.Run(); err != nil {
log.Printf("'%s' exited with error: %v", cmd, err)
// send error to UI
webservice.SendAlert(
configs.Error,
fmt.Sprintf("Plugin '%s' failed to start: %v", cmd, err),
true,
)
}
}
// Terminate kills the running process, logs a message, and sends Done() to the WaitGroup
func (grp *ProcessGrpCmd) Terminate() {
log.Printf("Terminating process '%s' with PID: %d", grp.Cmd.Path, grp.Cmd.Process.Pid)
if err := syscall.Kill(-grp.Cmd.Process.Pid, syscall.SIGKILL); err != nil {
log.Printf("Error terminating PID %d: %s", grp.Cmd.Process.Pid, err)
}
grp.waitgrp.Done()
}