devlopment branch (#1736)
This commit is contained in:
@@ -21,15 +21,15 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
JenkinsAllUserRoleName = "kubesphere-user"
|
||||
jenkinsAllUserRoleName = "kubesphere-user"
|
||||
)
|
||||
|
||||
type DevopsClient struct {
|
||||
type Client struct {
|
||||
jenkinsClient *gojenkins.Jenkins
|
||||
}
|
||||
|
||||
func NewDevopsClient(options *DevopsOptions) (*DevopsClient, error) {
|
||||
var d DevopsClient
|
||||
func NewDevopsClient(options *Options) (*Client, error) {
|
||||
var d Client
|
||||
|
||||
jenkins := gojenkins.CreateJenkins(nil, options.Host, options.MaxConnections, options.Username, options.Password)
|
||||
jenkins, err := jenkins.Init()
|
||||
@@ -49,34 +49,13 @@ func NewDevopsClient(options *DevopsOptions) (*DevopsClient, error) {
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func NewDevopsClientOrDie(options *DevopsOptions) *DevopsClient {
|
||||
jenkins := gojenkins.CreateJenkins(nil, options.Host, options.MaxConnections, options.Username, options.Password)
|
||||
jenkins, err := jenkins.Init()
|
||||
if err != nil {
|
||||
klog.Errorf("failed to connecto to jenkins role, %+v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d := &DevopsClient{
|
||||
jenkinsClient: jenkins,
|
||||
}
|
||||
|
||||
err = d.initializeJenkins()
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (c *DevopsClient) Jenkins() *gojenkins.Jenkins {
|
||||
func (c *Client) Jenkins() *gojenkins.Jenkins {
|
||||
return c.jenkinsClient
|
||||
}
|
||||
|
||||
var mutex = sync.Mutex{}
|
||||
|
||||
func (c *DevopsClient) initializeJenkins() error {
|
||||
func (c *Client) initializeJenkins() error {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
@@ -84,7 +63,7 @@ func (c *DevopsClient) initializeJenkins() error {
|
||||
return fmt.Errorf("jenkins intialization failed")
|
||||
}
|
||||
|
||||
globalRole, err := c.jenkinsClient.GetGlobalRole(JenkinsAllUserRoleName)
|
||||
globalRole, err := c.jenkinsClient.GetGlobalRole(jenkinsAllUserRoleName)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
@@ -92,14 +71,14 @@ func (c *DevopsClient) initializeJenkins() error {
|
||||
|
||||
// Jenkins uninitialized, create global role
|
||||
if globalRole == nil {
|
||||
_, err := c.jenkinsClient.AddGlobalRole(JenkinsAllUserRoleName, gojenkins.GlobalPermissionIds{GlobalRead: true}, true)
|
||||
_, err := c.jenkinsClient.AddGlobalRole(jenkinsAllUserRoleName, gojenkins.GlobalPermissionIds{GlobalRead: true}, true)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err = c.jenkinsClient.AddProjectRole(JenkinsAllUserRoleName, "\\n\\s*\\r", gojenkins.ProjectPermissionIds{SCMTag: true}, true)
|
||||
_, err = c.jenkinsClient.AddProjectRole(jenkinsAllUserRoleName, "\\n\\s*\\r", gojenkins.ProjectPermissionIds{SCMTag: true}, true)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
|
||||
18
pkg/simple/client/devops/interface.go
Normal file
18
pkg/simple/client/devops/interface.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package devops
|
||||
|
||||
type Job struct {
|
||||
}
|
||||
|
||||
type Interface interface {
|
||||
GetJob(projectId, pipelineName string) (*Job, error)
|
||||
|
||||
DeleteJob(projectId, pipelineId string) (bool, error)
|
||||
|
||||
CreateJobInFolder()
|
||||
|
||||
GetGlobalRole(roleName string)
|
||||
|
||||
AddGlobalRole(roleName string, permission string)
|
||||
|
||||
GetProjectRole(roleName string)
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
|
||||
)
|
||||
|
||||
type DevopsOptions struct {
|
||||
type Options struct {
|
||||
Host string `json:",omitempty" yaml:"host" description:"Jenkins service host address"`
|
||||
Username string `json:",omitempty" yaml:"username" description:"Jenkins admin username"`
|
||||
Password string `json:",omitempty" yaml:"password" description:"Jenkins admin password"`
|
||||
@@ -14,8 +14,8 @@ type DevopsOptions struct {
|
||||
}
|
||||
|
||||
// NewDevopsOptions returns a `zero` instance
|
||||
func NewDevopsOptions() *DevopsOptions {
|
||||
return &DevopsOptions{
|
||||
func NewDevopsOptions() *Options {
|
||||
return &Options{
|
||||
Host: "",
|
||||
Username: "",
|
||||
Password: "",
|
||||
@@ -24,15 +24,15 @@ func NewDevopsOptions() *DevopsOptions {
|
||||
}
|
||||
|
||||
// ApplyTo apply configuration to another options
|
||||
func (s *DevopsOptions) ApplyTo(options *DevopsOptions) {
|
||||
func (s *Options) ApplyTo(options *Options) {
|
||||
if s.Host != "" {
|
||||
reflectutils.Override(options, s)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate check if there is misconfiguration in options
|
||||
func (s *DevopsOptions) Validate() []error {
|
||||
errors := []error{}
|
||||
func (s *Options) Validate() []error {
|
||||
var errors []error
|
||||
|
||||
// devops is not needed, ignore rest options
|
||||
if s.Host == "" {
|
||||
@@ -50,18 +50,18 @@ func (s *DevopsOptions) Validate() []error {
|
||||
return errors
|
||||
}
|
||||
|
||||
func (s *DevopsOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.StringVar(&s.Host, "jenkins-host", s.Host, ""+
|
||||
func (s *Options) AddFlags(fs *pflag.FlagSet, c *Options) {
|
||||
fs.StringVar(&s.Host, "jenkins-host", c.Host, ""+
|
||||
"Jenkins service host address. If left blank, means Jenkins "+
|
||||
"is unnecessary.")
|
||||
|
||||
fs.StringVar(&s.Username, "jenkins-username", s.Username, ""+
|
||||
fs.StringVar(&s.Username, "jenkins-username", c.Username, ""+
|
||||
"Username for access to Jenkins service. Leave it blank if there isn't any.")
|
||||
|
||||
fs.StringVar(&s.Password, "jenkins-password", s.Password, ""+
|
||||
fs.StringVar(&s.Password, "jenkins-password", c.Password, ""+
|
||||
"Password for access to Jenkins service, used pair with username.")
|
||||
|
||||
fs.IntVar(&s.MaxConnections, "jenkins-max-connections", s.MaxConnections, ""+
|
||||
fs.IntVar(&s.MaxConnections, "jenkins-max-connections", c.MaxConnections, ""+
|
||||
"Maximum allowed connections to Jenkins. ")
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user