This repository was archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathcontainers_controller.rb
More file actions
183 lines (163 loc) · 7.16 KB
/
Copy pathcontainers_controller.rb
File metadata and controls
183 lines (163 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# module ForemanDocker
module Api
module V2
class ContainersController < ::Api::V2::BaseController
before_filter :find_resource, :except => %w(index create)
resource_description do
resource_id 'containers'
api_version 'v2'
api_base_url '/docker/api/v2'
end
api :GET, '/containers/', N_('List all containers')
api :GET, '/compute_resources/:compute_resource_id/containers/',
N_('List all containers in a compute resource')
param :compute_resource_id, :identifier
param_group :search_and_pagination, ::Api::V2::BaseController
def index
if params[:compute_resource_id].present?
scoped = Container.where(:compute_resource_id => params[:compute_resource_id])
else
scoped = Container.scoped
end
@containers = scoped.search_for(params[:search], :order => params[:order])
.paginate(:page => params[:page])
end
api :GET, '/containers/:id/', N_('Show a container')
api :GET, '/compute_resources/:compute_resource_id/containers/:id',
N_('Show container in a compute resource')
param :id, :identifier, :required => true
param :compute_resource_id, :identifier
def show
end
def_param_group :container do
param :container, Hash, :required => true, :action_aware => true do
param :name, String, :required => true
param_group :taxonomies, ::Api::V2::BaseController
param :compute_resource_id, :identifier, :required => true
param :registry_id, :identifier, :desc => N_('Registry this container will have to
use to get the image')
param :repository_name, String, :required => true,
:desc => N_('Name of the repository to use
to create the container. e.g: centos')
param :tag, String, :required => true,
:desc => N_('Tag to use to create the container. e.g: latest')
param :tty, :bool
param :entrypoint, String
param :command, String, :required => true
param :memory, String
param :cpu_shares, :number
param :cpu_sets, String
param :environment_variables, Hash
param :attach_stdout, :bool
param :attach_stdin, :bool
param :attach_stderr, :bool
param :capsule_id, :identifier, :desc => N_('The capsule this container will have to use
to get the image. Relevant for images
retrieved from katello registry.')
end
end
api :POST, '/containers/', N_('Create a container')
api :POST, '/compute_resources/:compute_resource_id/containers/',
N_('Create container in a compute resource')
param_group :container, :as => :create
def create
@container = ForemanDocker::Service::Containers.new.start_container!(set_wizard_state)
set_container_taxonomies
process_response @container.save
rescue ActiveModel::MassAssignmentSecurity::Error => e
render :json => { :error => _("Wrong attributes: %s") % e.message },
:status => :unprocessable_entity
end
api :DELETE, '/containers/:id/', N_('Delete a container')
api :DELETE, '/compute_resources/:compute_resource_id/containers/:id',
N_('Delete container in a compute resource')
param :id, :identifier, :required => true
param :compute_resource_id, :identifier
def destroy
process_response @container.destroy
end
api :GET, '/containers/:id/logs', N_('Show container logs')
api :GET, '/compute_resources/:compute_resource_id/containers/:id/logs',
N_('Show logs from a container in a compute resource')
param :id, :identifier, :required => true
param :compute_resource_id, :identifier
param :stdout, :bool
param :stderr, :bool
param :tail, Fixnum, N_('Number of lines to tail. Default: 100')
def logs
render :json => { :logs => Docker::Container.get(@container.uuid)
.logs(:stdout => (params[:stdout] || true),
:stderr => (params[:stderr] || false),
:tail => (params[:tail] || 100)) }
end
api :PUT, '/containers/:id/power', N_('Run power operation on a container')
api :PUT, '/compute_resources/:compute_resource_id/containers/:id/power',
N_('Run power operation on a container in a compute resource')
param :id, :identifier, :required => true
param :compute_resource_id, :identifier
param :power_action, String,
:required => true,
:desc => N_('power action, valid actions are (start), (stop), (status)')
def power
power_actions = %(start stop status)
if power_actions.include? params[:power_action]
response = if params[:power_action] == 'status'
{ :running => @container.in_fog.ready? }
else
{ :running => @container.in_fog.send(params[:power_action]) }
end
render :json => response
else
render :json =>
{ :error => _("Unknown method: available power operations are %s") %
power_actions.join(', ') }, :status => :unprocessable_entity
end
end
private
def wizard_properties
wizard_props = { :preliminary => [:compute_resource_id],
:image => [:registry_id, :repository_name, :tag],
:configuration => [:name, :command, :entrypoint, :cpu_set,
:cpu_shares, :memory],
:environment => [:tty, :attach_stdin, :attach_stdout,
:attach_stderr] }
if DockerContainerWizardStates::Image.attribute_names.include?("capsule_id")
wizard_props[:image] << :capsule_id
end
wizard_props
end
def set_wizard_state
wizard_state = DockerContainerWizardState.create
wizard_properties.each do |step, properties|
property_values = properties.each_with_object({}) do |property, values|
values[:"#{property}"] = params[:container][:"#{property}"]
end
wizard_state.send(:"create_#{step}", property_values)
end
if params[:container][:environment_variables].present?
wizard_state.environment.environment_variables =
params[:container][:environment_variables]
end
wizard_state.tap(&:save)
end
def set_container_taxonomies
Taxonomy.enabled_taxonomies.each do |taxonomy|
if params[:container][:"#{taxonomy}"].present?
@container.send(:"#{taxonomy}=", params[:container][:"#{taxonomy}"])
end
end
end
def action_permission
case params[:action]
when 'logs'
:view
when 'power'
:edit
else
super
end
end
end
end
end
# end