Skip to content

Commit

Permalink
Merge pull request ChicagoBoss#8 from jgordor/master
Browse files Browse the repository at this point in the history
cb_admin 0.7 compat (rebar)
  • Loading branch information
evanmiller committed Jan 29, 2012
2 parents 4ce2441 + e52f7fc commit 4665d1f
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 52 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ebin/*.beam
ebin/*.app
log/*.log
erl_crash.dump
16 changes: 0 additions & 16 deletions Makefile

This file was deleted.

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
chicagoboss_admin
cb_admin
=================

This is the admin interface for Chicago Boss, which can be configured as a standalone application with CB 0.6 or later.

Installation as a standalone server
-----------------------------------

This isn't very useful, but it's easy. First make sure ChicagoBoss is built in the parent directory; otherwise modify start.sh and start-dev.sh to point to the ChicagoBoss ebin/ directory on your machine.
Check your boss.config and make sure that the path's are correct, defaults to ../ChicagoBoss (framework) and ../cb_admin (the checkout dir).

Then:

mkdir log
make
./start.sh
./rebar compile
./init.sh start

Then visit http://localhost:8001/

Installation with an existing CB server
---------------------------------------

First "make" and add the ebin/ directory to the path of your CB startup scripts (start.sh and start-dev.sh).
First "./rebar compile" and add the app config section to the boss.config of your existing CB server.

Add something like this to your boss.config:

Expand All @@ -28,6 +27,7 @@ Add something like this to your boss.config:
...
]},
{cb_admin, [
{path, "../cb_admin"},
{allow_ip_blocks, ["127.0.0.1"]},
{base_url, "/admin"}
]}].
Expand Down
3 changes: 3 additions & 0 deletions boss.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[{boss, [
{path, "../ChicagoBoss"},
{vm_cookie, "abc123"},
{applications, [cb_admin]},
{db_host, "localhost"},
{db_port, 1978},
Expand All @@ -11,6 +13,7 @@
{session_exp_time, 525600}
]},
{cb_admin, [
{path, "../cb_admin"},
{allow_ip_blocks, ["127.0.0.1"]},
{base_url, "/admin"}
]}].
8 changes: 8 additions & 0 deletions init-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh
#
# Chicago Boss Dev Init System
# easy start dev server (most common task)

cd `dirname $0`

./init.sh start-dev
55 changes: 55 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env sh
#
# Chicago Boss Init System
#
# @author: Jose Luis Gordo Romero <jgordor@gmail.com>
#
# -------------------------------------------------------------------
# The shell commands are automatically generated by the boss rebar
# plugin/driver, all configuration params and paths are in boss.config
# -------------------------------------------------------------------

cd `dirname $0`

case "${1:-''}" in
'start')
# Start Boss in production mode
echo "starting boss in production mode..."
START=$(./rebar boss c=start_cmd|grep -v "==>")
$START
;;

'start-dev')
# Start Boss in development mode
START_DEV=$(./rebar boss c=start_dev_cmd|grep -v "==>")
$START_DEV
;;

'stop')
# Stop Boss daemon
echo "stopping boss..."
STOP=$(./rebar boss c=stop_cmd|grep -v "==>")
# After hours of shell quoting problems with the erl command,
# eval with the command quoted works!!!
eval "$STOP"
;;

'reload')
# Boss hot code reload <-- only the actual node, not the entire cluster
echo "Hot code reload, (WARN: Only this node)"
RELOAD=$(./rebar boss c=reload_cmd|grep -v "==>")
eval "$RELOAD"
;;

'restart')
# Boss complete restart
echo "Restarting (stop-start) boss..."
$0 stop
$0 start
;;
*)
echo "Chicago Boss Boot System"
echo "Usage: $SELF start|start-dev|stop|reload|restart"
exit 1
;;
esac
Empty file added log/.gitignore
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
-module(news).
-module(cb_admin_01_news).

-export([init/0]).
-export([init/0, stop/1]).

% This script is first executed at server startup and should
% return a list of WatchIDs that should be cancelled in the stop
% function below (stop is executed if the script is ever reloaded).
init() ->
ok.
{ok, []}.

stop(ListOfWatchIDs) ->
lists:map(fun boss_news:cancel_watch/1, ListOfWatchIDs).

%%%%%%%%%%% Ideas
% boss_news:watch_set("greetings",
% fun(created, NewGreeting) ->
Expand Down
155 changes: 155 additions & 0 deletions priv/rebar/boss_plugin.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
%%%-------------------------------------------------------------------
%%% @author Jose Luis Gordo Romero <jgordor@gmail.com>
%%% @doc Chicago Boss rebar plugin
%%% Manage compilation/configuration/scripts stuff the rebar way
%%% @end
%%%-------------------------------------------------------------------
-module(boss_plugin).

-export([boss/2,
pre_compile/2,
pre_eunit/2]).

-define(BOSS_CONFIG, "boss.config").

%% ====================================================================
%% Public API
%% ====================================================================

%%--------------------------------------------------------------------
%% @doc boss command
%% @spec boss(_Config, _AppFile) -> ok | {error, Reason}
%% Boss enabled rebar commands, usage:
%% ./rebar boss c=command
%% @end
%%--------------------------------------------------------------------
boss(RebarConf, AppFile) ->
{ok, BossConf} = init(RebarConf, AppFile),
case is_base_dir() of
true ->
Command = rebar_config:get_global(c, "help"),
case boss_rebar:run(Command, RebarConf, BossConf, AppFile) of
{error, command_not_found} ->
io:format("ERROR: boss command not found.~n"),
boss_rebar:help(),
halt(1);
{error, Reason} ->
io:format("ERROR: executing ~s task: ~s~n", [Command, Reason]),
halt(1);
ok -> ok
end;
false -> ok
end.

%%--------------------------------------------------------------------
%% @doc initializes the rebar boss connector plugin
%% @spec init(Config, AppFile) -> {ok, BossConf} | {error, Reason}
%% Set's the ebin cb_apps and loads the connector
%% @end
%%--------------------------------------------------------------------
init(_RebarConf, AppFile) ->
%% Compile and load the boss_rebar code, this can't be compiled
%% as a normal boss lib without the rebar source dep
%% The load of ./rebar boss:
%% - Rebar itself searchs in rebar.config for {plugin_dir, ["priv/rebar"]}.
%% - Rebar itself compile this plugin and adds it to the execution chain
%% - This plugin compiles and loads the boss_rebar code in ["cb/priv/rebar"],
%% so we can extend/bugfix/tweak the framework without the need of manually
%% recopy code to user apps
BossPath = case boss_config_value(boss, path) of
{error, _} ->
io:format("FATAL: Failed to read boss=>path config in boss.config.~n"),
halt(1);
Val -> Val
end,
RebarErls = rebar_utils:find_files(filename:join([BossPath, "priv", "rebar"]), ".*\\.erl\$"),

rebar_log:log(debug, "Auto-loading boss rebar modules ~p~n", [RebarErls]),

lists:map(fun(F) ->
case compile:file(F, [binary]) of
error ->
io:format("FATAL: Failed compilation of ~s module~n", [F]),
halt(1);
{ok, M, Bin} ->
{module, _} = code:load_binary(M, F, Bin),
rebar_log:log(debug, "Loaded ~s~n", [M])
end
end, RebarErls),

BossConf = boss_config(),

%% add all cb_apps defined in boss.config to code path
%% including the deps ebin dirs
[code:add_path(CodePath) || CodePath <- boss_rebar:all_ebin_dirs(BossConf, AppFile)],

{ok, BossConf}.

%%--------------------------------------------------------------------
%% @doc pre_compile hook
%% @spec pre_compile(_Config, AppFile) -> ok | {error, Reason}
%% Pre compile hook, compile the boss way
%% Compatibility hook, the normal ./rebar compile command works,
%% but only calls the ./rebar boss c=compile and halts (default
%% rebar task never hits)
%% @end
%%--------------------------------------------------------------------
pre_compile(RebarConf, AppFile) ->
{ok, BossConf} = init(RebarConf, AppFile),
case is_base_dir() of
true ->
boss_rebar:run(compile, RebarConf, BossConf, AppFile),
halt(0);
false -> ok
end.

%%--------------------------------------------------------------------
%% @doc pre_eunit hook
%% @spec pre_eunit(RebarConf, AppFile) -> ok | {error, Reason}
%% Pre eunit hook, .eunit compilation the boss way
%% Compatibility hook, the normal ./rebar eunit command works,
%% but only calls the ./rebar boss c=test_eunit and halts
%% (default rebar task never hits)
%% @end
%%--------------------------------------------------------------------
pre_eunit(RebarConf, AppFile) ->
{ok, BossConf} = init(RebarConf, AppFile),
case is_base_dir() of
true ->
boss_rebar:run(test_eunit, RebarConf, BossConf, AppFile),
halt(0);
false -> ok
end.

%% ===================================================================
%% Internal functions
%% ===================================================================

%% Checks if the current dir (rebar execution) is the base_dir
%% Used to prevent run boss tasks in deps directory
is_base_dir() ->
rebar_utils:get_cwd() == rebar_config:get_global(base_dir, undefined).

%% Gets the boss.config central configuration file
boss_config() ->
{ok, BossConfig} = file:consult(?BOSS_CONFIG),
hd(BossConfig).

%%--------------------------------------------------------------------
%% @doc Get Boss config value app, key
%% @spec boss_config_value(App, Key) -> Value | {error, Reason}
%% Searchs in boss config for a given App and Key
%% @end
%%--------------------------------------------------------------------
boss_config_value(App, Key) ->
case lists:keyfind(App, 1, boss_config()) of
false ->
{error, boss_config_app_not_found};
{App, AppConfig} ->
case lists:keyfind(Key, 1, AppConfig) of
false ->
{error, boss_config_app_setting_not_found};
{Key, KeyConfig} ->
KeyConfig
end
end.
Binary file added rebar
Binary file not shown.
3 changes: 3 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{plugin_dir, ["priv/rebar"]}.
{plugins, [boss_plugin]}.
{eunit_compile_opts, [{src_dirs, ["src/test"]}]}.
2 changes: 1 addition & 1 deletion cb_admin.app.src → src/cb_admin.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, cb_admin, [
{description, "Chicago Boss Admin Interface"},
{vsn, "0.6.8"},
{vsn, "0.7.0"},
{modules, []},
{registered, []},
{applications, [kernel, stdlib, crypto, boss]},
Expand Down
Empty file added src/test/eunit/.gitignore
Empty file.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-module(admin_test).
-module(cb_admin_test).
-compile(export_all).

start() ->
boss_web_test:get_request("/hello/world", [],
boss_web_test:get_request("/admin", [],
[ fun boss_assert:http_ok/1,
fun(Res) -> boss_assert:link_with_text("\"An Evening With Chicago Boss\"", Res) end,
fun(Res) -> boss_assert:link_with_text("chicagoboss.org", Res) end,
Expand Down
8 changes: 0 additions & 8 deletions start-dev.sh

This file was deleted.

2 changes: 0 additions & 2 deletions start-server.bat

This file was deleted.

14 changes: 0 additions & 14 deletions start.sh

This file was deleted.

0 comments on commit 4665d1f

Please sign in to comment.