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

Disable registry and upload a entire .env file #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions lib/kamal/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def initialize(*)

private
def load_envs
# TODO: How can we load envs from configs? Is necessary?
if destination = options[:destination]
Dotenv.load(".env.#{destination}", ".env")
else
Expand Down
3 changes: 3 additions & 0 deletions lib/kamal/cli/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class BuildError < StandardError; end

desc "deliver", "Build app and push app image to registry then pull image on servers"
def deliver
# TODO: We need a own "build" method instead of "push" (the docker remote build and push at same time)
push

# TODO: Skip if we disable the registry
pull
end

Expand Down
3 changes: 3 additions & 0 deletions lib/kamal/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ def envify
if destination = options[:destination]
env_template_path = ".env.#{destination}.erb"
env_path = ".env.#{destination}"
elsif envify_from_config = KAMAL.config.raw_config["envify"]
env_template_path = ".env.#{envify_from_config}.erb"
env_path = ".env.#{envify_from_config}"
else
env_template_path = ".env.erb"
env_path = ".env"
Expand Down
1 change: 1 addition & 0 deletions lib/kamal/commands/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def name
end

def target
# TODO: When remote, use the remote context
case
when !config.builder.multiarch? && !config.builder.cached?
native
Expand Down
7 changes: 7 additions & 0 deletions lib/kamal/commands/builder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ class BuilderError < StandardError; end
delegate :args, :secrets, :dockerfile, :target, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, to: :builder_config

def clean
# TODO: we need to clean BEFORE the BUILD only
return if disabled_registry_server?
docker :image, :rm, "--force", config.absolute_image
end

def pull
return if disabled_registry_server?
docker :pull, config.absolute_image
end

Expand All @@ -32,6 +35,10 @@ def validate_image


private
def disabled_registry_server?
config.registry["disabled"]
end

def build_tags
[ "-t", config.absolute_image, "-t", config.latest_image ]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/builder/multiarch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def info

def push
docker :buildx, :build,
"--push",
"--output=type=image,push=#{!disabled_registry_server?}",
"--platform", platform_names,
"--builder", builder_name,
*build_options,
Expand Down
12 changes: 8 additions & 4 deletions lib/kamal/commands/builder/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ def info
end

def push
combine \
docker(:build, *build_options, build_context),
docker(:push, config.absolute_image),
docker(:push, config.latest_image)
if disabled_registry_server?
docker(:build, *build_options, build_context)
else
combine \
docker(:build, *build_options, build_context),
docker(:push, config.absolute_image),
docker(:push, config.latest_image)
end
end
end
2 changes: 1 addition & 1 deletion lib/kamal/commands/builder/native/cached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def remove

def push
docker :buildx, :build,
"--push",
"--output=type=image,push=#{!disabled_registry_server?}",
*build_options,
build_context
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/builder/native/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def info

def push
docker :buildx, :build,
"--push",
"--output=type=image,push=#{!disabled_registry_server?}",
"--platform", platform,
"--builder", builder_name,
*build_options,
Expand Down
1 change: 1 addition & 0 deletions lib/kamal/commands/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Kamal::Commands::Registry < Kamal::Commands::Base
delegate :registry, to: :config

def login
return if registry["disabled"] || registry["disable_login"]
docker :login,
registry["server"],
"-u", sensitive(Kamal::Utils.escape_shell_value(lookup("username"))),
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/traefik.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def port

def env
Kamal::Configuration::Env.from_config \
config: config.traefik.fetch("env", {}),
config: config.traefik,
secrets_file: File.join(config.host_env_directory, "traefik", "traefik.env")
end

Expand Down
4 changes: 2 additions & 2 deletions lib/kamal/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ def ensure_required_keys_present
raise ArgumentError, "Missing required configuration for #{key}" unless raw_config[key].present?
end

if raw_config.registry["username"].blank?
if raw_config.registry["username"].blank? && raw_config.registry["disable_login"] != true && raw_config.registry["disabled"] != true
raise ArgumentError, "You must specify a username for the registry in config/deploy.yml"
end

if raw_config.registry["password"].blank?
if raw_config.registry["password"].blank? && raw_config.registry["disable_login"] != true && raw_config.registry["disabled"] != true
raise ArgumentError, "You must specify a password for the registry in config/deploy.yml (or set the ENV variable if that's used)"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/configuration/accessory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def label_args

def env
Kamal::Configuration::Env.from_config \
config: specifics.fetch("env", {}),
config: specifics,
secrets_file: File.join(config.host_env_directory, "accessories", "#{service_name}.env")
end

Expand Down
23 changes: 17 additions & 6 deletions lib/kamal/configuration/env.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
class Kamal::Configuration::Env
attr_reader :secrets_keys, :clear, :secrets_file
attr_reader :secrets_keys, :clear, :secrets_file, :env_file
delegate :argumentize, to: Kamal::Utils

def self.from_config(config:, secrets_file: nil)
secrets_keys = config.fetch("secret", [])
clear = config.fetch("clear", config.key?("secret") || config.key?("tags") ? {} : config)
env_key_config = config.class == Kamal::Configuration ? config.env : config.fetch("env", {})
secrets_keys = env_key_config.fetch("secret", [])
clear = env_key_config.fetch("clear", env_key_config.key?("secret") || env_key_config.key?("tags") ? {} : env_key_config)
# TODO: Support a wide env_file
env_file = config.class == Kamal::Configuration ? nil : config.fetch("env_file", nil)

new clear: clear, secrets_keys: secrets_keys, secrets_file: secrets_file
new clear: clear, secrets_keys: secrets_keys, secrets_file: secrets_file, env_file: env_file
end

def initialize(clear:, secrets_keys:, secrets_file:)
def initialize(clear:, secrets_keys:, secrets_file:, env_file:)
@clear = clear
@secrets_keys = secrets_keys
@secrets_file = secrets_file
@env_file = env_file
end

def args
Expand All @@ -24,7 +28,13 @@ def secrets_io
end

def secrets
@secrets ||= secrets_keys.to_h { |key| [ key, ENV.fetch(key) ] }
# TODO: More than one @env_file
# TODO: Considerer a merge between env_file and env
if @env_file
Dotenv::Environment.new(@env_file)
else
secrets_keys.to_h { |key| [ key, ENV.fetch(key) ] }
end
end

def secrets_directory
Expand All @@ -35,6 +45,7 @@ def merge(other)
self.class.new \
clear: @clear.merge(other.clear),
secrets_keys: @secrets_keys | other.secrets_keys,
env_file: @env_file ? @env_file : other.env_file,
secrets_file: secrets_file
end
end
4 changes: 2 additions & 2 deletions lib/kamal/configuration/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ def specializations
end

def specialized_env
Kamal::Configuration::Env.from_config config: specializations.fetch("env", {})
Kamal::Configuration::Env.from_config config: specializations
end

# Secrets are stored in an array, which won't merge by default, so have to do it by hand.
def base_env
Kamal::Configuration::Env.from_config \
config: config.env,
config: config,
secrets_file: File.join(config.host_env_directory, "roles", "#{container_prefix}.env")
end

Expand Down
Loading