feat(emqx-mgmt): manage mria tables explicitly during startup
This commit is contained in:
parent
21780e2126
commit
b6d77c164e
|
@ -93,6 +93,7 @@ default_auth_header() ->
|
||||||
create_default_app() ->
|
create_default_app() ->
|
||||||
Now = erlang:system_time(second),
|
Now = erlang:system_time(second),
|
||||||
ExpiredAt = Now + timer:minutes(10),
|
ExpiredAt = Now + timer:minutes(10),
|
||||||
|
case
|
||||||
emqx_mgmt_auth:create(
|
emqx_mgmt_auth:create(
|
||||||
?DEFAULT_APP_ID,
|
?DEFAULT_APP_ID,
|
||||||
?DEFAULT_APP_KEY,
|
?DEFAULT_APP_KEY,
|
||||||
|
@ -101,7 +102,13 @@ create_default_app() ->
|
||||||
ExpiredAt,
|
ExpiredAt,
|
||||||
<<"default app key for test">>,
|
<<"default app key for test">>,
|
||||||
?ROLE_API_SUPERUSER
|
?ROLE_API_SUPERUSER
|
||||||
).
|
)
|
||||||
|
of
|
||||||
|
{ok, App} ->
|
||||||
|
{ok, App};
|
||||||
|
{error, name_already_existed} ->
|
||||||
|
{ok, _} = emqx_mgmt_auth:read(?DEFAULT_APP_ID)
|
||||||
|
end.
|
||||||
|
|
||||||
delete_default_app() ->
|
delete_default_app() ->
|
||||||
emqx_mgmt_auth:delete(?DEFAULT_APP_ID).
|
emqx_mgmt_auth:delete(?DEFAULT_APP_ID).
|
||||||
|
|
|
@ -22,12 +22,9 @@
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
-include_lib("stdlib/include/ms_transform.hrl").
|
-include_lib("stdlib/include/ms_transform.hrl").
|
||||||
|
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
|
||||||
|
|
||||||
-behaviour(emqx_db_backup).
|
-behaviour(emqx_db_backup).
|
||||||
|
|
||||||
%% Mnesia bootstrap
|
-export([create_tables/0]).
|
||||||
-export([mnesia/1]).
|
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
add_user/4,
|
add_user/4,
|
||||||
|
@ -70,7 +67,7 @@
|
||||||
%% Mnesia bootstrap
|
%% Mnesia bootstrap
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
mnesia(boot) ->
|
create_tables() ->
|
||||||
ok = mria:create_table(?ADMIN, [
|
ok = mria:create_table(?ADMIN, [
|
||||||
{type, set},
|
{type, set},
|
||||||
{rlog_shard, ?DASHBOARD_SHARD},
|
{rlog_shard, ?DASHBOARD_SHARD},
|
||||||
|
@ -83,7 +80,8 @@ mnesia(boot) ->
|
||||||
{write_concurrency, true}
|
{write_concurrency, true}
|
||||||
]}
|
]}
|
||||||
]}
|
]}
|
||||||
]).
|
]),
|
||||||
|
[?ADMIN].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Data backup
|
%% Data backup
|
||||||
|
|
|
@ -26,7 +26,12 @@
|
||||||
-include("emqx_dashboard.hrl").
|
-include("emqx_dashboard.hrl").
|
||||||
|
|
||||||
start(_StartType, _StartArgs) ->
|
start(_StartType, _StartArgs) ->
|
||||||
ok = mria_rlog:wait_for_shards([?DASHBOARD_SHARD], infinity),
|
Tables = lists:append([
|
||||||
|
emqx_dashboard_admin:create_tables(),
|
||||||
|
emqx_dashboard_token:create_tables(),
|
||||||
|
emqx_dashboard_monitor:create_tables()
|
||||||
|
]),
|
||||||
|
ok = mria:wait_for_tables(Tables),
|
||||||
{ok, Sup} = emqx_dashboard_sup:start_link(),
|
{ok, Sup} = emqx_dashboard_sup:start_link(),
|
||||||
case emqx_dashboard:start_listeners() of
|
case emqx_dashboard:start_listeners() of
|
||||||
ok ->
|
ok ->
|
||||||
|
|
|
@ -22,8 +22,7 @@
|
||||||
|
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
-export([create_tables/0]).
|
||||||
|
|
||||||
-export([start_link/0]).
|
-export([start_link/0]).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
|
@ -35,8 +34,6 @@
|
||||||
code_change/3
|
code_change/3
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([mnesia/1]).
|
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
samplers/0,
|
samplers/0,
|
||||||
samplers/2,
|
samplers/2,
|
||||||
|
@ -67,14 +64,15 @@
|
||||||
data :: map()
|
data :: map()
|
||||||
}).
|
}).
|
||||||
|
|
||||||
mnesia(boot) ->
|
create_tables() ->
|
||||||
ok = mria:create_table(?TAB, [
|
ok = mria:create_table(?TAB, [
|
||||||
{type, set},
|
{type, set},
|
||||||
{local_content, true},
|
{local_content, true},
|
||||||
{storage, disc_copies},
|
{storage, disc_copies},
|
||||||
{record_name, emqx_monit},
|
{record_name, emqx_monit},
|
||||||
{attributes, record_info(fields, emqx_monit)}
|
{attributes, record_info(fields, emqx_monit)}
|
||||||
]).
|
]),
|
||||||
|
[?TAB].
|
||||||
|
|
||||||
%% -------------------------------------------------------------------------------------------------
|
%% -------------------------------------------------------------------------------------------------
|
||||||
%% API
|
%% API
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
|
|
||||||
-include("emqx_dashboard.hrl").
|
-include("emqx_dashboard.hrl").
|
||||||
|
|
||||||
|
-export([create_tables/0]).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
sign/2,
|
sign/2,
|
||||||
verify/2,
|
verify/2,
|
||||||
|
@ -27,10 +29,6 @@
|
||||||
destroy_by_username/1
|
destroy_by_username/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
|
||||||
|
|
||||||
-export([mnesia/1]).
|
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
-export([lookup_by_username/1, clean_expired_jwt/1]).
|
-export([lookup_by_username/1, clean_expired_jwt/1]).
|
||||||
-endif.
|
-endif.
|
||||||
|
@ -87,7 +85,7 @@ salt() ->
|
||||||
<<X:16/big-unsigned-integer>> = crypto:strong_rand_bytes(2),
|
<<X:16/big-unsigned-integer>> = crypto:strong_rand_bytes(2),
|
||||||
iolist_to_binary(io_lib:format("~4.16.0b", [X])).
|
iolist_to_binary(io_lib:format("~4.16.0b", [X])).
|
||||||
|
|
||||||
mnesia(boot) ->
|
create_tables() ->
|
||||||
ok = mria:create_table(?TAB, [
|
ok = mria:create_table(?TAB, [
|
||||||
{type, set},
|
{type, set},
|
||||||
{rlog_shard, ?DASHBOARD_SHARD},
|
{rlog_shard, ?DASHBOARD_SHARD},
|
||||||
|
@ -100,7 +98,8 @@ mnesia(boot) ->
|
||||||
{write_concurrency, true}
|
{write_concurrency, true}
|
||||||
]}
|
]}
|
||||||
]}
|
]}
|
||||||
]).
|
]),
|
||||||
|
[?TAB].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% jwt apply
|
%% jwt apply
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
-include("emqx_mgmt.hrl").
|
-include("emqx_mgmt.hrl").
|
||||||
|
|
||||||
start(_Type, _Args) ->
|
start(_Type, _Args) ->
|
||||||
|
ok = mria:wait_for_tables(emqx_mgmt_auth:create_tables()),
|
||||||
case emqx_mgmt_auth:init_bootstrap_file() of
|
case emqx_mgmt_auth:init_bootstrap_file() of
|
||||||
ok ->
|
ok ->
|
||||||
emqx_conf:add_handler([api_key], emqx_mgmt_auth),
|
emqx_conf:add_handler([api_key], emqx_mgmt_auth),
|
||||||
|
|
|
@ -22,8 +22,8 @@
|
||||||
-behaviour(emqx_db_backup).
|
-behaviour(emqx_db_backup).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([mnesia/1]).
|
-export([create_tables/0]).
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
|
||||||
-behaviour(emqx_config_handler).
|
-behaviour(emqx_config_handler).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
|
@ -70,7 +70,7 @@
|
||||||
|
|
||||||
-define(DEFAULT_HASH_LEN, 16).
|
-define(DEFAULT_HASH_LEN, 16).
|
||||||
|
|
||||||
mnesia(boot) ->
|
create_tables() ->
|
||||||
Fields = record_info(fields, ?APP),
|
Fields = record_info(fields, ?APP),
|
||||||
ok = mria:create_table(?APP, [
|
ok = mria:create_table(?APP, [
|
||||||
{type, set},
|
{type, set},
|
||||||
|
@ -78,7 +78,8 @@ mnesia(boot) ->
|
||||||
{storage, disc_copies},
|
{storage, disc_copies},
|
||||||
{record_name, ?APP},
|
{record_name, ?APP},
|
||||||
{attributes, Fields}
|
{attributes, Fields}
|
||||||
]).
|
]),
|
||||||
|
[?APP].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Data backup
|
%% Data backup
|
||||||
|
|
|
@ -30,8 +30,9 @@ init_suite(Apps, SetConfigs) when is_function(SetConfigs) ->
|
||||||
init_suite(Apps, SetConfigs, #{}).
|
init_suite(Apps, SetConfigs, #{}).
|
||||||
|
|
||||||
init_suite(Apps, SetConfigs, Opts) ->
|
init_suite(Apps, SetConfigs, Opts) ->
|
||||||
application:load(emqx_management),
|
emqx_common_test_helpers:start_apps(
|
||||||
emqx_common_test_helpers:start_apps(Apps ++ [emqx_dashboard], SetConfigs, Opts),
|
Apps ++ [emqx_management, emqx_dashboard], SetConfigs, Opts
|
||||||
|
),
|
||||||
_ = emqx_common_test_http:create_default_app(),
|
_ = emqx_common_test_http:create_default_app(),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
@ -40,8 +41,7 @@ end_suite() ->
|
||||||
|
|
||||||
end_suite(Apps) ->
|
end_suite(Apps) ->
|
||||||
emqx_common_test_http:delete_default_app(),
|
emqx_common_test_http:delete_default_app(),
|
||||||
emqx_common_test_helpers:stop_apps(Apps ++ [emqx_dashboard]),
|
emqx_common_test_helpers:stop_apps(Apps ++ [emqx_management, emqx_dashboard]),
|
||||||
application:unload(emqx_management),
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
set_special_configs(emqx_dashboard) ->
|
set_special_configs(emqx_dashboard) ->
|
||||||
|
|
Loading…
Reference in New Issue