diff --git a/TODO.md b/TODO.md index 95e6409..03a0a67 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/lib/cove/instance.rb b/lib/cove/instance.rb index 0ad1c2c..c5918b5 100644 --- a/lib/cove/instance.rb +++ b/lib/cove/instance.rb @@ -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 @@ -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 diff --git a/lib/cove/invocation/service_up.rb b/lib/cove/invocation/service_up.rb index 1314096..4fa1507 100644 --- a/lib/cove/invocation/service_up.rb +++ b/lib/cove/invocation/service_up.rb @@ -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 diff --git a/lib/cove/package.rb b/lib/cove/package.rb index 452c70e..d9a3aa0 100644 --- a/lib/cove/package.rb +++ b/lib/cove/package.rb @@ -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 @@ -15,7 +21,7 @@ def version def labels deployment.labels.merge({ - "cove.deployed_version" => version + "cove.version" => version }) end end diff --git a/lib/cove/runtime/container.rb b/lib/cove/runtime/container.rb index 80824d6..e637d12 100644 --- a/lib/cove/runtime/container.rb +++ b/lib/cove/runtime/container.rb @@ -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") ) @@ -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 diff --git a/lib/cove/steps/ensure_configs_exist.rb b/lib/cove/steps/ensure_configs_exist.rb index 4558c8e..887e661 100644 --- a/lib/cove/steps/ensure_configs_exist.rb +++ b/lib/cove/steps/ensure_configs_exist.rb @@ -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 diff --git a/lib/cove/steps/get_existing_container_details.rb b/lib/cove/steps/get_existing_container_details.rb index 5141e43..fcf5e0b 100644 --- a/lib/cove/steps/get_existing_container_details.rb +++ b/lib/cove/steps/get_existing_container_details.rb @@ -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 diff --git a/lib/cove/steps/pull_image.rb b/lib/cove/steps/pull_image.rb index 9cd3c62..c00978d 100644 --- a/lib/cove/steps/pull_image.rb +++ b/lib/cove/steps/pull_image.rb @@ -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 diff --git a/spec/cove/invocation/service_up_spec.rb b/spec/cove/invocation/service_up_spec.rb index e17220e..fe6b633 100644 --- a/spec/cove/invocation/service_up_spec.rb +++ b/spec/cove/invocation/service_up_spec.rb @@ -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) @@ -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) ]) } @@ -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) diff --git a/spec/cove/runtime/container_spec.rb b/spec/cove/runtime/container_spec.rb index 8841e85..99d4664 100644 --- a/spec/cove/runtime/container_spec.rb +++ b/spec/cove/runtime/container_spec.rb @@ -24,7 +24,7 @@ }, "Config" => { "Labels" => { - "cove.deployed_version" => "58da9973e60f0", + "cove.version" => "58da9973e60f0", "cove.index" => "1", "cove.role" => "web", "cove.service" => "nginx", @@ -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", diff --git a/spec/cove/steps/ensure_configs_exist_spec.rb b/spec/cove/steps/ensure_configs_exist_spec.rb index 64d957d..36cbbd8 100644 --- a/spec/cove/steps/ensure_configs_exist_spec.rb +++ b/spec/cove/steps/ensure_configs_exist_spec.rb @@ -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) diff --git a/spec/cove/steps/get_existing_container_details_spec.rb b/spec/cove/steps/get_existing_container_details_spec.rb index f692386..4dc2510 100644 --- a/spec/cove/steps/get_existing_container_details_spec.rb +++ b/spec/cove/steps/get_existing_container_details_spec.rb @@ -24,7 +24,7 @@ "Labels": { "cove.service": "test-service", "cove.role": "web", - "cove.deployed_version": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" + "cove.version": "9f86d081884c7d" } } }, @@ -39,7 +39,7 @@ "Labels": { "cove.service": "test-service", "cove.role": "web", - "cove.deployed_version": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" + "cove.version": "9f86d081884c7d" } } }