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

Make Kaiser work on ARM macs #91

Merged
merged 1 commit into from
Apr 2, 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
4 changes: 2 additions & 2 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: default
jobs:
rspec:
container:
image: ruby:2.7
image: ruby:3.0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -23,7 +23,7 @@ jobs:
run: bundle exec rspec
rubocop:
container:
image: ruby:2.7
image: ruby:3.0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
inherit_from: .rubocop_todo.yml

# don't look at blocklength for specs
# its basically what they do anyway
Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Lint/ConstantDefinitionInBlock:
# Offense count: 11
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 57
Max: 58

# Offense count: 7
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.
Expand Down
22 changes: 20 additions & 2 deletions lib/kaiser/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ def services
@services ||= Config.kaiserfile.services.map { |name, info| Service.new(envname, name, info) }
end

def force_platform
Config.kaiserfile.platform || ''
end

def db_port
Config.config[:envs][envname][:db_port]
end
Expand Down Expand Up @@ -520,6 +524,19 @@ def prepare_cert_volume!
end
end

def selenium_node_image
return ENV['OVERRIDE_SELENIUM_NODE_IMAGE'] unless ENV['OVERRIDE_SELENIUM_NODE_IMAGE'].nil?

if RUBY_PLATFORM.start_with?('arm64') || RUBY_PLATFORM.start_with?('aarch64')
# use the seleniarm image because its more stable in arm procs
# somehow the x64 image does not do well under qemu under arm
return 'seleniarm/standalone-chromium'
end

# default to x64 image
'selenium/standalone-chrome-debug'
end

def ensure_setup
ensure_env

Expand All @@ -540,9 +557,10 @@ def ensure_setup
Config.config[:shared_names][:chrome],
"docker run -d
-p 5900:5900
--shm-size='2g'
--name #{Config.config[:shared_names][:chrome]}
--network #{Config.config[:networkname]}
selenium/standalone-chrome-debug"
#{selenium_node_image}"
)
run_if_dead(
Config.config[:shared_names][:nginx],
Expand Down Expand Up @@ -580,7 +598,7 @@ def network

def container_dead?(container)
x = JSON.parse(`docker inspect #{container} 2>/dev/null`)
return true if x.empty? || x[0]['State']['Running'] == false
x.empty? || x[0]['State']['Running'] == false
end

def if_container_dead(container)
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/shutdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def usage
end

def execute(_opts)
Config.config[:shared_names].each do |_, container_name|
Config.config[:shared_names].each_value do |container_name|
killrm container_name
end

Expand Down
20 changes: 15 additions & 5 deletions lib/kaiser/cmds/up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ def execute(opts)
end
end

def build_cmd
platform_args = ''
platform_args = "--platform=#{force_platform}" unless force_platform.empty?
build_args = docker_build_args.map { |k, v| "--build-arg #{k}=#{v}" }
[
'docker build',
"-t kaiser:#{envname}-#{current_branch}",
"-f #{tmp_dockerfile_name} #{Config.work_dir}",
platform_args,
build_args.join(' ').to_s
]
end

def setup_app
Config.info_out.puts 'Setting up application'
File.write(tmp_dockerfile_name, docker_file_contents)
build_args = docker_build_args.map { |k, v| "--build-arg #{k}=#{v}" }
CommandRunner.run! Config.out, "docker build
-t kaiser:#{envname}-#{current_branch}
-f #{tmp_dockerfile_name} #{Config.work_dir}
#{build_args.join(' ')}"

CommandRunner.run! Config.out, build_cmd.join("\n\t")
FileUtils.rm(tmp_dockerfile_name)
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/kaiser/kaiserfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def db(image,
}
end

def force_platform(platform_name)
@platform = platform_name
end

def expose(port)
@port = port
end
Expand Down
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.6.7'
VERSION = '0.7.0'
end
20 changes: 20 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,24 @@
expect(cli.send(:db_image)).to eq('postgres:alpine')
end
end

describe '#selenium_node_image' do
it 'for x86 machines returns normal selenium' do
cli = Kaiser::Cli.new
stub_const('RUBY_PLATFORM', 'x86_64-linux')
expect(cli.send(:selenium_node_image)).to eq('selenium/standalone-chrome-debug')
end

it 'for arm machines on mac' do
cli = Kaiser::Cli.new
stub_const('RUBY_PLATFORM', 'arm64-darwin23')
expect(cli.send(:selenium_node_image)).to eq('seleniarm/standalone-chromium')
end

it 'for arm machines on linux' do
cli = Kaiser::Cli.new
stub_const('RUBY_PLATFORM', 'aarch64-linux')
expect(cli.send(:selenium_node_image)).to eq('seleniarm/standalone-chromium')
end
end
end
59 changes: 59 additions & 0 deletions spec/cmds/up_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

RSpec.describe Kaiser::Cmds::Up do
describe 'cmd' do
it 'generates correctly' do
kaiserfile = double(Kaiser::Kaiserfile)
kaiserconfig = double(Kaiser::Config)

allow(Kaiser::Config).to receive(:kaiserfile).and_return(kaiserfile)
allow(Kaiser::Config).to receive(:config).and_return(kaiserconfig)

allow(Kaiser::Config).to receive(:work_dir).and_return('somedir')

envs = double('Hash')
allow(envs).to receive(:[]).with('somedir').and_return('meow')

allow(Kaiser::Config.kaiserfile).to receive(:docker_build_args).and_return({})
allow(Kaiser::Config.config).to receive(:[]).with(:envnames).and_return(envs)

obj = described_class.new
allow(obj).to receive(:current_branch).and_return('master')

allow(kaiserfile).to receive(:platform).and_return('')

expect(obj.build_cmd[0]).to eq 'docker build'
expect(obj.build_cmd[1]).to eq '-t kaiser:meow-master'
expect(obj.build_cmd[2]).to eq '-f /meow-dockerfile somedir'
expect(obj.build_cmd[3].to_s).to eq ''
end

it 'generates the appropriate build platform' do
kaiserfile = double(Kaiser::Kaiserfile)
kaiserconfig = double(Kaiser::Config)

allow(Kaiser::Config).to receive(:kaiserfile).and_return(kaiserfile)
allow(Kaiser::Config).to receive(:config).and_return(kaiserconfig)

allow(Kaiser::Config).to receive(:work_dir).and_return('somedir')

envs = double('Hash')
allow(envs).to receive(:[]).with('somedir').and_return('meow')

allow(kaiserfile).to receive(:platform).and_return('linux/catcpu')

allow(Kaiser::Config.kaiserfile).to receive(:docker_build_args).and_return({})
allow(Kaiser::Config.config).to receive(:[]).with(:envnames).and_return(envs)

obj = described_class.new
allow(obj).to receive(:current_branch).and_return('master')

allow(kaiserfile).to receive(:platform).and_return('linux/catcpu')

expect(obj.build_cmd[0]).to eq 'docker build'
expect(obj.build_cmd[1]).to eq '-t kaiser:meow-master'
expect(obj.build_cmd[2]).to eq '-f /meow-dockerfile somedir'
expect(obj.build_cmd[3]).to eq '--platform=linux/catcpu'
end
end
end
20 changes: 20 additions & 0 deletions spec/kaiserfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@
end
end

context '#force_platform' do
context 'when force_platform is specified' do
let(:kaiserfile_contents) { "force_platform 'catcpu'" }

it 'sets platform' do
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
expect(kaiserfile.platform).to eq('catcpu')
end
end

context 'when force_platform is not specified' do
let(:kaiserfile_contents) { '' }

it 'platform is nil' do
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
expect(kaiserfile.platform).to be_nil
end
end
end

context '#service' do
context 'when service is specified' do
let(:kaiserfile_contents) { "service 'santaclaus'" }
Expand Down
2 changes: 1 addition & 1 deletion spec/plugins/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
allow(kaiserfile).to receive(:require).with('kaiser/databases/somebase') { true }

expect(db_class).to receive(:new) { db_driver }.with(hello: :meow)
expect(db_class).to receive(:new) { db_driver }.with({ hello: :meow })
expect(db_driver).to receive(:options_hash) { { port: 1234 } }
expect(db_driver).to receive(:image_name) { '' }
expect(kaiserfile).to receive(:db).with('', port: 1234)
Expand Down
Loading