main.go 7.59 KB
Newer Older
Mustafa Gezen's avatar
Mustafa Gezen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Copyright (c) 2021 The Srpmproc Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

Mustafa Gezen's avatar
Mustafa Gezen committed
21
22
23
package main

import (
24
	"fmt"
25
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob"
26
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/file"
27
28
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/gcs"
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/s3"
29
30
31
32
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/data"
	"github.com/go-git/go-billy/v5"
	"github.com/go-git/go-billy/v5/memfs"
	"github.com/go-git/go-billy/v5/osfs"
33
	"github.com/go-git/go-git/v5/plumbing/transport/ssh"
34
	"log"
35
	"os"
36
37
	"os/user"
	"path/filepath"
38
	"strings"
Mustafa Gezen's avatar
Mustafa Gezen committed
39

40
	"git.rockylinux.org/release-engineering/public/srpmproc/internal"
41
	"github.com/spf13/cobra"
Mustafa Gezen's avatar
Mustafa Gezen committed
42
43
44
)

var (
Mustafa Gezen's avatar
Mustafa Gezen committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	sourceRpm           string
	sshKeyLocation      string
	sshUser             string
	upstreamPrefix      string
	version             int
	storageAddr         string
	gitCommitterName    string
	gitCommitterEmail   string
	modulePrefix        string
	rpmPrefix           string
	importBranchPrefix  string
	branchPrefix        string
	singleTag           string
	noDupMode           bool
	moduleMode          bool
	tmpFsMode           string
	noStorageDownload   bool
	noStorageUpload     bool
	manualCommits       string
	upstreamPrefixHttps string
Mustafa Gezen's avatar
Mustafa Gezen committed
65
66
67
)

var root = &cobra.Command{
68
69
	Use: "srpmproc",
	Run: mn,
Mustafa Gezen's avatar
Mustafa Gezen committed
70
71
72
}

func mn(_ *cobra.Command, _ []string) {
73
74
75
76
77
78
79
	switch version {
	case 8:
		break
	default:
		log.Fatalf("unsupported upstream version %d", version)
	}

80
	var importer data.ImportMode
81
82
83
84
85
86
	var blobStorage blob.Storage

	if strings.HasPrefix(storageAddr, "gs://") {
		blobStorage = gcs.New(strings.Replace(storageAddr, "gs://", "", 1))
	} else if strings.HasPrefix(storageAddr, "s3://") {
		blobStorage = s3.New(strings.Replace(storageAddr, "s3://", "", 1))
87
88
	} else if strings.HasPrefix(storageAddr, "file://") {
		blobStorage = file.New(strings.Replace(storageAddr, "file://", "", 1))
89
90
	} else {
		log.Fatalf("invalid blob storage")
91
92
93
94
95
	}

	sourceRpmLocation := ""
	if strings.HasPrefix(sourceRpm, "file://") {
		sourceRpmLocation = strings.TrimPrefix(sourceRpm, "file://")
96
		importer = &internal.SrpmMode{}
97
	} else {
Mustafa Gezen's avatar
Mustafa Gezen committed
98
99
100
101
102
		if moduleMode {
			sourceRpmLocation = fmt.Sprintf("%s/%s", modulePrefix, sourceRpm)
		} else {
			sourceRpmLocation = fmt.Sprintf("%s/%s", rpmPrefix, sourceRpm)
		}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
		importer = &internal.GitMode{}
	}

	lastKeyLocation := sshKeyLocation
	if lastKeyLocation == "" {
		usr, err := user.Current()
		if err != nil {
			log.Fatalf("could not get user: %v", err)
		}
		lastKeyLocation = filepath.Join(usr.HomeDir, ".ssh/id_rsa")
	}
	// create ssh key authenticator
	authenticator, err := ssh.NewPublicKeysFromFile(sshUser, lastKeyLocation, "")
	if err != nil {
		log.Fatalf("could not get git authenticator: %v", err)
118
119
	}

Mustafa Gezen's avatar
Mustafa Gezen committed
120
	fsCreator := func(branch string) billy.Filesystem {
121
122
123
		return memfs.New()
	}

Mustafa Gezen's avatar
Mustafa Gezen committed
124
125
126
127
128
	if tmpFsMode != "" {
		log.Printf("using tmpfs dir: %s", tmpFsMode)
		fsCreator = func(branch string) billy.Filesystem {
			tmpDir := filepath.Join(tmpFsMode, branch)
			err := os.MkdirAll(tmpDir, 0755)
Mustafa Gezen's avatar
Mustafa Gezen committed
129
			if err != nil {
Mustafa Gezen's avatar
Mustafa Gezen committed
130
				log.Fatalf("could not create tmpfs dir: %v", err)
Mustafa Gezen's avatar
Mustafa Gezen committed
131
			}
132
133
134
135
			return osfs.New(tmpDir)
		}
	}

Mustafa Gezen's avatar
Mustafa Gezen committed
136
137
138
139
140
	var manualCs []string
	if strings.TrimSpace(manualCommits) != "" {
		manualCs = strings.Split(manualCommits, ",")
	}

141
	internal.ProcessRPM(&data.ProcessData{
Mustafa Gezen's avatar
Mustafa Gezen committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
		Importer:            importer,
		RpmLocation:         sourceRpmLocation,
		UpstreamPrefix:      upstreamPrefix,
		SshKeyLocation:      sshKeyLocation,
		SshUser:             sshUser,
		Version:             version,
		BlobStorage:         blobStorage,
		GitCommitterName:    gitCommitterName,
		GitCommitterEmail:   gitCommitterEmail,
		ModulePrefix:        modulePrefix,
		ImportBranchPrefix:  importBranchPrefix,
		BranchPrefix:        branchPrefix,
		SingleTag:           singleTag,
		Authenticator:       authenticator,
		NoDupMode:           noDupMode,
		ModuleMode:          moduleMode,
		TmpFsMode:           tmpFsMode,
		NoStorageDownload:   noStorageDownload,
		NoStorageUpload:     noStorageUpload,
		ManualCommits:       manualCs,
		UpstreamPrefixHttps: upstreamPrefixHttps,
		FsCreator:           fsCreator,
164
	})
Mustafa Gezen's avatar
Mustafa Gezen committed
165
166
167
}

func main() {
168
169
170
171
	root.Flags().StringVar(&sourceRpm, "source-rpm", "", "Location of RPM to process")
	_ = root.MarkFlagRequired("source-rpm")
	root.Flags().StringVar(&upstreamPrefix, "upstream-prefix", "", "Upstream git repository prefix")
	_ = root.MarkFlagRequired("upstream-prefix")
172
173
174
175
	root.Flags().IntVar(&version, "version", 0, "Upstream version")
	_ = root.MarkFlagRequired("version")
	root.Flags().StringVar(&storageAddr, "storage-addr", "", "Bucket to use as blob storage")
	_ = root.MarkFlagRequired("storage-addr")
176

177
178
	root.Flags().StringVar(&sshKeyLocation, "ssh-key-location", "", "Location of the SSH key to use to authenticate against upstream")
	root.Flags().StringVar(&sshUser, "ssh-user", "git", "SSH User")
179
180
	root.Flags().StringVar(&gitCommitterName, "git-committer-name", "rockyautomation", "Name of committer")
	root.Flags().StringVar(&gitCommitterEmail, "git-committer-email", "rockyautomation@rockylinux.org", "Email of committer")
181
182
	root.Flags().StringVar(&modulePrefix, "module-prefix", "https://git.centos.org/modules", "Where to retrieve modules if exists. Only used when source-rpm is a git repo")
	root.Flags().StringVar(&rpmPrefix, "rpm-prefix", "https://git.centos.org/rpms", "Where to retrieve SRPM content. Only used when source-rpm is not a local file")
183
184
	root.Flags().StringVar(&importBranchPrefix, "import-branch-prefix", "c", "Import branch prefix")
	root.Flags().StringVar(&branchPrefix, "branch-prefix", "r", "Branch prefix (replaces import-branch-prefix)")
185
	root.Flags().StringVar(&singleTag, "single-tag", "", "If set, only this tag is imported")
186
	root.Flags().BoolVar(&noDupMode, "no-dup-mode", false, "If enabled, skips already imported tags")
Mustafa Gezen's avatar
Mustafa Gezen committed
187
	root.Flags().BoolVar(&moduleMode, "module-mode", false, "If enabled, imports a module instead of a package")
Mustafa Gezen's avatar
Mustafa Gezen committed
188
	root.Flags().StringVar(&tmpFsMode, "tmpfs-mode", "", "If set, packages are imported to path and patched but not pushed")
189
190
	root.Flags().BoolVar(&noStorageDownload, "no-storage-download", false, "If enabled, blobs are always downloaded from upstream")
	root.Flags().BoolVar(&noStorageUpload, "no-storage-upload", false, "If enabled, blobs are not uploaded to blob storage")
Mustafa Gezen's avatar
Mustafa Gezen committed
191
192
	root.Flags().StringVar(&manualCommits, "manual-commits", "", "Comma separated branch and commit list for packages with broken release tags (Format: BRANCH:HASH)")
	root.Flags().StringVar(&upstreamPrefixHttps, "upstream-prefix-https", "", "Web version of upstream prefix. Required if module-mode")
193
194
195
196

	if err := root.Execute(); err != nil {
		log.Fatal(err)
	}
Mustafa Gezen's avatar
Mustafa Gezen committed
197
}