Project

General

Profile

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

foreman_docker / app / controllers / containers / steps_controller.rb @ ada932ab

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

    
5
    steps :preliminary, :image, :configuration, :environment
6
    before_filter :find_container
7

    
8
    def show
9
      case step
10
      when :preliminary
11
        @container_resources = ComputeResource.select { |cr| cr.provider == 'Docker' }
12
      when :image
13
      when :configuration
14
      when :environment
15
      end
16
      render_wizard
17
    end
18

    
19
    # rubocop:disable Metrics/MethodLength
20
    def update
21
      case step
22
      when :preliminary
23
        @container.update_attribute(:compute_resource_id, params[:container][:compute_resource_id])
24
      when :image
25
        image = update_or_create_image(params[:image][:image_id], params[:image][:registry_id])
26
        @container.update_attributes!(
27
            :image => image,
28
            :tag => DockerTag.find_or_create_by_tag_and_docker_image_id!(params[:container][:tag],
29
                                                                         image.id))
30
      when :configuration
31
        @container.update_attributes(params[:container])
32
      when :environment
33
        @container.update_attributes(params[:container])
34
        if (response = start_container)
35
          @container.uuid = response.id
36
        else
37
          process_error(:object => @container.compute_resource, :render => 'environment')
38
          return
39
        end
40
      end
41
      render_wizard @container
42
    end
43

    
44
    private
45

    
46
    def update_or_create_image(id, registry_id)
47
      image = DockerImage.find_or_create_by_image_id!(id)
48
      begin
49
        image.registries << DockerRegistry.find(registry_id) \
50
          unless params[:image][:registry_id].empty?
51
      # rubocop:disable Lint/HandleExceptions
52
      rescue ActiveRecord::StatementInvalid
53
        # ignore, someone else already added the image to the registry
54
      end
55
      image
56
    end
57

    
58
    def finish_wizard_path
59
      container_path(:id => params[:container_id])
60
    end
61

    
62
    def allowed_resources
63
      ComputeResource.authorized(:view_compute_resources)
64
    end
65

    
66
    def find_container
67
      @container = Container.find(params[:container_id])
68
    rescue ActiveRecord::RecordNotFound
69
      not_found
70
    end
71

    
72
    def start_container
73
      @container.compute_resource.create_container(@container.parametrize)
74
    end
75
  end
76
end