diff --git a/.changesets/add-ruby-config-file-method-to-installer.md b/.changesets/add-ruby-config-file-method-to-installer.md new file mode 100644 index 000000000..7510c491f --- /dev/null +++ b/.changesets/add-ruby-config-file-method-to-installer.md @@ -0,0 +1,6 @@ +--- +bump: patch +type: add +--- + +Add the `config/appsignal.rb` Ruby config file method to installer, `appsignal install`. diff --git a/lib/appsignal/cli/install.rb b/lib/appsignal/cli/install.rb index 83eed246a..9709e6f12 100644 --- a/lib/appsignal/cli/install.rb +++ b/lib/appsignal/cli/install.rb @@ -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] @@ -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 diff --git a/resources/appsignal.rb.erb b/resources/appsignal.rb.erb new file mode 100644 index 000000000..14d8e90af --- /dev/null +++ b/resources/appsignal.rb.erb @@ -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 diff --git a/spec/lib/appsignal/cli/install_spec.rb b/spec/lib/appsignal/cli/install_spec.rb index ff64b3200..19e735bd9 100644 --- a/spec/lib/appsignal/cli/install_spec.rb +++ b/spec/lib/appsignal/cli/install_spec.rb @@ -7,8 +7,10 @@ let(:output) { out_stream.read } let(:push_api_key) { "my_key" } let(:config) { Appsignal::Config.new(tmp_dir, "") } - let(:config_file_path) { File.join(tmp_dir, "config", "appsignal.yml") } - let(:config_file) { File.read(config_file_path) } + let(:yaml_config_file_path) { File.join(tmp_dir, "config", "appsignal.yml") } + let(:yaml_config_file) { File.read(yaml_config_file_path) } + let(:ruby_config_file_path) { File.join(tmp_dir, "config", "appsignal.rb") } + let(:ruby_config_file) { File.read(ruby_config_file_path) } let(:options) { {} } before do stub_api_validation_request @@ -45,22 +47,44 @@ end end - define :configure_app_name do |name| + define :configure_ruby_app_name do |name| + match do |file_contents| + file_contents =~ /config\.name = "#{name}"/ + end + end + define :configure_ruby_push_api_key do |key| + match do |file_contents| + file_contents =~ /config\.push_api_key = "#{key}"/ + end + end + define :configure_ruby_environment do |env| + match do |file_contents| + match = /config\.activate_if_environment\((.*)\)$/.match(file_contents) + match[1].include?(env) + end + end + define :include_ruby_file_config do + match do |log| + log.include?("Config file written to config/appsignal.rb") + end + end + + define :configure_yaml_app_name do |name| match do |file_contents| file_contents =~ /^ name: "#{name}"/ end end - define :configure_push_api_key do |key| + define :configure_yaml_push_api_key do |key| match do |file_contents| file_contents =~ /^ push_api_key: "#{key}"/ end end - define :configure_environment do |env| + define :configure_yaml_environment do |env| match do |file_contents| file_contents =~ /^#{env}:$/ end end - define :include_file_config do + define :include_yaml_file_config do match do |log| log.include?("Config file written to config/appsignal.yml") end @@ -80,14 +104,18 @@ def stub_api_validation_request alias_method :enter_app_name, :add_cli_input - def choose_config_file + def choose_ruby_config_file add_cli_input "1" end - def choose_environment_config + def choose_yaml_config_file add_cli_input "2" end + def choose_environment_config + add_cli_input "3" + end + def run Dir.chdir tmp_dir do prepare_cli_input @@ -266,18 +294,18 @@ def run File.delete(File.join(environments_dir, "development.rb")) File.delete(File.join(environments_dir, "staging.rb")) add_cli_input "n" - choose_config_file + choose_ruby_config_file end it "only configures the available environments" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to_not configure_environment("development") - expect(config_file).to_not configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to_not configure_ruby_environment("development") + expect(ruby_config_file).to_not configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") expect(output).to include(*installation_instructions) expect(output).to include_complete_install @@ -312,8 +340,34 @@ def run end end - context "with configuration using a configuration file" do - before { choose_config_file } + context "with configuration using a Ruby configuration file" do + before { choose_ruby_config_file } + + it_behaves_like "windows installation" + it_behaves_like "capistrano install" + it_behaves_like "demo data" + + it "writes configuration to file" do + run + + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") + end + + it "completes the installation" do + run + + expect(output).to include(*installation_instructions) + expect(output).to include_complete_install + end + end + + context "with configuration using a YAML configuration file" do + before { choose_yaml_config_file } it_behaves_like "windows installation" it_behaves_like "capistrano install" @@ -322,12 +376,12 @@ def run it "writes configuration to file" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_yaml_file_config + expect(yaml_config_file).to configure_yaml_app_name(app_name) + expect(yaml_config_file).to configure_yaml_push_api_key(push_api_key) + expect(yaml_config_file).to configure_yaml_environment("development") + expect(yaml_config_file).to configure_yaml_environment("staging") + expect(yaml_config_file).to configure_yaml_environment("production") end it "completes the installation" do @@ -379,10 +433,39 @@ def run end end - context "with configuration using a configuration file" do + context "with configuration using a Ruby configuration file" do + before do + enter_app_name app_name + choose_ruby_config_file + end + + it_behaves_like "windows installation" + it_behaves_like "capistrano install" + it_behaves_like "demo data" + + it "writes configuration to file" do + run + + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") + end + + it "completes the installation" do + run + + expect(output).to include(*installation_instructions) + expect(output).to include_complete_install + end + end + + context "with configuration using a YAML configuration file" do before do enter_app_name app_name - choose_config_file + choose_yaml_config_file end it_behaves_like "windows installation" @@ -392,12 +475,12 @@ def run it "writes configuration to file" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_yaml_file_config + expect(yaml_config_file).to configure_yaml_app_name(app_name) + expect(yaml_config_file).to configure_yaml_push_api_key(push_api_key) + expect(yaml_config_file).to configure_yaml_environment("development") + expect(yaml_config_file).to configure_yaml_environment("staging") + expect(yaml_config_file).to configure_yaml_environment("production") end it "completes the installation" do @@ -439,20 +522,20 @@ def run it "prompts the user to fill in an app name" do enter_app_name app_name - choose_config_file + choose_ruby_config_file run expect(output).to include("Installing for Ruby on Rails") expect(output).to include("Unable to automatically detect your Rails app's name.") expect(output).to include("Choose your app's display name for AppSignal.com:") - expect(output).to include_file_config + expect(output).to include_ruby_file_config expect(output).to include_complete_install - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") end end end @@ -499,8 +582,34 @@ def run end end - describe "configure with a configuration file" do - before { choose_config_file } + describe "configure with a Ruby configuration file" do + before { choose_ruby_config_file } + + it_behaves_like "windows installation" + it_behaves_like "capistrano install" + it_behaves_like "demo data" + + it "writes configuration to file" do + run + + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") + end + + it "completes the installation" do + run + + expect(output).to include(*installation_instructions) + expect(output).to include_complete_install + end + end + + describe "configure with a YAML configuration file" do + before { choose_yaml_config_file } it_behaves_like "windows installation" it_behaves_like "capistrano install" @@ -509,12 +618,12 @@ def run it "writes configuration to file" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_yaml_file_config + expect(yaml_config_file).to configure_yaml_app_name(app_name) + expect(yaml_config_file).to configure_yaml_push_api_key(push_api_key) + expect(yaml_config_file).to configure_yaml_environment("development") + expect(yaml_config_file).to configure_yaml_environment("staging") + expect(yaml_config_file).to configure_yaml_environment("production") end it "completes the installation" do @@ -569,8 +678,34 @@ def run end end - describe "configure with a configuration file" do - before { choose_config_file } + describe "configure with a Ruby configuration file" do + before { choose_ruby_config_file } + + it_behaves_like "windows installation" + it_behaves_like "capistrano install" + it_behaves_like "demo data" + + it "writes configuration to file" do + run + + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") + end + + it "completes the installation" do + run + + expect(output).to include(*installation_instructions) + expect(output).to include_complete_install + end + end + + describe "configure with a YAML configuration file" do + before { choose_yaml_config_file } it_behaves_like "windows installation" it_behaves_like "capistrano install" @@ -579,12 +714,12 @@ def run it "writes configuration to file" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_yaml_file_config + expect(yaml_config_file).to configure_yaml_app_name(app_name) + expect(yaml_config_file).to configure_yaml_push_api_key(push_api_key) + expect(yaml_config_file).to configure_yaml_environment("development") + expect(yaml_config_file).to configure_yaml_environment("staging") + expect(yaml_config_file).to configure_yaml_environment("production") end it "completes the installation" do @@ -636,8 +771,34 @@ def run end end - describe "configure with a configuration file" do - before { choose_config_file } + describe "configure with a Ruby configuration file" do + before { choose_ruby_config_file } + + it_behaves_like "windows installation" + it_behaves_like "capistrano install" + it_behaves_like "demo data" + + it "writes configuration to file" do + run + + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") + end + + it "completes the installation" do + run + + expect(output).to include(*installation_instructions) + expect(output).to include_complete_install + end + end + + describe "configure with a YAML configuration file" do + before { choose_yaml_config_file } it_behaves_like "windows installation" it_behaves_like "capistrano install" @@ -646,12 +807,12 @@ def run it "writes configuration to file" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_yaml_file_config + expect(yaml_config_file).to configure_yaml_app_name(app_name) + expect(yaml_config_file).to configure_yaml_push_api_key(push_api_key) + expect(yaml_config_file).to configure_yaml_environment("development") + expect(yaml_config_file).to configure_yaml_environment("staging") + expect(yaml_config_file).to configure_yaml_environment("production") end it "completes the installation" do @@ -706,8 +867,33 @@ def run end end - describe "configure with a configuration file" do - before { choose_config_file } + describe "configure with a Ruby configuration file" do + before { choose_ruby_config_file } + + it_behaves_like "windows installation" + it_behaves_like "capistrano install" + it_behaves_like "demo data" + + it "writes configuration to file" do + run + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") + end + + it "completes the installation" do + run + + expect(output).to include(*installation_instructions) + expect(output).to include_complete_install + end + end + + describe "configure with a YAML configuration file" do + before { choose_yaml_config_file } it_behaves_like "windows installation" it_behaves_like "capistrano install" @@ -715,12 +901,12 @@ def run it "writes configuration to file" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_yaml_file_config + expect(yaml_config_file).to configure_yaml_app_name(app_name) + expect(yaml_config_file).to configure_yaml_push_api_key(push_api_key) + expect(yaml_config_file).to configure_yaml_environment("development") + expect(yaml_config_file).to configure_yaml_environment("staging") + expect(yaml_config_file).to configure_yaml_environment("production") end it "completes the installation" do @@ -776,8 +962,33 @@ def run end end - describe "configure with a configuration file" do - before { choose_config_file } + describe "configure with a Ruby configuration file" do + before { choose_ruby_config_file } + + it_behaves_like "windows installation" + it_behaves_like "capistrano install" + it_behaves_like "demo data" + + it "writes configuration to file" do + run + expect(output).to include_ruby_file_config + expect(ruby_config_file).to configure_ruby_app_name(app_name) + expect(ruby_config_file).to configure_ruby_push_api_key(push_api_key) + expect(ruby_config_file).to configure_ruby_environment("development") + expect(ruby_config_file).to configure_ruby_environment("staging") + expect(ruby_config_file).to configure_ruby_environment("production") + end + + it "completes the installation" do + run + + expect(output).to include(*installation_instructions) + expect(output).to include_complete_install + end + end + + describe "configure with a YAML configuration file" do + before { choose_yaml_config_file } it_behaves_like "windows installation" it_behaves_like "capistrano install" @@ -785,12 +996,12 @@ def run it "writes configuration to file" do run - expect(output).to include_file_config - expect(config_file).to configure_app_name(app_name) - expect(config_file).to configure_push_api_key(push_api_key) - expect(config_file).to configure_environment("development") - expect(config_file).to configure_environment("staging") - expect(config_file).to configure_environment("production") + expect(output).to include_yaml_file_config + expect(yaml_config_file).to configure_yaml_app_name(app_name) + expect(yaml_config_file).to configure_yaml_push_api_key(push_api_key) + expect(yaml_config_file).to configure_yaml_environment("development") + expect(yaml_config_file).to configure_yaml_environment("staging") + expect(yaml_config_file).to configure_yaml_environment("production") end it "completes the installation" do