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

Fix full stemcell create flow #50

Merged
merged 5 commits into from
Jan 3, 2019
Merged
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
34 changes: 25 additions & 9 deletions src/bosh-alicloud-cpi/action/create_stemcell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import (
"strconv"
"strings"

"bytes"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"github.com/cppforlife/bosh-cpi-go/apiv1"
"github.com/google/uuid"
"os/exec"
"path"
)

const (
AlicloudImageNamePrefix = "stemcell"
MinImageDiskSize = 5 //in GB
OSS_BUCKET_NAME_MAX_LENGTH = 64
PART_SIZE = 5 * 1024 * 1024
)

type StemcellProps struct {
Expand All @@ -29,10 +33,10 @@ type StemcellProps struct {
Disk interface{} `json:"disk"`
DiskFormat string `json:"disk_format"`
diskGB int
Hypervisor string `json:"hypervisor"`
Name string `json:"name"`
OsDistro string `json:"os_distro"`
OsType string `json:"os_type"`
Hypervisor string `json:"hypervisor"`
Name string `json:"name"`
OsDistro string `json:"os_distro"`
OsType string `json:"os_type"`
//RootDeviceName string `json:"root_device_name"`
SourceUrl string `json:"source_url"`
//SourceSha1 string `json:"raw_disk_sha1,omitempty"`
Expand Down Expand Up @@ -186,26 +190,37 @@ func (a CreateStemcellMethod) importImage(props StemcellProps) (string, error) {
func (a CreateStemcellMethod) CreateFromTarball(imagePath string, props StemcellProps) (string, error) {
imageName := fmt.Sprintf("%s-%s.raw", AlicloudImageNamePrefix, a.getUUIDName(props))
bucketName := fmt.Sprintf("%s-%s", alicloud.AlicloudDefaultImageName, uuid.New().String())

if len(bucketName) > OSS_BUCKET_NAME_MAX_LENGTH {
bucketName = bucketName[0:OSS_BUCKET_NAME_MAX_LENGTH]
}

var out bytes.Buffer
var stderr bytes.Buffer
alexwo marked this conversation as resolved.
Show resolved Hide resolved

defer a.osses.DeleteBucket(bucketName)

if err := a.osses.CreateBucket(bucketName, oss.ACL(oss.ACLPublicRead)); err != nil {
return "", bosherr.WrapErrorf(err, "Creating Alicloud OSS Bucket")
}
defer a.osses.DeleteBucket(bucketName)

bucket, err := a.osses.GetBucket(bucketName)

if err != nil {
return "", bosherr.WrapErrorf(err, "Geting oss bucket")
}

imageFile, err := a.stemcells.OpenLocalFile(imagePath)
cmd := exec.Command("tar", "-xf", imagePath)
cmd.Dir = path.Dir(imagePath)
cmd.Stdout = &out
cmd.Stderr = &stderr
err = cmd.Run()
alexwo marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return "", bosherr.WrapErrorf(err, "Reading stemcell image file from local")
return "", bosherr.WrapErrorf(err, fmt.Sprintf("%s-(%s)-(%s)", "Unable to extract image", out.String(), stderr.String()))
}
defer imageFile.Close()

err = a.osses.UploadFile(*bucket, imageName, imagePath, 100*1024, oss.Routines(5))
err = a.osses.UploadFile(*bucket, imageName, fmt.Sprintf("%s/%s", path.Dir(imagePath), "root.img"), PART_SIZE, oss.Routines(5))
if err != nil {
return "", bosherr.WrapErrorf(err, "Uploading stemcell image file to oss")
}
Expand All @@ -217,6 +232,7 @@ func (a CreateStemcellMethod) CreateFromTarball(imagePath string, props Stemcell
if err != nil {
return "", bosherr.WrapErrorf(err, "Creating Alicloud Image from Tarball")
}

return image, err
}

Expand Down