github.com/coreos/mantle@v0.13.0/cmd/ore/azure/upload-blob-arm.go (about) 1 // Copyright 2018 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package azure 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "os" 21 "path/filepath" 22 "strings" 23 24 "github.com/Microsoft/azure-vhd-utils/vhdcore/validator" 25 "github.com/spf13/cobra" 26 27 "github.com/coreos/mantle/sdk" 28 ) 29 30 var ( 31 cmdUploadBlobARM = &cobra.Command{ 32 Use: "upload-blob-arm", 33 Short: "Upload a blob to Azure storage", 34 Run: runUploadBlobARM, 35 } 36 ) 37 38 func init() { 39 bv := cmdUploadBlobARM.Flags().BoolVar 40 sv := cmdUploadBlobARM.Flags().StringVar 41 42 bv(&ubo.overwrite, "overwrite", false, "overwrite blob") 43 bv(&ubo.validate, "validate", true, "validate blob as VHD file") 44 45 sv(&ubo.storageacct, "storage-account", "kola", "storage account name") 46 sv(&ubo.container, "container", "vhds", "container name") 47 sv(&ubo.blob, "blob-name", "", "name of the blob") 48 sv(&ubo.vhd, "file", defaultUploadFile(), "path to CoreOS image (build with ./image_to_vm.sh --format=azure ...)") 49 sv(&resourceGroup, "resource-group", "kola", "resource group name that owns the storage account") 50 51 Azure.AddCommand(cmdUploadBlobARM) 52 } 53 54 func defaultUploadFile() string { 55 build := sdk.BuildRoot() 56 return build + "/images/amd64-usr/latest/coreos_production_azure_image.vhd" 57 } 58 59 func runUploadBlobARM(cmd *cobra.Command, args []string) { 60 if ubo.blob == "" { 61 ver, err := sdk.VersionsFromDir(filepath.Dir(ubo.vhd)) 62 if err != nil { 63 plog.Fatalf("Unable to get version from iamge directory, provide a -blob-name flag or include a version.txt in the image directory: %v\n", err) 64 } 65 ubo.blob = fmt.Sprintf("Container-Linux-dev-%s-%s.vhd", os.Getenv("USER"), ver.Version) 66 } 67 68 if err := api.SetupClients(); err != nil { 69 plog.Fatalf("setting up clients: %v\n", err) 70 } 71 72 if ubo.validate { 73 plog.Printf("Validating VHD %q", ubo.vhd) 74 if !strings.HasSuffix(strings.ToLower(ubo.blob), ".vhd") { 75 plog.Fatalf("Blob name should end with .vhd") 76 } 77 78 if !strings.HasSuffix(strings.ToLower(ubo.vhd), ".vhd") { 79 plog.Fatalf("Image should end with .vhd") 80 } 81 82 if err := validator.ValidateVhd(ubo.vhd); err != nil { 83 plog.Fatal(err) 84 } 85 86 if err := validator.ValidateVhdSize(ubo.vhd); err != nil { 87 plog.Fatal(err) 88 } 89 } 90 91 kr, err := api.GetStorageServiceKeysARM(ubo.storageacct, resourceGroup) 92 if err != nil { 93 plog.Fatalf("Fetching storage service keys failed: %v", err) 94 } 95 96 if kr.Keys == nil { 97 plog.Fatalf("No storage service keys found") 98 } 99 100 for _, k := range *kr.Keys { 101 if err := api.UploadBlob(ubo.storageacct, *k.Value, ubo.vhd, ubo.container, ubo.blob, ubo.overwrite); err != nil { 102 plog.Fatalf("Uploading blob failed: %v", err) 103 } 104 break 105 } 106 107 err = json.NewEncoder(os.Stdout).Encode(&struct { 108 URL string 109 }{ 110 URL: fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", ubo.storageacct, ubo.container, ubo.blob), 111 }) 112 }