Skip to content

Commit

Permalink
Merge pull request nanovms#676 from fabiodmferreira-work/gcp-mounting
Browse files Browse the repository at this point in the history
Gcp mounting
  • Loading branch information
fabioDMFerreira authored Oct 9, 2020
2 parents 8eebc72 + addc55b commit 4bbf227
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 165 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ ops.exe
/Makefile.local
vendor/
.idea

config*.json
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package cmd

import (
"fmt"
"github.com/go-errors/errors"
"log"
"os"
"path"
"strconv"
"strings"

"github.com/go-errors/errors"

api "github.com/nanovms/ops/lepton"
"github.com/spf13/cobra"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func getCloudProvider(providerName string) (api.Provider, error) {

switch providerName {
case "gcp":
provider = &api.GCloud{}
provider = api.NewGCloud()
case "onprem":
provider = &api.OnPrem{}
case "aws":
Expand Down
36 changes: 35 additions & 1 deletion cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func volumeAttachCommandHandler(cmd *cobra.Command, args []string) {
name := args[1]
mount := args[2]
config, _ := cmd.Flags().GetString("config")
provider, _ := cmd.Flags().GetString("provider")
provider, _ := cmd.Flags().GetString("target-cloud")
conf := unWarpConfig(config)

var vol api.VolumeService
Expand All @@ -157,6 +157,29 @@ func volumeAttachCommandHandler(cmd *cobra.Command, args []string) {
}
}

func volumeDetachCommandHandler(cmd *cobra.Command, args []string) {
image := args[0]
name := args[1]
config, _ := cmd.Flags().GetString("config")
provider, _ := cmd.Flags().GetString("target-cloud")
conf := unWarpConfig(config)

var vol api.VolumeService
var err error
if provider == "onprem" {
vol = &api.OnPrem{}
} else {
vol, err = getCloudProvider(provider)
if err != nil {
log.Fatal(err)
}
}
err = vol.DetachVolume(conf, image, name)
if err != nil {
log.Fatal(err)
}
}

func volumeAttachCommand() *cobra.Command {
cmdVolumeAttach := &cobra.Command{
Use: "attach <image_name> <volume_name> <mount_path>",
Expand All @@ -167,6 +190,16 @@ func volumeAttachCommand() *cobra.Command {
return cmdVolumeAttach
}

func volumeDetachCommand() *cobra.Command {
cmdVolumeDetach := &cobra.Command{
Use: "detach <image_name> <volume_name>",
Short: "detach volume",
Run: volumeDetachCommandHandler,
Args: cobra.MinimumNArgs(2),
}
return cmdVolumeDetach
}

// VolumeCommands handles volumes related operations
func VolumeCommands() *cobra.Command {
var config, provider string
Expand All @@ -184,5 +217,6 @@ func VolumeCommands() *cobra.Command {
cmdVolume.AddCommand(volumeListCommand())
cmdVolume.AddCommand(volumeDeleteCommand())
cmdVolume.AddCommand(volumeAttachCommand())
cmdVolume.AddCommand(volumeDetachCommand())
return cmdVolume
}
132 changes: 40 additions & 92 deletions lepton/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -18,6 +19,11 @@ import (
compute "google.golang.org/api/compute/v1"
)

var (
errGCloudProjectIDMissing = func() error { return errors.New("projectid is missing. Please set env variable GCLOUD_PROJECT_ID") }
errGCloudZoneMissing = func() error { return errors.New("zone is missing. Please set env variable GCLOUD_ZONE") }
)

// GCloudOperation status check
type GCloudOperation struct {
service *compute.Service
Expand All @@ -27,7 +33,7 @@ type GCloudOperation struct {
operationType string
}

func checkCredentialsProvided() error {
func checkGCCredentialsProvided() error {
creds, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
if !ok {
return fmt.Errorf(ErrorColor, "error: GOOGLE_APPLICATION_CREDENTIALS not set.\nFollow https://cloud.google.com/storage/docs/reference/libraries to set it up.\n")
Expand Down Expand Up @@ -70,7 +76,15 @@ func (gop *GCloudOperation) isDone(ctx context.Context) (bool, error) {

// GCloud contains all operations for GCP
type GCloud struct {
Storage *GCPStorage
Storage *GCPStorage
Service *compute.Service
ProjectID string
Zone string
}

// NewGCloud returns an instance of GCloud
func NewGCloud() *GCloud {
return &GCloud{}
}

func (p *GCloud) getArchiveName(ctx *Context) string {
Expand Down Expand Up @@ -172,19 +186,12 @@ func (p *GCloud) BuildImageWithPackage(ctx *Context, pkgpath string) (string, er
// Initialize GCP related things
func (p *GCloud) Initialize() error {
p.Storage = &GCPStorage{}
return nil
}

// CreateImage - Creates image on GCP using nanos images
// TODO : re-use and cache DefaultClient and instances.
func (p *GCloud) CreateImage(ctx *Context) error {
if err := checkCredentialsProvided(); err != nil {
if err := checkGCCredentialsProvided(); err != nil {
return err
}

c := ctx.config
context := context.TODO()
client, err := google.DefaultClient(context, compute.CloudPlatformScope)
client, err := google.DefaultClient(context.Background(), compute.CloudPlatformScope)
if err != nil {
return err
}
Expand All @@ -194,6 +201,17 @@ func (p *GCloud) CreateImage(ctx *Context) error {
return err
}

p.Service = computeService

return nil
}

// CreateImage - Creates image on GCP using nanos images
// TODO : re-use and cache DefaultClient and instances.
func (p *GCloud) CreateImage(ctx *Context) error {
c := ctx.config
context := context.TODO()

sourceURL := fmt.Sprintf(GCPStorageURL,
c.CloudConfig.BucketName, p.getArchiveName(ctx))

Expand All @@ -204,12 +222,12 @@ func (p *GCloud) CreateImage(ctx *Context) error {
},
}

op, err := computeService.Images.Insert(c.CloudConfig.ProjectID, rb).Context(context).Do()
op, err := p.Service.Images.Insert(c.CloudConfig.ProjectID, rb).Context(context).Do()
if err != nil {
return fmt.Errorf("error:%+v", err)
}
fmt.Printf("Image creation started. Monitoring operation %s.\n", op.Name)
err = p.pollOperation(context, c.CloudConfig.ProjectID, computeService, *op)
err = p.pollOperation(context, c.CloudConfig.ProjectID, p.Service, *op)
if err != nil {
return err
}
Expand All @@ -219,9 +237,6 @@ func (p *GCloud) CreateImage(ctx *Context) error {

// GetImages return all images on GCloud
func (p *GCloud) GetImages(ctx *Context) ([]CloudImage, error) {
if err := checkCredentialsProvided(); err != nil {
return nil, err
}
context := context.TODO()
creds, err := google.FindDefaultCredentials(context)
if err != nil {
Expand Down Expand Up @@ -285,9 +300,6 @@ func (p *GCloud) ListImages(ctx *Context) error {

// DeleteImage deletes image from Gcloud
func (p *GCloud) DeleteImage(ctx *Context, imagename string) error {
if err := checkCredentialsProvided(); err != nil {
return err
}
context := context.TODO()
creds, err := google.FindDefaultCredentials(context)
if err != nil {
Expand Down Expand Up @@ -321,10 +333,6 @@ func (p *GCloud) SyncImage(config *Config, target Provider, image string) error

// CreateInstance - Creates instance on Google Cloud Platform
func (p *GCloud) CreateInstance(ctx *Context) error {
if err := checkCredentialsProvided(); err != nil {
return err
}

context := context.TODO()
creds, err := google.FindDefaultCredentials(context)
if err != nil {
Expand Down Expand Up @@ -449,24 +457,10 @@ func (p *GCloud) ListInstances(ctx *Context) error {

// GetInstances return all instances on GCloud
func (p *GCloud) GetInstances(ctx *Context) ([]CloudInstance, error) {
if err := checkCredentialsProvided(); err != nil {
return nil, err
}

context := context.TODO()
client, err := google.DefaultClient(context, compute.CloudPlatformScope)
if err != nil {
return nil, err
}

computeService, err := compute.New(client)
if err != nil {
return nil, err
}

var (
cinstances []CloudInstance
req = computeService.Instances.List(ctx.config.CloudConfig.ProjectID, ctx.config.CloudConfig.Zone)
req = p.Service.Instances.List(ctx.config.CloudConfig.ProjectID, ctx.config.CloudConfig.Zone)
)

if err := req.Pages(context, func(page *compute.InstanceList) error {
Expand Down Expand Up @@ -505,25 +499,14 @@ func (p *GCloud) GetInstances(ctx *Context) ([]CloudInstance, error) {

// DeleteInstance deletes instance from Gcloud
func (p *GCloud) DeleteInstance(ctx *Context, instancename string) error {
if err := checkCredentialsProvided(); err != nil {
return err
}
context := context.TODO()
client, err := google.DefaultClient(context, compute.CloudPlatformScope)
if err != nil {
return err
}
computeService, err := compute.New(client)
if err != nil {
return err
}
cloudConfig := ctx.config.CloudConfig
op, err := computeService.Instances.Delete(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Context(context).Do()
op, err := p.Service.Instances.Delete(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Context(context).Do()
if err != nil {
return err
}
fmt.Printf("Instance deletion started. Monitoring operation %s.\n", op.Name)
err = p.pollOperation(context, cloudConfig.ProjectID, computeService, *op)
err = p.pollOperation(context, cloudConfig.ProjectID, p.Service, *op)
if err != nil {
return err
}
Expand All @@ -533,29 +516,17 @@ func (p *GCloud) DeleteInstance(ctx *Context, instancename string) error {

// StartInstance starts an instance in GCloud
func (p *GCloud) StartInstance(ctx *Context, instancename string) error {
if err := checkCredentialsProvided(); err != nil {
return err
}

context := context.TODO()
client, err := google.DefaultClient(context, compute.CloudPlatformScope)
if err != nil {
return err
}

computeService, err := compute.New(client)
if err != nil {
return err
}

cloudConfig := ctx.config.CloudConfig
op, err := computeService.Instances.Start(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Context(context).Do()
op, err := p.Service.Instances.Start(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Context(context).Do()
if err != nil {
return err
}

fmt.Printf("Instance started. Monitoring operation %s.\n", op.Name)
err = p.pollOperation(context, cloudConfig.ProjectID, computeService, *op)
err = p.pollOperation(context, cloudConfig.ProjectID, p.Service, *op)
if err != nil {
return err
}
Expand All @@ -567,29 +538,16 @@ func (p *GCloud) StartInstance(ctx *Context, instancename string) error {

// StopInstance deletes instance from GCloud
func (p *GCloud) StopInstance(ctx *Context, instancename string) error {
if err := checkCredentialsProvided(); err != nil {
return err
}

context := context.TODO()
client, err := google.DefaultClient(context, compute.CloudPlatformScope)
if err != nil {
return err
}

computeService, err := compute.New(client)
if err != nil {
return err
}

cloudConfig := ctx.config.CloudConfig
op, err := computeService.Instances.Stop(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Context(context).Do()
op, err := p.Service.Instances.Stop(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Context(context).Do()
if err != nil {
return err
}

fmt.Printf("Instance stopping started. Monitoring operation %s.\n", op.Name)
err = p.pollOperation(context, cloudConfig.ProjectID, computeService, *op)
err = p.pollOperation(context, cloudConfig.ProjectID, p.Service, *op)
if err != nil {
return err
}
Expand All @@ -600,22 +558,12 @@ func (p *GCloud) StopInstance(ctx *Context, instancename string) error {

// GetInstanceLogs gets instance related logs
func (p *GCloud) GetInstanceLogs(ctx *Context, instancename string, watch bool) error {
if err := checkCredentialsProvided(); err != nil {
return err
}
context := context.TODO()
client, err := google.DefaultClient(context, compute.CloudPlatformScope)
if err != nil {
return err
}
computeService, err := compute.New(client)
if err != nil {
return err
}

cloudConfig := ctx.config.CloudConfig
lastPos := int64(0)
for {
resp, err := computeService.Instances.GetSerialPortOutput(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Start(lastPos).Context(context).Do()
resp, err := p.Service.Instances.GetSerialPortOutput(cloudConfig.ProjectID, cloudConfig.Zone, instancename).Start(lastPos).Context(context).Do()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4bbf227

Please sign in to comment.