Class: Chef::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_url) ⇒ Server

Returns a new instance of Server.



9
10
11
# File 'lib/chef/server.rb', line 9

def initialize(root_url)
  @root_url = root_url
end

Instance Attribute Details

#root_urlObject

Returns the value of attribute root_url.



8
9
10
# File 'lib/chef/server.rb', line 8

def root_url
  @root_url
end

Class Method Details

.from_chef_server_url(url) ⇒ Object



13
14
15
16
# File 'lib/chef/server.rb', line 13

def self.from_chef_server_url(url)
  url = url.gsub(/\/organizations\/+[^\/]+\/*$/, '')
  Chef::Server.new(url)
end

Instance Method Details

#direct_account_access?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/chef/server.rb', line 44

def 
  Chef::ServerAPI.new("http://127.0.0.1:9465").get("users")
  true
rescue
  false
end

#parse_server_version(line) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chef/server.rb', line 18

def parse_server_version(line)
  # first line from the /version endpoint will either be this format "chef-server 12.17.5\n"
  # or, when habitat, this format "Package: chef-server/chef-server-nginx/12.17.42/20180413212943\n"
  version_str = if line.include?('/')
                  line.split('/')[2]
                else
                  # Strip everything after '+' using String#partition.
                  # We avoid regex here since Ruby < 3.2 is vulnerable to ReDoS,
                  # and we support Ruby 3.1 in pipelines.
                  line.split(' ').last.partition('+').first
                end

  Gem::Version.new(version_str)
end

#supports_defaulting_to_pivotal?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/chef/server.rb', line 51

def supports_defaulting_to_pivotal?
  version >= Gem::Version.new('12.1.0')
end

#supports_public_key_read_access?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/chef/server.rb', line 55

def supports_public_key_read_access?
  version >= Gem::Version.new('12.5.0')
end

#supports_user_acls?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/chef/server.rb', line 40

def supports_user_acls?
  version >= Gem::Version.new("11.0.1")
end

#versionObject



33
34
35
36
37
38
# File 'lib/chef/server.rb', line 33

def version
  @version ||= begin
                 ver_line = Chef::ServerAPI.new(root_url).get('version').each_line.first
                 parse_server_version(ver_line)
               end
end