Skip to content

Commit

Permalink
brute force mounting things
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrer committed Dec 3, 2023
1 parent 262c024 commit ba19a56
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 52 deletions.
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

## Naming

- Is `Service` actually a `Stack` and `Role` is a `Service`? Is there an `Application` burried in there?
- `ServicePlacement` and `ServiceConfig` would be the input to the `Deployment`
- Or `Stack` and `Role`?
- Remove `Command::Builder` in favor of embedding commands
- Move `Command::Docker` to `Docker::CLI`
- Rename `Invocation` to `Command`
Expand Down
2 changes: 1 addition & 1 deletion lib/cove/command/docker/container/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.build(image:, name: nil, remove: false, interactive: false, labels: {},
end

Array(mounts).each do |mount|
builder += ["--mount", "type=volume,source=#{mount["source"]},target=#{mount["target"]}"]
builder += ["--mount", "type=#{mount["type"] || "volume"},source=#{mount["source"]},target=#{mount["target"]}"]
end

Array(labels).each do |label|
Expand Down
4 changes: 1 addition & 3 deletions lib/cove/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def version

# @return [Cove::EntityLabels] The labels of the deployment
def labels
role.labels.merge({
"cove.deployed_version" => version
})
role.labels
end

private
Expand Down
2 changes: 2 additions & 0 deletions lib/cove/deployment_config.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Cove
class DeploymentConfig
attr_reader :version

def self.prepare(registry, deployment)
entries = deployment.configs.map do |name, definition|
# TODO: Make sure we always symbolize keys when parsing the config
Expand Down
8 changes: 8 additions & 0 deletions lib/cove/deployment_config/deployable_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def files
DeployableFile.new(@base_path, file)
end
end

def source
@base_path
end

def target
@entry.target
end
end
end
end
2 changes: 1 addition & 1 deletion lib/cove/desired_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.from(instance)
image: instance.image,
command: instance.command,
labels: instance.labels,
environment_files: [EnvironmentFile.new(instance.deployment).host_file_path],
environment_files: [EnvironmentFile.new(instance.package).host_file_path],
version: instance.version,
index: instance.index,
ports: instance.ports,
Expand Down
12 changes: 6 additions & 6 deletions lib/cove/environment_file.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
module Cove
class EnvironmentFile
# @return [Cove::Role]
attr_reader :deployment
attr_reader :package

def initialize(deployment)
@deployment = deployment
def initialize(package)
@package = package
end

def host_directory_path
# TODO: Make the base path configurable
"#{Cove.host_base_dir}/env/#{deployment.service_name}/#{deployment.role_name}"
"#{Cove.host_base_dir}/env/#{package.service_name}/#{package.role_name}"
end

def host_file_path
"#{host_directory_path}/#{deployment.version}.env"
"#{host_directory_path}/#{package.version}.env"
end

def content
deployment.environment_variables.map do |key, value|
package.environment_variables.map do |key, value|
"#{key}=#{value}"
end.join("\n")
end
Expand Down
29 changes: 17 additions & 12 deletions lib/cove/instance.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
module Cove
class Instance
# @return [Cove::Deployment]
attr_reader :deployment
# @return [Cove::Package]
attr_reader :package
# @return [Integer]
attr_reader :index
# @return [Cove::Role]
delegate :role, to: :deployment
delegate :role, to: :package
# @return [Cove::Service]
delegate :service, to: :role
# @return [String] The version of the deployment
delegate :version, to: :deployment
# @return [String] The version of the package
delegate :version, to: :package
# @return [String] The command to run in the container
delegate :command, to: :role
# @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

# @param deployment [Cove::Deployment] The deployment the container is part of
# @param index [Integer] The index of the container in the deployment
def initialize(deployment, index)
@deployment = deployment
# @param package [Cove::package] The package the container is part of
# @param index [Integer] The index of the container in the package
def initialize(package, index)
@package = package
@index = index
end

Expand All @@ -30,11 +28,18 @@ def name

# @return [Cove::EntityLabels] The labels of the container
def labels
deployment.labels.merge({
package.labels.merge({
"cove.index" => index.to_s
})
end

# @return [Array<Hash>] The volumes to mount to the container
def mounts
Array(role.mounts) + package.deployment_config.entries.map do |entry|
{"source" => entry.source, "target" => entry.target, "type" => "bind"}
end
end

# @return [Array<Hash>] The port mapping to run in the container
def ports
@ports ||= role.ports.map { |port|
Expand Down
11 changes: 6 additions & 5 deletions lib/cove/invocation/service_up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def run
roles.each do |role|
deployment = Deployment.new(role)
config = DeploymentConfig.prepare(registry, deployment)
package = Package.new(deployment, config)

Steps::EnsureEnvironmentFileExists.call(connection, deployment)
Steps::EnsureConfigsExist.call(connection, config)
Steps::PullImage.call(connection, deployment)
Steps::CreateMissingContainers.call(connection, deployment)
Steps::RollContainers.call(connection, deployment)
Steps::EnsureEnvironmentFileExists.call(connection, package)
Steps::EnsureConfigsExist.call(connection, package)
Steps::PullImage.call(connection, package)
Steps::CreateMissingContainers.call(connection, package)
Steps::RollContainers.call(connection, package)
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/cove/package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Cove
class Package
attr_reader :deployment, :deployment_config

delegate_missing_to :deployment

def initialize(deployment, deployment_config)
@deployment = deployment
@deployment_config = deployment_config
end

def version
Digest::SHA256.hexdigest([deployment.version, deployment_config.version].join("/"))[0..12]
end

def labels
deployment.labels.merge({
"cove.deployed_version" => version
})
end
end
end
12 changes: 6 additions & 6 deletions lib/cove/steps/create_missing_containers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class CreateMissingContainers
# @return [SSHKit::Backend::Abstract]
attr_reader :connection
# @return [Cove::Service]
attr_reader :deployment
attr_reader :package

def initialize(connection, deployment)
@connection, @deployment = connection, deployment
def initialize(connection, package)
@connection, @package = connection, package
end

def call
Expand All @@ -26,12 +26,12 @@ def state_diff
end

def existing_containers
Steps::GetExistingContainerDetails.call(connection, deployment)
Steps::GetExistingContainerDetails.call(connection, package)
end

def desired_containers
1.upto(deployment.container_count).map do |index|
instance = Instance.new(deployment, index)
1.upto(package.container_count).map do |index|
instance = Instance.new(package, index)
DesiredContainer.from(instance)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cove/steps/ensure_configs_exist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class EnsureConfigsExist

attr_reader :connection, :deployment_config

def initialize(connection, deployment_config)
def initialize(connection, package)
@connection = connection
@deployment_config = deployment_config
@deployment_config = package.deployment_config
end

def call
Expand Down
10 changes: 5 additions & 5 deletions lib/cove/steps/ensure_environment_file_exists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class EnsureEnvironmentFileExists

# @return [SSHKit::Backend::Abstract]
attr_reader :connection
# @return [Cove::Deployment]
attr_reader :deployment
# @return [Cove::Package]
attr_reader :package

def initialize(connection, deployment)
@connection, @deployment = connection, deployment
def initialize(connection, package)
@connection, @package = connection, package
end

def call
env = EnvironmentFile.new(deployment)
env = EnvironmentFile.new(package)
connection.info("Ensuring environment file exists")
connection.execute(:mkdir, "-p", env.host_directory_path, "&&", "chmod", "700", env.host_directory_path)
connection.upload!(StringIO.new(env.content), env.host_file_path)
Expand Down
10 changes: 5 additions & 5 deletions lib/cove/steps/pull_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ module Steps
class PullImage
include Callable

attr_reader :connection, :deployment
attr_reader :connection, :package

def initialize(connection, deployment)
def initialize(connection, package)
@connection = connection
@deployment = deployment
@package = package
end

def call
connection.info "Pulling image #{deployment.image}"
connection.execute(*Command::Builder.pull_image(deployment.image))
connection.info "Pulling image #{package.image}"
connection.execute(*Command::Builder.pull_image(package.image))
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/cove/steps/roll_containers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class RollContainers
attr_reader :connection
# @return [Cove::Service]
attr_reader :role
# @return [Cove::Deployment]
attr_reader :deployment
# @return [Cove::package]
attr_reader :package

def initialize(connection, deployment)
@connection, @deployment = connection, deployment
@role = deployment.role
def initialize(connection, package)
@connection, @package = connection, package
@role = package.role
end

def call
Expand Down Expand Up @@ -48,7 +48,7 @@ def existing_containers

def desired_containers
1.upto(role.container_count).map do |index|
instance = Instance.new(deployment, index)
instance = Instance.new(package, index)
DesiredContainer.from(instance)
end
end
Expand Down

0 comments on commit ba19a56

Please sign in to comment.