Project

General

Profile

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

foreman-docker / app / controllers / api / v2 / containers_controller.rb @ 26c5ba32

1
# module ForemanDocker
2
module Api
3
  module V2
4
    class ContainersController < ::Api::V2::BaseController
5
      before_filter :find_resource, :except => %w(index create)
6

    
7
      resource_description do
8
        resource_id 'containers'
9
        api_version 'v2'
10
        api_base_url '/docker/api/v2'
11
      end
12

    
13
      api :GET, '/containers/', N_('List all containers')
14
      api :GET, '/compute_resources/:compute_resource_id/containers/',
15
          N_('List all containers on a compute resource')
16
      param :compute_resource_id, :identifier
17
      param_group :search_and_pagination, ::Api::V2::BaseController
18

    
19
      def index
20
        if params[:compute_resource_id].present?
21
          scoped = Container.where(:compute_resource_id => params[:compute_resource_id])
22
        else
23
          scoped = Container.where(nil)
24
        end
25
        @containers = scoped.search_for(params[:search], :order => params[:order])
26
                      .paginate(:page => params[:page])
27
      end
28

    
29
      api :GET, '/containers/:id/', N_('Show a container')
30
      api :GET, '/compute_resources/:compute_resource_id/containers/:id',
31
          N_('Show container on a compute resource')
32
      param :id, :identifier, :required => true
33
      param :compute_resource_id, :identifier
34

    
35
      def show
36
      end
37

    
38
      def_param_group :container do
39
        param :container, Hash, :required => true, :action_aware => true do
40
          param :name, String
41
          param_group :taxonomies, ::Api::V2::BaseController
42
          param :compute_resource_id, :identifier, :required => true
43
          param :registry_id, :identifier, :desc => N_('Registry this container will have to ' +
44
                                                        'use to get the image')
45
          param :repository_name, String, :required => true,
46
                                          :desc => N_('Name of the repository to use ' +
47
                                                       'to create the container. e.g. centos')
48
          param :tag, String, :required => true,
49
                              :desc => N_('Tag to use to create the container. e.g. latest')
50
          param :tty, :bool
51
          param :entrypoint, String
52
          param :command, String, :required => true
53
          param :memory, String
54
          param :cpu_shares, :number
55
          param :cpu_set, String
56
          param :environment_variables, Hash
57
          param :attach_stdout, :bool
58
          param :attach_stdin, :bool
59
          param :attach_stderr, :bool
60
          param :capsule_id, :identifier, :desc => N_('The capsule this container will have to ' +
61
                                                       'use to get the image. Relevant for images ' +
62
                                                       'retrieved from katello registry.')
63
        end
64
      end
65

    
66
      api :POST, '/containers/', N_('Create a container')
67
      api :POST, '/compute_resources/:compute_resource_id/containers/',
68
          N_('Create container on a compute resource')
69
      param_group :container, :as => :create
70

    
71
      def create
72
        service = Service::Containers.new
73
        @container = service.start_container!(set_wizard_state)
74
        if service.errors.any?
75
          render :json => { :errors => service.errors,
76
                            :full_messages => service.full_messages
77
                          },
78
                 :status => :unprocessable_entity
79
        else
80
          set_container_taxonomies
81
          process_response @container.save
82
        end
83
      rescue ActiveModel::MassAssignmentSecurity::Error => e
84
        render :json => { :error  => _("Wrong attributes: %s") % e.message },
85
               :status => :unprocessable_entity
86
      end
87

    
88
      api :DELETE, '/containers/:id/', N_('Delete a container')
89
      api :DELETE, '/compute_resources/:compute_resource_id/containers/:id',
90
          N_('Delete container on a compute resource')
91
      param :id, :identifier, :required => true
92
      param :compute_resource_id, :identifier
93

    
94
      def destroy
95
        deleted_identifier = ForemanDocker::ContainerRemover.remove_unmanaged(
96
          @container.compute_resource_id,
97
          @container.uuid)
98

    
99
        if deleted_identifier
100
          process_response @container.destroy
101
        else
102
          render :json => { :error => 'Could not delete container on Docker host' }, :status => :precondition_failed
103
        end
104
      end
105

    
106
      api :GET, '/containers/:id/logs', N_('Show container logs')
107
      api :GET, '/compute_resources/:compute_resource_id/containers/:id/logs',
108
          N_('Show logs from a container on a compute resource')
109
      param :id, :identifier, :required => true
110
      param :compute_resource_id, :identifier
111
      param :stdout, :bool
112
      param :stderr, :bool
113
      param :tail,   Fixnum, N_('Number of lines to tail. Default: 100')
114

    
115
      def logs
116
        render :json => { :logs => Docker::Container.get(@container.uuid)
117
          .logs(:stdout => (params[:stdout] || true),
118
                :stderr => (params[:stderr] || false),
119
                :tail   => (params[:tail] || 100)) }
120
      end
121

    
122
      api :PUT, '/containers/:id/power', N_('Run power operation on a container')
123
      api :PUT, '/compute_resources/:compute_resource_id/containers/:id/power',
124
          N_('Run power operation on a container on a compute resource')
125
      param :id, :identifier, :required => true
126
      param :compute_resource_id, :identifier
127
      param :power_action, String,
128
            :required => true,
129
            :desc     => N_('power action, valid actions are (start), (stop), (status)')
130

    
131
      def power
132
        power_actions = %(start stop status)
133
        if power_actions.include? params[:power_action]
134
          response = if params[:power_action] == 'status'
135
                       { :running => @container.in_fog.ready? }
136
                     else
137
                       { :running => @container.in_fog.send(params[:power_action]) }
138
                     end
139
          render :json => response
140
        else
141
          render :json =>
142
            { :error => _("Unknown method: available power operations are %s") %
143
              power_actions.join(', ') }, :status => :unprocessable_entity
144
        end
145
      end
146

    
147
      private
148

    
149
      def wizard_properties
150
        wizard_props = { :preliminary => [:compute_resource_id],
151
                         :image => [:registry_id, :repository_name, :tag],
152
                         :configuration => [:name, :command, :entrypoint, :cpu_set,
153
                                            :cpu_shares, :memory],
154
                         :environment => [:tty, :attach_stdin, :attach_stdout,
155
                                          :attach_stderr] }
156
        if DockerContainerWizardStates::Image.attribute_names.include?("capsule_id")
157
          wizard_props[:image] << :capsule_id
158
        end
159
        wizard_props
160
      end
161

    
162
      def set_wizard_state
163
        wizard_state = DockerContainerWizardState.create
164
        wizard_properties.each do |step, properties|
165
          property_values = properties.each_with_object({}) do |property, values|
166
            values[:"#{property}"] = params[:container][:"#{property}"]
167
          end
168
          wizard_state.send(:"create_#{step}", property_values)
169
        end
170

    
171
        if params[:container][:environment_variables].present?
172
          wizard_state.environment.environment_variables =
173
            params[:container][:environment_variables]
174
        end
175
        wizard_state.tap(&:save)
176
      end
177

    
178
      def set_container_taxonomies
179
        Taxonomy.enabled_taxonomies.each do |taxonomy|
180
          if params[:container][:"#{taxonomy}"].present?
181
            @container.send(:"#{taxonomy}=", params[:container][:"#{taxonomy}"])
182
          end
183
        end
184
      end
185

    
186
      def action_permission
187
        case params[:action]
188
        when 'logs'
189
          :view
190
        when 'power'
191
          :edit
192
        else
193
          super
194
        end
195
      end
196
    end
197
  end
198
end
199
# end