Project

General

Profile

Download (6.91 KB) Statistics
| Branch: | Tag: | Revision:

foreman-docker / lib / foreman_docker / engine.rb @ a223d2be

1
require 'fast_gettext'
2
require 'gettext_i18n_rails'
3
require 'fog/fogdocker'
4
require 'wicked'
5
require 'docker'
6
require 'deface'
7

    
8
module ForemanDocker
9
  # Inherit from the Rails module of the parent app (Foreman), not the plugin.
10
  # Thus, inherits from ::Rails::Engine and not from Rails::Engine
11
  class Engine < ::Rails::Engine
12
    engine_name 'foreman_docker'
13

    
14
    config.autoload_paths += Dir["#{config.root}/app/controllers/concerns"]
15
    config.autoload_paths += Dir["#{config.root}/app/models/concerns"]
16

    
17
    initializer 'foreman_docker.load_app_instance_data' do |app|
18
      ForemanDocker::Engine.paths['db/migrate'].existent.each do |path|
19
        app.config.paths['db/migrate'] << path
20
      end
21
    end
22

    
23
    foreman_docker_assets = %w[foreman_docker/autocomplete.css
24
                               foreman_docker/terminal.css
25
                               foreman_docker/container_image_search.js
26
                               foreman_docker/create_registry.js]
27

    
28
    initializer "foreman_docker.assets.precompile" do |app|
29
      app.config.assets.precompile += foreman_docker_assets
30
    end
31

    
32
    initializer 'foreman_docker.configure_assets', :group => :assets do
33
      SETTINGS[:foreman_docker] =
34
        { :assets => { :precompile => foreman_docker_assets } }
35
    end
36

    
37
    initializer 'foreman_docker.register_gettext', :after => :load_config_initializers do
38
      locale_dir = File.join(File.expand_path('../../..', __FILE__), 'locale')
39
      locale_domain = 'foreman_docker'
40

    
41
      Foreman::Gettext::Support.add_text_domain locale_domain, locale_dir
42
    end
43

    
44
    initializer 'foreman_docker.register_plugin', :before => :finisher_hook do
45
      Foreman::Plugin.register :foreman_docker do
46
        requires_foreman '>= 1.15'
47
        compute_resource ForemanDocker::Docker
48

    
49
        sub_menu :top_menu, :containers_menu, :caption => N_('Containers'),
50
                                              :icon => 'fa fa-cube',
51
                                              :after => :monitor_menu do
52
          menu :top_menu, :containers,    :caption => N_('All Containers'),
53
                                          :url_hash => { :controller => :containers,
54
                                                         :action => :index }
55
          menu :top_menu, :new_container, :caption => N_('Create Container'),
56
                                          :url_hash => { :controller => :containers,
57
                                                         :action => :new }
58
          menu :top_menu, :registries, :caption => N_('Registries'),
59
                                       :url_hash => { :controller => :registries,
60
                                                      :action => :index }
61
        end
62

    
63
        security_block :containers do
64
          permission :view_containers,
65
                     { :containers          => [:index, :show],
66
                       :'api/v2/containers' => [:index, :show, :logs] },
67
                     :resource_type => 'Container'
68
          permission :commit_containers, { :containers => [:commit] },
69
                     :resource_type => 'Container'
70
          permission :create_containers,
71
                     { :'containers/steps'  => [:show, :update],
72
                       :containers          => [:new],
73
                       :'api/v2/containers' => [:create, :power] },
74
                     :resource_type => 'Container'
75
          permission :destroy_containers,
76
                     { :containers          => [:destroy],
77
                       :'api/v2/containers' => [:destroy] },
78
                     :resource_type => 'Container'
79
          permission :power_compute_resources_vms,
80
                     { :containers          => [:power],
81
                       :'api/v2/containers' => [:create, :power] },
82
                     :resource_type => 'ComputeResource'
83
        end
84

    
85
        security_block :registries do
86
          permission :view_registries,
87
                     { :registries => [:index, :show],
88
                       :'api/v2/registries' => [:index, :show] },
89
                     :resource_type => 'DockerRegistry'
90
          permission :create_registries,
91
                     { :registries  => [:new, :create, :update, :edit],
92
                       :'api/v2/registries' => [:create, :update] },
93
                     :resource_type => 'DockerRegistry'
94
          permission :destroy_registries,
95
                     { :registries => [:destroy],
96
                       :'api/v2/registries' => [:destroy] },
97
                     :resource_type => 'DockerRegistry'
98
        end
99

    
100
        security_block :image_search do
101
          permission :search_repository_image_search,
102
                     { :image_search => [:auto_complete_repository_name,
103
                                         :auto_complete_image_tag,
104
                                         :search_repository] },
105
                     :resource_type => 'Docker/ImageSearch'
106
        end
107

    
108
        add_all_permissions_to_default_roles
109

    
110
        parameter_filter ComputeResource, :email
111

    
112
        # apipie API documentation
113
        # Only available in 1.8, otherwise it has to be in the initializer below
114
        if SETTINGS[:version].to_s.include?('develop') ||
115
           Gem::Version.new(SETTINGS[:version].notag) >= Gem::Version.new('1.8')
116
          apipie_documented_controllers [
117
            "#{ForemanDocker::Engine.root}/app/controllers/api/v2/*.rb"]
118
        end
119
      end
120
    end
121

    
122
    initializer "foreman_docker.apipie" do
123
      # this condition is here for compatibility reason to work with Foreman 1.4.x
124
      # Also need to handle the reverse of the 1.8 method above
125
      unless SETTINGS[:version].to_s.include?('develop') ||
126
             Gem::Version.new(SETTINGS[:version].notag) >= Gem::Version.new('1.8')
127
        if Apipie.configuration.api_controllers_matcher.is_a?(Array)
128
          Apipie.configuration.api_controllers_matcher <<
129
            "#{ForemanDocker::Engine.root}/app/controllers/api/v2/*.rb"
130
        end
131
      end
132
    end
133

    
134
    require 'fog/fogdocker/models/compute/server'
135
    require 'fog/fogdocker/models/compute/image'
136
    require 'fog/fogdocker/models/compute/images'
137
    require File.expand_path('../../../app/models/concerns/fog_extensions/fogdocker/server',
138
                             __FILE__)
139
    require File.expand_path('../../../app/models/concerns/fog_extensions/fogdocker/image',
140
                             __FILE__)
141
    require File.expand_path('../../../app/models/concerns/fog_extensions/fogdocker/images',
142
                             __FILE__)
143
    config.to_prepare do
144
      Fog::Compute::Fogdocker::Server.send(:include, ::FogExtensions::Fogdocker::Server)
145
      Fog::Compute::Fogdocker::Image.send(:include, ::FogExtensions::Fogdocker::Image)
146
      # Compatibility fixes - to be removed once 1.7 compatibility is no longer required
147
      Fog::Compute::Fogdocker::Images.send(:include, ::FogExtensions::Fogdocker::Images)
148
      ::Taxonomy.send(:include, ForemanDocker::TaxonomyExtensions)
149
      ComputeResource.send(:include, ForemanDocker::ComputeResourceExtensions)
150
    end
151
  end
152
end