2020-12-14 01:29:02 +00:00
package main
import (
2020-12-20 09:36:13 +00:00
"fmt"
2021-02-21 13:51:30 +00:00
"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob"
2021-02-24 07:27:51 +00:00
"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/file"
2021-02-21 13:51:30 +00:00
"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/gcs"
"git.rockylinux.org/release-engineering/public/srpmproc/internal/blob/s3"
2021-02-24 07:27:51 +00:00
"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"
2020-12-20 09:36:13 +00:00
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
2021-02-24 07:27:51 +00:00
"io/ioutil"
2020-12-15 08:56:09 +00:00
"log"
2021-02-24 07:27:51 +00:00
"os"
2020-12-20 09:36:13 +00:00
"os/user"
"path/filepath"
2020-12-15 08:56:09 +00:00
"strings"
2020-12-14 01:29:02 +00:00
2021-02-21 13:51:30 +00:00
"git.rockylinux.org/release-engineering/public/srpmproc/internal"
2020-12-15 08:56:09 +00:00
"github.com/spf13/cobra"
2020-12-14 01:29:02 +00:00
)
var (
2021-02-14 17:25:00 +00:00
sourceRpm string
sshKeyLocation string
sshUser string
upstreamPrefix string
version int
storageAddr string
gitCommitterName string
gitCommitterEmail string
modulePrefix string
rpmPrefix string
importBranchPrefix string
branchPrefix string
2021-02-24 07:27:51 +00:00
singleTag string
2021-02-14 17:25:00 +00:00
noDupMode bool
moduleMode bool
2021-02-24 07:27:51 +00:00
tmpFsMode bool
noStorageDownload bool
noStorageUpload bool
2020-12-14 01:29:02 +00:00
)
var root = & cobra . Command {
2020-12-15 08:56:09 +00:00
Use : "srpmproc" ,
Run : mn ,
2020-12-14 01:29:02 +00:00
}
func mn ( _ * cobra . Command , _ [ ] string ) {
2020-12-20 09:36:13 +00:00
switch version {
case 8 :
break
default :
log . Fatalf ( "unsupported upstream version %d" , version )
}
2021-02-24 07:27:51 +00:00
var importer data . ImportMode
2020-12-20 09:36:13 +00:00
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 ) )
2021-02-24 07:27:51 +00:00
} else if strings . HasPrefix ( storageAddr , "file://" ) {
blobStorage = file . New ( strings . Replace ( storageAddr , "file://" , "" , 1 ) )
2020-12-20 09:36:13 +00:00
} else {
log . Fatalf ( "invalid blob storage" )
2020-12-15 08:56:09 +00:00
}
sourceRpmLocation := ""
if strings . HasPrefix ( sourceRpm , "file://" ) {
sourceRpmLocation = strings . TrimPrefix ( sourceRpm , "file://" )
2020-12-20 09:36:13 +00:00
importer = & internal . SrpmMode { }
2020-12-15 08:56:09 +00:00
} else {
2020-12-22 16:28:04 +00:00
if moduleMode {
sourceRpmLocation = fmt . Sprintf ( "%s/%s" , modulePrefix , sourceRpm )
} else {
sourceRpmLocation = fmt . Sprintf ( "%s/%s" , rpmPrefix , sourceRpm )
}
2020-12-20 09:36:13 +00:00
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 )
2020-12-15 08:56:09 +00:00
}
2021-02-24 07:27:51 +00:00
fsCreator := func ( ) billy . Filesystem {
return memfs . New ( )
}
if tmpFsMode {
tmpDir , err := ioutil . TempDir ( os . TempDir ( ) , "srpmproc-*" )
if err != nil {
log . Fatalf ( "could not create temp dir: %v" , err )
}
log . Printf ( "using temp dir: %s" , tmpDir )
fsCreator = func ( ) billy . Filesystem {
return osfs . New ( tmpDir )
}
}
internal . ProcessRPM ( & data . ProcessData {
2021-02-14 17:25:00 +00:00
Importer : importer ,
RpmLocation : sourceRpmLocation ,
UpstreamPrefix : upstreamPrefix ,
SshKeyLocation : sshKeyLocation ,
SshUser : sshUser ,
Version : version ,
BlobStorage : blobStorage ,
GitCommitterName : gitCommitterName ,
GitCommitterEmail : gitCommitterEmail ,
ModulePrefix : modulePrefix ,
ImportBranchPrefix : importBranchPrefix ,
BranchPrefix : branchPrefix ,
2021-02-24 07:27:51 +00:00
SingleTag : singleTag ,
2021-02-14 17:25:00 +00:00
Authenticator : authenticator ,
NoDupMode : noDupMode ,
ModuleMode : moduleMode ,
2021-02-24 07:27:51 +00:00
TmpFsMode : tmpFsMode ,
NoStorageDownload : noStorageDownload ,
NoStorageUpload : noStorageUpload ,
FsCreator : fsCreator ,
2020-12-15 08:56:09 +00:00
} )
2020-12-14 01:29:02 +00:00
}
func main ( ) {
2020-12-15 08:56:09 +00:00
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" )
2020-12-20 09:36:13 +00:00
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" )
2020-12-15 08:56:09 +00:00
2020-12-17 11:12:10 +00:00
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" )
2021-02-15 04:20:42 +00:00
root . Flags ( ) . StringVar ( & gitCommitterName , "git-committer-name" , "rockyautomation" , "Name of committer" )
root . Flags ( ) . StringVar ( & gitCommitterEmail , "git-committer-email" , "rockyautomation@rockylinux.org" , "Email of committer" )
2020-12-20 09:36:13 +00:00
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" )
2021-02-14 17:25:00 +00:00
root . Flags ( ) . StringVar ( & importBranchPrefix , "import-branch-prefix" , "c" , "Import branch prefix" )
root . Flags ( ) . StringVar ( & branchPrefix , "branch-prefix" , "r" , "Branch prefix (replaces import-branch-prefix)" )
2021-02-24 07:27:51 +00:00
root . Flags ( ) . StringVar ( & singleTag , "single-tag" , "" , "If set, only this tag is imported" )
2020-12-22 05:13:27 +00:00
root . Flags ( ) . BoolVar ( & noDupMode , "no-dup-mode" , false , "If enabled, skips already imported tags" )
2020-12-22 16:28:04 +00:00
root . Flags ( ) . BoolVar ( & moduleMode , "module-mode" , false , "If enabled, imports a module instead of a package" )
2021-02-24 07:27:51 +00:00
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" )
2020-12-15 08:56:09 +00:00
if err := root . Execute ( ) ; err != nil {
log . Fatal ( err )
}
2020-12-14 01:29:02 +00:00
}