在CircleCI中设置Elasticsearch和Ruby on Rails [英] Setting up Elasticsearch and Ruby on Rails in CircleCI

查看:131
本文介绍了在CircleCI中设置Elasticsearch和Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails应用程序中使用Elasticsearch设置CircleCI。我认为已配置了映像,但是如何在CI中连接到映像?

I am trying to setup CircleCI with Elasticsearch in my Rails application. I think have the image configured, but how do I connect to it in CI?

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

So far I have tried...

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

错误消息

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

圆YAML配置

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

弹性搜索初始化器

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


推荐答案

来自Elastic的官方Docker映像预装了x-pack。

The official docker images from elastic come with x-pack preinstalled.

https://www.elastic.co/guide/zh-CN/elasticsearch/reference/5.4/docker.html

这意味着哟您的Elasticsearch实例正在启用安全性的情况下运行,但是您似乎没有向Elasticsearch客户端提供任何安全凭证,因此当您执行以下操作时会收到未授权的错误( 401 )尝试连接。

That means that your elasticsearch instance is running with security enabled, but you do not appear to be providing any security credentials to your elasticsearch client, and so you get an unauthorised (401) error when you try to connect.

您应该通过添加
xpack.security.enabled:false 到您的 elasticsearch.yml ,或在您的请求中提供有效的凭据。

You should either turn off security in your ES instance by adding xpack.security.enabled: false to your elasticsearch.yml, or provide valid credentials in your request.

这篇关于在CircleCI中设置Elasticsearch和Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆