Skip to content

Commit

Permalink
Cleanup config rendering (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrer authored Nov 26, 2023
1 parent df7adc0 commit a2a1bca
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 71 deletions.
94 changes: 59 additions & 35 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,37 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (7.0.4.3)
activesupport (= 7.0.4.3)
activesupport (7.0.4.3)
activemodel (7.1.1)
activesupport (= 7.1.1)
activesupport (7.1.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
airbrussh (1.4.2)
sshkit (>= 1.6.1, != 1.7.0)
ast (2.4.2)
backport (1.2.0)
base64 (0.1.1)
bcrypt_pbkdf (1.1.0)
benchmark (0.2.1)
bigdecimal (3.1.4)
byebug (11.1.3)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
diff-lcs (1.5.0)
dotenv (2.8.1)
drb (2.1.1)
ruby2_keywords
dry-configurable (1.1.0)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
dry-core (1.0.0)
dry-core (1.0.1)
concurrent-ruby (~> 1.0)
zeitwerk (~> 2.6)
dry-inflector (1.0.0)
Expand All @@ -44,7 +54,7 @@ GEM
concurrent-ruby (~> 1.0)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
dry-schema (1.13.2)
dry-schema (1.13.3)
concurrent-ruby (~> 1.0)
dry-configurable (~> 1.0, >= 1.0.1)
dry-core (~> 1.0, < 2)
Expand All @@ -66,68 +76,74 @@ GEM
zeitwerk (~> 2.6)
e2mmap (0.1.0)
ed25519 (1.3.0)
i18n (1.12.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
jaro_winkler (1.5.6)
json (2.6.3)
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
language_server-protocol (3.17.0.3)
minitest (5.18.0)
lint_roller (1.1.0)
minitest (5.20.0)
mocktail (2.0.0)
sorbet-eraser (~> 0.3.1)
sorbet-runtime (~> 0.5.9204)
mutex_m (0.1.2)
net-scp (4.0.0)
net-ssh (>= 2.6.5, < 8.0.0)
net-ssh (7.1.0)
nokogiri (1.14.3-arm64-darwin)
net-ssh (7.2.0)
nokogiri (1.15.4-arm64-darwin)
racc (~> 1.4)
nokogiri (1.14.3-x86_64-darwin)
nokogiri (1.15.4-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.14.3-x86_64-linux)
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
parallel (1.22.1)
parser (3.2.2.0)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc (1.6.2)
racc
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
rbs (2.8.4)
regexp_parser (2.7.0)
regexp_parser (2.8.2)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.1)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.2)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.48.1)
rspec-support (3.12.1)
rubocop (1.56.4)
base64 (~> 0.1.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.0.0)
parser (>= 3.2.2.3)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.26.0, < 2.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.28.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-performance (1.16.0)
rubocop-performance (1.19.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
solargraph (0.49.0)
backport (~> 1.2)
benchmark
Expand All @@ -145,21 +161,29 @@ GEM
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
sorbet-eraser (0.3.1)
sorbet-runtime (0.5.11064)
sshkit (1.21.4)
sorbet-runtime (0.5.11142)
sshkit (1.21.5)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
standard (1.26.0)
standard (1.31.2)
language_server-protocol (~> 3.17.0.2)
rubocop (~> 1.48.1)
rubocop-performance (~> 1.16.0)
thor (1.2.1)
tilt (2.1.0)
lint_roller (~> 1.0)
rubocop (~> 1.56.4)
standard-custom (~> 1.0.0)
standard-performance (~> 1.2)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.2.1)
lint_roller (~> 1.1)
rubocop-performance (~> 1.19.1)
thor (1.2.2)
tilt (2.3.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
unicode-display_width (2.5.0)
yard (0.9.34)
zeitwerk (2.6.11)
zeitwerk (2.6.12)

PLATFORMS
arm64-darwin-21
Expand Down
10 changes: 9 additions & 1 deletion lib/cove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
module Cove
class Error < StandardError; end

def self.host_base_dir
"/var/lib/cove"
end

def self.output=(output)
@output = output
end
Expand All @@ -31,11 +35,15 @@ def self.output
@output || $stdout
end

def self.init(config: ENV.fetch("COVE_CONFIG_DIR", "./"))
def self.init(config: root)
@registry = nil
Initialization.new(config, registry).perform
end

def self.root
ENV.fetch("COVE_CONFIG_DIR", "./")
end

def self.registry
@registry ||= Registry.new
end
Expand Down
24 changes: 21 additions & 3 deletions lib/cove/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ class Deployment
delegate :container_count, to: :role
# @return [String] The name of the role of the deployment
delegate :name, to: :role, prefix: true
# @return [String] The name of the service of the deployment
delegate :name, to: :service, prefix: true
# @return [Hash] The environment variables for the deployment
delegate :environment_variables, to: :role

# @return [String] The image of the deployment
delegate :image, to: :role

# param [Cove::Role] role The role to deploy
Expand All @@ -22,6 +20,26 @@ def initialize(role, version: nil)
@version = version
end

# @return [String] The name of the service of the deployment
def service_name
service.name
end

# @return [String] The id of the service of the deployment
def service_id
service.id
end

# @return [String] The directory of the service related to this deployment
def directory
service.directory
end

# @returnn [Array[Hash]] The configs for the deployment
def configs
role.configs
end

# @return [String] The version of the deployment
def version
@version ||= build_version
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Cove
class DeploymentConfig
class DeploymentConfigRenderer
attr_reader :registry, :deployment

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

def render(template)
def call(template)
ERB.new(template, trim_mode: "-").result(binding)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cove/environment_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(deployment)

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

def host_file_path
Expand Down
10 changes: 6 additions & 4 deletions lib/cove/invocation/service_up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module Invocation
class ServiceUp
include SSHKit::DSL

attr_reader :registry, :service

# @param registry [Cove::Registry]
# @param service [Cove::Service]
def initialize(registry:, service:)
Expand All @@ -16,11 +14,12 @@ def initialize(registry:, service:)
# @return nil
def invoke
service = @service # Need to set a local var to be able to reference it in the block below
registry = @registry
roles = registry.roles_for_service(service)

hosts = roles.flat_map(&:hosts).uniq.map(&:sshkit_host)
on(hosts) do |host|
coordinator = Coordinator.new(self, service, roles)
coordinator = Coordinator.new(self, registry, service, roles)
coordinator.run
end

Expand All @@ -32,13 +31,16 @@ def invoke
class Coordinator
# @return
attr_reader :connection
# @return [Cove::Registry]
attr_reader :registry
# @return [Cove::Service]
attr_reader :service
# @return [Array<Cove::Role>]
attr_reader :roles

def initialize(connection, service, roles)
def initialize(connection, registry, service, roles)
@connection = connection
@registry = registry
@service = service
@roles = roles
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cove/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def hosts_for_service(service)
# @param roles [Array<Cove::Role>]
# @return [Array<Cove::Host>]
def hosts_for_roles(*roles)
roles.flatten.compact.flat_map(&:hosts).uniq.map { |hostname| hosts[hostname] }
roles.flatten.compact.flat_map(&:hosts).uniq
end

# @param service [Cove::Service]
Expand Down
14 changes: 6 additions & 8 deletions lib/cove/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ class Role
attr_reader :command
# @return [Integer] The number of containers to run for this role
attr_reader :container_count
# @return [Array<Hash>] The ports to connect to the container
# @return [Array<Hash>] The ports to publish for the containers
attr_reader :ports
# @return [Array<Hash>] The volumes to mount to the container
# @return [Array<Hash>] The volumes to mount into the containers
attr_reader :mounts
# @return [Array<Hash>] The configs to mount into the containers
attr_reader :configs
# @return [String] The image this role uses
delegate :image, to: :service
# @return [String] The name of the service this role belongs to
delegate :name, to: :service, prefix: true

# @param name [String] The name of the role
# @param service [Cove::Service] The service this role belongs to
# @param hosts [Array<Cove::Host>] The hosts this role should run on
# @param command [Array<String>] The command to run in the container
# @param environment_variables [Hash<String, String>] The environment variables to set in the containers for this role
def initialize(name:, service:, hosts:, container_count: 1, command: [], environment_variables: {}, ports: [], mounts: [])
def initialize(name:, service:, hosts:, container_count: 1, command: [], environment_variables: {}, ports: [], mounts: [], configs: [])
@name = name
@service = service
@hosts = hosts
Expand All @@ -35,6 +32,7 @@ def initialize(name:, service:, hosts:, container_count: 1, command: [], environ
@container_count = container_count || 1
@ports = ports
@mounts = mounts
@configs = configs
end

# @return [String]
Expand Down
4 changes: 4 additions & 0 deletions lib/cove/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def id
name
end

def directory
File.join(Cove.root, id)
end

def labels
EntityLabels.new({
"cove.service" => name
Expand Down
Loading

0 comments on commit a2a1bca

Please sign in to comment.