Project

General

Profile

Download (1.87 KB) Statistics
| Branch: | Tag: | Revision:

foreman_pipeline / app / models / foreman_pipeline / jenkins_instance.rb @ 720671b8

1
require 'jenkins_api_client'
2

    
3
module ForemanPipeline
4
  class JenkinsInstance < Katello::Model
5
    self.include_root_in_json = false
6

    
7
    attr_accessor :client, :server_version
8
    include Katello::Glue
9
    include ForemanPipeline::Authorization::JenkinsInstance
10

    
11
    attr_accessible :name, :url, :organization_id, :pubkey, :jenkins_home, :cert_path, :jenkins_user_id
12

    
13
    belongs_to :organization
14
    has_many :jobs, :class_name => "ForemanPipeline::Job", :dependent => :nullify
15
    belongs_to :jenkins_user, :class_name => "ForemanPipeline::JenkinsUser"
16

    
17
    FILEPATH_REGEX = /\A(\/|~)[a-z0-9\-_.\/]*[^\/]\z/i
18

    
19
    validates :name, :presence => true
20
    validates :cert_path, :format => {:with => FILEPATH_REGEX }
21
    validates :url, :uniqueness => true, :format => { :with => /\A(http|https):\/\/\S+:\d{1,4}\z/}
22
    validates :organization, :presence => true
23
    validates :jenkins_home, :format => { :with => FILEPATH_REGEX }
24

    
25
    scoped_search :on => :name , :complete_value => true
26

    
27
    # TODO: loose coupling
28
    def create_client(username = nil, password = nil)
29
      fail "Cannot create Jenkins client: no url in Jenkins Instance" if url.nil?
30
      fail "Token required if username given." if !username.nil? && password.nil?
31

    
32
      if !@client.nil? && @client.username.nil? && !username.nil?
33
        @client = new_client username, password
34
      else
35
        @client ||= new_client username, password
36
      end
37
      @client
38
    end
39

    
40
    private
41

    
42
    def authenticated_client(username, password, hash_args)
43
      JenkinsApi::Client.new(hash_args.merge({:username => username,
44
                                              :password => password}))
45
    end
46

    
47
    def new_client(username, password)
48
      hash_args = {:server_url => url, :log_level => Logger::DEBUG}
49
      return JenkinsApi::Client.new(hash_args) if username.nil?
50
      authenticated_client username, password, hash_args
51
    end
52

    
53
  end
54
end