Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Factor out the ignite container command into a separate ignite-spawn …
Browse files Browse the repository at this point in the history
…binary
  • Loading branch information
luxas committed Jul 1, 2019
1 parent f39433e commit 0a1965e
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 289 deletions.
67 changes: 67 additions & 0 deletions cmd/ignite-spawn/ignite-spawn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"fmt"
"os"

"github.com/weaveworks/ignite/pkg/container"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
)

func main() {
if err := Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

// Run runs the main cobra command of this application
func Run() error {
if len(os.Args) != 1 {
fmt.Printf("Usage: ignite-spawn [VM ID]")
os.Exit(0)
}

vmID := os.Args[0]
opts, err := NewOptions(loader.NewResLoader(), vmID)
if err != nil {
return err
}

return StartVM(opts)
}

func StartVM(co *options) error {
// Setup networking inside of the container, return the available interfaces
dhcpIfaces, err := container.SetupContainerNetworking()
if err != nil {
return fmt.Errorf("network setup failed: %v", err)
}
// Serve DHCP requests for those interfaces
if err := container.StartDHCPServers(co.vm, dhcpIfaces); err != nil {
return err
}

// VM state handling
if err := co.vm.SetState(vmmd.Running); err != nil {
return fmt.Errorf("failed to update VM state: %v", err)
}
defer co.vm.SetState(vmmd.Stopped) // Performs a save, all other metadata-modifying defers need to be after this

// Remove the snapshot overlay post-run, which also removes the detached backing loop devices
defer co.vm.RemoveSnapshot()

// Remove the IP addresses post-run
defer co.vm.ClearIPAddresses()

// Remove the port mappings post-run
defer co.vm.ClearPortMappings()

// Execute Firecracker
if err := container.ExecuteFirecracker(co.vm, dhcpIfaces); err != nil {
return fmt.Errorf("runtime error for VM %q: %v", co.vm.ID, err)
}

return nil
}
24 changes: 24 additions & 0 deletions cmd/ignite-spawn/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
)

type options struct {
vm *vmmd.VMMetadata
}

func NewOptions(l *loader.ResLoader, vmMatch string) (*options, error) {
co := &options{}

if allVMS, err := l.VMs(); err == nil {
if co.vm, err = allVMS.MatchSingle(vmMatch); err != nil {
return nil, err
}
} else {
return nil, err
}

return co, nil
}
32 changes: 0 additions & 32 deletions cmd/ignite/cmd/container.go

This file was deleted.

8 changes: 7 additions & 1 deletion cmd/ignite/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/lithammer/dedent"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// NewIgniteCommand returns the root command for ignite
Expand Down Expand Up @@ -70,7 +71,7 @@ func NewIgniteCommand(in io.Reader, out, err io.Writer) *cobra.Command {
`, imageCmd.Short, kernelCmd.Short, vmCmd.Short)),
}

logs.AddQuietFlag(root.PersistentFlags())
AddQuietFlag(root.PersistentFlags())

root.AddCommand(imageCmd)
root.AddCommand(kernelCmd)
Expand All @@ -92,3 +93,8 @@ func NewIgniteCommand(in io.Reader, out, err io.Writer) *cobra.Command {
root.AddCommand(NewCmdVersion(os.Stdout))
return root
}

// AddQuietFlag adds the quiet flag to a flagset
func AddQuietFlag(fs *pflag.FlagSet) {
fs.BoolVarP(&logs.Quiet, "quiet", "q", logs.Quiet, "The quiet mode allows for machine-parsable output, by printing only IDs")
}
2 changes: 1 addition & 1 deletion cmd/ignite/cmd/vmcmd/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"io"

"github.com/weaveworks/ignite/cmd/ignite/run"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/errutils"
"github.com/weaveworks/ignite/pkg/metadata/loader"

"github.com/lithammer/dedent"
"github.com/spf13/cobra"
Expand Down
95 changes: 0 additions & 95 deletions cmd/ignite/run/container.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/ignite/run/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/c2h5oh/datasize"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/imgmd"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/util"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/ignite/run/kernels.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/c2h5oh/datasize"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/kernmd"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/util"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/ignite/run/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"

"github.com/c2h5oh/datasize"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/imgmd"
"github.com/weaveworks/ignite/pkg/metadata/kernmd"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/util"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ignite/run/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package run
import (
"fmt"

"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/logs"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/ignite/run/run.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package run

import (
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata"
"github.com/weaveworks/ignite/pkg/metadata/loader"
)

type RunFlags struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ignite/run/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package run
import (
"fmt"

"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/metadata/loader"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/util"
)
Expand Down
53 changes: 48 additions & 5 deletions pkg/container/dhcp.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
package container

import (
"fmt"
"net"
"os"
"time"

"github.com/weaveworks/ignite/pkg/constants"

dhcp "github.com/krolaw/dhcp4"
"github.com/krolaw/dhcp4/conn"
"github.com/miekg/dns"
"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/util"
)

var leaseDuration, _ = time.ParseDuration(constants.DHCP_INFINITE_LEASE) // Infinite lease time

// StartDHCPServers starts multiple DHCP servers for the VM, one per interface
func StartDHCPServers(vm *vmmd.VMMetadata, dhcpIfaces []DHCPInterface) error {
// Generate the MAC addresses for the VM's adapters
macAddresses := make([]string, 0, len(dhcpIfaces))
if err := util.NewMAC(&macAddresses); err != nil {
return fmt.Errorf("failed to generate MAC addresses: %v", err)
}

// Fetch the DNS servers given to the container
clientConfig, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return fmt.Errorf("failed to get DNS configuration: %v", err)
}

for i := range dhcpIfaces {
dhcpIface := &dhcpIfaces[i]
// Set the VM hostname to the VM ID
dhcpIface.Hostname = vm.ID.String()

// Set the MAC address filter for the DHCP server
dhcpIface.MACFilter = macAddresses[i]

// Add the DNS servers from the container
dhcpIface.SetDNSServers(clientConfig.Servers)

vm.AddIPAddress(dhcpIface.VMIPNet.IP)

go func() {
fmt.Printf("Starting DHCP server for interface %s (%s)\n", dhcpIface.Bridge, dhcpIface.VMIPNet.IP)
if err := dhcpIface.StartBlockingServer(); err != nil {
fmt.Fprintf(os.Stderr, "%s DHCP server error: %v\n", dhcpIface.Bridge, err)
}
}()
}
return nil
}

type DHCPInterface struct {
VMIPNet *net.IPNet
GatewayIP *net.IP
Expand All @@ -22,14 +63,16 @@ type DHCPInterface struct {
dnsServers []byte
}

func RunDHCP(dhcpIface *DHCPInterface) error {
packetConn, err := conn.NewUDP4BoundListener(dhcpIface.Bridge, ":67")
// StartBlockingServer starts a blocking DHCP server on port 67
func (i *DHCPInterface) StartBlockingServer() error {
packetConn, err := conn.NewUDP4BoundListener(i.Bridge, ":67")
if err != nil {
return err
}
return dhcp.Serve(packetConn, dhcpIface)
return dhcp.Serve(packetConn, i)
}

// ServeDHCP responds to a DHCP request
func (i *DHCPInterface) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) dhcp.Packet {
var respMsg dhcp.MessageType
switch msgType {
Expand Down
Loading

0 comments on commit 0a1965e

Please sign in to comment.