Skip to content

Commit

Permalink
Pare down InitSystem interface
Browse files Browse the repository at this point in the history
By reducing the surface, we make it easier to implement other
backends (mock & kubelet)
  • Loading branch information
justinsb committed Jul 25, 2020
1 parent e4a5ffd commit c77db63
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 64 deletions.
20 changes: 2 additions & 18 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,8 @@ var initCmd = &cobra.Command{
log.Fatalf("[initsystem] Error detecting the init system: %s", err)
}

active, err := initSystem.IsActive(constants.UnitFileBaseName)
if err != nil {
log.Fatalf("[start] Error checking if etcd service is active: %s", err)
}
if active {
if err := initSystem.Stop(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error stopping existing etcd service: %s", err)
}
}

enabled, err := initSystem.IsEnabled(constants.UnitFileBaseName)
if err != nil {
log.Fatalf("[install] Error checking if etcd service is enabled: %s", err)
}
if enabled {
if err := initSystem.Disable(constants.UnitFileBaseName); err != nil {
log.Fatalf("[install] Error disabling existing etcd service: %s", err)
}
if err := initSystem.DisableAndStopService(constants.UnitFileBaseName); err != nil {
log.Fatalf("[install] Error disabling and stopping etcd service: %s", err)
}

exists, err := util.Exists(etcdAdmConfig.DataDir)
Expand Down
20 changes: 2 additions & 18 deletions cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,8 @@ var joinCmd = &cobra.Command{
log.Fatalf("[initsystem] Error detecting the init system: %s", err)
}

active, err := initSystem.IsActive(constants.UnitFileBaseName)
if err != nil {
log.Fatalf("[start] Error checking if etcd service is active: %s", err)
}
if active {
if err := initSystem.Stop(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error stopping existing etcd service: %s", err)
}
}

enabled, err := initSystem.IsEnabled(constants.UnitFileBaseName)
if err != nil {
log.Fatalf("[start] Error checking if etcd service is enabled: %s", err)
}
if enabled {
if err := initSystem.Disable(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error disabling existing etcd service: %s", err)
}
if err := initSystem.DisableAndStopService(constants.UnitFileBaseName); err != nil {
log.Fatalf("[install] Error disabling and stopping etcd service: %s", err)
}

// cert management
Expand Down
15 changes: 3 additions & 12 deletions cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,11 @@ var resetCmd = &cobra.Command{
log.Println("[membership] Member was removed")
}
}
// Disable etcd service
if err := initSystem.Stop(constants.UnitFileBaseName); err != nil {
log.Fatalf("[reset] Error stopping existing etcd service: %s", err)
}
}
enabled, err := initSystem.IsEnabled(constants.UnitFileBaseName)
if err != nil {
log.Fatalf("[reset] Error checking if etcd service is enabled: %s", err)
}
if enabled {
if err := initSystem.Disable(constants.UnitFileBaseName); err != nil {
log.Fatalf("[reset] Error disabling existing etcd service: %s", err)
}
if err := initSystem.DisableAndStopService(constants.UnitFileBaseName); err != nil {
log.Fatalf("[reset] Error stopping etcd: %s", err)
}

// Remove etcd datastore
if err = os.RemoveAll(etcdAdmConfig.DataDir); err != nil {
log.Print(err)
Expand Down
5 changes: 0 additions & 5 deletions initsystem/initsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ import (

// InitSystem is the interface that describe behaviors of an init system
type InitSystem interface {
Start(service string) error
Stop(service string) error
Enable(service string) error
Disable(service string) error
IsActive(service string) (bool, error)
IsEnabled(service string) (bool, error)
EnableAndStartService(service string) error
DisableAndStopService(service string) error
}
Expand Down
40 changes: 29 additions & 11 deletions initsystem/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s SystemdInitSystem) reloadSystemd() error {
}

// Start a service
func (s SystemdInitSystem) Start(service string) error {
func (s SystemdInitSystem) start(service string) error {
// Before we try to start any service, make sure that systemd is ready
if err := s.reloadSystemd(); err != nil {
return err
Expand All @@ -45,7 +45,7 @@ func (s SystemdInitSystem) Start(service string) error {
}

// Stop a service
func (s SystemdInitSystem) Stop(service string) error {
func (s SystemdInitSystem) stop(service string) error {
// Before we try to start any service, make sure that systemd is ready
if err := s.reloadSystemd(); err != nil {
return err
Expand All @@ -58,7 +58,7 @@ func (s SystemdInitSystem) Stop(service string) error {
}

// Enable a service
func (s SystemdInitSystem) Enable(service string) error {
func (s SystemdInitSystem) enable(service string) error {
// Before we try to enable any service, make sure that systemd is ready
if err := s.reloadSystemd(); err != nil {
return err
Expand All @@ -71,7 +71,7 @@ func (s SystemdInitSystem) Enable(service string) error {
}

// Disable a service
func (s SystemdInitSystem) Disable(service string) error {
func (s SystemdInitSystem) disable(service string) error {
// Before we try to disable any service, make sure that systemd is ready
if err := s.reloadSystemd(); err != nil {
return err
Expand All @@ -85,18 +85,36 @@ func (s SystemdInitSystem) Disable(service string) error {

// EnableAndStartService enables and starts the etcd service
func (s SystemdInitSystem) EnableAndStartService(service string) error {
if err := s.Enable(service); err != nil {
if err := s.enable(service); err != nil {
return err
}
return s.Start(service)
return s.start(service)
}

// DisableAndStopService disables and stops the etcd service
func (s SystemdInitSystem) DisableAndStopService(service string) error {
if err := s.Disable(service); err != nil {
return err
enabled, err := s.isEnabled(service)
if err != nil {
return fmt.Errorf("error checking if etcd service is enabled: %w", err)
}
if enabled {
if err := s.disable(service); err != nil {
return err
}
}

active, err := s.IsActive(service)
if err != nil {
return fmt.Errorf("error checking if etcd service is active: %w", err)
}
return s.Stop(service)

if active {
if err := s.stop(service); err != nil {
return err
}
}

return nil
}

// IsActive checks if the systemd unit is active
Expand All @@ -115,8 +133,8 @@ func (s SystemdInitSystem) IsActive(service string) (bool, error) {
return true, nil
}

// IsEnabled checks if the systemd unit is enabled
func (s SystemdInitSystem) IsEnabled(service string) (bool, error) {
// isEnabled checks if the systemd unit is enabled
func (s SystemdInitSystem) isEnabled(service string) (bool, error) {
args := []string{"is-enabled", service}
if err := exec.Command("systemctl", args...).Run(); err != nil {
switch v := err.(type) {
Expand Down

0 comments on commit c77db63

Please sign in to comment.