/
main.go
136 lines (115 loc) · 4.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"context"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/log"
art "github.com/ovh/cds/contrib/integrations/artifactory"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/grpcplugin/integrationplugin"
)
/*
This plugin have to be used as a upload artifact integration plugin
Artifactory upload artifact plugin must configured as following:
name: artifactory-upload-artifact-plugin
type: integration
author: "Steven Guiheux"
description: "OVH Artifactory Upload Artifact Plugin"
$ cdsctl admin plugins import artifactory-upload-artifact-plugin.yml
Build the present binaries and import in CDS:
os: linux
arch: amd64
cmd: <path-to-binary-file>
$ cdsctl admin plugins binary-add artifactory-upload-artifact-plugin artifactory-upload-artifact-plugin-bin.yml <path-to-binary-file>
Artifactory integration must configured as following
name: Artifactory
default_config:
artifactory.url:
type: string
artifactory.token:
type: password
artifactory.cds_repository:
type: string
artifact_manager: true
*/
type artifactoryUploadArtifactPlugin struct {
integrationplugin.Common
}
func (e *artifactoryUploadArtifactPlugin) Manifest(_ context.Context, _ *empty.Empty) (*integrationplugin.IntegrationPluginManifest, error) {
return &integrationplugin.IntegrationPluginManifest{
Name: "OVH Artifactory Upload Artifact Plugin",
Author: "Steven Guiheux",
Description: "OVH Artifactory Upload Artifact Plugin",
Version: sdk.VERSION,
}, nil
}
func (e *artifactoryUploadArtifactPlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
prefix := "cds.integration.artifact_manager"
cdsRepo := opts.GetOptions()[fmt.Sprintf("%s.%s", prefix, sdk.ArtifactoryConfigCdsRepository)]
artifactoryURL := opts.GetOptions()[fmt.Sprintf("%s.%s", prefix, sdk.ArtifactoryConfigURL)]
token := opts.GetOptions()[fmt.Sprintf("%s.%s", prefix, sdk.ArtifactoryConfigToken)]
pathToUpload := opts.GetOptions()[fmt.Sprintf("%s.upload.path", prefix)]
projectKey := opts.GetOptions()["cds.project"]
workflowName := opts.GetOptions()["cds.workflow"]
version := opts.GetOptions()["cds.version"]
buildInfo := opts.GetOptions()[fmt.Sprintf("%s.%s", prefix, sdk.ArtifactoryConfigBuildInfoPrefix)]
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()
artiClient, err := art.CreateArtifactoryClient(ctx, artifactoryURL, token)
if err != nil {
return fail("unable to create artifactory client: %v", err)
}
log.SetLogger(log.NewLogger(log.INFO, os.Stdout))
params := services.NewUploadParams()
params.Pattern = pathToUpload
params.Target = fmt.Sprintf("%s/%s/%s/%s/", cdsRepo, projectKey, workflowName, version)
params.Flat = true
params.BuildProps = fmt.Sprintf("build.name=%s/%s/%s;build.number=%s;build.timestamp=%d", buildInfo, projectKey, workflowName, url.QueryEscape(version), time.Now().Unix())
summary, err := artiClient.UploadFilesWithSummary(params)
if err != nil || summary.TotalFailed > 0 {
return fail("unable to upload file %s into artifactory[%s] %s: %v", pathToUpload, artifactoryURL, params.Target, err)
}
defer summary.Close()
result := make(map[string]string)
for artDetails := new(utils.ArtifactDetails); summary.ArtifactsDetailsReader.NextRecord(artDetails) == nil; artDetails = new(utils.ArtifactDetails) {
art, err := artDetails.ToBuildInfoArtifact()
if err != nil {
return fail("unable to get artifact build info: %v", err)
}
result[sdk.ArtifactUploadPluginOutputPathMD5] = artDetails.Checksums.Md5
result[sdk.ArtifactUploadPluginOutputPathFilePath] = strings.TrimPrefix(art.Path, cdsRepo+"/")
result[sdk.ArtifactUploadPluginOutputPathFileName] = art.Name
result[sdk.ArtifactUploadPluginOutputPathRepoType] = "generic"
result[sdk.ArtifactUploadPluginOutputPathRepoName] = cdsRepo
}
fileMode, err := os.Stat(pathToUpload)
if err != nil {
return fail("unable to get file stat: %v", err)
}
result[sdk.ArtifactUploadPluginOutputPerm] = strconv.FormatUint(uint64(fileMode.Mode().Perm()), 10)
result[sdk.ArtifactUploadPluginOutputSize] = strconv.FormatInt(fileMode.Size(), 10)
return &integrationplugin.RunResult{
Status: sdk.StatusSuccess,
Outputs: result,
}, nil
}
func main() {
e := artifactoryUploadArtifactPlugin{}
if err := integrationplugin.Start(context.Background(), &e); err != nil {
panic(err)
}
}
func fail(format string, args ...interface{}) (*integrationplugin.RunResult, error) {
msg := fmt.Sprintf(format, args...)
return &integrationplugin.RunResult{
Details: msg,
Status: sdk.StatusFail,
}, nil
}