1 |
9c1fa5fb
|
Daniel Lobato
|
module Containers
|
2 |
|
|
class StepsController < ::ApplicationController
|
3 |
|
|
include Wicked::Wizard
|
4 |
e4e8739b
|
Daniel Lobato
|
include ForemanDocker::FindContainer
|
5 |
fa9ff801
|
Daniel Lobato
|
|
6 |
9c1fa5fb
|
Daniel Lobato
|
steps :preliminary, :image, :configuration, :environment
|
7 |
30891c0a
|
Daniel Lobato
|
|
8 |
f4c209f1
|
Dmitri Dolguikh
|
before_filter :find_state
|
9 |
|
|
before_filter :build_state, :only => [:update]
|
10 |
|
|
before_filter :set_form, :only => [:show]
|
11 |
fa9ff801
|
Daniel Lobato
|
|
12 |
9c1fa5fb
|
Daniel Lobato
|
def show
|
13 |
30891c0a
|
Daniel Lobato
|
@container_resources = allowed_resources if step == :preliminary
|
14 |
9c1fa5fb
|
Daniel Lobato
|
render_wizard
|
15 |
fa9ff801
|
Daniel Lobato
|
end
|
16 |
|
|
|
17 |
9c1fa5fb
|
Daniel Lobato
|
def update
|
18 |
30891c0a
|
Daniel Lobato
|
if step == wizard_steps.last
|
19 |
8e2848d5
|
Vanya Jauhal
|
if process_resource!(@state).nil?
|
20 |
|
|
render_wizard @state
|
21 |
|
|
else
|
22 |
d5d9d39e
|
Shlomi Zadok
|
params[:start_on_create] ? create_container : create_container(false)
|
23 |
8e2848d5
|
Vanya Jauhal
|
end
|
24 |
30891c0a
|
Daniel Lobato
|
else
|
25 |
|
|
render_wizard @state
|
26 |
9c1fa5fb
|
Daniel Lobato
|
end
|
27 |
fa9ff801
|
Daniel Lobato
|
end
|
28 |
|
|
|
29 |
9c1fa5fb
|
Daniel Lobato
|
private
|
30 |
fa9ff801
|
Daniel Lobato
|
|
31 |
f4c209f1
|
Dmitri Dolguikh
|
def find_state
|
32 |
6d101d7a
|
Dmitri Dolguikh
|
@state = DockerContainerWizardState.find(params[:wizard_state_id])
|
33 |
9c1fa5fb
|
Daniel Lobato
|
rescue ActiveRecord::RecordNotFound
|
34 |
|
|
not_found
|
35 |
|
|
end
|
36 |
e4e8739b
|
Daniel Lobato
|
|
37 |
f4c209f1
|
Dmitri Dolguikh
|
def build_state
|
38 |
cc6d0d73
|
David Davis
|
s = @state.send(:"build_#{step}", state_params)
|
39 |
63ae5d99
|
Dmitri Dolguikh
|
instance_variable_set("@docker_container_wizard_states_#{step}", s)
|
40 |
f4c209f1
|
Dmitri Dolguikh
|
end
|
41 |
|
|
|
42 |
2b80fdd9
|
orrabin
|
def docker_parameters_permited_params
|
43 |
|
|
[:key, :reference_id, :value, :_destroy, :id]
|
44 |
|
|
end
|
45 |
|
|
|
46 |
cc6d0d73
|
David Davis
|
def state_params
|
47 |
|
|
attrs = case step
|
48 |
|
|
when :preliminary
|
49 |
|
|
[:wizard_state, :compute_resource_id]
|
50 |
|
|
when :image
|
51 |
|
|
[:repository_name, :tag, :wizard_state, :registry_id, :capsule_id, :katello]
|
52 |
|
|
when :configuration
|
53 |
|
|
[:name, :command, :entrypoint, :cpu_set, :cpu_shares, :memory, :wizard_state]
|
54 |
|
|
when :environment
|
55 |
|
|
[:tty, :docker_container_wizard_state_id,
|
56 |
|
|
:attach_stdin, :attach_stdout, :attach_stderr,
|
57 |
2b80fdd9
|
orrabin
|
:exposed_ports_attributes => docker_parameters_permited_params,
|
58 |
|
|
:environment_variables_attributes => docker_parameters_permited_params,
|
59 |
|
|
:dns_attributes => docker_parameters_permited_params
|
60 |
cc6d0d73
|
David Davis
|
]
|
61 |
|
|
end
|
62 |
|
|
|
63 |
|
|
params.require("docker_container_wizard_states_#{step}").permit(*attrs)
|
64 |
|
|
end
|
65 |
|
|
|
66 |
e4e8739b
|
Daniel Lobato
|
def set_form
|
67 |
f4c209f1
|
Dmitri Dolguikh
|
instance_variable_set(
|
68 |
7ff105d1
|
David Davis
|
"@docker_container_wizard_states_#{step}",
|
69 |
|
|
@state.send(:"#{step}") || @state.send(:"build_#{step}"))
|
70 |
e4e8739b
|
Daniel Lobato
|
end
|
71 |
30891c0a
|
Daniel Lobato
|
|
72 |
d5d9d39e
|
Shlomi Zadok
|
def create_container(start = true)
|
73 |
cc6d0d73
|
David Davis
|
@state.send(:"create_#{step}", state_params)
|
74 |
d5d9d39e
|
Shlomi Zadok
|
service = Service::Containers.new
|
75 |
|
|
container = if start.is_a? TrueClass
|
76 |
|
|
service.start_container!(@state)
|
77 |
|
|
else
|
78 |
|
|
service.create_container!(@state)
|
79 |
|
|
end
|
80 |
2b80fdd9
|
orrabin
|
|
81 |
30891c0a
|
Daniel Lobato
|
if container.present?
|
82 |
|
|
process_success(:object => container, :success_redirect => container_path(container))
|
83 |
|
|
else
|
84 |
63ae5d99
|
Dmitri Dolguikh
|
@docker_container_wizard_states_environment = @state.environment
|
85 |
|
|
process_error(
|
86 |
31e5a925
|
Ori Rabin
|
:redirect => containers_path,
|
87 |
|
|
:error_msg => service.full_messages.join(','),
|
88 |
|
|
:object => @state.environment)
|
89 |
30891c0a
|
Daniel Lobato
|
end
|
90 |
|
|
end
|
91 |
fa9ff801
|
Daniel Lobato
|
end
|
92 |
|
|
end |