-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor runCustom. Create ServiceRun and InstanceOnDemand classes
- Loading branch information
Showing
7 changed files
with
120 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Cove | ||
class InstanceOnDemand | ||
# @return [Cove::Deployment] | ||
attr_reader :deployment | ||
# @return [Cove::Role] | ||
delegate :role, to: :deployment | ||
# @return [Cove::Service] | ||
delegate :service, to: :role | ||
# @return [String] The version of the deployment | ||
delegate :version, to: :deployment | ||
# @return [String] The command to run in the container | ||
attr_reader :command | ||
# @return [Array<Hash>] The port mapping to run in the container | ||
attr_reader :ports | ||
# @return [Array<Hash>] The volumes to mount to the container | ||
delegate :mounts, to: :role | ||
# @return [String] The image of the container | ||
delegate :image, to: :role | ||
# @return [Cove::EntityLabels] The labels of the container | ||
delegate :labels, to: :deployment | ||
|
||
# @param deployment [Cove::Deployment] The deployment the container is part of | ||
def initialize(deployment, command) | ||
@deployment = deployment | ||
@command = command | ||
@ports = [] | ||
end | ||
|
||
def name | ||
"#{service.name}-#{role.name}-#{version}" | ||
end | ||
|
||
def labels | ||
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require_relative "invocation/service_up" | ||
require_relative "invocation/service_down" | ||
require_relative "invocation/service_ps" | ||
require_relative "invocation/service_run" |
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,63 @@ | ||
module Cove | ||
module Invocation | ||
class ServiceRun | ||
include SSHKit::DSL | ||
|
||
# @return [Cove::Registry] | ||
attr_reader :registry | ||
# @return [Cove::Service] | ||
attr_reader :service | ||
attr_reader :custom_cmd | ||
attr_reader :role | ||
attr_reader :host | ||
|
||
# @param registry [Cove::Registry] | ||
# @param service [Cove::Service] | ||
# @param custom_cmd [Array<String>] | ||
# @param role [Cove::Role] | ||
# @param host [Cove::Host] | ||
def initialize(registry:, service:, custom_cmd:, role:, host:) | ||
@service = service | ||
@registry = registry | ||
@custom_cmd = custom_cmd | ||
@role = role | ||
@host = host | ||
end | ||
|
||
# @return nil | ||
def invoke | ||
Cove.output.puts "service: #{service.name}, role: #{role.name}, host: #{host.name}, commands: #{custom_cmd}." | ||
deployment = Cove::Deployment.new(role) | ||
instance_on_demand = Cove::InstanceOnDemand.new(deployment, custom_cmd) | ||
desired_container = Cove::DesiredContainer.new( | ||
name: instance_on_demand.name, | ||
image: instance_on_demand.image, | ||
command: instance_on_demand.command, | ||
labels: instance_on_demand.labels, | ||
environment_files: [EnvironmentFile.new(instance_on_demand.deployment).host_file_path], | ||
version: instance_on_demand.version, | ||
ports: instance_on_demand.ports, | ||
mounts: instance_on_demand.mounts | ||
) | ||
ssh_cmd = ["ssh", "-t", host.ssh_destination_string] | ||
create_cmd = Cove::Command::Builder.create_container(desired_container, remove: true, interactive: true) | ||
start_cmd = Cove::Command::Builder.start_attached_container(desired_container.name) | ||
create_cmd = create_cmd.map { |el| el.to_s } | ||
start_cmd = ssh_cmd + start_cmd | ||
start_cmd = start_cmd.map { |el| el.to_s } | ||
|
||
on(host.sshkit_host) do | ||
Steps::EnsureEnvironmentFileExists.call(self, deployment) | ||
Steps::PullImage.call(self, deployment) | ||
info "Creating container #{desired_container.name}" | ||
execute(*create_cmd) | ||
end | ||
|
||
run_locally do | ||
info "Starting container #{desired_container.name}" | ||
Kernel.exec(*start_cmd) | ||
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