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 30, 2024
1 parent cd82032 commit 7e6a246
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 47 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 @@ -167,7 +168,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
70 changes: 67 additions & 3 deletions internal/cli/kraft/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"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 +23,14 @@ import (
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/remove"
"kraftkit.sh/internal/cli/kraft/start"
"kraftkit.sh/internal/cli/kraft/stop"
"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 Down Expand Up @@ -51,6 +58,7 @@ type RunOptions struct {
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"`
Runtime string `long:"runtime" short:"r" usage:"Set an alternative unikernel runtime"`
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 @@ -376,15 +384,71 @@ 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{
Detach: opts.Detach,
err = start.Start(ctx, &start.StartOptions{
Detach: opts.Systemd || opts.Detach,
Platform: opts.platform.String(),
Remove: opts.Remove,
}, machine.Name)

// Installs systemd serivce that runs a new unikernel instance on each start.
if err == nil && opts.Systemd {
// Stops & removes the created testing instance if that is still runnning & present.
if err = stop.Stop(ctx, &stop.StopOptions{Platform: opts.platform.String()}, machine.Name); err != nil {
log.G(ctx).Debugf("instance %s was already stopped", machine.Name)
}
if err = remove.Remove(ctx, &remove.RemoveOptions{Platform: opts.platform.String()}, machine.Name); err != nil {
log.G(ctx).Errorf("could not remove %s", machine.Name)
}

opts.Name = machine.Name
opts.Remove = true
sysdArgs := []string{"run"}
sysdArgs = append(sysdArgs, opts.GetArgs()...)

if len(args) > 0 {
if strings.HasPrefix(args[0], ".") {
pwd, err := os.Getwd()
if err != nil {
return err
}
args[0] = filepath.Join(pwd, args[0])
}
sysdArgs = append(sysdArgs, args[0])
} else {
sysdArgs = append(sysdArgs, opts.workdir)
}

svcConfig, err := systemd.NewMachineV1alpha1ServiceSystemdWrapper(
ctx,
systemd.WithName(machine.Name),
systemd.WithDescription("created by Kraftkit"),
systemd.WithArguments(sysdArgs),
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)
}

return err
}
63 changes: 63 additions & 0 deletions internal/cli/kraft/run/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,66 @@ func (opts *RunOptions) parseEnvs(_ context.Context, machine *machineapi.Machine

return nil
}

// GetArgs returns all the arguments of run command as an array of strings.
func (opts *RunOptions) GetArgs() []string {
args := []string{}
if opts.Detach {
args = append(args, "--detach")
}
if opts.DisableAccel {
args = append(args, "--disable-acceleration")
}
if opts.PrefixName {
args = append(args, "--prefix-name")
}
if opts.WithKernelDbg {
args = append(args, "--symbolic")
}
if opts.Remove {
args = append(args, "--rm")
}
if len(opts.Name) > 0 {
args = append(args, "--name", opts.Name)
}
if len(opts.Architecture) > 0 {
args = append(args, "--arch", opts.Architecture)
}
if len(opts.RunAs) > 0 {
args = append(args, "--as", opts.RunAs)
}
if len(opts.KernelArgs) > 0 {
args = append(args, "--kernel-arg", strings.Join(opts.KernelArgs, " "))
}
if len(opts.Kraftfile) > 0 {
args = append(args, "--kraftfile", opts.Kraftfile)
}
if len(opts.MacAddress) > 0 {
args = append(args, "--mac", opts.MacAddress)
}
if len(opts.Memory) > 0 {
args = append(args, "--memory", opts.Memory)
}
if len(opts.Networks) > 0 {
args = append(args, "--network", strings.Join(opts.Networks, " "))
}
if len(opts.Platform) > 0 {
args = append(args, "--plat", opts.Platform)
}
if len(opts.Ports) > 0 {
args = append(args, "--port", strings.Join(opts.Ports, " "))
}
if len(opts.Prefix) > 0 {
args = append(args, "--prefix", opts.Prefix)
}
if len(opts.Rootfs) > 0 {
args = append(args, "--rootfs", opts.Rootfs)
}
if len(opts.Target) > 0 {
args = append(args, "--target ", opts.Target)
}
if len(opts.Volumes) > 0 {
args = append(args, "--volume", strings.Join(opts.Volumes, " "))
}
return args
}
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 7e6a246

Please sign in to comment.