Project

General

Profile

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

foreman_docker / lib / foreman_docker / engine.rb @ d2e54db4

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
                     :resource_type => 'Container'
64
          permission :commit_containers, { :containers => [:commit] },
65
                     :resource_type => 'Container'
66
          permission :create_containers,
67
                     { :'containers/steps'  => [:show, :update],
68
                       :containers          => [:new],
69
                       :'api/v2/containers' => [:create, :power] },
70
                     :resource_type => 'Container'
71
          permission :destroy_containers,
72
                     { :containers          => [:destroy],
73
                       :'api/v2/containers' => [:destroy] },
74
                     :resource_type => 'Container'
75
          permission :power_compute_resources_vms,
76
                     { :containers          => [:power],
77
                       :'api/v2/containers' => [:create, :power] },
78
                     :resource_type => 'ComputeResource'
79
        end
80

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

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

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

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

    
126
    rake_tasks do
127
      load "#{ForemanDocker::Engine.root}/lib/foreman_docker/tasks/test.rake"
128
    end
129

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