talos

Форк
0
125 строк · 3.1 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
// Package main implements Talos cloud image uploader.
6
package main
7

8
import (
9
	"context"
10
	cryptorand "crypto/rand"
11
	"encoding/json"
12
	"fmt"
13
	"io"
14
	"log"
15
	"os"
16
	"path/filepath"
17
	"sync"
18

19
	"github.com/spf13/pflag"
20
	"golang.org/x/sync/errgroup"
21
)
22

23
// Result of the upload process.
24
type Result []CloudImage
25

26
// CloudImage is the record official cloud image.
27
type CloudImage struct {
28
	Cloud  string `json:"cloud"`
29
	Tag    string `json:"version"`
30
	Region string `json:"region"`
31
	Arch   string `json:"arch"`
32
	Type   string `json:"type"`
33
	ID     string `json:"id"`
34
}
35

36
var (
37
	result   Result
38
	resultMu sync.Mutex
39
)
40

41
func pushResult(image CloudImage) {
42
	resultMu.Lock()
43
	defer resultMu.Unlock()
44

45
	result = append(result, image)
46
}
47

48
func main() {
49
	if err := run(); err != nil {
50
		log.Fatalf("%s", err)
51
	}
52
}
53

54
func run() error {
55
	var err error
56

57
	pflag.StringSliceVar(&DefaultOptions.TargetClouds, "target-clouds", DefaultOptions.TargetClouds, "cloud targets to upload to")
58
	pflag.StringSliceVar(&DefaultOptions.Architectures, "architectures", DefaultOptions.Architectures, "list of architectures to process")
59
	pflag.StringVar(&DefaultOptions.ArtifactsPath, "artifacts-path", DefaultOptions.ArtifactsPath, "artifacts path")
60
	pflag.StringVar(&DefaultOptions.Tag, "tag", DefaultOptions.Tag, "tag (version) of the uploaded image")
61
	pflag.StringVar(&DefaultOptions.NamePrefix, "name-prefix", DefaultOptions.NamePrefix, "prefix for the name of the uploaded image")
62

63
	pflag.StringSliceVar(&DefaultOptions.AWSRegions, "aws-regions", DefaultOptions.AWSRegions, "list of AWS regions to upload to")
64
	pflag.StringSliceVar(&DefaultOptions.AzureRegions, "azure-regions", DefaultOptions.AzureRegions, "list of Azure regions to upload to")
65

66
	pflag.Parse()
67

68
	seed := make([]byte, 8)
69
	if _, err = cryptorand.Read(seed); err != nil {
70
		log.Fatalf("error seeding rand: %s", err)
71
	}
72

73
	ctx, cancel := context.WithCancel(context.Background())
74
	defer cancel()
75

76
	var g *errgroup.Group
77

78
	g, ctx = errgroup.WithContext(ctx)
79

80
	for _, target := range DefaultOptions.TargetClouds {
81
		switch target {
82
		case "aws":
83
			g.Go(func() error {
84
				if len(DefaultOptions.AWSRegions) == 0 {
85
					DefaultOptions.AWSRegions, err = GetAWSDefaultRegions()
86
					if err != nil {
87
						log.Printf("failed to get a list of enabled AWS regions: %s, ignored", err)
88
					}
89
				}
90

91
				aws := AWSUploader{
92
					Options: DefaultOptions,
93
				}
94

95
				return aws.Upload(ctx)
96
			})
97
		case "azure":
98
			g.Go(func() error {
99
				azure := AzureUploader{
100
					Options: DefaultOptions,
101
				}
102

103
				return azure.AzureGalleryUpload(ctx)
104
			})
105
		default:
106
			return fmt.Errorf("unknown target: %s", target)
107
		}
108
	}
109

110
	if err = g.Wait(); err != nil {
111
		return fmt.Errorf("failed: %w", err)
112
	}
113

114
	f, err := os.Create(filepath.Join(DefaultOptions.ArtifactsPath, "cloud-images.json"))
115
	if err != nil {
116
		return fmt.Errorf("failed: %w", err)
117
	}
118

119
	defer f.Close() //nolint:errcheck
120

121
	e := json.NewEncoder(io.MultiWriter(os.Stdout, f))
122
	e.SetIndent("", "  ")
123

124
	return e.Encode(&result)
125
}
126

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.