Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Service feature #98

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ inherit_from: .rubocop_todo.yml
# its basically what they do anyway
Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
- 'spec/**/*.rb'

Layout/LineLength:
Exclude:
- 'spec/**/*'
12 changes: 7 additions & 5 deletions lib/kaiser/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ def start_services
services.each do |service|
Config.info_out.puts "Starting service: #{service.name}"
run_if_dead(
service.shared_name,
"docker run -d
--name #{service.shared_name}
--network #{Config.config[:networkname]}
#{service.image}"
service.shared_name, service.start_docker_command
)
end
end
Expand Down Expand Up @@ -246,6 +242,10 @@ def default_db_image
end

def attach_app
start_services

puts 'Attaching to app...'

cmd = (ARGV || []).join(' ')
killrm app_container_name

Expand All @@ -265,6 +265,8 @@ def attach_app
#{app_params}
kaiser:#{envname}-#{current_branch} #{cmd}".tr("\n", ' ')

stop_services

Config.out.puts 'Cleaning up...'
end

Expand Down
14 changes: 12 additions & 2 deletions lib/kaiser/kaiserfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,20 @@ def type(value)
@server_type = value
end

def service(name, image: name)
def service(name,
image: name,
command: nil,
binds: {},
env: {})

raise "duplicate service #{name.inspect}" if @services.key?(name)

@services[name] = { image: image }
@services[name] = {
image: image,
command: command,
binds: binds,
env: env
}
end
end
end
38 changes: 38 additions & 0 deletions lib/kaiser/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,43 @@ def shared_name
def image
@service_info[:image]
end

def command
@service_info[:command].to_s
end

def binds
@service_info[:binds] || {}
end

def env
@service_info[:env] || {}
end

def start_docker_command
envstring = env.map do |k, v|
"-e #{k}=#{v}"
end.join(' ')

bindstring = binds.map do |k, v|
"-v #{k}:#{v}"
end.join(' ')

commandstring = command

cmd_array = [
'docker run -d',
"--name #{shared_name}",
"--network #{Config.config[:networkname]}",
envstring,
bindstring,
image,
commandstring
]

cmd_array.filter! { |x| !x.empty? }

cmd_array.join(' ')
end
end
end
2 changes: 1 addition & 1 deletion lib/kaiser/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Kaiser
VERSION = '0.7.1'
VERSION = '0.8.0'
end
21 changes: 18 additions & 3 deletions spec/kaiserfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@

it 'adds a service' do
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
expect(kaiserfile.services).to eq('santaclaus' => { image: 'santaclaus' })
expect(kaiserfile.services).to eq('santaclaus' => {
image: 'santaclaus',
command: nil,
binds: {},
env: {}
})
end
end

Expand All @@ -206,7 +211,12 @@

it 'adds a service with the image name' do
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
expect(kaiserfile.services).to eq('santaclaus' => { image: 'northpole/santaclaus' })
expect(kaiserfile.services).to eq('santaclaus' => {
image: 'northpole/santaclaus',
command: nil,
binds: {},
env: {}
})
end
end

Expand All @@ -215,7 +225,12 @@

it 'adds a service with the image name' do
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
expect(kaiserfile.services).to eq('santaclaus' => { image: 'northpole/santaclaus:last_christmas' })
expect(kaiserfile.services).to eq('santaclaus' => {
image: 'northpole/santaclaus:last_christmas',
command: nil,
binds: {},
env: {}
})
end
end
end
Expand Down
43 changes: 43 additions & 0 deletions spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,47 @@

expect(s.name).to eq 'santa'
end

describe '#start_docker_command' do
before do
allow(Kaiser::Config).to receive(:config).and_return({ networkname: 'testnet' })
end

it 'provides a proper docker command' do
s = Kaiser::Service.new('meow', 'santa', { image: 'np/santa:lol' })

expect(s.start_docker_command).to eq 'docker run -d --name meow-santa --network testnet np/santa:lol'
end

it 'provides a command with envs' do
s = Kaiser::Service.new('meow', 'santa', {
image: 'np/santa:lol',
env: {
'HELLO' => 'world'
}
})

expect(s.start_docker_command).to eq 'docker run -d --name meow-santa --network testnet -e HELLO=world np/santa:lol'
end

it 'provides a command with binds' do
s = Kaiser::Service.new('meow', 'santa', {
image: 'np/santa:lol',
binds: {
'/home/user' => '/home/inside/container'
}
})

expect(s.start_docker_command).to eq 'docker run -d --name meow-santa --network testnet -v /home/user:/home/inside/container np/santa:lol'
end

it 'provides a command with command' do
s = Kaiser::Service.new('meow', 'santa', {
image: 'np/santa:lol',
command: 'hohoho'
})

expect(s.start_docker_command).to eq 'docker run -d --name meow-santa --network testnet np/santa:lol hohoho'
end
end
end
Loading