Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
support overriding environment name
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rad committed Nov 8, 2017
1 parent ccc2b1a commit af5c18f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
14 changes: 7 additions & 7 deletions commands/prepare-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ 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()
},
}

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)
},
}

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()
},
}

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()
},
}

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()
},
Expand Down
16 changes: 8 additions & 8 deletions commands/prepare-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -41,15 +41,15 @@ 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()
},
}

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) {
Expand All @@ -69,23 +69,23 @@ 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")
},
}

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")
},
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion commands/prepare-unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() + "'"
}
4 changes: 2 additions & 2 deletions commands/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
27 changes: 18 additions & 9 deletions work/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{},
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -239,15 +247,15 @@ 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")
if err != nil {
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
Expand All @@ -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])
}
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion work/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion work/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit af5c18f

Please sign in to comment.