Sha256: 3175a04b74ab04988d2536ca3752716b9867876063a2b254e3559ff3e6ec219f

Contents?: true

Size: 1.17 KB

Versions: 8

Compression:

Stored size: 1.17 KB

Contents

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
foreman_ansible-1.4.2 app/services/foreman_ansible/roles_importer.rb
foreman_ansible-1.4.1 app/services/foreman_ansible/roles_importer.rb
foreman_ansible-1.4.0 app/services/foreman_ansible/roles_importer.rb
foreman_ansible-1.3.1 app/services/foreman_ansible/roles_importer.rb
foreman_ansible-1.3.0 app/services/foreman_ansible/roles_importer.rb
foreman_ansible-1.2.1 app/services/foreman_ansible/roles_importer.rb
foreman_ansible-1.2 app/services/foreman_ansible/roles_importer.rb
foreman_ansible-1.1 app/services/foreman_ansible/roles_importer.rb