Skip to content

Commit

Permalink
Strip out env loading, envify, env push
Browse files Browse the repository at this point in the history
  • Loading branch information
djmb committed Sep 4, 2024
1 parent 5c4c33e commit 6a06efc
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 215 deletions.
48 changes: 3 additions & 45 deletions lib/kamal/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,60 +32,18 @@ def initialize(args = [], local_options = {}, config = {})
super
end
@original_env = ENV.to_h.dup
load_env
initialize_commander(options_with_subcommand_class_options)
end

private
def reload_env
reset_env
load_env
end

def load_env
def load_secrets
if destination = options[:destination]
if File.exist?(".kamal/env.#{destination}") || File.exist?(".kamal/env")
Dotenv.load(".kamal/env.#{destination}", ".kamal/env")
else
loading_files = [ (".env" if File.exist?(".env")), (".env.#{destination}" if File.exist?(".env.#{destination}")) ].compact
if loading_files.any?
warn "Loading #{loading_files.join(" and ")} from the project root, use .kamal/env* instead"
Dotenv.load(".env.#{destination}", ".env")
end
end
Dotenv.parse(".kamal/secrets.#{destination}", ".kamal/secrets")
else
if File.exist?(".kamal/env")
Dotenv.load(".kamal/env")
elsif File.exist?(".env")
warn "Loading .env from the project root is deprecated, use .kamal/env instead"
Dotenv.load(".env")
end
end
end

def reset_env
replace_env @original_env
end

def replace_env(env)
ENV.clear
ENV.update(env)
end

def with_original_env
keeping_current_env do
reset_env
yield
Dotenv.parse(".kamal/secrets")
end
end

def keeping_current_env
current_env = ENV.to_h.dup
yield
ensure
replace_env(current_env)
end

def options_with_subcommand_class_options
options.merge(@_initializer.last[:class_options] || {})
end
Expand Down
54 changes: 0 additions & 54 deletions lib/kamal/cli/env.rb

This file was deleted.

43 changes: 0 additions & 43 deletions lib/kamal/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def setup
say "Ensure Docker is installed...", :magenta
invoke "kamal:cli:server:bootstrap", [], invoke_options

say "Evaluate and push env files...", :magenta
invoke "kamal:cli:main:envify", [], invoke_options
invoke "kamal:cli:env:push", [], invoke_options

invoke "kamal:cli:accessory:boot", [ "all" ], invoke_options
deploy
end
Expand Down Expand Up @@ -179,45 +175,6 @@ def init
end
end

desc "envify", "Create .env by evaluating .env.erb (or .env.staging.erb -> .env.staging when using -d staging)"
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip .env file push"
def envify
if destination = options[:destination]
env_template_path = ".kamal/env.#{destination}.erb"
env_path = ".kamal/env.#{destination}"
else
env_template_path = ".kamal/env.erb"
env_path = ".kamal/env"
end

unless Pathname.new(File.expand_path(env_template_path)).exist?
if destination = options[:destination]
env_template_path = ".env.#{destination}.erb"
env_path = ".env.#{destination}"
else
env_template_path = ".env.erb"
env_path = ".env"
end

if Pathname.new(File.expand_path(env_template_path)).exist?
warn "Loading #{env_template_path} from the project root is deprecated, use .kamal/env[.<DESTINATION>].erb instead"
end
end

if Pathname.new(File.expand_path(env_template_path)).exist?
# Ensure existing env doesn't pollute template evaluation
content = with_original_env { ERB.new(File.read(env_template_path), trim_mode: "-").result }
File.write(env_path, content, perm: 0600)

unless options[:skip_push]
reload_env
invoke "kamal:cli:env:push", options
end
else
puts "Skipping envify (no #{env_template_path} exist)"
end
end

desc "remove", "Remove Traefik, app, accessories, and registry session from servers"
option :confirmed, aliases: "-y", type: :boolean, default: false, desc: "Proceed without confirmation question"
def remove
Expand Down
47 changes: 0 additions & 47 deletions test/cli/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class CliMainTest < CliTestCase
invoke_options = { "config_file" => "test/fixtures/deploy_simple.yml", "version" => "999", "skip_hooks" => false }

Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:server:bootstrap", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:main:envify", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:boot", [ "all" ], invoke_options)
Kamal::Cli::Main.any_instance.expects(:deploy)

Expand All @@ -24,7 +22,6 @@ class CliMainTest < CliTestCase

Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:server:bootstrap", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:main:envify", [], invoke_options)
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:accessory:boot", [ "all" ], invoke_options)
# deploy
Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:registry:login", [], invoke_options.merge(skip_local: true))
Expand Down Expand Up @@ -438,50 +435,6 @@ class CliMainTest < CliTestCase
end
end

test "envify" do
with_test_env_files("env.erb": "HELLO=<%= 'world' %>") do
run_command("envify")
assert_equal("HELLO=world", File.read(".kamal/env"))
end
end

test "envify with blank line trimming" do
file = <<~EOF
HELLO=<%= 'world' %>
<% if true -%>
KEY=value
<% end -%>
EOF

with_test_env_files("env.erb": file) do
run_command("envify")
assert_equal("HELLO=world\nKEY=value\n", File.read(".kamal/env"))
end
end

test "envify with destination" do
with_test_env_files("env.world.erb": "HELLO=<%= 'world' %>") do
run_command("envify", "-d", "world", config_file: "deploy_for_dest")
assert_equal "HELLO=world", File.read(".kamal/env.world")
end
end

test "envify with skip_push" do
Pathname.any_instance.expects(:exist?).returns(true).times(2)
File.expects(:read).with(".kamal/env.erb").returns("HELLO=<%= 'world' %>")
File.expects(:write).with(".kamal/env", "HELLO=world", perm: 0600)

Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push").never
run_command("envify", "--skip-push")
end

test "envify with clean env" do
with_test_env_files("env": "HELLO=already", "env.erb": "HELLO=<%= ENV.fetch 'HELLO', 'never' %>") do
run_command("envify", "--skip-push")
assert_equal "HELLO=never", File.read(".kamal/env")
end
end

test "remove with confirmation" do
run_command("remove", "-y", config_file: "deploy_with_accessories").tap do |output|
assert_match /docker container stop traefik/, output
Expand Down
4 changes: 0 additions & 4 deletions test/integration/accessory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

class AccessoryTest < IntegrationTest
test "boot, stop, start, restart, logs, remove" do
kamal :envify

kamal :accessory, :boot, :busybox
assert_accessory_running :busybox

Expand All @@ -21,8 +19,6 @@ class AccessoryTest < IntegrationTest

kamal :accessory, :remove, :busybox, "-y"
assert_accessory_not_running :busybox

kamal :env, :delete
end

private
Expand Down
2 changes: 0 additions & 2 deletions test/integration/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

class AppTest < IntegrationTest
test "stop, start, boot, logs, images, containers, exec, remove" do
kamal :envify

kamal :deploy

assert_app_is_up
Expand Down
2 changes: 0 additions & 2 deletions test/integration/broken_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class BrokenDeployTest < IntegrationTest
test "deploying a bad image" do
@app = "app_with_roles"

kamal :envify

first_version = latest_app_version

kamal :deploy
Expand Down
2 changes: 0 additions & 2 deletions test/integration/lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

class LockTest < IntegrationTest
test "acquire, release, status" do
kamal :envify

kamal :lock, :acquire, "-m 'Integration Tests'"

status = kamal :lock, :status, capture: true
Expand Down
17 changes: 5 additions & 12 deletions test/integration/main_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require_relative "integration_test"

class MainTest < IntegrationTest
test "envify, deploy, redeploy, rollback, details and audit" do
kamal :envify
test "deploy, redeploy, rollback, details and audit" do
assert_env_files
remove_local_env_file

Expand Down Expand Up @@ -37,16 +36,11 @@ class MainTest < IntegrationTest

audit = kamal :audit, capture: true
assert_match /Booted app version #{first_version}.*Booted app version #{second_version}.*Booted app version #{first_version}.*/m, audit

kamal :env, :delete
assert_no_remote_env_file
end

test "app with roles" do
@app = "app_with_roles"

kamal :envify

version = latest_app_version

assert_app_is_down
Expand Down Expand Up @@ -103,7 +97,6 @@ class MainTest < IntegrationTest
kamal :remove, "-y"
assert_no_images_or_containers

kamal :envify
kamal :setup
assert_images_and_containers

Expand All @@ -113,7 +106,7 @@ class MainTest < IntegrationTest

private
def assert_local_env_file(contents)
assert_equal contents, deployer_exec("cat .kamal/env", capture: true)
assert_equal contents, deployer_exec("cat .kamal/secrets", capture: true)
end

def assert_envs(version:)
Expand Down Expand Up @@ -143,15 +136,15 @@ def assert_env_files
end

def remove_local_env_file
deployer_exec("rm .kamal/env")
deployer_exec("rm .kamal/secrets")
end

def assert_remote_env_file(contents, vm:)
assert_equal contents, docker_compose("exec #{vm} cat /root/.kamal/env/roles/app-web.env", capture: true)
assert_equal contents, docker_compose("exec #{vm} cat /root/.kamal/secrets/roles/app-web.env", capture: true)
end

def assert_no_remote_env_file
assert_equal "nofile", docker_compose("exec vm1 stat /root/.kamal/env/roles/app-web.env 2> /dev/null || echo nofile", capture: true)
assert_equal "nofile", docker_compose("exec vm1 stat /root/.kamal/secrets/roles/app-web.env 2> /dev/null || echo nofile", capture: true)
end

def assert_accumulated_assets(*versions)
Expand Down
4 changes: 0 additions & 4 deletions test/integration/traefik_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

class TraefikTest < IntegrationTest
test "boot, reboot, stop, start, restart, logs, remove" do
kamal :envify

kamal :traefik, :boot
assert_traefik_running

Expand Down Expand Up @@ -46,8 +44,6 @@ class TraefikTest < IntegrationTest

kamal :traefik, :remove
assert_traefik_not_running

kamal :env, :delete
end

private
Expand Down

0 comments on commit 6a06efc

Please sign in to comment.