Skip to content

Commit

Permalink
Implement init system package to adapter different OS
Browse files Browse the repository at this point in the history
  • Loading branch information
pytimer authored and dlipovetsky committed Aug 29, 2019
1 parent 466a356 commit 7844750
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 35 deletions.
16 changes: 11 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/etcdadm/certs"
"sigs.k8s.io/etcdadm/constants"
"sigs.k8s.io/etcdadm/etcd"
"sigs.k8s.io/etcdadm/initsystem"
log "sigs.k8s.io/etcdadm/pkg/logrus"
"sigs.k8s.io/etcdadm/service"
"sigs.k8s.io/etcdadm/util"
Expand All @@ -42,22 +43,27 @@ var initCmd = &cobra.Command{
log.Fatalf("[defaults] Error: %s", err)
}

active, err := service.Active(constants.UnitFileBaseName)
initSystem, err := initsystem.GetInitSystem()
if err != nil {
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 := service.Stop(constants.UnitFileBaseName); err != nil {
if err := initSystem.Stop(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error stopping existing etcd service: %s", err)
}
}

enabled, err := service.Enabled(constants.UnitFileBaseName)
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 := service.Disable(constants.UnitFileBaseName); err != nil {
if err := initSystem.Disable(constants.UnitFileBaseName); err != nil {
log.Fatalf("[install] Error disabling existing etcd service: %s", err)
}
}
Expand Down Expand Up @@ -112,7 +118,7 @@ var initCmd = &cobra.Command{
if err = service.WriteUnitFile(&etcdAdmConfig); err != nil {
log.Fatalf("[configure] Error: %s", err)
}
if err = service.EnableAndStartService(constants.UnitFileBaseName); err != nil {
if err = initSystem.EnableAndStartService(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error: %s", err)
}
if err = service.WriteEtcdctlEnvFile(&etcdAdmConfig); err != nil {
Expand Down
16 changes: 11 additions & 5 deletions cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/etcdadm/certs"
"sigs.k8s.io/etcdadm/constants"
"sigs.k8s.io/etcdadm/etcd"
"sigs.k8s.io/etcdadm/initsystem"
"sigs.k8s.io/etcdadm/service"

"github.com/spf13/cobra"
Expand All @@ -51,22 +52,27 @@ var joinCmd = &cobra.Command{
log.Fatalf("[defaults] Error: %s", err)
}

active, err := service.Active(constants.UnitFileBaseName)
initSystem, err := initsystem.GetInitSystem()
if err != nil {
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 := service.Stop(constants.UnitFileBaseName); err != nil {
if err := initSystem.Stop(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error stopping existing etcd service: %s", err)
}
}

enabled, err := service.Enabled(constants.UnitFileBaseName)
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 := service.Disable(constants.UnitFileBaseName); err != nil {
if err := initSystem.Disable(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error disabling existing etcd service: %s", err)
}
}
Expand Down Expand Up @@ -166,7 +172,7 @@ var joinCmd = &cobra.Command{
if err := service.WriteUnitFile(&etcdAdmConfig); err != nil {
log.Fatalf("[configure] Error: %s", err)
}
if err := service.EnableAndStartService(constants.UnitFileBaseName); err != nil {
if err := initSystem.EnableAndStartService(constants.UnitFileBaseName); err != nil {
log.Fatalf("[start] Error: %s", err)
}
if err := service.WriteEtcdctlEnvFile(&etcdAdmConfig); err != nil {
Expand Down
15 changes: 10 additions & 5 deletions cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"sigs.k8s.io/etcdadm/binary"
"sigs.k8s.io/etcdadm/constants"
"sigs.k8s.io/etcdadm/etcd"
"sigs.k8s.io/etcdadm/service"
"sigs.k8s.io/etcdadm/initsystem"
)

var skipRemoveMember bool
Expand All @@ -44,7 +44,12 @@ var resetCmd = &cobra.Command{
log.Fatalf("[defaults] Error: %s", err)
}

active, err := service.Active(constants.UnitFileBaseName)
initSystem, err := initsystem.GetInitSystem()
if err != nil {
log.Fatalf("[initsystem] Error detecting the init system: %s", err)
}

active, err := initSystem.IsActive(constants.UnitFileBaseName)
if err != nil {
log.Fatalf("[reset] Error checking if etcd service is active: %s", err)
}
Expand Down Expand Up @@ -86,16 +91,16 @@ var resetCmd = &cobra.Command{
}
}
// Disable etcd service
if err := service.Stop(constants.UnitFileBaseName); err != nil {
if err := initSystem.Stop(constants.UnitFileBaseName); err != nil {
log.Fatalf("[reset] Error stopping existing etcd service: %s", err)
}
}
enabled, err := service.Enabled(constants.UnitFileBaseName)
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 := service.Disable(constants.UnitFileBaseName); err != nil {
if err := initSystem.Disable(constants.UnitFileBaseName); err != nil {
log.Fatalf("[reset] Error disabling existing etcd service: %s", err)
}
}
Expand Down
45 changes: 45 additions & 0 deletions initsystem/initsystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package initsystem

import (
"fmt"
"os/exec"
)

// 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
}

// GetInitSystem returns an InitSystem for the current system, or error
// if we cannot detect a supported init system.
func GetInitSystem() (InitSystem, error) {
_, err := exec.LookPath("systemctl")
if err == nil {
return &SystemdInitSystem{}, nil
}

return nil, fmt.Errorf("systemd not detected; ensure that `systemctl` is in the PATH")
}
43 changes: 23 additions & 20 deletions service/systemd.go → initsystem/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package service
package initsystem

import (
"fmt"
"os/exec"
)

func reloadSystemd() error {
// SystemdInitSystem defines systemd init system
type SystemdInitSystem struct{}

func (s SystemdInitSystem) reloadSystemd() error {
if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
return fmt.Errorf("failed to reload systemd: %v", err)
}
return nil
}

// Start a service
func 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 := reloadSystemd(); err != nil {
if err := s.reloadSystemd(); err != nil {
return err
}
args := []string{"start", service}
Expand All @@ -42,9 +45,9 @@ func Start(service string) error {
}

// Stop a service
func 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 := reloadSystemd(); err != nil {
if err := s.reloadSystemd(); err != nil {
return err
}
args := []string{"stop", service}
Expand All @@ -55,9 +58,9 @@ func Stop(service string) error {
}

// Enable a service
func 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 := reloadSystemd(); err != nil {
if err := s.reloadSystemd(); err != nil {
return err
}
args := []string{"enable", service}
Expand All @@ -68,9 +71,9 @@ func Enable(service string) error {
}

// Disable a service
func 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 := reloadSystemd(); err != nil {
if err := s.reloadSystemd(); err != nil {
return err
}
args := []string{"disable", service}
Expand All @@ -81,23 +84,23 @@ func Disable(service string) error {
}

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

// DisableAndStopService disables and stops the etcd service
func DisableAndStopService(service string) error {
if err := Disable(service); err != nil {
func (s SystemdInitSystem) DisableAndStopService(service string) error {
if err := s.Disable(service); err != nil {
return err
}
return Stop(service)
return s.Stop(service)
}

// Active checks if the systemd unit is active
func Active(service string) (bool, error) {
// IsActive checks if the systemd unit is active
func (s SystemdInitSystem) IsActive(service string) (bool, error) {
args := []string{"is-active", service}
if err := exec.Command("systemctl", args...).Run(); err != nil {
switch v := err.(type) {
Expand All @@ -112,8 +115,8 @@ func Active(service string) (bool, error) {
return true, nil
}

// Enabled checks if the systemd unit is enabled
func Enabled(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 7844750

Please sign in to comment.