forked from theforeman/foreman_ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roles_importer.rb
51 lines (42 loc) · 1.17 KB
/
roles_importer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module ForemanAnsible
# imports roles from smart proxy
class RolesImporter
attr_reader :ansible_proxy
def initialize(proxy = nil)
@ansible_proxy = proxy
end
def import_role_names
return import_roles remote_roles if ansible_proxy
import_roles local_roles
end
def import_roles(roles)
imported = roles.map do |role_name|
::AnsibleRole.find_or_initialize_by(:name => role_name)
end
detect_changes imported
end
def detect_changes(imported)
changes = {}.with_indifferent_access
old, changes[:new] = imported.partition { |role| role.id.present? }
changes[:obsolete] = ::AnsibleRole.where.not(:id => old.map(&:id))
changes
end
private
def find_proxy_api
raise ::Foreman::Exception.new(N_('Proxy not found')) unless ansible_proxy
@proxy_api = ::ProxyAPI::Ansible.new(:url => ansible_proxy.url)
end
def proxy_api
return @proxy_api if @proxy_api
find_proxy_api
end
def local_roles
Dir.glob('/etc/ansible/roles/*').map do |path|
path.split('/').last
end
end
def remote_roles
proxy_api.roles
end
end
end