Class: CHBuild::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/chbuild/utils.rb

Overview

Utils class

Class Method Summary collapse

Class Method Details

.fqdn(prefix: nil, job_name: nil, branch_name: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chbuild/utils.rb', line 25

def self.fqdn(prefix: nil, job_name: nil, branch_name: nil)
  vhost = virtual_hostname(
    prefix: prefix,
    job_name: job_name,
    branch_name: branch_name
  )

  main_domain = ENV['DOMAIN'] || CHBuild::DEFAULT_DOMAIN

  "#{vhost}.#{main_domain}"
end

.generate_conatiner_name(prefix: nil, job_name: nil, build_id: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/chbuild/utils.rb', line 5

def self.generate_conatiner_name(prefix: nil, job_name: nil, build_id: nil)
  prefix ||= ENV['CONTAINER_PREFIX']
  prefix = "#{prefix}-" unless prefix.nil? || prefix.empty?

  job_name ||= ENV['JOB_NAME'] || random_string
  build_id ||= ENV['BUILD_ID'] || "r#{Random.rand(1000)}"

  "#{prefix}#{job_name}-#{build_id}"
end

.random_string(length: 8) ⇒ Object



37
38
39
40
# File 'lib/chbuild/utils.rb', line 37

def self.random_string(length: 8)
  o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
  (0...length).map { o[rand(o.length)] }.join
end

.virtual_hostname(prefix: nil, job_name: nil, branch_name: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/chbuild/utils.rb', line 15

def self.virtual_hostname(prefix: nil, job_name: nil, branch_name: nil)
  prefix ||= ENV['HOST_PREFIX']
  prefix = "#{prefix}." unless prefix.nil? || prefix.empty?

  job_name ||= ENV['JOB_NAME'] || random_string.downcase
  branch_name ||= (ENV['BRANCH_NAME'] || 'master').gsub(%r{.*/}, '')

  "#{prefix}#{branch_name}.#{job_name}"
end