internal/cloud: use logrus for logging

and log upload & sharing failures
This commit is contained in:
Diaa Sami 2021-12-13 17:39:59 +01:00 committed by Tom Gundersen
parent 8f80dd06d6
commit 487e2d0669

View file

@ -2,7 +2,6 @@ package awscloud
import (
"fmt"
"log"
"os"
"time"
@ -13,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/sirupsen/logrus"
)
type AWS struct {
@ -63,7 +63,7 @@ func (a *AWS) Upload(filename, bucket, key string) (*s3manager.UploadOutput, err
return nil, err
}
log.Printf("[AWS] 🚀 Uploading image to S3: %s/%s", bucket, key)
logrus.Infof("[AWS] 🚀 Uploading image to S3: %s/%s", bucket, key)
return a.uploader.Upload(
&s3manager.UploadInput{
Bucket: aws.String(bucket),
@ -144,7 +144,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
return nil, fmt.Errorf("ec2 doesn't support the following arch: %s", rpmArch)
}
log.Printf("[AWS] 📥 Importing snapshot from image: %s/%s", bucket, key)
logrus.Infof("[AWS] 📥 Importing snapshot from image: %s/%s", bucket, key)
snapshotDescription := fmt.Sprintf("Image Builder AWS Import of %s", name)
importTaskOutput, err := a.ec2.ImportSnapshot(
&ec2.ImportSnapshotInput{
@ -158,11 +158,11 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
},
)
if err != nil {
log.Printf("[AWS] error importing snapshot: %s", err)
logrus.Warnf("[AWS] error importing snapshot: %s", err)
return nil, err
}
log.Printf("[AWS] 🚚 Waiting for snapshot to finish importing: %s", *importTaskOutput.ImportTaskId)
logrus.Infof("[AWS] 🚚 Waiting for snapshot to finish importing: %s", *importTaskOutput.ImportTaskId)
err = WaitUntilImportSnapshotTaskCompleted(
a.ec2,
&ec2.DescribeImportSnapshotTasksInput{
@ -176,7 +176,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
}
// we no longer need the object in s3, let's just delete it
log.Printf("[AWS] 🧹 Deleting image from S3: %s/%s", bucket, key)
logrus.Infof("[AWS] 🧹 Deleting image from S3: %s/%s", bucket, key)
_, err = a.s3.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
@ -199,7 +199,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
snapshotID := importOutput.ImportSnapshotTasks[0].SnapshotTaskDetail.SnapshotId
if len(shareWith) > 0 {
log.Printf("[AWS] 🎥 Sharing ec2 snapshot")
logrus.Info("[AWS] 🎥 Sharing ec2 snapshot")
var userIds []*string
for _, v := range shareWith {
// Implicit memory alasing doesn't couse any bug in this case
@ -215,9 +215,10 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
},
)
if err != nil {
logrus.Warnf("[AWS] 📨 Error sharing ec2 snapshot: %v", err)
return nil, err
}
log.Println("[AWS] 📨 Shared ec2 snapshot")
logrus.Info("[AWS] 📨 Shared ec2 snapshot")
}
// Tag the snapshot with the image name.
@ -237,7 +238,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
return nil, err
}
log.Printf("[AWS] 📋 Registering AMI from imported snapshot: %s", *snapshotID)
logrus.Infof("[AWS] 📋 Registering AMI from imported snapshot: %s", *snapshotID)
registerOutput, err := a.ec2.RegisterImage(
&ec2.RegisterImageInput{
Architecture: aws.String(ec2Arch),
@ -259,7 +260,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
return nil, err
}
log.Printf("[AWS] 🎉 AMI registered: %s", *registerOutput.ImageId)
logrus.Infof("[AWS] 🎉 AMI registered: %s", *registerOutput.ImageId)
// Tag the image with the image name.
req, _ = a.ec2.CreateTagsRequest(
@ -279,7 +280,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
}
if len(shareWith) > 0 {
log.Println("[AWS] 💿 Sharing ec2 AMI")
logrus.Info("[AWS] 💿 Sharing ec2 AMI")
var launchPerms []*ec2.LaunchPermission
for _, id := range shareWith {
launchPerms = append(launchPerms, &ec2.LaunchPermission{
@ -297,9 +298,10 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str
},
)
if err != nil {
logrus.Warnf("[AWS] 📨 Error sharing AMI: %v", err)
return nil, err
}
log.Println("[AWS] 💿 Shared AMI")
logrus.Info("[AWS] 💿 Shared AMI")
}
return registerOutput.ImageId, nil
@ -332,7 +334,7 @@ func (a *AWS) RemoveSnapshotAndDeregisterImage(image *ec2.Image) error {
)
if err != nil {
// TODO return err?
log.Println("Unable to remove snapshot", s)
logrus.Warn("Unable to remove snapshot", s)
}
}
return err
@ -356,7 +358,7 @@ func (a *AWS) DescribeImagesByTag(tagKey, tagValue string) ([]*ec2.Image, error)
}
func (a *AWS) S3ObjectPresignedURL(bucket, objectKey string) (string, error) {
log.Printf("[AWS] 📋 Generating Presigned URL for S3 object %s/%s", bucket, objectKey)
logrus.Infof("[AWS] 📋 Generating Presigned URL for S3 object %s/%s", bucket, objectKey)
req, _ := a.s3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(objectKey),
@ -365,6 +367,6 @@ func (a *AWS) S3ObjectPresignedURL(bucket, objectKey string) (string, error) {
if err != nil {
return "", err
}
log.Print("[AWS] 🎉 S3 Presigned URL ready")
logrus.Info("[AWS] 🎉 S3 Presigned URL ready")
return url, nil
}