Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
feat(worker): worker option to use dot env (#289)
Browse files Browse the repository at this point in the history
* fix(monit): workers can use dot env when variables exceed 4kb procmatch limit

* fix(monit): add worker dot env to docs, move env_config to helper

* test: worker spec doesnt need env vars besides rails mode when dotenv

* fix(helper): reference environment in options
  • Loading branch information
phongsi authored Jun 30, 2022
1 parent 66ff499 commit ba3d312
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 17 deletions.
1 change: 1 addition & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ suites:
extra_config_ssl: true
worker:
adapter: "sidekiq"
dot_env: true
config:
concurency: 5
verbose: true
Expand Down
1 change: 1 addition & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
default['defaults']['worker']['adapter'] = 'null'
default['defaults']['worker']['process_count'] = 2
default['defaults']['worker']['syslog'] = true
default['defaults']['worker']['dot_env'] = false

default['monit']['basedir'] = if platform?('centos', 'redhat', 'fedora', 'amazon')
'/etc/monit.d'
Expand Down
5 changes: 5 additions & 0 deletions docs/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,11 @@ Worker configuration. Currently sidekiq, delayed_job, resque, shoryuken, good_jo
- **Default** `opsworks_ruby`
- The name of the cookbook from which the worker monit template(s) will be drawn.

- `app['worker']['dot_env']`
- **Supported values:** `true`, `false`
- **Default:** `false`
- Creates a `.env` file with all pre-configured environment variables. Useful for gems like [dotenv](https://github.com/bkeepers/dotenv)

#### sidekiq

- `app['worker']['config']`
Expand Down
20 changes: 6 additions & 14 deletions libraries/drivers_appserver_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,18 @@ def setup_application_yml
return unless raw_out[:application_yml]

append_to_overwritable_defaults('symlinks', 'config/application.yml' => 'config/application.yml')
env_config(source_file: 'config/application.yml', destination_file: 'config/application.yml')
env_config(
source_file: 'config/application.yml',
destination_file: 'config/application.yml',
environment: environment
)
end

def setup_dot_env
return unless raw_out[:dot_env]

append_to_overwritable_defaults('symlinks', 'dot_env' => '.env')
env_config(source_file: 'dot_env', destination_file: 'dot_env')
end

def env_config(options = { source_file: nil, destination_file: nil })
deploy_to = deploy_dir(app)
env = environment

context.template File.join(deploy_to, 'shared', options[:destination_file]) do
owner node['deployer']['user']
group www_group
source "#{File.basename(options[:source_file])}.erb"
variables environment: env
end
env_config(source_file: 'dot_env', destination_file: 'dot_env', environment: environment)
end

def environment
Expand Down
20 changes: 19 additions & 1 deletion libraries/drivers_worker_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ def setup
handle_packages
end

def before_deploy
super
setup_dot_env
end

def validate_app_engine; end

protected

# Adds or updates the monit configs for the worker and notifies monit to
# reload the configuration.
# rubocop:disable Metrics/AbcSize
def add_worker_monit
opts = {
adapter: adapter,
app_shortname: app['shortname'],
application: app['shortname'],
deploy_to: deploy_dir(app),
environment: environment,
environment: embed_environment_in_monit? ? environment : { 'RAILS_ENV' => deploy_env },
name: app['name'],
out: out,
source_cookbook: worker_monit_template_cookbook
Expand All @@ -38,6 +44,18 @@ def add_worker_monit
notifies :run, 'execute[monit reload]', :immediately
end
end
# rubocop:enable Metrics/AbcSize

def embed_environment_in_monit?
!raw_out[:dot_env]
end

def setup_dot_env
return unless raw_out[:dot_env]

append_to_overwritable_defaults('symlinks', 'dot_env' => '.env')
env_config(source_file: 'dot_env', destination_file: 'dot_env', environment: environment)
end

def worker_monit_template_cookbook
node['deploy'][app['shortname']][driver_type]['monit_template_cookbook'] || context.cookbook_name
Expand Down
1 change: 1 addition & 0 deletions libraries/drivers_worker_shoryuken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def configure
end

def before_deploy
super
quiet_shoryuken
end

Expand Down
1 change: 1 addition & 0 deletions libraries/drivers_worker_sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def configure
end

def before_deploy
super
quiet_sidekiq
end

Expand Down
12 changes: 12 additions & 0 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,15 @@ def append_to_overwritable_defaults(field, options) # rubocop:disable Metrics/Ab
end
node.default['deploy'][app['shortname']]['global'][field].merge!(options)
end

def env_config(options = { source_file: nil, destination_file: nil, environment: {} })
deploy_to = deploy_dir(app)
env = options[:environment]

context.template File.join(deploy_to, 'shared', options[:destination_file]) do
owner node['deployer']['user']
group www_group
source "#{File.basename(options[:source_file])}.erb"
variables environment: env
end
end
4 changes: 2 additions & 2 deletions test/integration/all_options/serverspec/all_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@
its(:content) { should include 'group sidekiq_dummy_project_group' }
its(:content) { should include 'check process sidekiq_dummy_project-1' }
its(:content) do
should include 'RAILS_ENV="staging" HOME="/home/deploy" USER="deploy" ' \
should include 'RAILS_ENV="staging" ' \
'bundle exec sidekiq -C /srv/www/dummy_project/shared/config/sidekiq_1.yml'
end
its(:content) { should include 'logger -t sidekiq-dummy_project-1' }
its(:content) { should include 'check process sidekiq_dummy_project-2' }
its(:content) do
should include 'RAILS_ENV="staging" HOME="/home/deploy" USER="deploy" ' \
should include 'RAILS_ENV="staging" ' \
'bundle exec sidekiq -C /srv/www/dummy_project/shared/config/sidekiq_2.yml'
end
its(:content) { should include 'logger -t sidekiq-dummy_project-2' }
Expand Down

0 comments on commit ba3d312

Please sign in to comment.