1
|
|
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 '/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 :pagination, ::Api::V2::BaseController
|
18
|
|
19
|
def index
|
20
|
if params[:compute_resource_id].present?
|
21
|
@containers = Container.where(:compute_resource_id => params[:compute_resource_id])
|
22
|
else
|
23
|
@containers = Container.all
|
24
|
end
|
25
|
end
|
26
|
|
27
|
api :GET, '/containers/:id/', N_('Show a container')
|
28
|
api :GET, '/compute_resources/:compute_resource_id/containers/:id',
|
29
|
N_('Show container in a compute resource')
|
30
|
param :id, :identifier, :required => true
|
31
|
param :compute_resource_id, :identifier
|
32
|
|
33
|
def show
|
34
|
end
|
35
|
|
36
|
def_param_group :container do
|
37
|
param :container, Hash, :required => true, :action_aware => true do
|
38
|
param :name, String, :required => true
|
39
|
param_group :taxonomies, ::Api::V2::BaseController
|
40
|
param :compute_resource_id, :identifier, :required => true
|
41
|
param :registry_id, :identifier, :desc => N_('Registry this container will have to use
|
42
|
to get the image')
|
43
|
param :image, String, :desc => N_('Image to use to create the container.
|
44
|
Format should be repository:tag, e.g: centos:7')
|
45
|
param :tty, :bool
|
46
|
param :entrypoint, String
|
47
|
param :cmd, String
|
48
|
param :memory, String
|
49
|
param :cpu_shares, :number
|
50
|
param :cpu_sets, String
|
51
|
param :environment_variables, Hash
|
52
|
param :attach_stdout, :bool
|
53
|
param :attach_stdin, :bool
|
54
|
param :attach_stderr, :bool
|
55
|
param :katello, :bool
|
56
|
end
|
57
|
end
|
58
|
|
59
|
api :POST, '/containers/', N_('Create a container')
|
60
|
api :POST, '/compute_resources/:compute_resource_id/containers/',
|
61
|
N_('Create container in a compute resource')
|
62
|
param_group :container, :as => :create
|
63
|
|
64
|
def create
|
65
|
@container = Service::Containers.new.start_container!(set_wizard_state)
|
66
|
set_container_taxonomies
|
67
|
process_response @container.save
|
68
|
rescue ActiveModel::MassAssignmentSecurity::Error => e
|
69
|
render :json => { :error => _("Wrong attributes: %s") % e.message },
|
70
|
:status => :unprocessable_entity
|
71
|
end
|
72
|
|
73
|
api :DELETE, '/containers/:id/', N_('Delete a container')
|
74
|
api :DELETE, '/compute_resources/:compute_resource_id/containers/:id',
|
75
|
N_('Delete container in a compute resource')
|
76
|
param :id, :identifier, :required => true
|
77
|
param :compute_resource_id, :identifier
|
78
|
|
79
|
def destroy
|
80
|
process_response @container.destroy
|
81
|
end
|
82
|
|
83
|
api :GET, '/containers/:id/logs', N_('Show container logs')
|
84
|
api :GET, '/compute_resources/:compute_resource_id/containers/:id/logs',
|
85
|
N_('Show logs from a container in a compute resource')
|
86
|
param :id, :identifier, :required => true
|
87
|
param :compute_resource_id, :identifier
|
88
|
param :stdout, :bool
|
89
|
param :stderr, :bool
|
90
|
param :tail, Fixnum, N_('Number of lines to tail. Default: 100')
|
91
|
|
92
|
def logs
|
93
|
render :json => { :logs => Docker::Container.get(@container.uuid)
|
94
|
.logs(:stdout => (params[:stdout] || true),
|
95
|
:stderr => (params[:stderr] || false),
|
96
|
:tail => (params[:tail] || 100)) }
|
97
|
end
|
98
|
|
99
|
api :PUT, '/containers/:id/power', N_('Run power operation on a container')
|
100
|
api :PUT, '/compute_resources/:compute_resource_id/containers/:id/power',
|
101
|
N_('Run power operation on a container in a compute resource')
|
102
|
param :id, :identifier, :required => true
|
103
|
param :compute_resource_id, :identifier
|
104
|
param :power_action, String,
|
105
|
:required => true,
|
106
|
:desc => N_('power action, valid actions are (start), (stop), (status)')
|
107
|
|
108
|
def power
|
109
|
power_actions = %(start stop status)
|
110
|
if power_actions.include? params[:power_action]
|
111
|
response = if params[:power_action] == 'status'
|
112
|
{ :running => @container.in_fog.ready? }
|
113
|
else
|
114
|
{ :running => @container.in_fog.send(params[:power_action]) }
|
115
|
end
|
116
|
render :json => response
|
117
|
else
|
118
|
render :json =>
|
119
|
{ :error => _("Unknown method: available power operations are %s") %
|
120
|
power_actions.join(', ') }, :status => :unprocessable_entity
|
121
|
end
|
122
|
end
|
123
|
|
124
|
private
|
125
|
|
126
|
def set_wizard_state
|
127
|
wizard_properties = { :preliminary => [:compute_resource_id],
|
128
|
:image => [:registry_id, :repository_name, :tag, :katello],
|
129
|
:configuration => [:name, :command, :entrypoint, :cpu_set,
|
130
|
:cpu_shares, :memory],
|
131
|
:environment => [:tty, :attach_stdin, :attach_stdout,
|
132
|
:attach_stderr] }
|
133
|
|
134
|
wizard_state = DockerContainerWizardState.create
|
135
|
wizard_properties.each do |step, properties|
|
136
|
property_values = properties.each_with_object({}) do |property, values|
|
137
|
values[:"#{property}"] = params[:container][:"#{property}"]
|
138
|
end
|
139
|
wizard_state.send(:"create_#{step}", property_values)
|
140
|
end
|
141
|
|
142
|
if params[:container][:environment_variables].present?
|
143
|
wizard_state.environment.environment_variables =
|
144
|
params[:container][:environment_variables]
|
145
|
end
|
146
|
wizard_state.tap(&:save)
|
147
|
end
|
148
|
|
149
|
def set_container_taxonomies
|
150
|
Taxonomy.enabled_taxonomies.each do |taxonomy|
|
151
|
if params[:container][:"#{taxonomy}"].present?
|
152
|
@container.send(:"#{taxonomy}=", params[:container][:"#{taxonomy}"])
|
153
|
end
|
154
|
end
|
155
|
end
|
156
|
|
157
|
def action_permission
|
158
|
case params[:action]
|
159
|
when 'logs'
|
160
|
:view
|
161
|
when 'power'
|
162
|
:edit
|
163
|
else
|
164
|
super
|
165
|
end
|
166
|
end
|
167
|
end
|
168
|
end
|
169
|
end
|
170
|
|