Project

General

Profile

Revision e4e8739b

Added by Daniel Lobato Garcia over 7 years ago

Refs #7867 - Container wizard state refactor

View differences:

app/controllers/concerns/foreman_docker/find_container.rb
1
# To be replaced by find_resource, FindCommon after 1.6 support is deprecated
2
module ForemanDocker
3
  module FindContainer
4
    extend ActiveSupport::Concern
5

  
6
    def find_container
7
      if params[:id].blank?
8
        not_found
9
        return
10
      end
11
      @container = Container.authorized("#{action_permission}_#{controller_name}".to_sym)
12
                            .find(params[:id])
13
    end
14

  
15
    def allowed_resources
16
      ForemanDocker::Docker.authorized(:view_compute_resources)
17
    end
18
  end
19
end
app/controllers/containers/steps_controller.rb
1 1
module Containers
2 2
  class StepsController < ::ApplicationController
3 3
    include Wicked::Wizard
4
    include ForemanDocker::FindContainer
4 5

  
5 6
    steps :preliminary, :image, :configuration, :environment
6
    before_filter :find_state
7
    before_filter :build_state
8
    before_filter :set_form
7 9

  
8
    # rubocop:disable Metrics/CyclomaticComplexity
9 10
    def show
10 11
      case step
11 12
      when :preliminary
12
        @container_resources = allowed_resources.select { |cr| cr.provider == 'Docker' }
13
        @preliminary = @state.preliminary || @state.build_preliminary
14
      when :image
15
        @image = @state.image || @state.build_image
16
      when :configuration
17
        @configuration = @state.configuration || @state.build_configuration
18
      when :environment
19
        @environment = @state.environment || @state.build_environment
13
        @container_resources = allowed_resources
20 14
      end
21 15
      render_wizard
22 16
    end
23 17

  
24
    # rubocop:disable Metrics/MethodLength
25 18
    def update
26 19
      case step
27
      when :preliminary
28
        @state.create_preliminary!(params[:docker_container_wizard_states_preliminary])
29
      when :image
30
        @state.create_image!(params[:docker_container_wizard_states_image])
31
      when :configuration
32
        @state.create_configuration!(params[:docker_container_wizard_states_configuration])
33 20
      when :environment
34
        @state.create_environment!(params[:docker_container_wizard_states_environment])
21
        @state.create_environment(params[:"docker_container_wizard_states_#{step}"])
35 22
        container = Service::Containers.start_container!(@state)
36 23
        if container
37 24
          return redirect_to container_path(container)
......
46 33

  
47 34
    private
48 35

  
49
    def allowed_resources
50
      ComputeResource.authorized(:view_compute_resources)
51
    end
52

  
53
    def find_state
36
    def build_state
54 37
      @state = DockerContainerWizardState.find(params[:wizard_state_id])
38
      @state.send(:"build_#{step}", params[:"docker_container_wizard_states_#{step}"])
55 39
    rescue ActiveRecord::RecordNotFound
56 40
      not_found
57 41
    end
42

  
43
    def set_form
44
      instance_variable_set("@#{step}", @state.send(:"#{step}") || @state.send(:"build_#{step}"))
45
    end
58 46
  end
59 47
end
app/controllers/containers_controller.rb
1
# rubocop:disable Metrics/ClassLength
2 1
class ContainersController < ::ApplicationController
2
  include ForemanDocker::FindContainer
3

  
3 4
  before_filter :find_container, :only => [:show, :commit]
4 5

  
5 6
  def index
......
14 15
  end
15 16

  
16 17
  def new
17
    redirect_to wizard_state_step_path(
18
                    :wizard_state_id => DockerContainerWizardState.create!.id,
19
                    :id => :preliminary)
18
    redirect_to wizard_state_step_path(:wizard_state_id => DockerContainerWizardState.create.id,
19
                                       :id              => :preliminary)
20 20
  end
21 21

  
22 22
  def destroy
23
    if resource_deletion
23
    if container_deletion
24 24
      process_success(:success_redirect => containers_path,
25 25
                      :success_msg      => (_("Container %s is being deleted.") %
26 26
                                            @deleted_identifier))
......
61 61
    end
62 62
  end
63 63

  
64
  def resource_deletion
64
  def container_deletion
65 65
    # Unmanaged container - only present in Compute Resource
66 66
    if params[:compute_resource_id].present?
67 67
      @deleted_identifier  = params[:id]
......
83 83
    logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
84 84
    false
85 85
  end
86

  
87
  def allowed_resources
88
    ForemanDocker::Docker.authorized(:view_compute_resources)
89
  end
90

  
91
  # To be replaced by find_resource after 1.6 support is deprecated
92
  def find_container
93
    if params[:id].blank?
94
      not_found
95
      return
96
    end
97
    @container = Container.authorized("#{action_permission}_#{controller_name}".to_sym)
98
                          .find(params[:id])
99
  end
100 86
end
app/controllers/image_search_controller.rb
30 30
  end
31 31

  
32 32
  def hub_image_exists?(terms)
33
    @cr.exist?(terms)
33
    @compute_resource.exist?(terms)
34 34
  end
35 35

  
36 36
  def hub_auto_complete_image_tags(terms)
37
    @cr.tags(terms)
37
    @compute_resource.tags(terms)
38 38
  end
39 39

  
40 40
  def hub_search_image(terms)
41
    @cr.search(terms)
41
    @compute_resource.search(terms)
42 42
  end
43 43

  
44 44
  def registry_image_exists?(term)
......
67 67
  end
68 68

  
69 69
  def find_resource
70
    if !params[:registry_id].empty?
70
    if params[:registry_id].present?
71 71
      @registry = DockerRegistry.authorized(:view_registries).find(params[:registry_id])
72 72
    else
73
      @cr = ComputeResource.authorized(:view_compute_resources).find(params[:id])
73
      @compute_resource = ComputeResource.authorized(:view_compute_resources).find(params[:id])
74 74
    end
75 75
  rescue ActiveRecord::RecordNotFound
76 76
    not_found
app/models/docker_container_wizard_state.rb
8 8
  has_one :environment, :class_name => DockerContainerWizardStates::Environment,
9 9
          :dependent => :destroy, :validate => true, :autosave => true
10 10

  
11
  delegate :compute_resource_id, :to => :preliminary
11
  delegate :compute_resource_id,   :to => :preliminary
12 12
  delegate :environment_variables, :to => :environment
13 13

  
14 14
  def container_attributes
15
    { :repository_name => image.repository_name,
16
      :tag => image.tag,
17
      :registry_id => image.registry_id,
18
      :name  => configuration.name,
15
    { :repository_name     => image.repository_name,
16
      :tag                 => image.tag,
17
      :registry_id         => image.registry_id,
18
      :name                => configuration.name,
19 19
      :compute_resource_id => preliminary.compute_resource_id,
20
      :tty => environment.tty,
21
      :memory => configuration.memory,
22
      :entrypoint => configuration.entrypoint,
23
      :command => configuration.command,
24
      :attach_stdout => environment.attach_stdout,
25
      :attach_stdin => environment.attach_stdin,
26
      :attach_stderr => environment.attach_stderr,
27
      :cpu_shares => configuration.cpu_shares,
28
      :cpu_set => configuration.cpu_set }
20
      :tty                 => environment.tty,
21
      :memory              => configuration.memory,
22
      :entrypoint          => configuration.entrypoint,
23
      :command             => configuration.command,
24
      :attach_stdout       => environment.attach_stdout,
25
      :attach_stdin        => environment.attach_stdin,
26
      :attach_stderr       => environment.attach_stderr,
27
      :cpu_shares          => configuration.cpu_shares,
28
      :cpu_set             => configuration.cpu_set }
29 29
  end
30 30
end
app/models/docker_container_wizard_states/image.rb
5 5
                              :foreign_key => :docker_container_wizard_state_id
6 6
    delegate :compute_resource_id, :to => :wizard_state
7 7

  
8
    validates :tag,   :presence => true
8
    validates :tag,             :presence => true
9 9
    validates :repository_name, :presence => true
10 10
  end
11 11
end
lib/foreman_docker/engine.rb
11 11
  class Engine < ::Rails::Engine
12 12
    engine_name 'foreman_docker'
13 13

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

  
16 17
    initializer 'foreman_docker.load_app_instance_data' do |app|

Also available in: Unified diff