Project

General

Profile

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

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

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.scoped
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, :required => true
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_sets, 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
        @container = Service::Containers.new.start_container!(set_wizard_state)
73
        set_container_taxonomies
74
        process_response @container.save
75
      rescue ActiveModel::MassAssignmentSecurity::Error => e
76
        render :json => { :error  => _("Wrong attributes: %s") % e.message },
77
               :status => :unprocessable_entity
78
      end
79

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

    
86
      def destroy
87
        process_response @container.destroy
88
      end
89

    
90
      api :GET, '/containers/:id/logs', N_('Show container logs')
91
      api :GET, '/compute_resources/:compute_resource_id/containers/:id/logs',
92
          N_('Show logs from a container in a compute resource')
93
      param :id, :identifier, :required => true
94
      param :compute_resource_id, :identifier
95
      param :stdout, :bool
96
      param :stderr, :bool
97
      param :tail,   Fixnum, N_('Number of lines to tail. Default: 100')
98

    
99
      def logs
100
        render :json => { :logs => Docker::Container.get(@container.uuid)
101
          .logs(:stdout => (params[:stdout] || true),
102
                :stderr => (params[:stderr] || false),
103
                :tail   => (params[:tail]   || 100)) }
104
      end
105

    
106
      api :PUT, '/containers/:id/power', N_('Run power operation on a container')
107
      api :PUT, '/compute_resources/:compute_resource_id/containers/:id/power',
108
          N_('Run power operation on a container in a compute resource')
109
      param :id, :identifier, :required => true
110
      param :compute_resource_id, :identifier
111
      param :power_action, String,
112
            :required => true,
113
            :desc     => N_('power action, valid actions are (start), (stop), (status)')
114

    
115
      def power
116
        power_actions = %(start stop status)
117
        if power_actions.include? params[:power_action]
118
          response = if params[:power_action] == 'status'
119
                       { :running => @container.in_fog.ready? }
120
                     else
121
                       { :running => @container.in_fog.send(params[:power_action]) }
122
                     end
123
          render :json => response
124
        else
125
          render :json =>
126
            { :error => _("Unknown method: available power operations are %s") %
127
              power_actions.join(', ') }, :status => :unprocessable_entity
128
        end
129
      end
130

    
131
      private
132

    
133
      def wizard_properties
134
        wizard_props = { :preliminary => [:compute_resource_id],
135
                         :image => [:registry_id, :repository_name, :tag],
136
                         :configuration => [:name, :command, :entrypoint, :cpu_set,
137
                                            :cpu_shares, :memory],
138
                         :environment => [:tty, :attach_stdin, :attach_stdout,
139
                                          :attach_stderr] }
140
        if DockerContainerWizardStates::Image.attribute_names.include?("capsule_id")
141
          wizard_props[:image] << :capsule_id
142
        end
143
        wizard_props
144
      end
145

    
146
      def set_wizard_state
147
        wizard_state = DockerContainerWizardState.create
148
        wizard_properties.each do |step, properties|
149
          property_values = properties.each_with_object({}) do |property, values|
150
            values[:"#{property}"] = params[:container][:"#{property}"]
151
          end
152
          wizard_state.send(:"create_#{step}", property_values)
153
        end
154

    
155
        if params[:container][:environment_variables].present?
156
          wizard_state.environment.environment_variables =
157
            params[:container][:environment_variables]
158
        end
159
        wizard_state.tap(&:save)
160
      end
161

    
162
      def set_container_taxonomies
163
        Taxonomy.enabled_taxonomies.each do |taxonomy|
164
          if params[:container][:"#{taxonomy}"].present?
165
            @container.send(:"#{taxonomy}=", params[:container][:"#{taxonomy}"])
166
          end
167
        end
168
      end
169

    
170
      def action_permission
171
        case params[:action]
172
        when 'logs'
173
          :view
174
        when 'power'
175
          :edit
176
        else
177
          super
178
        end
179
      end
180
    end
181
  end
182
end
183
# end