Skip to content

Commit

Permalink
support macro substitution of app
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejingru authored and ludanfeng committed Sep 28, 2020
1 parent 19856b7 commit e4e953d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 53 deletions.
14 changes: 0 additions & 14 deletions ami/hostpath_unix.go

This file was deleted.

32 changes: 24 additions & 8 deletions ami/native/native.go
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/baetyl/baetyl/v2/ami"
"github.com/baetyl/baetyl/v2/config"
"github.com/baetyl/baetyl/v2/engine"
"github.com/baetyl/baetyl/v2/program"
)

Expand All @@ -32,12 +33,25 @@ func init() {
}

type nativeImpl struct {
mapping *native.ServiceMapping
portAllocator *native.PortAllocator
log *log.Logger
logHostPath string
runHostPath string
mapping *native.ServiceMapping
portAllocator *native.PortAllocator
hostPathLibRun string
log *log.Logger
}

func newNativeImpl(cfg config.AmiConfig) (ami.AMI, error) {
var hostPathLib string
if val := os.Getenv(context.KeyBaetylHostPathLib); val == "" {
err := os.Setenv(context.KeyBaetylHostPathLib, engine.DefaultHostPathLib)
if err != nil {
return nil, errors.Trace(err)
}
hostPathLib = engine.DefaultHostPathLib
} else {
hostPathLib = val
}
portAllocator, err := native.NewPortAllocator(cfg.Native.PortsRange.Start, cfg.Native.PortsRange.End)
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -47,6 +61,8 @@ func newNativeImpl(cfg config.AmiConfig) (ami.AMI, error) {
return nil, errors.Trace(err)
}
return &nativeImpl{
logHostPath: filepath.Join(hostPathLib, "log"),
runHostPath: filepath.Join(hostPathLib, "run"),
mapping: mapping,
portAllocator: portAllocator,
log: log.With(log.Any("ami", "native")),
Expand All @@ -64,7 +80,7 @@ func (impl *nativeImpl) ApplyApp(ns string, app v1.Application, configs map[stri
impl.log.Warn("failed to delete old app", log.Error(err))
}

appDir := filepath.Join(ami.RunHostPath, ns, app.Name, app.Version)
appDir := filepath.Join(impl.runHostPath, ns, app.Name, app.Version)
err = os.MkdirAll(appDir, 0755)
if err != nil {
return errors.Trace(err)
Expand Down Expand Up @@ -174,7 +190,7 @@ func (impl *nativeImpl) ApplyApp(ns string, app v1.Application, configs map[stri
Env: env,
Logger: log.Config{
Level: "debug",
Filename: filepath.Join(ami.LogHostPath, ns, app.Name, app.Version, fmt.Sprintf("%s-%d.log", s.Name, i)),
Filename: filepath.Join(impl.logHostPath, ns, app.Name, app.Version, fmt.Sprintf("%s-%d.log", s.Name, i)),
},
}
prgYml, err := yaml.Marshal(prgCfg)
Expand Down Expand Up @@ -222,7 +238,7 @@ func (impl *nativeImpl) ApplyApp(ns string, app v1.Application, configs map[stri

func (impl *nativeImpl) DeleteApp(ns string, appName string) error {
// scan app version
curAppDir := filepath.Join(ami.RunHostPath, ns, appName)
curAppDir := filepath.Join(impl.runHostPath, ns, appName)
appVerFiles, err := ioutil.ReadDir(curAppDir)
if err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -290,11 +306,11 @@ func (impl *nativeImpl) DeleteApp(ns string, appName string) error {

func (impl *nativeImpl) StatsApps(ns string) ([]v1.AppStats, error) {
var stats []v1.AppStats
if !utils.DirExists(ami.RunHostPath) {
if !utils.DirExists(impl.runHostPath) {
return stats, nil
}

curNsPath := filepath.Join(ami.RunHostPath, ns)
curNsPath := filepath.Join(impl.runHostPath, ns)
if !utils.DirExists(curNsPath) {
return stats, nil
}
Expand Down
13 changes: 10 additions & 3 deletions cmd/apply.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"

Expand Down Expand Up @@ -71,11 +72,15 @@ func apply() {
if err != nil {
return
}
if os.Getenv(context.KeyBaetylHostPathLib) == "" {
var hostPathLib string
if val := os.Getenv(context.KeyBaetylHostPathLib); val == "" {
err := os.Setenv(context.KeyBaetylHostPathLib, "/var/lib/baetyl")
if err != nil {
return
}
hostPathLib = "/var/lib/baetyl"
} else {
hostPathLib = val
}

// download data
Expand Down Expand Up @@ -116,9 +121,10 @@ func apply() {
}

// download config objects if exist
objectHostPath := filepath.Join(hostPathLib, "object")
for _, cfg := range configs {
sync.FilterConfig(&cfg)
err = sync.DownloadConfig(cli, ami.ObjectHostPath, &cfg)
err = sync.DownloadConfig(cli, objectHostPath, &cfg)
if err != nil {
return
}
Expand Down Expand Up @@ -150,7 +156,8 @@ func apply() {
}

// prepare app
err = sync.PrepareApp(ami.HostHostPath, ami.ObjectHostPath, &app, configs)
hostHostPath := filepath.Join(hostPathLib, "host")
err = sync.PrepareApp(hostHostPath, objectHostPath, &app, configs)
if err != nil {
return
}
Expand Down
66 changes: 38 additions & 28 deletions engine/engine.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
gosync "sync"
"time"
Expand All @@ -31,6 +32,7 @@ const (
SystemCertVolumePrefix = "baetyl-cert-volume-"
SystemCertSecretPrefix = "baetyl-cert-secret-"
SystemCertPath = "/var/lib/baetyl/system/certs"
DefaultHostPathLib = "/var/lib/baetyl"
)

//go:generate mockgen -destination=../mock/engine.go -package=mock -source=engine.go Engine
Expand All @@ -45,23 +47,35 @@ type Engine interface {

// pipes: remote debugging of the routing table between the router and the channel. key={ns}_{name}_{container}
type engineImpl struct {
mode string
cfg config.Config
syn sync.Sync
ami ami.AMI
nod *node.Node
sto *bh.Store
log *log.Logger
sec security.Security
hp helper.Helper
chains gosync.Map
tomb utils.Tomb
mode string
hostHostPath string
objectHostPath string
cfg config.Config
syn sync.Sync
ami ami.AMI
nod *node.Node
sto *bh.Store
log *log.Logger
sec security.Security
hp helper.Helper
chains gosync.Map
tomb utils.Tomb
}

func NewEngine(cfg config.Config, sto *bh.Store, nod *node.Node, syn sync.Sync, helper helper.Helper) (Engine, error) {
mode := context.RunMode()
log.L().Info("app running mode", log.Any("mode", mode))

var hostPathLib string
if val := os.Getenv(context.KeyBaetylHostPathLib); val == "" {
err := os.Setenv(context.KeyBaetylHostPathLib, DefaultHostPathLib)
if err != nil {
return nil, errors.Trace(err)
}
hostPathLib = DefaultHostPathLib
} else {
hostPathLib = val
}
am, err := ami.NewAMI(mode, cfg.AMI)
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -70,23 +84,19 @@ func NewEngine(cfg config.Config, sto *bh.Store, nod *node.Node, syn sync.Sync,
if err != nil {
return nil, errors.Trace(err)
}
if os.Getenv(context.KeyBaetylHostPathLib) == "" {
err := os.Setenv(context.KeyBaetylHostPathLib, "/var/lib/baetyl")
if err != nil {
return nil, errors.Trace(err)
}
}
eng := &engineImpl{
mode: mode,
ami: am,
sto: sto,
syn: syn,
nod: nod,
cfg: cfg,
sec: sec,
hp: helper,
chains: gosync.Map{},
log: log.With(),
mode: mode,
hostHostPath: filepath.Join(hostPathLib, "host"),
objectHostPath: filepath.Join(hostPathLib, "object"),
ami: am,
sto: sto,
syn: syn,
nod: nod,
cfg: cfg,
sec: sec,
hp: helper,
chains: gosync.Map{},
log: log.With(),
}
return eng, nil
}
Expand Down Expand Up @@ -355,7 +365,7 @@ func (e *engineImpl) applyApp(ns string, info specv1.AppInfo) error {
secs[secret.Name] = secret
}
}
if err := sync.PrepareApp(ami.HostHostPath, ami.ObjectHostPath, app, cfgs); err != nil {
if err := sync.PrepareApp(e.hostHostPath, e.objectHostPath, app, cfgs); err != nil {
e.log.Error("failed to revise applications", log.Any("app", app), log.Error(err))
return errors.Trace(err)
}
Expand Down

0 comments on commit e4e953d

Please sign in to comment.