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

Create/use a runtime interface instead of direct calls to Docker #211

Merged
merged 12 commits into from
Jul 18, 2019
Merged
9 changes: 7 additions & 2 deletions cmd/ignite-spawn/ignite-spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"os"
"path"

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

log "github.com/sirupsen/logrus"
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/container"
"github.com/weaveworks/ignite/pkg/container/prometheus"
"github.com/weaveworks/ignite/pkg/logs"
"github.com/weaveworks/ignite/pkg/providers"
)

func main() {
Expand All @@ -23,6 +23,11 @@ func main() {

// Run runs the main cobra command of this application
func Run() error {
// Populate the providers
if err := providers.Populate(); err != nil {
return err
}

if len(os.Args) != 2 {
fmt.Printf("Usage: ignite-spawn [VM ID]")
os.Exit(0)
Expand Down
1 change: 0 additions & 1 deletion cmd/ignite/cmd/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"

"github.com/lithammer/dedent"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/weaveworks/ignite/pkg/errutils"
Expand Down
1 change: 0 additions & 1 deletion cmd/ignite/cmd/kerncmd/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"

"github.com/lithammer/dedent"

"github.com/spf13/cobra"
"github.com/weaveworks/ignite/cmd/ignite/run"
"github.com/weaveworks/ignite/pkg/errutils"
Expand Down
3 changes: 1 addition & 2 deletions cmd/ignite/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package cmd
import (
"io"

"github.com/weaveworks/ignite/cmd/ignite/cmd/vmcmd"

"github.com/spf13/cobra"
"github.com/weaveworks/ignite/cmd/ignite/cmd/vmcmd"
)

// NewCmdLogs is an alias for vmcmd.NewCmdLogs
Expand Down
3 changes: 1 addition & 2 deletions cmd/ignite/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package cmd
import (
"io"

"github.com/weaveworks/ignite/cmd/ignite/run"

"github.com/spf13/cobra"
"github.com/weaveworks/ignite/cmd/ignite/run"
"github.com/weaveworks/ignite/pkg/errutils"
)

Expand Down
3 changes: 1 addition & 2 deletions cmd/ignite/cmd/vmcmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"io"

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

"github.com/lithammer/dedent"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/weaveworks/ignite/cmd/ignite/cmd/cmdutil"
"github.com/weaveworks/ignite/cmd/ignite/run"
"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/errutils"
)

Expand Down
6 changes: 6 additions & 0 deletions cmd/ignite/ignite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/weaveworks/ignite/cmd/ignite/cmd"
"github.com/weaveworks/ignite/pkg/providers"
)

func main() {
Expand All @@ -17,6 +18,11 @@ func main() {

// Run runs the main cobra command of this application
func Run() error {
// Populate the providers
if err := providers.Populate(); err != nil {
return err
}

c := cmd.NewIgniteCommand(os.Stdin, os.Stdout, os.Stderr)
return c.Execute()
}
2 changes: 2 additions & 0 deletions cmd/ignite/run/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func Attach(ao *attachOptions) error {
}

// Attach to the VM in Docker
// TODO: Implement the pseudo-TTY and remove this call, see
// https://github.com/weaveworks/ignite/pull/211#issuecomment-512809841
if ec, err := util.ExecForeground("docker", dockerArgs...); err != nil {
if ec != 1 { // Docker's detach sequence (^P^Q) has an exit code of -1
return fmt.Errorf("failed to attach to container for VM %s: %v", ao.vm.GetUID(), err)
Expand Down
8 changes: 4 additions & 4 deletions cmd/ignite/run/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/apis/ignite/scheme"
meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/metadata"
"github.com/weaveworks/ignite/pkg/metadata/imgmd"
"github.com/weaveworks/ignite/pkg/metadata/kernmd"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/operations"
"github.com/weaveworks/ignite/pkg/providers"
)

func NewCreateFlags() *CreateFlags {
Expand Down Expand Up @@ -98,7 +98,7 @@ func (cf *CreateFlags) NewCreateOptions(args []string) (*createOptions, error) {

// Get the image, or import it if it doesn't exist
var err error
co.image, err = operations.FindOrImportImage(client.DefaultClient, cf.VM.Spec.Image.OCIClaim.Ref)
co.image, err = operations.FindOrImportImage(providers.Client, cf.VM.Spec.Image.OCIClaim.Ref)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +107,7 @@ func (cf *CreateFlags) NewCreateOptions(args []string) (*createOptions, error) {
cf.VM.SetImage(co.image.Image)

// Get the kernel, or import it if it doesn't exist
co.kernel, err = operations.FindOrImportKernel(client.DefaultClient, cf.VM.Spec.Kernel.OCIClaim.Ref)
co.kernel, err = operations.FindOrImportKernel(providers.Client, cf.VM.Spec.Kernel.OCIClaim.Ref)
if err != nil {
return nil, err
}
Expand All @@ -120,7 +120,7 @@ func (cf *CreateFlags) NewCreateOptions(args []string) (*createOptions, error) {
func Create(co *createOptions) error {
// Create new metadata for the VM
var err error
if co.newVM, err = vmmd.NewVM(co.VM, client.DefaultClient); err != nil {
if co.newVM, err = vmmd.NewVM(co.VM, providers.Client); err != nil {
return err
}
defer metadata.Cleanup(co.newVM, false) // TODO: Handle silent
Expand Down
6 changes: 3 additions & 3 deletions cmd/ignite/run/helpers.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package run

import (
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/filter"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/providers"
)

// TODO: This
func getVMForMatch(vmMatch string) (*vmmd.VM, error) {
apiVM, err := client.VMs().Find(filter.NewIDNameFilter(vmMatch))
apiVM, err := providers.Client.VMs().Find(filter.NewIDNameFilter(vmMatch))
if err != nil {
return nil, err
}
Expand All @@ -29,7 +29,7 @@ func getVMsForMatches(vmMatches []string) ([]*vmmd.VM, error) {
}

func getAllVMs() (allVMs []*vmmd.VM, err error) {
allAPIVMs, err := client.VMs().FindAll(filter.NewAllFilter())
allAPIVMs, err := providers.Client.VMs().FindAll(filter.NewAllFilter())
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ignite/run/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package run

import (
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/filter"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/util"
)

Expand All @@ -13,7 +13,7 @@ type imagesOptions struct {

func NewImagesOptions() (io *imagesOptions, err error) {
io = &imagesOptions{}
io.allImages, err = client.Images().FindAll(filter.NewAllFilter())
io.allImages, err = providers.Client.Images().FindAll(filter.NewAllFilter())
return
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/ignite/run/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package run

import (
meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/metadata"
"github.com/weaveworks/ignite/pkg/metadata/imgmd"
"github.com/weaveworks/ignite/pkg/metadata/kernmd"
"github.com/weaveworks/ignite/pkg/operations"
"github.com/weaveworks/ignite/pkg/providers"
)

func ImportImage(source string) (*imgmd.Image, error) {
Expand All @@ -15,7 +15,7 @@ func ImportImage(source string) (*imgmd.Image, error) {
return nil, err
}

runImage, err := operations.FindOrImportImage(client.DefaultClient, ociRef)
runImage, err := operations.FindOrImportImage(providers.Client, ociRef)
if err != nil {
return nil, err
}
Expand All @@ -30,7 +30,7 @@ func ImportKernel(source string) (*kernmd.Kernel, error) {
return nil, err
}

runKernel, err := operations.FindOrImportKernel(client.DefaultClient, ociRef)
runKernel, err := operations.FindOrImportKernel(providers.Client, ociRef)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ignite/run/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/apis/ignite/scheme"
meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/filter"
"github.com/weaveworks/ignite/pkg/providers"
)

type InspectFlags struct {
Expand Down Expand Up @@ -37,7 +37,7 @@ func (i *InspectFlags) NewInspectOptions(k, objectMatch string) (*inspectOptions
return nil, fmt.Errorf("unrecognized kind: %q", k)
}

if io.object, err = client.Dynamic(kind).Find(filter.NewIDNameFilter(objectMatch)); err != nil {
if io.object, err = providers.Client.Dynamic(kind).Find(filter.NewIDNameFilter(objectMatch)); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ignite/run/kernels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package run

import (
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/filter"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/util"
)

Expand All @@ -13,7 +13,7 @@ type kernelsOptions struct {

func NewKernelsOptions() (ko *kernelsOptions, err error) {
ko = &kernelsOptions{}
ko.allKernels, err = client.Kernels().FindAll(filter.NewAllFilter())
ko.allKernels, err = providers.Client.Kernels().FindAll(filter.NewAllFilter())
return
}

Expand Down
20 changes: 11 additions & 9 deletions cmd/ignite/run/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package run

import (
"fmt"
"io/ioutil"

"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/util"
)

Expand All @@ -24,18 +25,19 @@ func Logs(lo *logsOptions) error {
return fmt.Errorf("VM %q is not running", lo.vm.GetUID())
}

dockerArgs := []string{
"logs",
constants.IGNITE_PREFIX + lo.vm.GetUID().String(),
// Fetch the VM logs
rc, err := providers.Runtime.ContainerLogs(util.NewPrefixer().Prefix(lo.vm.GetUID()))
if err != nil {
return fmt.Errorf("failed to get logs for VM %q: %v", lo.vm.GetUID(), err)
}

// Fetch the VM logs from docker
output, err := util.ExecuteCommand("docker", dockerArgs...)
// Read the stream to a byte buffer
b, err := ioutil.ReadAll(rc)
if err != nil {
return fmt.Errorf("failed to get logs for VM %q: %v", lo.vm.GetUID(), err)
return err
}

// Print the ID and the VM logs
fmt.Println(output)
// Print the logs
fmt.Printf("%s\n", b)
return nil
}
4 changes: 2 additions & 2 deletions cmd/ignite/run/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package run

import (
api "github.com/weaveworks/ignite/pkg/apis/ignite"
"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/filter"
"github.com/weaveworks/ignite/pkg/providers"
"github.com/weaveworks/ignite/pkg/util"
)

Expand All @@ -18,7 +18,7 @@ type psOptions struct {

func (pf *PsFlags) NewPsOptions() (po *psOptions, err error) {
po = &psOptions{PsFlags: pf}
po.allVMs, err = client.VMs().FindAll(filter.NewVMFilterAll("", po.All))
po.allVMs, err = providers.Client.VMs().FindAll(filter.NewVMFilterAll("", po.All))
return
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ignite/run/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package run
import (
"fmt"

"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/operations"
"github.com/weaveworks/ignite/pkg/providers"
)

type RmFlags struct {
Expand All @@ -31,7 +31,7 @@ func Rm(ro *rmOptions) error {
}

// This will first kill the VM container, and then remove it
if err := operations.RemoveVM(client.DefaultClient, vm); err != nil {
if err := operations.RemoveVM(providers.Client, vm); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ignite/run/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"os"

"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/filter"
"github.com/weaveworks/ignite/pkg/metadata/imgmd"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/providers"
)

type RmiFlags struct {
Expand All @@ -24,7 +24,7 @@ func (rf *RmiFlags) NewRmiOptions(imageMatches []string) (*rmiOptions, error) {
ro := &rmiOptions{RmiFlags: rf}

for _, match := range imageMatches {
if image, err := client.Images().Find(filter.NewIDNameFilter(match)); err == nil {
if image, err := providers.Client.Images().Find(filter.NewIDNameFilter(match)); err == nil {
ro.images = append(ro.images, imgmd.WrapImage(image))
} else {
return nil, err
Expand Down
5 changes: 2 additions & 3 deletions cmd/ignite/run/rmk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"fmt"
"os"

"github.com/weaveworks/ignite/pkg/client"
"github.com/weaveworks/ignite/pkg/filter"

"github.com/weaveworks/ignite/pkg/metadata/kernmd"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/providers"
)

type RmkFlags struct {
Expand All @@ -25,7 +24,7 @@ func (rf *RmkFlags) NewRmkOptions(kernelMatches []string) (*rmkOptions, error) {
ro := &rmkOptions{RmkFlags: rf}

for _, match := range kernelMatches {
if kernel, err := client.Kernels().Find(filter.NewIDNameFilter(match)); err == nil {
if kernel, err := providers.Client.Kernels().Find(filter.NewIDNameFilter(match)); err == nil {
ro.kernels = append(ro.kernels, kernmd.WrapKernel(kernel))
} else {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion cmd/ignite/run/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"path"

log "github.com/sirupsen/logrus"

"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/metadata/vmmd"
"github.com/weaveworks/ignite/pkg/util"
Expand Down
Loading