Skip to content

Commit

Permalink
Merge pull request #1327 from appsignal/config-rb-install
Browse files Browse the repository at this point in the history
Add Ruby config file option to installer
  • Loading branch information
tombruijn authored Nov 11, 2024
2 parents 2623790 + d6a0fbd commit f5381a0
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 90 deletions.
6 changes: 6 additions & 0 deletions .changesets/add-ruby-config-file-method-to-installer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: add
---

Add the `config/appsignal.rb` Ruby config file method to installer, `appsignal install`.
58 changes: 46 additions & 12 deletions lib/appsignal/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,35 +229,49 @@ def install_for_unknown_framework(config)
done_notice
end

def configure(config, environments, name_overwritten)
def configure(config, environments, name_overwritten) # rubocop:disable Metrics/AbcSize
install_for_capistrano

ENV["APPSIGNAL_APP_ENV"] = "development"

puts "How do you want to configure AppSignal?"
puts " (1) a config file"
puts " (2) environment variables"
puts " (1) a Ruby config file"
puts " (2) a YAML config file (legacy)"
puts " (3) environment variables"
puts
puts " See our docs for information on the different configuration methods: "
puts " https://docs.appsignal.com/ruby/configuration.html"
puts
loop do
print " Choose (1/2): "
loop do # rubocop:disable Metrics/BlockLength
print " Choose (1-3): "
case ask_for_input
when "1"
puts
print "Writing config file"
print "Writing Ruby config file"
periods
puts
puts colorize " Config file written to config/appsignal.yml", :green
write_config_file(
write_ruby_config_file(
:push_api_key => config[:push_api_key],
:app_name => config[:name],
:environments => environments
)
puts colorize " Config file written to config/appsignal.rb", :green
puts
break
when "2"
puts
print "Writing YAML config file"
periods
puts
write_yaml_config_file(
:push_api_key => config[:push_api_key],
:app_name => config[:name],
:environments => environments
)
puts colorize " Config file written to config/appsignal.yml", :green
puts
break
when "3"
ENV["APPSIGNAL_ACTIVE"] = "true"
ENV["APPSIGNAL_PUSH_API_KEY"] = config[:push_api_key]
ENV["APPSIGNAL_APP_NAME"] = config[:name]
Expand Down Expand Up @@ -325,17 +339,37 @@ def rails_environments
).map { |o| File.basename(o, ".rb") }.sort - EXCLUDED_ENVIRONMENTS
end

def write_config_file(data)
filename = File.join(
def write_ruby_config_file(data)
template = File.join(
File.dirname(__FILE__),
"../../../resources/appsignal.rb.erb"
)
write_config_file(
template,
File.join(Dir.pwd, "config/appsignal.rb"),
data
)
end

def write_yaml_config_file(data)
template = File.join(
File.dirname(__FILE__),
"../../../resources/appsignal.yml.erb"
)
file_contents = File.read(filename)
write_config_file(
template,
File.join(Dir.pwd, "config/appsignal.yml"),
data
)
end

def write_config_file(template_path, path, data)
file_contents = File.read(template_path)
template = ERB.new(file_contents, :trim_mode => "-")
config = template.result(OpenStruct.new(data).instance_eval { binding })

FileUtils.mkdir_p(File.join(Dir.pwd, "config"))
File.write(File.join(Dir.pwd, "config/appsignal.yml"), config)
File.write(path, config)
end

def new_config
Expand Down
22 changes: 22 additions & 0 deletions resources/appsignal.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# AppSignal Ruby gem configuration
# Visit our documentation for a list of all available configuration options.
# https://docs.appsignal.com/ruby/configuration/options.html
Appsignal.configure do |config|
config.activate_if_environment(<%= environments.map(&:inspect).join(", ") %>)
config.name = <%= app_name.inspect %>
# The application's Push API key
# We recommend removing this line and setting this option with the
# APPSIGNAL_PUSH_API_KEY environment variable instead.
# https://docs.appsignal.com/ruby/configuration/options.html#option-push_api_key
config.push_api_key = "<%= push_api_key %>"

# Configure actions that should not be monitored by AppSignal.
# For more information see our docs:
# https://docs.appsignal.com/ruby/configuration/ignore-actions.html
# config.ignore_actions << "ApplicationController#isup"

# Configure errors that should not be recorded by AppSignal.
# For more information see our docs:
# https://docs.appsignal.com/ruby/configuration/ignore-errors.html
# config.ignore_errors << "MyCustomError"
end
Loading

0 comments on commit f5381a0

Please sign in to comment.