Skip to content

Commit

Permalink
Adds basic implementation for Systemd and adds systemd support for kr…
Browse files Browse the repository at this point in the history
…aft run cmd

Signed-off-by: MdSahil-oss <[email protected]>
  • Loading branch information
MdSahil-oss committed Apr 26, 2024
1 parent 6504a5f commit ac38211
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 46 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/henvic/httpretty v0.1.3
github.com/kardianos/service v1.2.2
github.com/kubescape/go-git-url v0.0.30
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.20
Expand Down Expand Up @@ -160,7 +161,6 @@ 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
66 changes: 64 additions & 2 deletions internal/cli/kraft/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/kardianos/service"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand All @@ -19,11 +21,13 @@ import (
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/instance/stop"
"kraftkit.sh/internal/cli/kraft/start"
"kraftkit.sh/internal/set"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
mplatform "kraftkit.sh/machine/platform"
"kraftkit.sh/machine/platform/systemd"
"kraftkit.sh/packmanager"
"kraftkit.sh/tui/selection"
ukarch "kraftkit.sh/unikraft/arch"
Expand All @@ -49,6 +53,7 @@ type RunOptions struct {
Remove bool `long:"rm" usage:"Automatically remove the unikernel when it shutsdown"`
Rootfs string `long:"rootfs" usage:"Specify a path to use as root file system (can be volume or initramfs)"`
RunAs string `long:"as" usage:"Force a specific runner"`
Systemd bool `long:"systemd" usage:"runs unikernel as systemd process"`
Target string `long:"target" short:"t" usage:"Explicitly use the defined project target"`
Volumes []string `long:"volume" short:"v" usage:"Bind a volume to the instance"`
WithKernelDbg bool `long:"symbolic" usage:"Use the debuggable (symbolic) unikernel"`
Expand Down Expand Up @@ -374,15 +379,72 @@ func (opts *RunOptions) Run(ctx context.Context, args []string) error {
return err
}

if opts.NoStart {
if opts.NoStart && !opts.Systemd {
// Output the name of the instance such that it can be piped
fmt.Fprintf(iostreams.G(ctx).Out, "%s\n", machine.Name)
return nil
}

return start.Start(ctx, &start.StartOptions{
err = start.Start(ctx, &start.StartOptions{
Detach: opts.Detach,
Platform: opts.platform.String(),
Remove: opts.Remove,
}, machine.Name)

// Install systemd serivce that runs a new unikernel instance on each start.
if err == nil && opts.Systemd {
// Stops the created testing instance if that is still runnning.
if err = stop.Stop(ctx, &stop.StopOptions{
Force: true,
}, machine.Name); err != nil {
log.G(ctx).Debugf("instance %s is already stopped", machine.Name)
}

boolArgs := ""
if opts.Detach {
boolArgs += "-d"
}
if opts.DisableAccel {
boolArgs += " -W"
}
if opts.PrefixName {
boolArgs += " --prefix-name"
}
if opts.WithKernelDbg {
boolArgs += " --symbolic"
}

svcConfig, err := systemd.NewMachineV1alpha1ServiceSystemdWrapper(
ctx,
systemd.WithName(machine.Name),
systemd.WithDescription("created by kraftkit"),
systemd.WithArguments([]string{"run", "-m", opts.Architecture, "--as", opts.RunAs, "--kernel-arg", strings.Join(opts.KernelArgs, " "),
"-K", opts.Kraftfile, "--mac", opts.MacAddress, "--memory", opts.Memory, "--network", strings.Join(opts.Networks, " "),
"--plat", opts.Platform, "--port", strings.Join(opts.Ports, " "), "--prefix", opts.Prefix, "--rootfs", opts.Rootfs, "--target", opts.Target,
"--volume", strings.Join(opts.Volumes, " "), "--rm", boolArgs, machine.Name,
}),
systemd.WithOptions(service.KeyValue{
"Restart": "never",
}),
)
if err != nil {
return err
}

machine, err = svcConfig.Create(ctx, machine)
if err != nil {
return err
}
log.G(ctx).Infof("created a systemd process named %s ", svcConfig.Name)

_, err = svcConfig.Start(ctx, machine)
if err != nil {
return err
}
log.G(ctx).Infof("started running %s as systemd process", svcConfig.Name)

opts.NoStart = true
}

return err
}
5 changes: 4 additions & 1 deletion machine/platform/systemd/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ func (p *startStop) Start(s service.Service) error {
go p.run()
return nil
}
func (p *startStop) run() {

func (p *startStop) run() error {
// Do work here
return nil
}

func (p *startStop) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
Expand Down
36 changes: 26 additions & 10 deletions machine/platform/systemd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,58 @@ import "github.com/kardianos/service"

type ServiceConfigOption func(*ServiceConfig) error

// WithName sets the name of the systemd process.
// WithName sets the name of systemd service.
func WithName(name string) ServiceConfigOption {
return func(config *ServiceConfig) error {
config.name = name
config.Name = name
return nil
}
}

// WithDisplayName sets the display-name of the systemd process.
// WithDisplayName sets the display-name of systemd service.
func WithDisplayName(dName string) ServiceConfigOption {
return func(config *ServiceConfig) error {
config.displayName = dName
config.DisplayName = dName
return nil
}
}

// WithDescription sets the description of the systemd process.
// WithDescription sets the description/heading of systemd service.
func WithDescription(desc string) ServiceConfigOption {
return func(config *ServiceConfig) error {
config.description = desc
config.Description = desc
return nil
}
}

// WithDependencies sets the dependencies of the systemd process.
// WithDependencies sets the dependencies of systemd service.
func WithDependencies(deps []string) ServiceConfigOption {
return func(config *ServiceConfig) error {
config.dependencies = deps
config.Dependencies = deps
return nil
}
}

// WithOptions sets the options of the systemd process.
// WithEnvVars sets the environment variables for systemd service.
func WithEnvVars(envVars map[string]string) ServiceConfigOption {
return func(config *ServiceConfig) error {
config.EnvVars = envVars
return nil
}
}

// WithArguments sets the arguments to the command executed by systemd service.
func WithArguments(args []string) ServiceConfigOption {
return func(config *ServiceConfig) error {
config.Arguments = args
return nil
}
}

// WithOptions sets the options of systemd service.
func WithOptions(opts service.KeyValue) ServiceConfigOption {
return func(config *ServiceConfig) error {
config.option = opts
config.Option = opts
return nil
}
}
Loading

0 comments on commit ac38211

Please sign in to comment.