在CircleCI中设置Elasticsearch和Ruby on Rails

4

我正试图在我的Rails应用程序中使用CircleCI和Elasticsearch。我认为已经配置了图像,但是在CI中如何连接它呢?

到目前为止,我尝试过...

https://github.com/elastic/elasticsearch/issues/23866

错误信息

Elasticsearch::Transport::Transport::Errors::Unauthorized: [401]

Circle YAML Config

version: 2
jobs:
  build:
    working_directory: ~/gathrly-smartforms
    docker:
      - image: circleci/ruby:2.4.1-node
        environment:
          RAILS_ENV: continous_integration
          PGHOST: 127.0.0.1
          PGUSER: rails_test_user

      - image: circleci/postgres:9.6.3-alpine
        environment:
          POSTGRES_USER: rails_test_user
          POSTGRES_PASSWORD: ""
          POSTGRES_DB: continous_integration

      - image: redis:4.0.2
      - image: docker.elastic.co/elasticsearch/elasticsearch:5.4.2

    steps:
      - checkout

      - restore_cache:
          keys:
            - my-application-{{ checksum "Gemfile.lock" }}
            - my-application-

      - save_cache:
          key: rails-demo-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle

      - run:
          name: Setup Bundler and Gems
          command: |
            gem install bundler
            gem update bundler
            gem install brakeman
            gem install rubocop
            gem install rubocop-rspec
            gem install scss_lint
            gem install eslint-rails
            gem install execjs
            bundle config without development:test
            bundle check --path=vendor/bundle || bundle install --without development test --path=vendor/bundle --jobs 4 --retry 3

      - run:
          name: Setup Postgres
          command: |
            sudo apt-get install postgresql-client

      - run:
          name: Setup Rails Database
          command: |
            RAILS_ENV=continous_integration bundle exec rake db:drop
            RAILS_ENV=continous_integration bundle exec rake db:setup

      - run:
          name: Run Rspec
          command: |
            RAILS_ENV=continous_integration bundle exec rspec --format RspecJunitFormatter -o /tmp/test-results/rspec.xml

      - store_test_results:
          path: /tmp/test-results

Elastic Search 初始化程序

require 'faraday_middleware/aws_signers_v4'

if Rails.env.production? || Rails.env.staging?
  Elasticsearch::Model.client = Elasticsearch::Client.new(url: ENV["AWS_ELASTICSEARCH_HOST"]) do |f|
    f.request :aws_signers_v4,
      credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY'], ENV['AWS_ACCESS_SECRET_KEY']),
      service_name: 'es',
      region: ENV['AWS_REGION']

    f.adapter Faraday.default_adapter
  end
else
  config = {
    host: "http://127.0.0.1:9200",
    transport_options: {
      request: { timeout: 5 }
    }
  }
  if File.exists?("config/elasticsearch.yml")
    config.merge!(YAML.load_file("config/elasticsearch.yml"))
  end

  Elasticsearch::Model.client = Elasticsearch::Client.new(config)
end

嗨,Chris。如果您有答案材料,请始终将其作为自我回答添加在下面,而不是将其与问题合并。这里有一个“回答你自己的问题”的按钮供此目的使用。 - halfer
此外,我注意到您的问题标题由小写标签列表组成。社区通常更喜欢使用完整的英语句子来构建标题,并且符合适当的大小写规则以提高可读性。我发现将标题作为问题也可以很好地运作,但我并不认为这是强制性的。请查看我的社区答案了解更多信息。 - halfer
2个回答

2
弹性公司的官方Docker镜像都预装了X-Pack。

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/docker.html

这意味着您的 Elasticsearch 实例正在运行安全模式,但似乎您没有向 Elasticsearch 客户端提供任何安全凭据,因此当您尝试连接时会收到未经授权(401)的错误。
您应该通过在 elasticsearch.yml 中添加 xpack.security.enabled: false 来关闭 ES 实例中的安全性,或者在请求中提供有效的凭据。

0

(代表问题作者发布)

从提供的其他答案中:

development: &default
  host: 'http://localhost:9200/'
  transport_options:
    request:
      timeout: !!integer 300

test:
  <<: *default

staging:
  <<: *default

continous_integration:
  <<: *default
  xpack.security.enabled: false

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接