Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce systemd (WIP) #1537

Draft
wants to merge 2 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 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
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,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 @@ -1429,6 +1431,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
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")
}
Comment on lines +463 to +471
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out: https://pkg.go.dev/reflect#StructTag.Get

This will simplify getting the flag name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it 👍🏽

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
}
21 changes: 21 additions & 0 deletions machine/platform/systemd/manage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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() 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
}
61 changes: 61 additions & 0 deletions machine/platform/systemd/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package systemd

import "github.com/kardianos/service"

type ServiceConfigOption func(*ServiceConfig) error

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

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

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

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

// 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
return nil
}
}
Loading
Loading