Project

General

Profile

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

foreman-docker / app / controllers / containers / steps_controller.rb @ 9d5ef1c9

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_action :find_state
9
    before_action :build_state, :only => [:update]
10
    before_action :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}", state_params)
39
      instance_variable_set("@docker_container_wizard_states_#{step}", s)
40
    end
41

    
42
    def docker_parameters_permited_params
43
      [:key, :reference_id, :value, :_destroy, :id]
44
    end
45

    
46
    def state_params
47
      attrs = case step
48
              when :preliminary
49
                [:wizard_state, :compute_resource_id]
50
              when :image
51
                [:repository_name, :tag, :wizard_state, :registry_id, :capsule_id, :katello]
52
              when :configuration
53
                [:name, :command, :entrypoint, :cpu_set, :cpu_shares, :memory, :wizard_state]
54
              when :environment
55
                [:tty, :docker_container_wizard_state_id,
56
                 :attach_stdin, :attach_stdout, :attach_stderr,
57
                 :exposed_ports_attributes => docker_parameters_permited_params,
58
                 :environment_variables_attributes => docker_parameters_permited_params,
59
                 :dns_attributes => docker_parameters_permited_params
60
                ]
61
              end
62

    
63
      params.require("docker_container_wizard_states_#{step}").permit(*attrs)
64
    end
65

    
66
    def set_form
67
      instance_variable_set(
68
        "@docker_container_wizard_states_#{step}",
69
        @state.send(:"#{step}") || @state.send(:"build_#{step}"))
70
    end
71

    
72
    def create_container(start = true)
73
      @state.send(:"create_#{step}", state_params)
74
      service = Service::Containers.new
75
      container = if start.is_a? TrueClass
76
                    service.start_container!(@state)
77
                  else
78
                    service.create_container!(@state)
79
                  end
80
      
81
      if container.present?
82
        process_success(:object => container, :success_redirect => container_path(container))
83
      else
84
        @docker_container_wizard_states_environment = @state.environment
85
        process_error(
86
          :redirect => containers_path,
87
          :error_msg => service.full_messages.join(','),
88
          :object => @state.environment)
89
      end
90
    end
91
  end
92
end