Project

General

Profile

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

foreman_docker / lib / foreman_docker / engine.rb @ 5274f6bc

1
require 'fast_gettext'
2
require 'gettext_i18n_rails'
3
require 'fog'
4
require 'fog/fogdocker'
5
require 'wicked'
6
require 'docker'
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
      app.config.paths['db/migrate'] += ForemanDocker::Engine.paths['db/migrate'].existent
19
    end
20

    
21
    initializer "foreman_docker.assets.precompile" do |app|
22
      app.config.assets.precompile += %w(foreman_docker/autocomplete.css
23
                                         foreman_docker/terminal.css
24
                                         foreman_docker/image_step.js)
25
    end
26

    
27
    initializer 'foreman_docker.configure_assets', :group => :assets do
28
      SETTINGS[:foreman_docker] =
29
        { :assets => { :precompile => ['foreman_docker/autocomplete.css',
30
                                       'foreman_docker/terminal.css',
31
                                       'foreman_docker/image_step.js'] } }
32
    end
33

    
34
    initializer 'foreman_docker.register_gettext', :after => :load_config_initializers do
35
      locale_dir = File.join(File.expand_path('../../..', __FILE__), 'locale')
36
      locale_domain = 'foreman_docker'
37

    
38
      Foreman::Gettext::Support.add_text_domain locale_domain, locale_dir
39
    end
40

    
41
    initializer 'foreman_docker.register_plugin', :after => :finisher_hook do
42
      Foreman::Plugin.register :foreman_docker do
43
        requires_foreman '> 1.4'
44
        compute_resource ForemanDocker::Docker
45

    
46
        sub_menu :top_menu, :containers_menu, :caption => N_('Containers'),
47
                                              :after => :monitor_menu do
48
          menu :top_menu, :containers,    :caption => N_('All containers'),
49
                                          :url_hash => { :controller => :containers,
50
                                                         :action => :index }
51
          menu :top_menu, :new_container, :caption => N_('New container'),
52
                                          :url_hash => { :controller => :containers,
53
                                                         :action => :new }
54
          menu :top_menu, :registries, :caption => N_('Registries'),
55
                                       :url_hash => { :controller => :registries,
56
                                                      :action => :index }
57
        end
58

    
59
        security_block :containers do
60
          permission :view_containers,
61
                     :containers          => [:index, :show],
62
                     :'api/v2/containers' => [:index, :show, :logs]
63
          permission :commit_containers, :containers => [:commit]
64
          permission :create_containers,
65
                     :'containers/steps'  => [:show, :update],
66
                     :containers          => [:new],
67
                     :'api/v2/containers' => [:create, :power]
68
          permission :destroy_containers,
69
                     :containers          => [:destroy],
70
                     :'api/v2/containers' => [:destroy]
71
          permission :power_compute_resources_vms,
72
                     :containers          => [:power],
73
                     :'api/v2/containers' => [:create, :power]
74
        end
75

    
76
        security_block :registries do
77
          permission :view_registries,    :registries         => [:index, :show]
78
          permission :create_registries,  :registries         => [:new, :create, :update, :edit]
79
          permission :destroy_registries, :registries         => [:destroy]
80
        end
81

    
82
        security_block :image_search do
83
          permission :search_repository_image_search,
84
                     :image_search => [:auto_complete_repository_name,
85
                                       :auto_complete_image_tag,
86
                                       :search_repository]
87
        end
88

    
89
        # apipie API documentation
90
        # Only available in 1.8, otherwise it has to be in the initializer below
91
        if SETTINGS[:version].to_s.include?('develop') ||
92
           Gem::Version.new(SETTINGS[:version]) >= Gem::Version.new('1.8')
93
          apipie_documented_controllers [
94
            "#{ForemanDocker::Engine.root}/app/controllers/api/v2/*.rb"]
95
        end
96
      end
97
    end
98

    
99
    initializer "foreman_docker.apipie" do
100
      # this condition is here for compatibility reason to work with Foreman 1.4.x
101
      # Also need to handle the reverse of the 1.8 method above
102
      unless SETTINGS[:version].to_s.include?('develop') ||
103
             Gem::Version.new(SETTINGS[:version]) >= Gem::Version.new('1.8')
104
        if Apipie.configuration.api_controllers_matcher.is_a?(Array)
105
          Apipie.configuration.api_controllers_matcher <<
106
            "#{ForemanDocker::Engine.root}/app/controllers/api/v2/*.rb"
107
        end
108
      end
109
    end
110

    
111
    rake_tasks do
112
      load "#{ForemanDocker::Engine.root}/lib/foreman_docker/tasks/test.rake"
113
    end
114

    
115
    require 'fog/fogdocker/models/compute/server'
116
    require 'fog/fogdocker/models/compute/image'
117
    require 'fog/fogdocker/models/compute/images'
118
    require File.expand_path('../../../app/models/concerns/fog_extensions/fogdocker/server',
119
                             __FILE__)
120
    require File.expand_path('../../../app/models/concerns/fog_extensions/fogdocker/image',
121
                             __FILE__)
122
    require File.expand_path('../../../app/models/concerns/fog_extensions/fogdocker/images',
123
                             __FILE__)
124
    config.to_prepare do
125
      Fog::Compute::Fogdocker::Server.send(:include, ::FogExtensions::Fogdocker::Server)
126
      Fog::Compute::Fogdocker::Image.send(:include, ::FogExtensions::Fogdocker::Image)
127
      # Compatibility fixes - to be removed once 1.7 compatibility is no longer required
128
      Fog::Compute::Fogdocker::Images.send(:include, ::FogExtensions::Fogdocker::Images)
129
      ::Taxonomy.send(:include, ForemanDocker::TaxonomyExtensions)
130
    end
131
  end
132
end