Class: ChefLicensing::LicenseKeyFetcher::File
- Inherits:
-
Object
- Object
- ChefLicensing::LicenseKeyFetcher::File
- Defined in:
- lib/chef-licensing/license_key_fetcher/file.rb
Overview
Represents a fethced license ID recorded on disk
Constant Summary collapse
- LICENSE_KEY_FILE =
"licenses.yaml".freeze
- LICENSE_FILE_FORMAT_VERSION =
"4.0.0".freeze
- LICENSE_TYPES =
License types list
{ free: :free, trial: :trial, commercial: :commercial, }.freeze
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#local_dir ⇒ Object
Optional local path to use to seek.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
- .default_file_location ⇒ Object
- .fetch_license_keys_based_on_type(license_type, opts = {}) ⇒ Object
- .fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil, opts = {}) ⇒ Object
- .user_has_active_trial_license?(opts = {}) ⇒ Boolean
Instance Method Summary collapse
- #fetch ⇒ Object
- #fetch_allowed_license_types_for_addition ⇒ Object
- #fetch_license_keys(licenses) ⇒ Object
- #fetch_license_keys_based_on_type(license_type) ⇒ Object
- #fetch_license_types ⇒ Object
- #fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil) ⇒ Object
-
#initialize(opts) ⇒ File
constructor
A new instance of File.
-
#persist(license_key, license_type = nil) ⇒ Object
Writes a license_id file to disk in the location specified, with the content given.
-
#persisted? ⇒ Boolean
Returns true if a license_key file exists.
- #user_has_active_trial_license? ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ File
Returns a new instance of File.
31 32 33 34 35 36 37 38 39 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 31 def initialize(opts) @opts = opts @logger = ChefLicensing::Config.logger @contents_ivar = nil @location = nil @opts[:dir] ||= LicenseKeyFetcher::File.default_file_location @local_dir = @opts[:dir] end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
28 29 30 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 28 def contents @contents end |
#local_dir ⇒ Object
Optional local path to use to seek
29 30 31 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 29 def local_dir @local_dir end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
28 29 30 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 28 def location @location end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
28 29 30 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 28 def logger @logger end |
Class Method Details
.default_file_location ⇒ Object
149 150 151 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 149 def self.default_file_location ChefConfig::PathHelper.home(".chef") end |
.fetch_license_keys_based_on_type(license_type, opts = {}) ⇒ Object
153 154 155 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 153 def self.fetch_license_keys_based_on_type(license_type, opts = {}) new(opts).fetch_license_keys_based_on_type(license_type) end |
.fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil, opts = {}) ⇒ Object
161 162 163 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 161 def self.fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil, opts = {}) new(opts).fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system) end |
.user_has_active_trial_license?(opts = {}) ⇒ Boolean
157 158 159 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 157 def self.user_has_active_trial_license?(opts = {}) new(opts).user_has_active_trial_license? end |
Instance Method Details
#fetch ⇒ Object
41 42 43 44 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 41 def fetch read_license_key_file contents&.key?(:licenses) ? fetch_license_keys(contents[:licenses]) : [] end |
#fetch_allowed_license_types_for_addition ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 70 def fetch_allowed_license_types_for_addition license_types = i{free trial commercial} existing_license_types = fetch_license_types license_types -= [:trial] if existing_license_types.include? :trial license_types -= [:free] if existing_license_types.include?(:free) || user_has_active_trial_license? license_types.uniq end |
#fetch_license_keys(licenses) ⇒ Object
46 47 48 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 46 def fetch_license_keys(licenses) licenses.collect { |x| x[:license_key] } end |
#fetch_license_keys_based_on_type(license_type) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 79 def fetch_license_keys_based_on_type(license_type) read_license_key_file if contents.nil? [] else contents[:licenses].collect do |x| x[:license_key] if x[:license_type] == license_type end.compact end end |
#fetch_license_types ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 50 def fetch_license_types read_license_key_file if contents.nil? || contents[:licenses].nil? [] else contents[:licenses].collect { |x| x[:license_type] } end end |
#fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 118 def fetch_or_persist_url(license_server_url_from_config, license_server_url_from_system = nil) dir = @opts[:dir] license_key_file_path = "#{dir}/#{LICENSE_KEY_FILE}" create_license_directory_if_not_exist(dir, license_key_file_path) @contents = load_license_file(license_key_file_path) # Two possible cases: # 1. If contents is nil, load basic license data with the latest structure. # 2. If contents is not nil, but the license server URL in contents is different from the system's, # update the license server URL in contents and licenses.yaml file. if @contents.nil? url = license_server_url_from_system || license_server_url_from_config load_basic_license_data_to_contents(url, []) elsif @contents && license_server_url_from_system && license_server_url_from_system != @contents[:license_server_url] @contents[:license_server_url] = license_server_url_from_system end # Ensure the license server URL is returned to the caller in all cases # (even if it's not persisted to the licenses.yaml file on the disk) begin write_license_file(license_key_file_path) rescue StandardError => e handle_error(e) ensure @license_server_url = @contents[:license_server_url] end logger.debug "License server URL: #{@license_server_url}" @license_server_url end |
#persist(license_key, license_type = nil) ⇒ Object
Writes a license_id file to disk in the location specified, with the content given.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 93 def persist(license_key, license_type = nil) raise LicenseKeyNotPersistedError.new("License type #{license_type} is not a valid license type.") unless LICENSE_TYPES[license_type.to_sym] license_data = { license_key: license_key, license_type: LICENSE_TYPES[license_type.to_sym], update_time: DateTime.now.to_s, } dir = @opts[:dir] license_key_file_path = "#{dir}/#{LICENSE_KEY_FILE}" create_license_directory_if_not_exist(dir, license_key_file_path) @contents = load_license_file(license_key_file_path) load_license_data_to_contents(license_data) write_license_file(license_key_file_path) [] end |
#persisted? ⇒ Boolean
Returns true if a license_key file exists.
114 115 116 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 114 def persisted? !!seek end |
#user_has_active_trial_license? ⇒ Boolean
60 61 62 63 64 65 66 67 68 |
# File 'lib/chef-licensing/license_key_fetcher/file.rb', line 60 def user_has_active_trial_license? @active_trial_status = false read_license_key_file if contents&.key?(:licenses) @active_trial_status = contents[:licenses].any? { |license| license[:license_type] == :trial && ChefLicensing.client(license_keys: [license[:license_key]]).active? } end @active_trial_status end |