diff --git a/commands/prepare-env.go b/commands/prepare-env.go index 98084a4..c931925 100644 --- a/commands/prepare-env.go +++ b/commands/prepare-env.go @@ -8,13 +8,13 @@ import ( func prepareEnvCommands(env *work.Env) *cobra.Command { envCmd := &cobra.Command{ - Use: env.GetName(), - Short: "Run command for " + env.GetName(), + Use: env.GetDirName(), + Short: "Run command for " + env.GetDirName(), } checkCmd := &cobra.Command{ Use: "check", - Short: "Check of " + env.GetName(), + Short: "Check of " + env.GetDirName(), Run: func(cmd *cobra.Command, args []string) { env.Check() }, @@ -22,7 +22,7 @@ func prepareEnvCommands(env *work.Env) *cobra.Command { fleetctlCmd := &cobra.Command{ Use: "fleetctl", - Short: "Run fleetctl command on " + env.GetName(), + Short: "Run fleetctl command on " + env.GetDirName(), Run: func(cmd *cobra.Command, args []string) { env.Fleetctl(args) }, @@ -30,7 +30,7 @@ func prepareEnvCommands(env *work.Env) *cobra.Command { listUnitsCmd := &cobra.Command{ Use: "list-units", - Short: "Run list-units command on " + env.GetName(), + Short: "Run list-units command on " + env.GetDirName(), Run: func(cmd *cobra.Command, args []string) { env.FleetctlListUnits() }, @@ -38,7 +38,7 @@ func prepareEnvCommands(env *work.Env) *cobra.Command { listMachinesCmd := &cobra.Command{ Use: "list-machines", - Short: "Run list-machines command on " + env.GetName(), + Short: "Run list-machines command on " + env.GetDirName(), Run: func(cmd *cobra.Command, args []string) { env.FleetctlListMachines() }, @@ -46,7 +46,7 @@ func prepareEnvCommands(env *work.Env) *cobra.Command { generateCmd := &cobra.Command{ Use: "generate", - Short: "Generate units for " + env.GetName(), + Short: "Generate units for " + env.GetDirName(), Run: func(cmd *cobra.Command, args []string) { env.Generate() }, diff --git a/commands/prepare-service.go b/commands/prepare-service.go index 81bfe47..4ae781f 100644 --- a/commands/prepare-service.go +++ b/commands/prepare-service.go @@ -15,12 +15,12 @@ func prepareServiceCommands(service *work.Service) *cobra.Command { serviceCmd := &cobra.Command{ Use: service.Name, - Short: "run command for " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "run command for " + service.Name + " on env " + service.GetEnv().GetDirName(), } generateCmd := &cobra.Command{ Use: "generate", - Short: "generate units for " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "generate units for " + service.Name + " on env " + service.GetEnv().GetDirName(), Long: `generate units using remote resolved or local pod/aci manifests`, Run: func(cmd *cobra.Command, args []string) { if err := service.Generate(); err != nil { @@ -31,7 +31,7 @@ func prepareServiceCommands(service *work.Service) *cobra.Command { checkCmd := &cobra.Command{ Use: "check [manifest...]", - Short: "Check units for " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "Check units for " + service.Name + " on env " + service.GetEnv().GetDirName(), Run: func(cmd *cobra.Command, args []string) { if err := service.Check(); err != nil { logs.WithE(err).Fatal("Check failed") @@ -41,7 +41,7 @@ func prepareServiceCommands(service *work.Service) *cobra.Command { diffCmd := &cobra.Command{ Use: "diff [manifest...]", - Short: "diff units for " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "diff units for " + service.Name + " on env " + service.GetEnv().GetDirName(), Run: func(cmd *cobra.Command, args []string) { service.Diff() }, @@ -49,7 +49,7 @@ func prepareServiceCommands(service *work.Service) *cobra.Command { lockCmd := &cobra.Command{ Use: "lock [message...]", - Short: "lock " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "lock " + service.Name + " on env " + service.GetEnv().GetDirName(), Long: `Add a lock to the service in etcd to prevent somebody else to do modification actions on this service/units.` + `lock is ignored if set by the current user`, Run: func(cmd *cobra.Command, args []string) { @@ -69,7 +69,7 @@ func prepareServiceCommands(service *work.Service) *cobra.Command { unlockCmd := &cobra.Command{ Use: "unlock", - Short: "unlock " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "unlock " + service.Name + " on env " + service.GetEnv().GetDirName(), Run: func(cmd *cobra.Command, args []string) { service.Unlock("service/unlock") }, @@ -77,7 +77,7 @@ func prepareServiceCommands(service *work.Service) *cobra.Command { listCmd := &cobra.Command{ Use: "list-units", - Short: "list-units on " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "list-units on " + service.Name + " on env " + service.GetEnv().GetDirName(), Run: func(cmd *cobra.Command, args []string) { service.FleetListUnits("service/unlock") }, @@ -85,7 +85,7 @@ func prepareServiceCommands(service *work.Service) *cobra.Command { updateCmd := &cobra.Command{ Use: "update", - Short: "update " + service.Name + " on env " + service.GetEnv().GetName(), + Short: "update " + service.Name + " on env " + service.GetEnv().GetDirName(), Run: func(cmd *cobra.Command, args []string) { err := service.Update() if err != nil { diff --git a/commands/prepare-unit.go b/commands/prepare-unit.go index a507c9f..589e9ee 100644 --- a/commands/prepare-unit.go +++ b/commands/prepare-unit.go @@ -131,5 +131,5 @@ func prepareUnitCommands(unit *work.Unit) *cobra.Command { } func getShortDescription(unit *work.Unit, action string) string { - return action + " '" + unit.Name + "' from '" + unit.Service.GetName() + "' on env '" + unit.Service.GetEnv().GetName() + "'" + return action + " '" + unit.Name + "' from '" + unit.Service.GetName() + "' on env '" + unit.Service.GetEnv().GetDirName() + "'" } diff --git a/commands/prepare.go b/commands/prepare.go index e1b5e98..ddfaf65 100644 --- a/commands/prepare.go +++ b/commands/prepare.go @@ -19,8 +19,8 @@ func loadEnvCommandsReturnNewRoot(osArgs []string, rootCmd *cobra.Command) *cobr env := work.LoadEnv(envNames) envCmd := &cobra.Command{ - Use: env.GetName(), - Short: "Run command for " + env.GetName(), + Use: env.GetDirName(), + Short: "Run command for " + env.GetDirName(), Run: func(cmd *cobra.Command, args []string) { newRootCmd.AddCommand(prepareEnvCommands(env)) diff --git a/work/env.go b/work/env.go index 1f7491d..6978278 100644 --- a/work/env.go +++ b/work/env.go @@ -34,11 +34,12 @@ type Config struct { Sudo bool `yaml:"sudo,omitempty"` Driver string `yaml:"driver,omitempty"` } `yaml:"fleet,omitempty"` + EnvName string `yaml:"envName,omitempty"` } type Env struct { path string - name string + dirName string fields data.Fields attributes map[string]interface{} config Config @@ -59,7 +60,7 @@ func NewEnvironment(root string, name string) *Env { services: map[string]*Service{}, servicesMutex: &sync.Mutex{}, path: path, - name: name, + dirName: name, fields: fields, config: Config{}, } @@ -69,8 +70,12 @@ func NewEnvironment(root string, name string) *Env { return env } -func (e Env) GetName() string { - return e.name +func (e Env) GetDirName() string { + return e.dirName +} + +func (e Env) GetEnvName() string { + return e.config.EnvName } func (e Env) GetFields() data.Fields { @@ -134,6 +139,9 @@ func (e *Env) loadConfig() { if e.config.Fleet.Driver == "" { e.config.Fleet.Driver = "etcd" } + if e.config.EnvName == "" { + e.config.EnvName = e.dirName + } src := strings.Split(e.config.Fleet.Endpoint, ",") dest := make([]string, len(src)) @@ -239,7 +247,7 @@ func (e Env) ListMachineNames() ([]string, error) { return inMemoryNames, nil } - data, modification := ggn.Home.LoadMachinesCacheWithDate(e.name) + data, modification := ggn.Home.LoadMachinesCacheWithDate(e.dirName) if data == "" || modification.Add(12*time.Hour).Before(time.Now()) { logs.WithFields(e.fields).Debug("reloading list machines cache") datatmp, _, err := e.RunFleetCmdGetOutput("list-machines", "--fields=metadata", "--no-legend") @@ -247,7 +255,7 @@ func (e Env) ListMachineNames() ([]string, error) { return nil, errors.Annotate(err, "Cannot list-machines") } data = datatmp - ggn.Home.SaveMachinesCache(e.name, data) + ggn.Home.SaveMachinesCache(e.dirName, data) } var names []string @@ -257,7 +265,7 @@ func (e Env) ListMachineNames() ([]string, error) { metas := strings.Split(machine, ",") for _, meta := range metas { elem := strings.Split(meta, "=") - if elem[0] == "name" { + if elem[0] == "dirName" { // TODO this is specific to blablacar's metadata ?? names = append(names, elem[1]) } @@ -294,7 +302,8 @@ func (e Env) runHookAndGetNumRun(path string, info HookInfo) error { } envs := map[string]string{} - envs["ENV"] = e.name + envs["ENVDIR"] = e.dirName + envs["ENV"] = e.config.EnvName envs["COMMAND"] = info.Command if info.Unit != nil { envs["UNIT"] = info.Unit.GetName() @@ -310,7 +319,7 @@ func (e Env) runHookAndGetNumRun(path string, info HookInfo) error { for _, f := range files { if !f.IsDir() { - hookFields := data.WithField("name", f.Name()) + hookFields := data.WithField("dirName", f.Name()) args := []string{e.path + PATH_HOOKS + path + "/" + f.Name()} for key, val := range envs { diff --git a/work/service.go b/work/service.go index 241e885..c43a322 100644 --- a/work/service.go +++ b/work/service.go @@ -184,7 +184,7 @@ func (s *Service) FleetListUnits(command string) { } unitStatuses := strings.Split(stdout, "\n") - prefix := s.env.GetName() + "_" + s.Name + "_" + prefix := s.env.GetEnvName() + "_" + s.Name + "_" for _, unitStatus := range unitStatuses { if strings.HasPrefix(unitStatus, prefix) { fmt.Println(unitStatus) diff --git a/work/unit.go b/work/unit.go index 71c1ec4..db31e5f 100644 --- a/work/unit.go +++ b/work/unit.go @@ -34,7 +34,7 @@ type Unit struct { func NewUnit(path string, hostname string, utype UnitType, service *Service) *Unit { l := service.GetFields() - filename := service.GetEnv().GetName() + "_" + service.GetName() + "_" + hostname + utype.String() + filename := service.GetEnv().GetEnvName() + "_" + service.GetName() + "_" + hostname + utype.String() name := hostname if utype != TYPE_SERVICE {