main.go 6.5 KB
Newer Older
Mustafa Gezen's avatar
Mustafa Gezen committed
1
2
3
package main

import (
4
	"fmt"
5
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob"
6
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/file"
7
8
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/gcs"
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/s3"
9
10
11
12
	"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"
13
	"github.com/go-git/go-git/v5/plumbing/transport/ssh"
14
	"log"
15
	"os"
16
17
	"os/user"
	"path/filepath"
18
	"strings"
Mustafa Gezen's avatar
Mustafa Gezen committed
19

20
	"git.rockylinux.org/release-engineering/public/srpmproc/internal"
21
	"github.com/spf13/cobra"
Mustafa Gezen's avatar
Mustafa Gezen committed
22
23
24
)

var (
Mustafa Gezen's avatar
Mustafa Gezen committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	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
45
46
47
)

var root = &cobra.Command{
48
49
	Use: "srpmproc",
	Run: mn,
Mustafa Gezen's avatar
Mustafa Gezen committed
50
51
52
}

func mn(_ *cobra.Command, _ []string) {
53
54
55
56
57
58
59
	switch version {
	case 8:
		break
	default:
		log.Fatalf("unsupported upstream version %d", version)
	}

60
	var importer data.ImportMode
61
62
63
64
65
66
	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))
67
68
	} else if strings.HasPrefix(storageAddr, "file://") {
		blobStorage = file.New(strings.Replace(storageAddr, "file://", "", 1))
69
70
	} else {
		log.Fatalf("invalid blob storage")
71
72
73
74
75
	}

	sourceRpmLocation := ""
	if strings.HasPrefix(sourceRpm, "file://") {
		sourceRpmLocation = strings.TrimPrefix(sourceRpm, "file://")
76
		importer = &internal.SrpmMode{}
77
	} else {
Mustafa Gezen's avatar
Mustafa Gezen committed
78
79
80
81
82
		if moduleMode {
			sourceRpmLocation = fmt.Sprintf("%s/%s", modulePrefix, sourceRpm)
		} else {
			sourceRpmLocation = fmt.Sprintf("%s/%s", rpmPrefix, sourceRpm)
		}
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		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)
98
99
	}

Mustafa Gezen's avatar
Mustafa Gezen committed
100
	fsCreator := func(branch string) billy.Filesystem {
101
102
103
		return memfs.New()
	}

Mustafa Gezen's avatar
Mustafa Gezen committed
104
105
106
107
108
	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
109
			if err != nil {
Mustafa Gezen's avatar
Mustafa Gezen committed
110
				log.Fatalf("could not create tmpfs dir: %v", err)
Mustafa Gezen's avatar
Mustafa Gezen committed
111
			}
112
113
114
115
			return osfs.New(tmpDir)
		}
	}

Mustafa Gezen's avatar
Mustafa Gezen committed
116
117
118
119
120
	var manualCs []string
	if strings.TrimSpace(manualCommits) != "" {
		manualCs = strings.Split(manualCommits, ",")
	}

121
	internal.ProcessRPM(&data.ProcessData{
Mustafa Gezen's avatar
Mustafa Gezen committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
		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,
144
	})
Mustafa Gezen's avatar
Mustafa Gezen committed
145
146
147
}

func main() {
148
149
150
151
	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")
152
153
154
155
	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")
156

157
158
	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")
159
160
	root.Flags().StringVar(&gitCommitterName, "git-committer-name", "rockyautomation", "Name of committer")
	root.Flags().StringVar(&gitCommitterEmail, "git-committer-email", "rockyautomation@rockylinux.org", "Email of committer")
161
162
	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")
163
164
	root.Flags().StringVar(&importBranchPrefix, "import-branch-prefix", "c", "Import branch prefix")
	root.Flags().StringVar(&branchPrefix, "branch-prefix", "r", "Branch prefix (replaces import-branch-prefix)")
165
	root.Flags().StringVar(&singleTag, "single-tag", "", "If set, only this tag is imported")
166
	root.Flags().BoolVar(&noDupMode, "no-dup-mode", false, "If enabled, skips already imported tags")
Mustafa Gezen's avatar
Mustafa Gezen committed
167
	root.Flags().BoolVar(&moduleMode, "module-mode", false, "If enabled, imports a module instead of a package")
Mustafa Gezen's avatar
Mustafa Gezen committed
168
	root.Flags().StringVar(&tmpFsMode, "tmpfs-mode", "", "If set, packages are imported to path and patched but not pushed")
169
170
	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
171
172
	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")
173
174
175
176

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