Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up and attach #27

Merged
merged 9 commits into from
Oct 11, 2019
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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
kaiser (0.4.0)
kaiser (0.4.1)
optimist

GEM
Expand Down
1 change: 1 addition & 0 deletions lib/kaiser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'kaiser/version'
require 'kaiser/kaiserfile'
require 'kaiser/cli_options'
require 'kaiser/cli'
require 'kaiser/after_dotter'
require 'kaiser/dotter'
Expand Down
30 changes: 27 additions & 3 deletions lib/kaiser/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module Kaiser
# The commandline
class Cli
extend Kaiser::CliOptions

def set_config
# This is here for backwards compatibility since it can be used in Kaiserfiles.
# It would be a good idea to deprecate this and make it more abstract.
Expand Down Expand Up @@ -43,7 +45,7 @@ def self.register(name, klass)

def self.run_command(name, global_opts)
cmd = @subcommands[name]
opts = cmd.define_options(global_opts)
opts = cmd.define_options(global_opts + cmd.class.options)

# The define_options method has stripped all arguments from the cli so now
# all that we're left with in ARGV are the subcommand to be run and possibly
Expand All @@ -57,7 +59,6 @@ def self.run_command(name, global_opts)
# unless they create a Kaiserfile firest.
out = Kaiser::Dotter.new
info_out = Kaiser::AfterDotter.new(dotter: out)

if opts[:quiet]
out = File.open(File::NULL, 'w')
info_out = File.open(File::NULL, 'w')
Expand All @@ -72,7 +73,7 @@ def self.run_command(name, global_opts)
)
cmd.set_config

cmd.execute
cmd.execute(opts)
end

def self.all_subcommands_usage
Expand Down Expand Up @@ -216,6 +217,29 @@ def default_db_image
db_image_path('.default')
end

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

attach_mounts = Config.kaiserfile.attach_mounts
volumes = attach_mounts.map { |from, to| "-v #{`pwd`.chomp}/#{from}:#{to}" }.join(' ')

system "docker run -ti
--name #{app_container_name}
--network #{network_name}
--dns #{ip_of_container(Config.config[:shared_names][:dns])}
--dns-search #{http_suffix}
-p #{app_port}:#{app_expose}
-e DEV_APPLICATION_HOST=#{envname}.#{http_suffix}
-e VIRTUAL_HOST=#{envname}.#{http_suffix}
-e VIRTUAL_PORT=#{app_expose}
#{volumes}
#{app_params}
kaiser:#{envname}-#{current_branch} #{cmd}".tr("\n", ' ')

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

def start_app
Config.info_out.puts 'Starting up application'
killrm app_container_name
Expand Down
14 changes: 14 additions & 0 deletions lib/kaiser/cli_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Kaiser
module CliOptions
def option(*option)
@options ||= []
@options << option
end

def options
@options || []
end
end
end
28 changes: 4 additions & 24 deletions lib/kaiser/cmds/attach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,17 @@ def usage
<<~EOS
Shuts down the application container and starts it up again with the current directory bind mounted inside. This way the application will run from the source code in the current directory and any edits you make will immediately show up inside the container. This is ideal for development.

Once the attached container exits (through the use of control+c for example) it will be replaced by a regular non-attached version of the app container.

USAGE: kaiser attach
EOS
end

def execute
def execute(opts)
ensure_setup
cmd = (ARGV || []).join(' ')
killrm app_container_name

volumes = attach_mounts.map { |from, to| "-v #{`pwd`.chomp}/#{from}:#{to}" }.join(' ')

system "docker run -ti
--name #{app_container_name}
--network #{network_name}
--dns #{ip_of_container(Config.config[:shared_names][:dns])}
--dns-search #{http_suffix}
-p #{app_port}:#{app_expose}
-e DEV_APPLICATION_HOST=#{envname}.#{http_suffix}
-e VIRTUAL_HOST=#{envname}.#{http_suffix}
-e VIRTUAL_PORT=#{app_expose}
#{volumes}
#{app_params}
kaiser:#{envname}-#{current_branch} #{cmd}".tr("\n", ' ')

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

def attach_mounts
Config.kaiserfile.attach_mounts
end
end
end
end
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/db_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def usage
EOS
end

def execute
def execute(opts)
ensure_setup
name = ARGV.shift || '.default'
load_db(name)
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/db_reset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def usage
EOS
end

def execute
def execute(opts)
ensure_setup
load_db('.default')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/db_reset_hard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def usage
EOS
end

def execute
def execute(opts)
ensure_setup
FileUtils.rm db_image_path('.default') if File.exist?(db_image_path('.default'))
setup_db
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/db_save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def usage
EOS
end

def execute
def execute(opts)
ensure_setup
name = ARGV.shift || '.default'
save_db(name)
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/deinit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def usage
EOS
end

def execute
def execute(opts)
down
Config.config[:envs].delete(envname)
Config.config[:envnames].delete(Config.work_dir)
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/down.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def usage
EOS
end

def execute
def execute(opts)
down
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def usage
EOS
end

def execute
def execute(opts)
return Optimist.die "Already initialized as #{envname}" if envname

name = ARGV.shift
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def usage
EOS
end

def execute
def execute(opts)
ensure_setup
cmd = (ARGV || []).join(' ')
exec "docker exec -ti #{app_container_name} #{cmd}"
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def usage
EOS
end

def execute
def execute(opts)
exec "docker logs -f #{app_container_name}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def usage
EOS
end

def execute
def execute(opts)
cmd = ARGV.shift
if cmd == 'cert-url'
Config.config[:cert_source] = {
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/cmds/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def usage
EOS
end

def execute
def execute(opts)
ensure_setup
cmd = ARGV.shift
valid_cmds = 'ports cert-source http-suffix'
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 @@ -13,7 +13,7 @@ def usage
EOS
end

def execute
def execute(opts)
Config.config[:shared_names].each do |_, container_name|
killrm container_name
end
Expand Down
11 changes: 9 additions & 2 deletions lib/kaiser/cmds/up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Kaiser
module Cmds
class Up < Cli
option :attach, "Bind mount the current source code directory in the app container (as the \`kaiser attach\` command would)", short: '-a'

def usage
<<~EOS
Boots up the application in docker as defined in the \`Kaiserfile\` in its source code. Usually this will create two docker containers \`<ENV_NAME>-db\` and \`<ENV_NAME>-app\` running your database and application respectively.
Expand All @@ -13,11 +15,16 @@ def usage
EOS
end

def execute
def execute(opts)
ensure_setup
setup_app
setup_db
start_app

if opts[:attach]
attach_app
else
start_app
end
end

def setup_app
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.4.0'
VERSION = '0.4.1'
end
6 changes: 5 additions & 1 deletion spec/kaiser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
shared_examples 'single help' do
it "prints the help message for just #{name} and nothing else" do
# Remove the help arguments generated by Optimist as they won't be in the usage method
unwrapped_output = cmd_stdout.lines.delete_if { |l| l =~ /^ -.+/ }.join
# We remove each line after the first argument. We can't just check for lines that start
# with ' -' because optimist might wrap them over multiple lines.
lines = cmd_stdout.lines
args_line = lines.index { |l| l =~ /^ -.+/ }
unwrapped_output = lines[0, args_line].join

# Remove all newlines because Optimist will wordwrap according to terminal size
unwrapped_output.gsub!("\n", ' ')
Expand Down