This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #8409 - Pull docker image asynchronously
- Loading branch information
Showing
23 changed files
with
375 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/models/foreman_docker/service/actions/container/provision.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module ForemanDocker | ||
module Service | ||
module Actions | ||
module Container | ||
class Provision < ::Actions::EntryAction | ||
def plan(container) | ||
action_subject(container) | ||
|
||
sequence do | ||
plan_action(Container::Pull, container) | ||
plan_action(Container::Start, container) | ||
end | ||
end | ||
|
||
def humanized_name | ||
_('Provision container') | ||
end | ||
|
||
def finalize | ||
action_logger.info('Finished provisioning of Docker container') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
50 changes: 50 additions & 0 deletions
50
app/models/foreman_docker/service/actions/container/pull.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module ForemanDocker | ||
module Service | ||
module Actions | ||
module Container | ||
class Pull < ::Actions::EntryAction | ||
def plan(container) | ||
action_subject(container) | ||
plan_self(:container => container.id) | ||
end | ||
|
||
def run(event = nil) | ||
case event | ||
when :done # do nothing and move to next step | ||
when StandardError | ||
action_logger.error(event.message) | ||
action_logger.debug(event.backtrace.join("\n ")) | ||
error!(event) | ||
else | ||
suspend do |suspended_action| | ||
pull_docker_image(::Container.find(input[:container]), suspended_action) | ||
end | ||
end | ||
end | ||
|
||
def pull_docker_image(container, suspended_action) | ||
Thread.new do | ||
begin | ||
container.compute_resource | ||
.create_image(:fromImage => container.repository_pull_url) | ||
suspended_action << :done | ||
rescue => e | ||
# encapsulate e as Foreman::Exception | ||
suspended_action << e | ||
end | ||
end | ||
end | ||
|
||
def humanized_name | ||
_('Pull') | ||
end | ||
|
||
def finalize | ||
container = ::Container.find(input[:container]) | ||
action_logger.info("[Docker] Finished pulling image #{container.repository_pull_url}") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
app/models/foreman_docker/service/actions/container/start.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module ForemanDocker | ||
module Service | ||
module Actions | ||
module Container | ||
class Start < ::Actions::EntryAction | ||
def plan(container) | ||
action_subject(container) | ||
plan_self(:container => container.id) | ||
end | ||
|
||
def run | ||
container = ::Container.find(input[:container]) | ||
started = container.compute_resource.create_container(container.parametrize) | ||
if started | ||
container.update_attribute(:uuid, started.id) | ||
started | ||
else | ||
[container.compute_resource.errors[:base]] | ||
end | ||
end | ||
|
||
def humanized_name | ||
_('Start') | ||
end | ||
|
||
def finalize | ||
container = ::Container.find(input[:container]) | ||
action_logger.info("[Docker] container #{container.name} successfully started") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module ForemanDocker | ||
module Service | ||
class Containers | ||
def errors | ||
@errors ||= [] | ||
end | ||
|
||
def start_container!(wizard_state) | ||
container = ActiveRecord::Base.transaction do | ||
container = Container.new(wizard_state.container_attributes) do |r| | ||
# eagerly load environment variables | ||
state = | ||
DockerContainerWizardState.includes(:environment => [:environment_variables]) | ||
.find(wizard_state.id) | ||
state.environment_variables.each do |environment_variable| | ||
r.environment_variables.build :name => environment_variable.name, | ||
:value => environment_variable.value, | ||
:priority => environment_variable.priority | ||
end | ||
end | ||
Taxonomy.enabled_taxonomies.each do |taxonomy| | ||
container.send(:"#{taxonomy}=", wizard_state.preliminary.send(:"#{taxonomy}")) | ||
end | ||
container.save! | ||
container | ||
end | ||
|
||
destroy_wizard_state(wizard_state) | ||
ForemanTasks.async_task(Service::Actions::Container::Provision, container) | ||
container | ||
end | ||
|
||
def destroy_wizard_state(wizard_state) | ||
wizard_state.destroy | ||
DockerContainerWizardState.destroy_all(["updated_at < ?", (Time.now - 24.hours)]) | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
object @container | ||
|
||
extends "api/v2/containers/show" |
Oops, something went wrong.