Project

General

Profile

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

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

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 in 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 in 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 use
61
                                                       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 in 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 in a compute resource')
91
      param :id, :identifier, :required => true
92
      param :compute_resource_id, :identifier
93

    
94
      def destroy
95
        process_response @container.destroy
96
      end
97

    
98
      api :GET, '/containers/:id/logs', N_('Show container logs')
99
      api :GET, '/compute_resources/:compute_resource_id/containers/:id/logs',
100
          N_('Show logs from a container in a compute resource')
101
      param :id, :identifier, :required => true
102
      param :compute_resource_id, :identifier
103
      param :stdout, :bool
104
      param :stderr, :bool
105
      param :tail,   Fixnum, N_('Number of lines to tail. Default: 100')
106

    
107
      def logs
108
        render :json => { :logs => Docker::Container.get(@container.uuid)
109
          .logs(:stdout => (params[:stdout] || true),
110
                :stderr => (params[:stderr] || false),
111
                :tail   => (params[:tail] || 100)) }
112
      end
113

    
114
      api :PUT, '/containers/:id/power', N_('Run power operation on a container')
115
      api :PUT, '/compute_resources/:compute_resource_id/containers/:id/power',
116
          N_('Run power operation on a container in a compute resource')
117
      param :id, :identifier, :required => true
118
      param :compute_resource_id, :identifier
119
      param :power_action, String,
120
            :required => true,
121
            :desc     => N_('power action, valid actions are (start), (stop), (status)')
122

    
123
      def power
124
        power_actions = %(start stop status)
125
        if power_actions.include? params[:power_action]
126
          response = if params[:power_action] == 'status'
127
                       { :running => @container.in_fog.ready? }
128
                     else
129
                       { :running => @container.in_fog.send(params[:power_action]) }
130
                     end
131
          render :json => response
132
        else
133
          render :json =>
134
            { :error => _("Unknown method: available power operations are %s") %
135
              power_actions.join(', ') }, :status => :unprocessable_entity
136
        end
137
      end
138

    
139
      private
140

    
141
      def wizard_properties
142
        wizard_props = { :preliminary => [:compute_resource_id],
143
                         :image => [:registry_id, :repository_name, :tag],
144
                         :configuration => [:name, :command, :entrypoint, :cpu_set,
145
                                            :cpu_shares, :memory],
146
                         :environment => [:tty, :attach_stdin, :attach_stdout,
147
                                          :attach_stderr] }
148
        if DockerContainerWizardStates::Image.attribute_names.include?("capsule_id")
149
          wizard_props[:image] << :capsule_id
150
        end
151
        wizard_props
152
      end
153

    
154
      def set_wizard_state
155
        wizard_state = DockerContainerWizardState.create
156
        wizard_properties.each do |step, properties|
157
          property_values = properties.each_with_object({}) do |property, values|
158
            values[:"#{property}"] = params[:container][:"#{property}"]
159
          end
160
          wizard_state.send(:"create_#{step}", property_values)
161
        end
162

    
163
        if params[:container][:environment_variables].present?
164
          wizard_state.environment.environment_variables =
165
            params[:container][:environment_variables]
166
        end
167
        wizard_state.tap(&:save)
168
      end
169

    
170
      def set_container_taxonomies
171
        Taxonomy.enabled_taxonomies.each do |taxonomy|
172
          if params[:container][:"#{taxonomy}"].present?
173
            @container.send(:"#{taxonomy}=", params[:container][:"#{taxonomy}"])
174
          end
175
        end
176
      end
177

    
178
      def action_permission
179
        case params[:action]
180
        when 'logs'
181
          :view
182
        when 'power'
183
          :edit
184
        else
185
          super
186
        end
187
      end
188
    end
189
  end
190
end
191
# end