-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: MdSahil-oss <[email protected]>
- Loading branch information
1 parent
6371181
commit 6504a5f
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package systemd | ||
|
||
import "github.com/kardianos/service" | ||
|
||
type startStop struct{} | ||
|
||
func (p *startStop) Start(s service.Service) error { | ||
// Start should not block. Do the actual work async. | ||
go p.run() | ||
return nil | ||
} | ||
func (p *startStop) run() { | ||
// Do work here | ||
} | ||
func (p *startStop) Stop(s service.Service) error { | ||
// Stop should not block. Return with a few seconds. | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package systemd | ||
|
||
import "github.com/kardianos/service" | ||
|
||
type ServiceConfigOption func(*ServiceConfig) error | ||
|
||
// WithName sets the name of the systemd process. | ||
func WithName(name string) ServiceConfigOption { | ||
return func(config *ServiceConfig) error { | ||
config.name = name | ||
return nil | ||
} | ||
} | ||
|
||
// WithDisplayName sets the display-name of the systemd process. | ||
func WithDisplayName(dName string) ServiceConfigOption { | ||
return func(config *ServiceConfig) error { | ||
config.displayName = dName | ||
return nil | ||
} | ||
} | ||
|
||
// WithDescription sets the description of the systemd process. | ||
func WithDescription(desc string) ServiceConfigOption { | ||
return func(config *ServiceConfig) error { | ||
config.description = desc | ||
return nil | ||
} | ||
} | ||
|
||
// WithDependencies sets the dependencies of the systemd process. | ||
func WithDependencies(deps []string) ServiceConfigOption { | ||
return func(config *ServiceConfig) error { | ||
config.dependencies = deps | ||
return nil | ||
} | ||
} | ||
|
||
// WithOptions sets the options of the systemd process. | ||
func WithOptions(opts service.KeyValue) ServiceConfigOption { | ||
return func(config *ServiceConfig) error { | ||
config.option = opts | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package systemd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kardianos/service" | ||
machinev1alpha1 "kraftkit.sh/api/machine/v1alpha1" | ||
) | ||
|
||
type ServiceConfig struct { | ||
name string | ||
displayName string | ||
description string | ||
dependencies []string | ||
option service.KeyValue | ||
|
||
service service.Service | ||
logger service.Logger | ||
} | ||
|
||
func NewMachineV1alpha1ServiceSystemdWrapper(ctx context.Context, opts ...ServiceConfigOption) (ServiceConfig, error) { | ||
config := ServiceConfig{} | ||
|
||
for _, opt := range opts { | ||
if err := opt(&config); err != nil { | ||
return config, err | ||
} | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func (sc ServiceConfig) Create(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) { | ||
var err error | ||
uid := os.Getuid() | ||
if uid != 0 { | ||
return machine, fmt.Errorf("requires root permission") | ||
} | ||
|
||
svcConfig := &service.Config{ | ||
Name: sc.name, | ||
DisplayName: sc.displayName, | ||
Description: sc.description, | ||
Dependencies: sc.dependencies, | ||
Option: sc.option, | ||
} | ||
sys := service.ChosenSystem() | ||
sc.service, err = sys.New(&startStop{}, svcConfig) | ||
if err != nil { | ||
return machine, err | ||
} | ||
|
||
errs := make(chan error, 5) | ||
sc.logger, err = sc.service.Logger(errs) | ||
if err != nil { | ||
return machine, err | ||
} | ||
|
||
err = sc.service.Install() | ||
if err != nil { | ||
return machine, err | ||
} | ||
|
||
return machine, nil | ||
} | ||
|
||
func (sc ServiceConfig) Start(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) { | ||
// Implement `Start()` -> to start running systemd process, It also checks for the user permission same as above first. | ||
return &machinev1alpha1.Machine{}, nil | ||
} | ||
|
||
func (sc ServiceConfig) Pause(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) { | ||
return &machinev1alpha1.Machine{}, nil | ||
} | ||
|
||
func (sc ServiceConfig) Stop(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) { | ||
// Implement `Stop()` -> to stop running systemd process, It also checks for the user permission same as above first. | ||
return &machinev1alpha1.Machine{}, nil | ||
} | ||
|
||
func (sc ServiceConfig) Update(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) { | ||
return &machinev1alpha1.Machine{}, nil | ||
} | ||
|
||
func (sc ServiceConfig) Delete(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) { | ||
// Implement `Delete()` -> to uninstall systemd process, | ||
// It also checks for the user permission same as above first & stop the process if it's in running state. | ||
return &machinev1alpha1.Machine{}, nil | ||
} | ||
|
||
func (sc ServiceConfig) Get(ctx context.Context, machine *machinev1alpha1.Machine) (*machinev1alpha1.Machine, error) { | ||
// Implement `Get()` -> to return the systemd process with following info: `name`, `status` & etc. | ||
return &machinev1alpha1.Machine{}, nil | ||
} | ||
|
||
func (sc ServiceConfig) List(ctx context.Context, machineList *machinev1alpha1.MachineList) (*machinev1alpha1.MachineList, error) { | ||
return &machinev1alpha1.MachineList{}, nil | ||
} | ||
|
||
func (sc ServiceConfig) Watch(ctx context.Context, machine *machinev1alpha1.Machine) (chan *machinev1alpha1.Machine, chan error, error) { | ||
events := make(chan *machinev1alpha1.Machine) | ||
return events, nil, nil | ||
} | ||
|
||
func (sc ServiceConfig) Logs(ctx context.Context, machine *machinev1alpha1.Machine) (chan string, chan error, error) { | ||
logs := make(chan string) | ||
return logs, nil, nil | ||
} |