Project

General

Profile

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

foreman_pipeline / app / models / foreman_pipeline / jenkins_instance.rb @ be52ca64

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 Glue::ElasticSearch::JenkinsInstance
10
    include ForemanPipeline::Authorization::JenkinsInstance
11

    
12
    belongs_to :organization
13
    has_many :jobs, :class_name => "ForemanPipeline::Job", :dependent => :nullify
14

    
15
    FILEPATH_REGEX = /^(\/|~)[a-z0-9\-_.\/]*[^\/]$/i
16
    
17
    validates :name, :presence => true
18
    validates :cert_path, :format => {:with => FILEPATH_REGEX }
19
    validates :url, :uniqueness => true, :format => { :with => /^(http|https):\/\/\S+:\d{1,4}$/}
20
    validates :organization, :presence => true
21
    validates :jenkins_home, :format => { :with => FILEPATH_REGEX }
22

    
23
    # TODO: loose coupling
24
    def create_client(username = nil, password = nil)
25
      fail "Cannot create Jenkins client: no url in Jenkins Instance" if url.nil?
26
      fail "Token required if username given." if !username.nil? && password.nil?
27
      
28
      if !@client.nil? && @client.username.nil? && !username.nil?
29
        @client = new_client username, password
30
      else
31
        @client ||= new_client username, password
32
      end
33
      @client
34
    end
35
    
36
    private
37

    
38
    def authenticated_client(username, password, hash_args) 
39
      JenkinsApi::Client.new(hash_args.merge({:username => username,
40
                                              :password => password}))          
41
    end
42

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