Project

General

Profile

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

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

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
    initializer "foreman_docker.assets.precompile" do |app|
24
      app.config.assets.precompile += %w(foreman_docker/autocomplete.css
25
                                         foreman_docker/terminal.css
26
                                         foreman_docker/image_step.js)
27
    end
28

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

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

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

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

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

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

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

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

    
106
        parameter_filter ComputeResource, :email
107

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

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

    
130
    rake_tasks do
131
      load "#{ForemanDocker::Engine.root}/lib/foreman_docker/tasks/test.rake"
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