Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrer committed Dec 4, 2023
1 parent 52b3682 commit 7137a23
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 27 deletions.
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@
- Move `Command::Docker` to `Docker::CLI`
- Rename `Invocation` to `Command`
- Add tests for the DockerCLI classes

## Other

- Use objects for `mounts` and `ports` instead of hashes
4 changes: 1 addition & 3 deletions lib/cove/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class Instance
attr_reader :index
# @return [Cove::Role]
delegate :role, to: :package
# @return [Cove::Service]
delegate :service, to: :role
# @return [String] The version of the package
delegate :version, to: :package
# @return [String] The command to run in the container
Expand All @@ -23,7 +21,7 @@ def initialize(package, index)
end

def name
"#{service.name}-#{role.name}-#{version}-#{index}"
"#{package.service_name}-#{package.role_name}-#{version}-#{index}"
end

# @return [Cove::EntityLabels] The labels of the container
Expand Down
8 changes: 3 additions & 5 deletions lib/cove/invocation/service_up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ def initialize(connection, registry, service, roles)

def run
roles.each do |role|
deployment = Deployment.new(role)
config = DeploymentConfig.prepare(registry, deployment)
package = Package.new(deployment, config)
package = Package.build(registry, role)

Steps::EnsureEnvironmentFileExists.call(connection, package)
Steps::EnsureConfigsExist.call(connection, package)
Steps::PullImage.call(connection, package)
Steps::EnsureConfigsExist.call(connection, package.deployment_config)
Steps::PullImage.call(connection, package.image)
Steps::CreateMissingContainers.call(connection, package)
Steps::RollContainers.call(connection, package)
end
Expand Down
8 changes: 7 additions & 1 deletion lib/cove/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ class Package

delegate_missing_to :deployment

def self.build(registry, role)
deployment = Deployment.new(role)
config = DeploymentConfig.prepare(registry, deployment)
new(deployment, config)
end

def initialize(deployment, deployment_config)
@deployment = deployment
@deployment_config = deployment_config
Expand All @@ -15,7 +21,7 @@ def version

def labels
deployment.labels.merge({
"cove.deployed_version" => version
"cove.version" => version
})
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/cove/runtime/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def self.build_from_config(container)
status: container.dig("State", "Status"),
service: container.dig("Config", "Labels", "cove.service"),
role: container.dig("Config", "Labels", "cove.role"),
version: container.dig("Config", "Labels", "cove.deployed_version"),
version: container.dig("Config", "Labels", "cove.version"),
role_version: container.dig("Config", "Labels", "cove.role_version"),
config_version: container.dig("Config", "Labels", "cove.config_version"),
index: container.dig("Config", "Labels", "cove.index"),
health_status: container.dig("State", "Health", "Status")
)
Expand All @@ -25,6 +27,8 @@ def self.build_from_config(container)
attribute :service, :string
attribute :role, :string
attribute :version, :string
attribute :role_version, :string
attribute :config_version, :string
attribute :index, :integer
attribute :health_status, :string

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, package)
def initialize(connection, deployment_config)
@connection = connection
@deployment_config = package.deployment_config
@deployment_config = deployment_config
end

def call
Expand Down
2 changes: 1 addition & 1 deletion lib/cove/steps/get_existing_container_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GetExistingContainerDetails
attr_reader :entity

# @param connection [SSHKit::Backend::Abstract]
# @param entity [Cove::Service, Cove::Role, Cove::Deployment, Cove::Instance]
# @param entity [Cove::Service, Cove::Role, Cove::Deployment, Cove::Instance, Cove::Package]
def initialize(connection, entity)
@connection, @entity = connection, entity
end
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, :package
attr_reader :connection, :image

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

def call
connection.info "Pulling image #{package.image}"
connection.execute(*Command::Builder.pull_image(package.image))
connection.info "Pulling image #{image}"
connection.execute(*Command::Builder.pull_image(image))
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions spec/cove/invocation/service_up_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
context "with no existing containers" do
it "should start a container" do
registry, service, role = setup_environment(service_name: "test", role_name: "web", image: "app:latest", command: ["ping", "8.8.8.8"], ports: [{"type" => "port", "source" => 8080, "target" => 80}], mounts: [{"type" => "volume", "source" => "my-volume", "target" => "/data"}])
deployment = Cove::Deployment.new(role)
instance = Cove::Instance.new(deployment, 1)

package = Cove::Package.build(registry, role)
instance = Cove::Instance.new(package, 1)

desired_container = Cove::DesiredContainer.from(instance)

allow(Cove::Steps::GetExistingContainerDetails).to receive(:call).with(kind_of(SSHKit::Backend::Abstract), kind_of(Cove::Deployment)) {
allow(Cove::Steps::GetExistingContainerDetails).to receive(:call).with(kind_of(SSHKit::Backend::Abstract), kind_of(Cove::Package)) {
Cove::Runtime::ContainerList.new([
Cove::Runtime::Container.new(id: "1234", name: "legacy_container1", image: service.image, status: "running", service: service.name, role: role.name, version: "fake", index: 1),
Cove::Runtime::Container.new(id: "4567", name: "legacy_container2", image: service.image, status: "running", service: service.name, role: role.name, version: "fake", index: 2)
Expand All @@ -26,7 +27,7 @@
Cove::Runtime::ContainerList.new([
Cove::Runtime::Container.new(id: "1111", name: "legacy_container1", image: service.image, status: "running", service: service.name, role: role.name, version: "fake", index: 1),
Cove::Runtime::Container.new(id: "1112", name: "legacy_container2", image: service.image, status: "running", service: service.name, role: role.name, version: "fake", index: 2),
Cove::Runtime::Container.new(id: "9991", name: desired_container.name, image: service.image, status: "created", service: service.name, role: role.name, version: deployment.version, index: 1)
Cove::Runtime::Container.new(id: "9991", name: desired_container.name, image: service.image, status: "created", service: service.name, role: role.name, version: package.version, index: 1)
])
}

Expand All @@ -37,7 +38,7 @@
stubs << stub_command(/docker container stop legacy_container1/).with_exit_status(0)
stubs << stub_command(/docker container stop legacy_container2/).with_exit_status(0)
stubs << stub_command(/docker container start #{desired_container.name}/).with_exit_status(0)
stubs << stub_upload(File.join(Cove.host_base_dir, "env", service.name, role.name, "#{deployment.version}.env"))
stubs << stub_upload(File.join(Cove.host_base_dir, "env", service.name, role.name, "#{package.version}.env"))

invocation = described_class.new(registry: registry, service: service)

Expand Down
4 changes: 2 additions & 2 deletions spec/cove/runtime/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"Config" => {
"Labels" => {
"cove.deployed_version" => "58da9973e60f0",
"cove.version" => "58da9973e60f0",
"cove.index" => "1",
"cove.role" => "web",
"cove.service" => "nginx",
Expand Down Expand Up @@ -116,7 +116,7 @@ def build_container_config(name: "foobar", status: "running", health_status: nil
},
"Config" => {
"Labels" => {
"cove.deployed_version" => "58da9973e60f0",
"cove.version" => "58da9973e60f0",
"cove.index" => "1",
"cove.role" => "web",
"cove.service" => "nginx",
Expand Down
1 change: 1 addition & 0 deletions spec/cove/steps/ensure_configs_exist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ssh_host = Cove::Host.new(name: "host1").sshkit_host
connection = SSHTestKit::Backend.new(ssh_host)

package = Mocktail.of(Cove::Package)
deployment_config = Mocktail.of(Cove::DeploymentConfig)
file = Mocktail.of(Cove::DeploymentConfig::DeployableFile)

Expand Down
4 changes: 2 additions & 2 deletions spec/cove/steps/get_existing_container_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Labels": {
"cove.service": "test-service",
"cove.role": "web",
"cove.deployed_version": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
"cove.version": "9f86d081884c7d"
}
}
},
Expand All @@ -39,7 +39,7 @@
"Labels": {
"cove.service": "test-service",
"cove.role": "web",
"cove.deployed_version": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
"cove.version": "9f86d081884c7d"
}
}
}
Expand Down

0 comments on commit 7137a23

Please sign in to comment.