Skip to content

Commit 197d830

Browse files
committed
ore aws: add ability to tag images
We need the ability to tag images to facilitate garbage collection of RHCOS AMIs that are not used in boot images. The pre-existing tagging functionality on the initial upload cannot be leveraged because we don't know whether a RHCOS build will be used as a boot image at build time.
1 parent 7d7e544 commit 197d830

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

mantle/cmd/ore/aws/tag-image.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2017 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 aws
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"strings"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
var (
26+
cmdTagImage = &cobra.Command{
27+
Use: "tag-image --ami <ami_id> --tags foo=bar ...",
28+
Short: "Tag an AMI",
29+
Run: runTagImage,
30+
}
31+
tags []string
32+
)
33+
34+
func init() {
35+
// Initialize the command and its flags
36+
AWS.AddCommand(cmdTagImage)
37+
cmdTagImage.Flags().StringVar(&amiID, "ami", "", "AWS AMI ID")
38+
cmdTagImage.Flags().StringVar(&region, "region", "", "Region")
39+
cmdTagImage.Flags().StringSliceVar(&tags, "tags", []string{}, "list of key=value tags to attach to the AMI")
40+
}
41+
42+
func runTagImage(cmd *cobra.Command, args []string) {
43+
if amiID == "" {
44+
fmt.Fprintf(os.Stderr, "Provide --ami to tag\n")
45+
os.Exit(1)
46+
}
47+
48+
if region == "" {
49+
fmt.Fprintf(os.Stderr, "Provide --region\n")
50+
os.Exit(1)
51+
}
52+
53+
tagMap := make(map[string]string)
54+
for _, tag := range tags {
55+
splitTag := strings.SplitN(tag, "=", 2)
56+
if len(splitTag) != 2 {
57+
fmt.Fprintf(os.Stderr, "invalid tag format; should be key=value, not %v\n", tag)
58+
os.Exit(1)
59+
}
60+
key, value := splitTag[0], splitTag[1]
61+
tagMap[key] = value
62+
}
63+
64+
err := API.CreateTags([]string{amiID}, tagMap)
65+
if err != nil {
66+
fmt.Fprintf(os.Stderr, "couldn't create image tags: %v", err)
67+
os.Exit(1)
68+
}
69+
}

0 commit comments

Comments
 (0)