main.go 5.92 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 (
25
26
27
28
29
30
31
32
33
34
35
36
	sourceRpm          string
	sshKeyLocation     string
	sshUser            string
	upstreamPrefix     string
	version            int
	storageAddr        string
	gitCommitterName   string
	gitCommitterEmail  string
	modulePrefix       string
	rpmPrefix          string
	importBranchPrefix string
	branchPrefix       string
37
	singleTag          string
38
39
	noDupMode          bool
	moduleMode         bool
Mustafa Gezen's avatar
Mustafa Gezen committed
40
	tmpFsMode          string
41
42
	noStorageDownload  bool
	noStorageUpload    bool
Mustafa Gezen's avatar
Mustafa Gezen committed
43
44
45
)

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

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

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

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

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

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

	internal.ProcessRPM(&data.ProcessData{
115
116
117
118
119
120
121
122
123
124
125
126
		Importer:           importer,
		RpmLocation:        sourceRpmLocation,
		UpstreamPrefix:     upstreamPrefix,
		SshKeyLocation:     sshKeyLocation,
		SshUser:            sshUser,
		Version:            version,
		BlobStorage:        blobStorage,
		GitCommitterName:   gitCommitterName,
		GitCommitterEmail:  gitCommitterEmail,
		ModulePrefix:       modulePrefix,
		ImportBranchPrefix: importBranchPrefix,
		BranchPrefix:       branchPrefix,
127
		SingleTag:          singleTag,
128
129
130
		Authenticator:      authenticator,
		NoDupMode:          noDupMode,
		ModuleMode:         moduleMode,
131
132
133
134
		TmpFsMode:          tmpFsMode,
		NoStorageDownload:  noStorageDownload,
		NoStorageUpload:    noStorageUpload,
		FsCreator:          fsCreator,
135
	})
Mustafa Gezen's avatar
Mustafa Gezen committed
136
137
138
}

func main() {
139
140
141
142
	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")
143
144
145
146
	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")
147

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

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