## Copyright:: Chef Software Inc.# License:: Apache License, Version 2.0## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#autoload:Set,"set"require"chef/server_api"require_relative"../service_exceptions"moduleChefCLImodulePolicyfileclassRevIDLockDataMapattr_reader:policy_nameattr_reader:lock_info_by_rev_iddefinitialize(policy_name,lock_info_by_rev_id)@policy_name=policy_name@lock_info_by_rev_id=lock_info_by_rev_idenddefcb_info_for(rev_id,cookbook_name)lock=lock_info_by_rev_id[rev_id]cookbook_lock=lock["cookbook_locks"][cookbook_name]ifcookbook_lock[cookbook_lock["version"],cookbook_lock["identifier"]]elsenilendenddefcbs_with_differing_idscbs_with_differing_ids=Set.newall_cookbook_names.eachdo|cookbook_name|all_identifiers=lock_info_by_rev_id.inject(Set.new)do|id_set,(_rev_id,rev_info)|cookbook_lock=rev_info["cookbook_locks"][cookbook_name]identifier=cookbook_lock&&cookbook_lock["identifier"]id_set<<identifierendcbs_with_differing_ids<<cookbook_nameifall_identifiers.size>1endcbs_with_differing_idsenddefall_cookbook_nameslock_info_by_rev_id.inject(Set.new)do|cb_set,(_rev_id,rev_info)|cb_set.merge(rev_info["cookbook_locks"].keys)endendendclassPolicyGroupRevIDMapincludeEnumerableattr_reader:policy_nameattr_reader:revision_ids_by_groupdefinitialize(policy_name,revision_ids_by_group)@policy_name=policy_name@revision_ids_by_group=revision_ids_by_groupenddefunique_revision_idsrevision_ids_by_group.values.uniqenddefpolicy_group_namesrevision_ids_by_group.keysenddefmax_group_name_lengthpolicy_group_names.map(&:size).maxenddefformat_revision_idsrevision_ids_by_group.inject({})do|map,(group_name,rev_id)|map[group_name]=yieldrev_idmapendenddefempty?policy_group_names.empty?enddefeachrevision_ids_by_group.eachdo|group_name,rev_id|yieldgroup_name,rev_idendendendclassListerattr_accessor:policy_lock_contentattr_reader:configdefinitialize(config: nil)@config=config@policies_by_name=nil@policies_by_group=nil@policy_lock_content={}@active_revisions=nilend# A Hash with the following format# {# "appserver" => {# "1111111111111111111111111111111111111111111111111111111111111111" => {},# "2222222222222222222222222222222222222222222222222222222222222222" => {}# },defpolicies_by_name@policies_by_name||fetch_policy_lists@policies_by_nameend# A Hash with the following format:# "dev" => {# "appserver" => "1111111111111111111111111111111111111111111111111111111111111111",# "load-balancer" => "5555555555555555555555555555555555555555555555555555555555555555",# "db" => "9999999999999999999999999999999999999999999999999999999999999999"# }defpolicies_by_group@policies_by_group||fetch_policy_lists@policies_by_groupenddefrevision_info_for(policy_name,_revision_id_list)RevIDLockDataMap.new(policy_name,policy_lock_content[policy_name])enddefrevision_ids_by_group_for_each_policypolicies_by_name.eachdo|policy_name,_policies|rev_id_by_group=revision_ids_by_group_for(policy_name)yieldpolicy_name,rev_id_by_groupendenddefrevision_ids_by_group_for(policy_name)map=policies_by_group.inject({})do|rev_id_map,(group_name,rev_id_map_for_group)|rev_id_map[group_name]=rev_id_map_for_group[policy_name]rev_id_mapendPolicyGroupRevIDMap.new(policy_name,map)enddeforphaned_revisions(policy_name)orphans=[]policies_by_name[policy_name].eachdo|rev_id,_data|orphans<<rev_idunlessactive_revisions.include?(rev_id)endorphansenddefactive_revisions@active_revisions||=policies_by_group.inject(Set.new)do|set,(_group,policy_name_rev_id_map)|policy_name_rev_id_map.eachdo|policy_name,rev_id|set<<rev_idendsetendenddefempty?policies_by_name.empty?&&policies_by_group.empty?enddefhttp_client@http_client||=Chef::ServerAPI.new(config.chef_server_url,signing_key_filename: config.client_key,client_name: config.node_name)end# @api private# Sets internal copy of policyfile data to policies_by_name and# policies_by_group. Used for internal testing.defset!(policies_by_name,policies_by_group)@policies_by_name=policies_by_name@policies_by_group=policies_by_group@active_revisions=nilendprivatedeffetch_policy_listspolicy_list_data=http_client.get("policies")set_policies_by_name_from_api(policy_list_data)policy_group_data=http_client.get("policy_groups")set_policies_by_group_from_api(policy_group_data)enddefset_policies_by_name_from_api(policy_list_data)@policies_by_name=policy_list_data.inject({})do|map,(policy_name,policy_info)|map[policy_name]=policy_info["revisions"]mapendenddefset_policies_by_group_from_api(policy_group_data)@policies_by_group=policy_group_data.inject({})do|map,(policy_group,policy_info)|map[policy_group]=(policy_info["policies"]||[]).inject({})do|rev_map,(policy_name,rev_info)|rev_map[policy_name]=rev_info["revision_id"];rev_mapendmapendendendendend