This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/ore/azure: add ARM based image creation/upload & GC
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
Showing
3 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |