2019-09-14 06:33:14 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2020-05-13 12:07:53 +02:00
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
2019-09-14 06:33:14 +02:00
|
|
|
"errors"
|
2020-04-01 15:40:15 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-09-14 06:33:14 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-01-20 15:54:31 +02:00
|
|
|
"github.com/asim/go-micro/v3/logger"
|
|
|
|
"github.com/asim/go-micro/v3/runtime/local/git"
|
2021-01-25 09:51:17 +02:00
|
|
|
"github.com/nxadm/tail"
|
2019-09-14 06:33:14 +02:00
|
|
|
)
|
|
|
|
|
2020-05-19 12:01:06 +02:00
|
|
|
// defaultNamespace to use if not provided as an option
|
|
|
|
const defaultNamespace = "default"
|
|
|
|
|
2019-09-14 06:33:14 +02:00
|
|
|
type runtime struct {
|
|
|
|
sync.RWMutex
|
2019-11-02 15:25:10 +02:00
|
|
|
// options configure runtime
|
|
|
|
options Options
|
2019-09-14 17:07:36 +02:00
|
|
|
// used to stop the runtime
|
|
|
|
closed chan bool
|
|
|
|
// used to start new services
|
|
|
|
start chan *service
|
|
|
|
// indicates if we're running
|
|
|
|
running bool
|
2020-05-19 12:01:06 +02:00
|
|
|
// namespaces stores services grouped by namespace, e.g. namespaces["foo"]["go.micro.auth:latest"]
|
|
|
|
// would return the latest version of go.micro.auth from the foo namespace
|
|
|
|
namespaces map[string]map[string]*service
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// NewRuntime creates new local runtime and returns it
|
|
|
|
func NewRuntime(opts ...Option) Runtime {
|
|
|
|
// get default options
|
|
|
|
options := Options{}
|
2019-09-14 06:33:14 +02:00
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-09-14 06:33:14 +02:00
|
|
|
|
2020-04-11 12:22:02 +02:00
|
|
|
// make the logs directory
|
|
|
|
path := filepath.Join(os.TempDir(), "micro", "logs")
|
|
|
|
_ = os.MkdirAll(path, 0755)
|
|
|
|
|
2019-09-14 06:33:14 +02:00
|
|
|
return &runtime{
|
2020-05-19 12:01:06 +02:00
|
|
|
options: options,
|
|
|
|
closed: make(chan bool),
|
|
|
|
start: make(chan *service, 128),
|
|
|
|
namespaces: make(map[string]map[string]*service),
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:30:43 +02:00
|
|
|
// @todo move this to runtime default
|
|
|
|
func (r *runtime) checkoutSourceIfNeeded(s *Service) error {
|
2020-04-28 10:51:39 +02:00
|
|
|
// Runtime service like config have no source.
|
|
|
|
// Skip checkout in that case
|
|
|
|
if len(s.Source) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 12:07:53 +02:00
|
|
|
// @todo make this come from config
|
|
|
|
cpath := filepath.Join(os.TempDir(), "micro", "uploads", s.Source)
|
|
|
|
path := strings.ReplaceAll(cpath, ".tar.gz", "")
|
|
|
|
if ex, _ := exists(cpath); ex {
|
|
|
|
err := os.RemoveAll(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = uncompress(cpath, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Source = path
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-23 16:30:43 +02:00
|
|
|
source, err := git.ParseSourceLocal("", s.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
source.Ref = s.Version
|
2020-04-28 10:51:39 +02:00
|
|
|
|
2020-04-23 16:30:43 +02:00
|
|
|
err = git.CheckoutSource(os.TempDir(), source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Source = source.FullPath
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:07:53 +02:00
|
|
|
// modified version of: https://gist.github.com/mimoo/25fc9716e0f1353791f5908f94d6e726
|
|
|
|
func uncompress(src string, dst string) error {
|
|
|
|
file, err := os.OpenFile(src, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ungzip
|
|
|
|
zr, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// untar
|
|
|
|
tr := tar.NewReader(zr)
|
|
|
|
|
|
|
|
// uncompress each element
|
|
|
|
for {
|
|
|
|
header, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break // End of archive
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
target := header.Name
|
|
|
|
|
|
|
|
// validate name against path traversal
|
|
|
|
if !validRelPath(header.Name) {
|
|
|
|
return fmt.Errorf("tar contained invalid name error %q\n", target)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add dst + re-format slashes according to system
|
|
|
|
target = filepath.Join(dst, header.Name)
|
|
|
|
// if no join is needed, replace with ToSlash:
|
|
|
|
// target = filepath.ToSlash(header.Name)
|
|
|
|
|
|
|
|
// check the type
|
|
|
|
switch header.Typeflag {
|
|
|
|
|
|
|
|
// if its a dir and it doesn't exist create it (with 0755 permission)
|
|
|
|
case tar.TypeDir:
|
|
|
|
if _, err := os.Stat(target); err != nil {
|
|
|
|
// @todo think about this:
|
|
|
|
// if we don't nuke the folder, we might end up with files from
|
|
|
|
// the previous decompress.
|
|
|
|
if err := os.MkdirAll(target, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if it's a file create it (with same permission)
|
|
|
|
case tar.TypeReg:
|
|
|
|
// the truncating is probably unnecessary due to the `RemoveAll` of folders
|
|
|
|
// above
|
|
|
|
fileToWrite, err := os.OpenFile(target, os.O_TRUNC|os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// copy over contents
|
|
|
|
if _, err := io.Copy(fileToWrite, tr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// manually close here after each file operation; defering would cause each file close
|
|
|
|
// to wait until all operations have completed.
|
|
|
|
fileToWrite.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for path traversal and correct forward slashes
|
|
|
|
func validRelPath(p string) bool {
|
|
|
|
if p == "" || strings.Contains(p, `\`) || strings.HasPrefix(p, "/") || strings.Contains(p, "../") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// Init initializes runtime options
|
|
|
|
func (r *runtime) Init(opts ...Option) error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2019-09-14 06:33:14 +02:00
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&r.options)
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// run runs the runtime management loop
|
|
|
|
func (r *runtime) run(events <-chan Event) {
|
2019-09-24 19:05:51 +02:00
|
|
|
t := time.NewTicker(time.Second * 5)
|
|
|
|
defer t.Stop()
|
|
|
|
|
2019-11-26 19:33:41 +02:00
|
|
|
// process event processes an incoming event
|
2020-05-19 12:01:06 +02:00
|
|
|
processEvent := func(event Event, service *service, ns string) error {
|
2019-11-26 19:33:41 +02:00
|
|
|
// get current vals
|
|
|
|
r.RLock()
|
|
|
|
name := service.Name
|
|
|
|
updated := service.updated
|
|
|
|
r.RUnlock()
|
|
|
|
|
|
|
|
// only process if the timestamp is newer
|
|
|
|
if !event.Timestamp.After(updated) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-05-19 12:01:06 +02:00
|
|
|
logger.Debugf("Runtime updating service %s in %v namespace", name, ns)
|
2020-03-11 19:55:39 +02:00
|
|
|
}
|
2019-11-26 19:33:41 +02:00
|
|
|
|
|
|
|
// this will cause a delete followed by created
|
2020-05-19 12:01:06 +02:00
|
|
|
if err := r.Update(service.Service, UpdateNamespace(ns)); err != nil {
|
2019-11-26 19:33:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the local timestamp
|
|
|
|
r.Lock()
|
|
|
|
service.updated = updated
|
|
|
|
r.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-24 19:05:51 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
// check running services
|
|
|
|
r.RLock()
|
2020-05-19 12:01:06 +02:00
|
|
|
for _, sevices := range r.namespaces {
|
|
|
|
for _, service := range sevices {
|
|
|
|
if !service.ShouldStart() {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-24 19:05:51 +02:00
|
|
|
|
2020-05-19 12:01:06 +02:00
|
|
|
// TODO: check service error
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-05-19 12:01:06 +02:00
|
|
|
logger.Debugf("Runtime starting %s", service.Name)
|
|
|
|
}
|
|
|
|
if err := service.Start(); err != nil {
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error starting %s: %v", service.Name, err)
|
|
|
|
}
|
2020-03-11 19:55:39 +02:00
|
|
|
}
|
2019-09-24 19:05:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r.RUnlock()
|
|
|
|
case service := <-r.start:
|
2020-02-07 14:02:41 +02:00
|
|
|
if !service.ShouldStart() {
|
2019-09-24 19:05:51 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO: check service error
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime starting service %s", service.Name)
|
|
|
|
}
|
2019-09-24 19:05:51 +02:00
|
|
|
if err := service.Start(); err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error starting service %s: %v", service.Name, err)
|
|
|
|
}
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
case event := <-events:
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime received notification event: %v", event)
|
|
|
|
}
|
2019-11-02 15:25:10 +02:00
|
|
|
// NOTE: we only handle Update events for now
|
|
|
|
switch event.Type {
|
|
|
|
case Update:
|
2020-05-19 11:14:07 +02:00
|
|
|
if event.Service != nil {
|
2020-05-19 12:01:06 +02:00
|
|
|
ns := defaultNamespace
|
|
|
|
if event.Options != nil && len(event.Options.Namespace) > 0 {
|
|
|
|
ns = event.Options.Namespace
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:26:21 +02:00
|
|
|
r.RLock()
|
2020-05-19 12:01:06 +02:00
|
|
|
if _, ok := r.namespaces[ns]; !ok {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
2020-05-19 12:01:06 +02:00
|
|
|
logger.Debugf("Runtime unknown namespace: %s", ns)
|
2020-03-11 19:55:39 +02:00
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
r.RUnlock()
|
2019-11-02 15:25:10 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
service, ok := r.namespaces[ns][fmt.Sprintf("%v:%v", event.Service.Name, event.Service.Version)]
|
|
|
|
r.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
logger.Debugf("Runtime unknown service: %s", event.Service)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := processEvent(event, service, ns); err != nil {
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error updating service %s: %v", event.Service, err)
|
|
|
|
}
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-11-26 19:33:41 +02:00
|
|
|
|
|
|
|
r.RLock()
|
2020-05-19 12:01:06 +02:00
|
|
|
namespaces := r.namespaces
|
2019-11-26 19:33:41 +02:00
|
|
|
r.RUnlock()
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// if blank service was received we update all services
|
2020-05-19 12:01:06 +02:00
|
|
|
for ns, services := range namespaces {
|
|
|
|
for _, service := range services {
|
|
|
|
if err := processEvent(event, service, ns); err != nil {
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime error updating service %s: %v", service.Name, err)
|
|
|
|
}
|
2020-03-11 19:55:39 +02:00
|
|
|
}
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 19:05:51 +02:00
|
|
|
}
|
2019-11-02 15:25:10 +02:00
|
|
|
case <-r.closed:
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime stopped")
|
|
|
|
}
|
2019-09-24 19:05:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
func logFile(serviceName string) string {
|
2020-04-11 12:22:02 +02:00
|
|
|
// make the directory
|
2020-04-01 15:40:15 +02:00
|
|
|
name := strings.Replace(serviceName, "/", "-", -1)
|
2020-04-11 12:22:02 +02:00
|
|
|
path := filepath.Join(os.TempDir(), "micro", "logs")
|
|
|
|
return filepath.Join(path, fmt.Sprintf("%v.log", name))
|
2020-04-01 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:54:29 +02:00
|
|
|
func serviceKey(s *Service) string {
|
|
|
|
return fmt.Sprintf("%v:%v", s.Name, s.Version)
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// Create creates a new service which is then started by runtime
|
2019-09-24 19:32:35 +02:00
|
|
|
func (r *runtime) Create(s *Service, opts ...CreateOption) error {
|
2020-04-28 10:51:39 +02:00
|
|
|
err := r.checkoutSourceIfNeeded(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-14 06:33:14 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2019-09-24 19:32:35 +02:00
|
|
|
var options CreateOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = defaultNamespace
|
|
|
|
}
|
2019-11-29 13:35:00 +02:00
|
|
|
if len(options.Command) == 0 {
|
2020-03-20 00:38:37 +02:00
|
|
|
options.Command = []string{"go"}
|
|
|
|
options.Args = []string{"run", "."}
|
2019-09-24 19:32:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:01:06 +02:00
|
|
|
if _, ok := r.namespaces[options.Namespace]; !ok {
|
|
|
|
r.namespaces[options.Namespace] = make(map[string]*service)
|
|
|
|
}
|
|
|
|
if _, ok := r.namespaces[options.Namespace][serviceKey(s)]; ok {
|
|
|
|
return errors.New("service already running")
|
|
|
|
}
|
|
|
|
|
2019-12-24 19:51:30 +02:00
|
|
|
// create new service
|
|
|
|
service := newService(s, options)
|
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
f, err := os.OpenFile(logFile(service.Name), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if service.output != nil {
|
|
|
|
service.output = io.MultiWriter(service.output, f)
|
|
|
|
} else {
|
|
|
|
service.output = f
|
|
|
|
}
|
2019-12-24 19:51:30 +02:00
|
|
|
// start the service
|
|
|
|
if err := service.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// save service
|
2020-05-19 12:01:06 +02:00
|
|
|
r.namespaces[options.Namespace][serviceKey(s)] = service
|
2019-09-14 17:07:36 +02:00
|
|
|
|
2019-09-14 06:33:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:49:39 +02:00
|
|
|
// exists returns whether the given file or directory exists
|
|
|
|
func exists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
// @todo: Getting existing lines is not supported yet.
|
|
|
|
// The reason for this is because it's hard to calculate line offset
|
|
|
|
// as opposed to character offset.
|
|
|
|
// This logger streams by default and only supports the `StreamCount` option.
|
|
|
|
func (r *runtime) Logs(s *Service, options ...LogsOption) (LogStream, error) {
|
|
|
|
lopts := LogsOptions{}
|
|
|
|
for _, o := range options {
|
|
|
|
o(&lopts)
|
|
|
|
}
|
|
|
|
ret := &logStream{
|
|
|
|
service: s.Name,
|
|
|
|
stream: make(chan LogRecord),
|
|
|
|
stop: make(chan bool),
|
|
|
|
}
|
2020-04-28 09:49:39 +02:00
|
|
|
|
|
|
|
fpath := logFile(s.Name)
|
|
|
|
if ex, err := exists(fpath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !ex {
|
|
|
|
return nil, fmt.Errorf("Log file %v does not exists", fpath)
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:42:15 +02:00
|
|
|
// have to check file size to avoid too big of a seek
|
|
|
|
fi, err := os.Stat(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
size := fi.Size()
|
|
|
|
|
2020-04-28 09:49:39 +02:00
|
|
|
whence := 2
|
|
|
|
// Multiply by length of an average line of log in bytes
|
2020-04-28 14:42:15 +02:00
|
|
|
offset := lopts.Count * 200
|
|
|
|
|
|
|
|
if offset > size {
|
|
|
|
offset = size
|
|
|
|
}
|
|
|
|
offset *= -1
|
2020-04-28 09:49:39 +02:00
|
|
|
|
2020-04-28 14:42:15 +02:00
|
|
|
t, err := tail.TailFile(fpath, tail.Config{Follow: lopts.Stream, Location: &tail.SeekInfo{
|
2020-04-28 09:49:39 +02:00
|
|
|
Whence: whence,
|
|
|
|
Offset: int64(offset),
|
2020-04-01 15:40:15 +02:00
|
|
|
}, Logger: tail.DiscardingLogger})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-28 09:49:39 +02:00
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
ret.tail = t
|
|
|
|
go func() {
|
2020-04-28 09:49:39 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line, ok := <-t.Lines:
|
|
|
|
if !ok {
|
|
|
|
ret.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret.stream <- LogRecord{Message: line.Text}
|
|
|
|
case <-ret.stop:
|
|
|
|
return
|
|
|
|
}
|
2020-04-01 15:40:15 +02:00
|
|
|
}
|
2020-04-28 09:49:39 +02:00
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
}()
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type logStream struct {
|
|
|
|
tail *tail.Tail
|
|
|
|
service string
|
|
|
|
stream chan LogRecord
|
2020-04-02 13:16:35 +02:00
|
|
|
sync.Mutex
|
|
|
|
stop chan bool
|
|
|
|
err error
|
2020-04-01 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logStream) Chan() chan LogRecord {
|
|
|
|
return l.stream
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:03:26 +02:00
|
|
|
func (l *logStream) Error() error {
|
|
|
|
return l.err
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
func (l *logStream) Stop() error {
|
2020-04-02 13:16:35 +02:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2020-04-28 09:49:39 +02:00
|
|
|
|
2020-04-01 15:40:15 +02:00
|
|
|
select {
|
|
|
|
case <-l.stop:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(l.stop)
|
2020-04-28 09:49:39 +02:00
|
|
|
close(l.stream)
|
|
|
|
err := l.tail.Stop()
|
|
|
|
if err != nil {
|
2020-04-28 14:42:15 +02:00
|
|
|
logger.Errorf("Error stopping tail: %v", err)
|
2020-04-28 09:49:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-04-01 15:40:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 18:31:14 +02:00
|
|
|
// Read returns all instances of requested service
|
2019-11-15 15:41:40 +02:00
|
|
|
// If no service name is provided we return all the track services.
|
2019-11-29 13:35:00 +02:00
|
|
|
func (r *runtime) Read(opts ...ReadOption) ([]*Service, error) {
|
2019-09-14 06:58:03 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2019-11-25 18:31:14 +02:00
|
|
|
gopts := ReadOptions{}
|
2019-11-15 15:41:40 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&gopts)
|
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
if len(gopts.Namespace) == 0 {
|
|
|
|
gopts.Namespace = defaultNamespace
|
|
|
|
}
|
2019-11-15 15:41:40 +02:00
|
|
|
|
2019-11-29 13:35:00 +02:00
|
|
|
save := func(k, v string) bool {
|
|
|
|
if len(k) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return k == v
|
|
|
|
}
|
|
|
|
|
2019-12-03 21:59:44 +02:00
|
|
|
//nolint:prealloc
|
2019-11-15 15:41:40 +02:00
|
|
|
var services []*Service
|
2019-11-29 13:35:00 +02:00
|
|
|
|
2020-05-19 12:01:06 +02:00
|
|
|
if _, ok := r.namespaces[gopts.Namespace]; !ok {
|
|
|
|
return make([]*Service, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range r.namespaces[gopts.Namespace] {
|
2019-11-29 13:35:00 +02:00
|
|
|
if !save(gopts.Service, service.Name) {
|
|
|
|
continue
|
2019-11-15 15:41:40 +02:00
|
|
|
}
|
2019-11-29 13:35:00 +02:00
|
|
|
if !save(gopts.Version, service.Version) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO deal with service type
|
2019-11-15 15:41:40 +02:00
|
|
|
// no version has sbeen requested, just append the service
|
2019-11-29 13:35:00 +02:00
|
|
|
services = append(services, service.Service)
|
2019-11-15 15:41:40 +02:00
|
|
|
}
|
2019-11-29 13:35:00 +02:00
|
|
|
|
2019-11-15 15:41:40 +02:00
|
|
|
return services, nil
|
2019-09-14 06:58:03 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 11:47:25 +02:00
|
|
|
// Update attempts to update the service
|
2020-04-23 14:53:42 +02:00
|
|
|
func (r *runtime) Update(s *Service, opts ...UpdateOption) error {
|
2020-05-19 12:01:06 +02:00
|
|
|
var options UpdateOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = defaultNamespace
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:51:39 +02:00
|
|
|
err := r.checkoutSourceIfNeeded(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
|
2020-04-16 17:50:24 +02:00
|
|
|
r.Lock()
|
2020-05-19 12:01:06 +02:00
|
|
|
srvs, ok := r.namespaces[options.Namespace]
|
2020-04-16 17:50:24 +02:00
|
|
|
r.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Service not found")
|
2019-11-14 16:26:21 +02:00
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
|
|
|
|
r.Lock()
|
|
|
|
service, ok := srvs[serviceKey(s)]
|
|
|
|
r.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Service not found")
|
|
|
|
}
|
|
|
|
|
2020-06-08 11:47:25 +02:00
|
|
|
if err := service.Stop(); err != nil && err.Error() != "no such process" {
|
|
|
|
logger.Errorf("Error stopping service %s: %s", service.Name, err)
|
2019-10-29 14:29:21 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
|
2020-04-16 17:50:24 +02:00
|
|
|
return service.Start()
|
2019-10-29 14:29:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 15:41:40 +02:00
|
|
|
// Delete removes the service from the runtime and stops it
|
2020-04-23 14:53:42 +02:00
|
|
|
func (r *runtime) Delete(s *Service, opts ...DeleteOption) error {
|
2019-11-15 15:41:40 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2020-05-19 12:01:06 +02:00
|
|
|
var options DeleteOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
if len(options.Namespace) == 0 {
|
|
|
|
options.Namespace = defaultNamespace
|
|
|
|
}
|
|
|
|
|
|
|
|
srvs, ok := r.namespaces[options.Namespace]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime deleting service %s", s.Name)
|
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
|
|
|
|
service, ok := srvs[serviceKey(s)]
|
|
|
|
if !ok {
|
2019-11-15 15:41:40 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:01:06 +02:00
|
|
|
// check if running
|
|
|
|
if !service.Running() {
|
|
|
|
delete(srvs, service.key())
|
|
|
|
r.namespaces[options.Namespace] = srvs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// otherwise stop it
|
|
|
|
if err := service.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// delete it
|
|
|
|
delete(srvs, service.key())
|
|
|
|
r.namespaces[options.Namespace] = srvs
|
2019-11-15 15:41:40 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// Start starts the runtime
|
2019-09-14 06:58:03 +02:00
|
|
|
func (r *runtime) Start() error {
|
2019-09-14 06:33:14 +02:00
|
|
|
r.Lock()
|
2019-09-24 19:05:51 +02:00
|
|
|
defer r.Unlock()
|
2019-09-14 06:33:14 +02:00
|
|
|
|
|
|
|
// already running
|
|
|
|
if r.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// set running
|
|
|
|
r.running = true
|
|
|
|
r.closed = make(chan bool)
|
2019-09-14 17:07:36 +02:00
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
var events <-chan Event
|
2020-01-16 15:34:04 +02:00
|
|
|
if r.options.Scheduler != nil {
|
2019-11-02 15:25:10 +02:00
|
|
|
var err error
|
2020-01-16 15:34:04 +02:00
|
|
|
events, err = r.options.Scheduler.Notify()
|
2019-11-02 15:25:10 +02:00
|
|
|
if err != nil {
|
|
|
|
// TODO: should we bail here?
|
2020-03-11 19:55:39 +02:00
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime failed to start update notifier")
|
|
|
|
}
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go r.run(events)
|
2019-09-14 06:33:14 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// Stop stops the runtime
|
2019-09-14 06:33:14 +02:00
|
|
|
func (r *runtime) Stop() error {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
if !r.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-r.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(r.closed)
|
|
|
|
|
|
|
|
// set not running
|
|
|
|
r.running = false
|
|
|
|
|
|
|
|
// stop all the services
|
2020-05-19 12:01:06 +02:00
|
|
|
for _, services := range r.namespaces {
|
|
|
|
for _, service := range services {
|
|
|
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
|
|
|
logger.Debugf("Runtime stopping %s", service.Name)
|
|
|
|
}
|
|
|
|
service.Stop()
|
2020-03-11 19:55:39 +02:00
|
|
|
}
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
2020-05-19 12:01:06 +02:00
|
|
|
|
2020-01-16 15:34:04 +02:00
|
|
|
// stop the scheduler
|
|
|
|
if r.options.Scheduler != nil {
|
|
|
|
return r.options.Scheduler.Close()
|
2019-11-02 15:25:10 +02:00
|
|
|
}
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-02 15:25:10 +02:00
|
|
|
|
|
|
|
// String implements stringer interface
|
|
|
|
func (r *runtime) String() string {
|
|
|
|
return "local"
|
|
|
|
}
|