Project

General

Profile

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

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

1
# module ForemanDocker
2
module Api
3
  module V2
4
    class ContainersController < ::Api::V2::BaseController
5
      before_action :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
        scope = resource_scope
21
        scope = scope.where(:compute_resource => params[:compute_resource_id]) if params[:compute_resource_id].present?
22
        @containers = scope.search_for(params[:search], :order => params[:order])
23
                      .paginate(:page => params[:page])
24
      end
25

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

    
32
      def show
33
      end
34

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

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

    
68
      def create
69
        service = Service::Containers.new
70
        @container = service.start_container!(set_wizard_state)
71
        if service.errors.any?
72
          render :json => { :errors => service.errors,
73
                            :full_messages => service.full_messages.join(', ')
74
                          },
75
                 :status => :unprocessable_entity
76
        else
77
          set_container_taxonomies
78
          process_response @container.save
79
        end
80
      end
81

    
82
      api :DELETE, '/containers/:id/', N_('Delete a container')
83
      api :DELETE, '/compute_resources/:compute_resource_id/containers/:id',
84
          N_('Delete container on a compute resource')
85
      param :id, :identifier, :required => true
86
      param :compute_resource_id, :identifier
87

    
88
      def destroy
89
        deleted_identifier = ForemanDocker::ContainerRemover.remove_unmanaged(
90
          @container.compute_resource_id,
91
          @container.uuid)
92

    
93
        if deleted_identifier
94
          process_response @container.destroy
95
        else
96
          render :json => { :error => 'Could not delete container on Docker host' }, :status => :precondition_failed
97
        end
98
      end
99

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

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

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

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

    
141
      private
142

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

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

    
165
        if params[:container][:environment_variables].present?
166
          environment_variables = []
167
          params[:container][:environment_variables].each do |env_var|
168
            environment_variable = DockerContainerWizardStates::EnvironmentVariable.new
169
            environment_variable.key = env_var[:key]
170
            environment_variable.value = env_var[:value]
171
            environment_variables << environment_variable
172
          end
173
          wizard_state.environment.environment_variables = 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