Project

General

Profile

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

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

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, Array, :desc => N_("Optional array of environment variables hashes. e.g. 'environment_variables': [{'name' => 'example', 'value' => '123'}]")
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.join(', ')
77
                          },
78
                 :status => :unprocessable_entity
79
        else
80
          set_container_taxonomies
81
          process_response @container.save
82
        end
83
      end
84

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

    
91
      def destroy
92
        deleted_identifier = ForemanDocker::ContainerRemover.remove_unmanaged(
93
          @container.compute_resource_id,
94
          @container.uuid)
95

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

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

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

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

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

    
144
      private
145

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

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

    
168
        if params[:container][:environment_variables].present?
169
          environment_variables = []
170
          params[:container][:environment_variables].each do |env_var|
171
            environment_variable = DockerContainerWizardStates::EnvironmentVariable.new
172
            environment_variable.key = env_var[:key]
173
            environment_variable.value = env_var[:value]
174
            environment_variables << environment_variable
175
          end
176
          wizard_state.environment.environment_variables = environment_variables
177
        end
178
        wizard_state.tap(&:save)
179
      end
180

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

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