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

Commit

Permalink
cmd/ore/azure: add ARM based image creation/upload & GC
Browse files Browse the repository at this point in the history
Adds a new version of the create-image & upload-blob commands which
use ARM credentials (Azure AD service providers), as well as a GC
command to clean up resource groups.
  • Loading branch information
arithx committed Nov 29, 2017
1 parent e9ee041 commit 5ba0c74
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/ore/azure/create-image-arm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package azure

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var (
cmdCreateImageARM = &cobra.Command{
Use: "create-image-arm",
Short: "Create Azure image",
Long: "Create Azure image from a blob url",
RunE: runCreateImageARM,
}

imageName string
blobUrl string
resourceGroup string
)

func init() {
sv := cmdCreateImageARM.Flags().StringVar

sv(&imageName, "img-name", "", "image name")
sv(&blobUrl, "img-blob", "", "source blob url")
sv(&resourceGroup, "resource-group", "kola", "resource group name")

Azure.AddCommand(cmdCreateImageARM)
}

func runCreateImageARM(cmd *cobra.Command, args []string) error {
_, err := api.CreateImage(imageName, resourceGroup, blobUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't create image: %v\n", err)
os.Exit(1)
}
return nil
}
48 changes: 48 additions & 0 deletions cmd/ore/azure/gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package azure

import (
"fmt"
"os"
"time"

"github.com/spf13/cobra"
)

var (
cmdGC = &cobra.Command{
Use: "gc",
Short: "GC resources in Azure",
Long: `Delete instances created over the given duration ago`,
RunE: runGC,
}

gcDuration time.Duration
)

func init() {
Azure.AddCommand(cmdGC)
cmdGC.Flags().DurationVar(&gcDuration, "duration", 5*time.Hour, "how old resources must be before they're considered garbage")
}

func runGC(cmd *cobra.Command, args []string) error {
err := api.GC(gcDuration)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't gc: %v\n", err)
os.Exit(1)
}
return nil
}
86 changes: 86 additions & 0 deletions cmd/ore/azure/upload-blob-arm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package azure

import (
"fmt"
"strings"

"github.com/Microsoft/azure-vhd-utils-for-go/vhdcore/validator"
"github.com/spf13/cobra"
)

var (
cmdUploadBlobARM = &cobra.Command{
Use: "upload-blob-arm storage-account container blob-name file",
Short: "Upload a blob to Azure storage",
Run: runUploadBlobARM,
}
)

func init() {
bv := cmdUploadBlobARM.Flags().BoolVar

bv(&ubo.overwrite, "overwrite", false, "overwrite blob")
bv(&ubo.validate, "validate", true, "validate blob as VHD file")

Azure.AddCommand(cmdUploadBlobARM)
}

func runUploadBlobARM(cmd *cobra.Command, args []string) {
if len(args) != 4 {
plog.Fatalf("Expecting 4 arguments, got %d", len(args))
}

ubo.storageacct = args[0]
ubo.container = args[1]
ubo.blob = args[2]
ubo.vhd = args[3]

if ubo.validate {
plog.Printf("Validating VHD %q", ubo.vhd)
if !strings.HasSuffix(strings.ToLower(ubo.blob), ".vhd") {
plog.Fatalf("Blob name should end with .vhd")
}

if err := validator.ValidateVhd(ubo.vhd); err != nil {
plog.Fatal(err)
}

if err := validator.ValidateVhdSize(ubo.vhd); err != nil {
plog.Fatal(err)
}
}

kr, err := api.GetStorageServiceKeysARM(ubo.storageacct)
if err != nil {
plog.Fatalf("Fetching storage service keys failed: %v", err)
}

if kr.Keys == nil {
plog.Fatalf("No storage service keys found")
}

for _, k := range *kr.Keys {
if err := api.UploadBlob(ubo.storageacct, *k.Value, ubo.vhd, ubo.container, ubo.blob, ubo.overwrite); err != nil {
plog.Fatalf("Uploading blob failed: %v", err)
}
break
}

uri := fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", ubo.storageacct, ubo.container, ubo.blob)

plog.Printf("Blob uploaded to %q", uri)
}

0 comments on commit 5ba0c74

Please sign in to comment.