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

[WIP] Specify env file + envify on the config file #825

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
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/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}"
Comment on lines +175 to +177
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can extract this to his own PR.

else
env_template_path = ".env.erb"
env_path = ".env"
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
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", {})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "config" variable name is not too clear to what this represents. This is not "config", this is the "env" key from config with this values. Maybe we can also extract this to his own PR also?

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
Comment on lines +31 to +37
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main goal. Not sure how we can tackle this.
The reason behind this is to push an env file with "gazillions" of environment variables that can be related to the application code or not.

Any thoughts @djmb ? (sorry to tag you, but I saw that you did the last merges around here)

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