main.go 6 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
	"io/ioutil"
15
	"log"
16
	"os"
17
18
	"os/user"
	"path/filepath"
19
	"strings"
Mustafa Gezen's avatar
Mustafa Gezen committed
20

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

var (
26
27
28
29
30
31
32
33
34
35
36
37
	sourceRpm          string
	sshKeyLocation     string
	sshUser            string
	upstreamPrefix     string
	version            int
	storageAddr        string
	gitCommitterName   string
	gitCommitterEmail  string
	modulePrefix       string
	rpmPrefix          string
	importBranchPrefix string
	branchPrefix       string
38
	singleTag          string
39
40
	noDupMode          bool
	moduleMode         bool
41
42
43
	tmpFsMode          bool
	noStorageDownload  bool
	noStorageUpload    bool
Mustafa Gezen's avatar
Mustafa Gezen committed
44
45
46
)

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

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

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

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

99
100
101
102
103
	fsCreator := func() billy.Filesystem {
		return memfs.New()
	}

	if tmpFsMode {
Mustafa Gezen's avatar
Mustafa Gezen committed
104
		tmpBaseDir, err := ioutil.TempDir(os.TempDir(), "srpmproc-*")
105
106
107
		if err != nil {
			log.Fatalf("could not create temp dir: %v", err)
		}
Mustafa Gezen's avatar
Mustafa Gezen committed
108
		log.Printf("using temp dir: %s", tmpBaseDir)
109
		fsCreator = func() billy.Filesystem {
Mustafa Gezen's avatar
Mustafa Gezen committed
110
111
112
113
			tmpDir, err := ioutil.TempDir(tmpBaseDir, "*")
			if err != nil {
				log.Fatalf("could not create temp dir: %v", err)
			}
114
115
116
117
118
			return osfs.New(tmpDir)
		}
	}

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

func main() {
143
144
145
146
	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")
147
148
149
150
	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")
151

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

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