Project

General

Profile

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

foreman-docker / app / controllers / containers / steps_controller.rb @ 31e5a925

1
module Containers
2
  class StepsController < ::ApplicationController
3
    include Wicked::Wizard
4
    include ForemanDocker::FindContainer
5

    
6
    steps :preliminary, :image, :configuration, :environment
7

    
8
    before_filter :find_state
9
    before_filter :build_state, :only => [:update]
10
    before_filter :set_form, :only => [:show]
11

    
12
    def show
13
      @container_resources = allowed_resources if step == :preliminary
14
      render_wizard
15
    end
16

    
17
    def update
18
      if step == wizard_steps.last
19
        if process_resource!(@state).nil?
20
          render_wizard @state
21
        else
22
          params[:start_on_create] ? create_container : create_container(false)
23
        end
24
      else
25
        render_wizard @state
26
      end
27
    end
28

    
29
    private
30

    
31
    def find_state
32
      @state = DockerContainerWizardState.find(params[:wizard_state_id])
33
    rescue ActiveRecord::RecordNotFound
34
      not_found
35
    end
36

    
37
    def build_state
38
      s = @state.send(:"build_#{step}", params[:"docker_container_wizard_states_#{step}"])
39
      instance_variable_set("@docker_container_wizard_states_#{step}", s)
40
    end
41

    
42
    def set_form
43
      instance_variable_set(
44
        "@docker_container_wizard_states_#{step}",
45
        @state.send(:"#{step}") || @state.send(:"build_#{step}"))
46
    end
47

    
48
    def create_container(start = true)
49
      @state.send(:"create_#{step}", params[:"docker_container_wizard_states_#{step}"])
50
      service = Service::Containers.new
51
      container = if start.is_a? TrueClass
52
                    service.start_container!(@state)
53
                  else
54
                    service.create_container!(@state)
55
                  end
56
      if container.present?
57
        process_success(:object => container, :success_redirect => container_path(container))
58
      else
59
        @docker_container_wizard_states_environment = @state.environment
60
        process_error(
61
          :redirect => containers_path,
62
          :error_msg => service.full_messages.join(','),
63
          :object => @state.environment)
64
      end
65
    end
66
  end
67
end