Skip to content

Commit

Permalink
Add ping for chef_index that checks rabbitmq
Browse files Browse the repository at this point in the history
  • Loading branch information
jaym committed Jul 31, 2017
1 parent 59324ec commit d5a9fc4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@
retries 10
end

execute "#{rmq_ctl} set_permissions -p #{rabbitmq['vhost']} #{rabbitmq['management_user']} \".*\" \".*\" \".*\"" do
environment (rabbitmq_env)
user opc_username
not_if "#{rmq_ctl_chpst} list_user_permissions #{rabbitmq['management_user']}|grep #{rabbitmq['vhost']}", :environment => rabbitmq_env, :user => "root"
retries 10
end

execute "#{rmq_ctl} set_permissions -p / #{rabbitmq['management_user']} \".*\" \".*\" \".*\"" do
environment (rabbitmq_env)
user opc_username
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
oc_chef_action,
<% end %>
chef_sql,
chef_index,
chef_<%= node['private_chef']['opscode-erchef']['search_provider'] %>
]},
{base_resource_url, <%= @helper.erl_atom_or_string(node['private_chef']['opscode-erchef']['base_resource_url']) %>},
Expand Down Expand Up @@ -230,6 +231,24 @@
{max_age, <%= @solr_http_max_age %>},
{max_connection_duration, <%= @solr_http_max_connection_duration %>},
{ibrowse_options, <%= @solr_ibrowse_options %>}
]},
{rabbitmq_index_management_service, [
{user, "<%= @node['private_chef']['rabbitmq']['management_user'] %>"},
<% if node['private_chef']['fips_enabled'] -%>
{root_url, "http://<%= node['private_chef']['rabbitmq']['vip'] %>:<%= @node['private_chef']['rabbitmq']['management_port'] %>/api"},
<% else -%>
{root_url, "https://<%= node['private_chef']['rabbitmq']['vip'] %>:<%= @node['private_chef']['rabbitmq']['management_port'] %>/api"},
<% end %>
{timeout, <%= @node['private_chef']['rabbitmq']['rabbit_mgmt_timeout'] %>},
{init_count, <%= @node['private_chef']['rabbitmq']['rabbit_mgmt_http_init_count'] %>},
{max_count, <%= @node['private_chef']['rabbitmq']['rabbit_mgmt_http_max_count'] %>},
{cull_interval, {<%= @node['private_chef']['rabbitmq']['rabbit_mgmt_http_cull_interval'] %>, sec}},
{max_age, {<%= @node['private_chef']['rabbitmq']['rabbit_mgmt_http_max_age'] %>, sec}},
{max_connection_duration, {<%= @node['private_chef']['rabbitmq']['rabbit_mgmt_http_max_connection_duration'] %>, sec}},

{ibrowse_options, [
<%= @node['private_chef']['rabbitmq']['rabbit_mgmt_ibrowse_options'] %>
]}
]}
]},

Expand Down
11 changes: 10 additions & 1 deletion src/oc_erchef/apps/chef_index/src/chef_index.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
delete/4,
add/5,
add_batch/1,
search_provider/0
search_provider/0,
ping/0
]).

-include("chef_solr.hrl").
Expand Down Expand Up @@ -187,3 +188,11 @@ send_to_solr(batch, Doc) ->
chef_index_batch:add_item(Doc);
send_to_solr(inline, Doc) ->
chef_index_expand:send_item(Doc).

ping() ->
case queue_mode() of
rabbitmq ->
chef_index_queue:ping(envy:get(chef_index, rabbitmq_vhost, binary));
_ ->
pong
end.
26 changes: 25 additions & 1 deletion src/oc_erchef/apps/chef_index/src/chef_index_queue.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
delete/4,
delete/5,
set/5,
set/6
set/6,
create_management_pool/3,
ping/1
]).

-ifdef(TEST).
Expand All @@ -36,6 +38,8 @@
-type solr_url() :: [byte()] | binary() | undefined.
-type vhost() :: binary().

-define(POOLNAME, rabbitmq_index_management_service).

%%%%
%% Public API
%%%%
Expand Down Expand Up @@ -103,10 +107,29 @@ package_for_delete(Type, ID, DatabaseName, SolrUrl) ->
InnerEnvelope = inner_envelope(Type, ID, DatabaseName, {[]}, SolrUrl),
{[{action, delete}, {payload, InnerEnvelope}]}.


create_management_pool(Username, Password, Config) ->
chef_wm_rabbitmq_management:create_pool(?POOLNAME, add_basic_auth(Username, Password, Config)).

-spec ping(binary()) -> pong | pang.
ping(VHost) ->
% TODO: chef_wm_rabbitmq_management needs to be moved to a shared app
case chef_wm_rabbitmq_management:check_aliveness(
?POOLNAME, binary_to_list(VHost)) of
true -> pong;
_ -> pang
end.

%%%%
%% Internal
%%%%

add_basic_auth(Username, Password, Config) ->
IbrowseOptions = proplists:get_value(ibrowse_options, Config),
Config1 = proplists:delete(ibrowse_options, Config),
IbrowseOptions1 = [{basic_auth, {Username, erlang:binary_to_list(Password)}} | IbrowseOptions],
[{ibrowse_options, IbrowseOptions1} | Config1].

-spec inner_envelope(chef_indexable_type(), uuid_binary(), chef_db_name(), ejson(), solr_url()) -> ejson().
inner_envelope(Type, ID, DatabaseName, Item, SolrUrl) ->
%% SAMPLE ENVELOPE:
Expand Down Expand Up @@ -157,3 +180,4 @@ object_id_to_i(UUID) ->
unix_time() ->
{MS, S, _US} = os:timestamp(),
(1000000 * MS) + S.

16 changes: 15 additions & 1 deletion src/oc_erchef/apps/chef_index/src/chef_index_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
%% API
-export([
server_for_vhost/1,
start_link/0
start_link/0,
maybe_rabbitmq_monitoring/0
]).

%% Supervisor callbacks
Expand All @@ -49,9 +50,22 @@ init([]) ->
error_logger:info_msg("Starting chef_index_sup.~n", []),
error_logger:info_msg("Creating HTTP pool for Solr.~n"),
chef_index_http:create_pool(),
maybe_rabbitmq_monitoring(),
Children = child_spec(),
{ok, {{one_for_one, 60, 10}, Children}}.

maybe_rabbitmq_monitoring() ->
case envy:get(chef_index, search_queue_mode, rabbitmq, envy:one_of([rabbitmq, batch, inline])) of
rabbitmq ->
Config = envy:get(chef_index, rabbitmq_index_management_service, [], any),
Username = proplists:get_value(user, Config),
Config1 = proplists:delete(user, Config),
{ok, Password} = chef_secrets:get(<<"rabbitmq">>, <<"management_password">>),
chef_index_queue:create_management_pool(Username, Password, Config1);
_ ->
ok
end.

%% Return a spec for a bunnyc gen_server or the chef_index_batch gen_server based on the
%% search_queue_mode configuration.
%%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,6 @@ check_aliveness(PoolNameAtom, Vhost) ->
{ok, "200", _, _} ->
true;
Resp ->
lager:error("Error getting Rabbitmq aliveness: ~p", Resp),
lager:error("Error getting Rabbitmq aliveness: ~p", [Resp]),
false
end.

0 comments on commit d5a9fc4

Please sign in to comment.