Skip to content

Commit

Permalink
feat: init commit
Browse files Browse the repository at this point in the history
Signed-off-by: MdSahil-oss <[email protected]>
  • Loading branch information
MdSahil-oss committed Apr 10, 2024
1 parent 6371181 commit 6504a5f
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ require (
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kardianos/service v1.2.2 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
Expand Down Expand Up @@ -1381,6 +1383,7 @@ golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200922070232-aee5d888a860/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201117170446-d9b008d0a637/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
18 changes: 18 additions & 0 deletions machine/platform/systemd/manage.go
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
}

Check failure on line 11 in machine/platform/systemd/manage.go

View workflow job for this annotation

GitHub Actions / All

File is not `gofumpt`-ed (gofumpt)
func (p *startStop) run() {
// Do work here
}

Check failure on line 14 in machine/platform/systemd/manage.go

View workflow job for this annotation

GitHub Actions / All

File is not `gofumpt`-ed (gofumpt)
func (p *startStop) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
45 changes: 45 additions & 0 deletions machine/platform/systemd/options.go
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
}
}
110 changes: 110 additions & 0 deletions machine/platform/systemd/systemd.go
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
}

0 comments on commit 6504a5f

Please sign in to comment.