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
Fixes #8409 - Pull docker image asynchronously #72
Open
dLobatog
wants to merge
1
commit into
theforeman:master
Choose a base branch
from
dLobatog:8409
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should start the container here |
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was changed so that when no Cmd is specified, it uses the default. Otherwise a
.merge
will not replace a[]
as it's notnil
.