Browse Source

--allow-empty-message

Maxim Sokhatsky 11 years ago
commit
bdff5907d4

+ 21 - 0
Makefile

@@ -0,0 +1,21 @@
+RELEASE := kakaranet
+COOKIE  := sample
+APPS    := kernel stdlib sasl sync gproc cowboy mimetypes ranch erlydtl n2o face
+VER     := 1.0.0
+VM      := rels/web/files/vm.args
+SYS     := rels/web/files/sys.config
+PLT_NAME := ~/.n2o_dialyzer.plt
+ERL_ARGS := -args_file $(VM) -config $(SYS)
+RUN_DIR ?= rels/web/devbox
+LOG_DIR ?= rels/web/devbox/logs
+N2O     := deps/n2o/priv/static
+APP     := apps/face/priv/static/nitrogen
+
+default: get-deps compile static-link
+static-link:
+	rm -rf $(N2O)
+	rm -rf $(APP)
+	ln -s ../../n2o_scripts $(N2O)
+	ln -s ../../../../deps/n2o/priv/static/n2o $(APP)
+
+include otp.mk

+ 64 - 0
README.md

@@ -0,0 +1,64 @@
+N2O: Erlang Web Framework on WebSockets
+=======================================
+
+Samples
+-------
+
+Samples provided as Erlang release packaged
+with *web* Erlang application which contains modules:
+
+* REST samples
+* N2O samples
+* XEN support
+
+Run
+---
+
+To run just perform on Windows, Linux, BSD and Mac
+
+    $ make && make console
+
+And open it in browser [http://localhost:8000](http://localhost:8000)
+If you want to try pure Single Page Application (SPA) wich
+connects to Erlang N2O Application Server you should use
+[http://localhost:8000/static/spa/spa.htm](http://localhost:8000/static/spa/spa.htm)
+
+For full features of make please refer to [https://github.com/synrc/otp.mk](https://github.com/synrc/otp.mk)
+
+Xen
+---
+
+To run on Xen is a bit tricky:
+
+    $ sudo apt-get install xen-hypervisor-amd64
+    $ echo XENTOOLSTACK=xl > /etc/default/xen
+
+Boot into Xen 4.2 Domain-0 and create network bridge:
+
+    $ sudo brctl addbr docker0
+    $ sudo ip addr add 172.16.42.1/24 dev docker0
+
+Compile Image at Erlang on Xen builder:
+
+    $ rebar get-deps compile
+    $ ./nitrogen_static.sh
+    $ rebar ling-build-image
+    $ sudo xl create -c xen.config
+
+Inside Ling start n2o_sample application:
+
+    Ling 0.2.2 is here
+    Started in 49438 us
+    Erlang [ling-0.2.2]
+
+    Eshell V5.10.2  (abort with ^G)
+    1> application:start(n2o_sample).
+
+And open it in browser [http://172.16.42.108:8000](http://172.16.42.108:8000)
+
+Credits
+-------
+
+* Maxim Sokhatsky
+
+OM A HUM

+ 4 - 0
apps/face/README.md

@@ -0,0 +1,4 @@
+Kakaranet Front Application
+===========================
+
+Web App Skeleton for Voxoz PaaS

+ 13 - 0
apps/face/include/users.hrl

@@ -0,0 +1,13 @@
+-record(user, {id, name, email, proplist = [{facebook, udefined},
+                                            {github, "github.com/b0oh"},
+                                            {local, undefined},
+                                            {twitter, udefined}],
+               string = "common",
+               number = 12,
+               list_of_strings = ["one", "two", "three"],
+               list_of_numbers = [34958726345, 12],
+               nested_proplists = [{nested, [{number, 12},
+                                             {string, "common"},
+                                             {list_of_strings, ["one", "two", "three"]},
+                                             {list_of_atoms, [one, two, three]},
+                                             {list_of_numbers, [100000, 2,3 ]}]}]}).

+ 0 - 0
apps/face/priv/static/README.md


+ 56 - 0
apps/face/priv/static/react.htm

@@ -0,0 +1,56 @@
+<html>
+  <head>
+    <title>Hello React</title>
+    <script src="http://fb.me/react-0.5.0.js"></script>
+    <script src="http://fb.me/JSXTransformer-0.5.0.js"></script>
+  </head>
+  <body>
+    <div id="content"></div>
+    <script type="text/jsx">
+
+/** @jsx React.DOM */
+
+var User = React.createClass({
+    render: function() {
+        console.log(this.props);
+        props = JSON.stringify(this.props.tokens);
+        nested = JSON.stringify(this.props.nested);
+        return (
+            <div><h2>{this.props.id}</h2>
+            Email: {this.props.email},<br/> Tokens: {props},<br/> Nested: {nested}</div> ); } });
+
+var CommentList = React.createClass({
+    render: function() {
+        var users = this.props.data.map(function (item) {
+            return <User id={item.id} email={item.email} tokens={item.proplist} nested={item.nested_proplists}/>; });
+        return (<div class="commentList">{users}</div>); }
+});
+
+var CommentBox = React.createClass({
+    getInitialState: function() { return {data: []}; },
+    loadData: function() {
+        var p = this;
+        var request = new XMLHttpRequest;
+        request.open('GET', this.props.url, true);
+        request.onload = function() {
+            if (request.status >= 200 && request.status < 400) {
+                data = JSON.parse(request.responseText);
+                request = new XMLHttpRequest;
+                p.setState({data: data.users});
+            } else {
+                console.log("Request: " + request);
+            }
+        };
+        request.send();
+    },
+    componentWillMount: function() { this.loadData(); },
+    render: function() {
+        return (<div><h1>Users</h1><CommentList data={this.state.data}/></div> ); }});
+
+React.renderComponent(
+    <CommentBox url="http://localhost:8000/rest/users" pollInterval={5000}/>,
+    document.getElementById('content'));
+
+    </script>
+  </body>
+</html>

+ 19 - 0
apps/face/priv/static/spa/index.htm

@@ -0,0 +1,19 @@
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>N2O</title>
+</head>
+<body>
+<span>'/index?x=' is undefined</span>
+<div id="history"></div>
+<input id="message" type="text">
+<button id="send" type="button">Chat</button>
+<script>var transition = {pid: '', port:'8000'}</script>
+<script src='/static/nitrogen/jquery.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bullet.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/n2o.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bert.js' type='text/javascript' charset='utf-8'></script>
+</script><div id="n2ostatus"></div>
+</body>
+</html>
+

+ 20 - 0
apps/face/priv/static/spa/spa.htm

@@ -0,0 +1,20 @@
+<html>
+<head>
+<title>Login</title>
+</head>
+<body>
+<span id="display"></span><br>
+<span>Login: </span>
+<input id="user" type="text"><br>
+<span>Password: </span>
+<input id="pass" type="password" placeholder="password">
+<button id="login" type="button">Login</button>
+
+<script>var transition = {pid: '', port:'8000'}</script>
+<script src='/static/nitrogen/jquery.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bullet.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/n2o.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bert.js' type='text/javascript' charset='utf-8'></script>
+</script><div id="n2ostatus"></div>
+</body>
+</html>

+ 14 - 0
apps/face/priv/templates/hello.html

@@ -0,0 +1,14 @@
+<html>
+<head>
+<title>{{title}}</title>
+</head>
+<body>
+{{hello}}
+<script>{{script}}</script>
+<script src='/static/nitrogen/jquery.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bullet.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/n2o.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bert.js' type='text/javascript' charset='utf-8'></script>
+<div id="n2ostatus"></div>
+</body>
+</html>

+ 15 - 0
apps/face/priv/templates/index.html

@@ -0,0 +1,15 @@
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>{{title}}</title>
+</head>
+<body>
+{{body}}
+<script>{{script}}</script>
+<script src='/static/nitrogen/jquery.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bullet.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/n2o.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bert.js' type='text/javascript' charset='utf-8'></script>
+</script><div id="n2ostatus"></div>
+</body>
+</html>

+ 14 - 0
apps/face/priv/templates/login.html

@@ -0,0 +1,14 @@
+<html>
+<head>
+<title>{{title}}</title>
+</head>
+<body>
+{{body}}
+<script>{{script}}</script>
+<script src='/static/nitrogen/jquery.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bullet.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/n2o.js' type='text/javascript' charset='utf-8'></script>
+<script src='/static/nitrogen/bert.js' type='text/javascript' charset='utf-8'></script>
+</script><div id="n2ostatus"></div>
+</body>
+</html>

+ 11 - 0
apps/face/rebar.config

@@ -0,0 +1,11 @@
+{deps, [
+    {erlydtl,".*",{git,"git://github.com/evanmiller/erlydtl.git",{tag,"0.8.0"}}},
+    {n2o,           ".*", {git, "git://github.com/5HT/n2o",                "HEAD"}}
+]}.
+{erlydtl_opts, [
+    {doc_root,   "priv/templates"},
+    {out_dir,    "ebin"},
+   {compiler_options, [report, return, debug_info]},
+    {source_ext, ".html"},
+    {module_ext, "_view"}
+]}.

+ 9 - 0
apps/face/src/face.app.src

@@ -0,0 +1,9 @@
+{application, face,
+ [
+  {description, "Kakaranet Lite"},
+  {vsn, "1"},
+  {registered, []},
+  {applications, [kernel, stdlib]},
+  {mod, { web_app, []}},
+  {env, []}
+ ]}.

+ 56 - 0
apps/face/src/index.erl

@@ -0,0 +1,56 @@
+-module(index).
+-compile(export_all).
+-include_lib("n2o/include/wf.hrl").
+
+main() -> 
+    case wf:user() of
+         undefined -> wf:redirect("/login"), #dtl{file="index",app=n2o_sample,bindings=[{title,""},{body,""}]};
+         _ -> #dtl{file = "index", app=n2o_sample,bindings=[{title,title()},{body,body()}]}
+     end.
+
+title() -> [ <<"N2O">> ].
+
+body() ->
+    {ok,Pid} = wf:comet(fun() -> chat_loop() end), 
+    [ #span{ body = io_lib:format("'/index?x=' is ~p",[wf:qs(<<"x">>)]) },
+      #panel{ id=history },
+      #textbox{ id=message },
+      #button{ id=send, body= <<"Chat">>, postback={chat,Pid}, source=[message] } ].
+
+event(init) ->
+    User = wf:user(),
+    wf:reg(room),
+    X = wf:qs(<<"x">>),
+    wf:insert_bottom(history, [ #span{id=text, body = io_lib:format("User ~s logged in. X = ~p", [User,X]) },
+                                #button{id=logout, body="Logout", postback=logout}, #br{} ]);
+
+event({chat,Pid}) ->
+    error_logger:info_msg("Chat Pid: ~p",[Pid]),
+    Username = wf:user(),
+    Message = wf:q(message),
+    wf:wire(#jq{target=message,method=[focus,select]}),
+    wf:update(text,[#panel{body= <<"Text">>},#panel{body= <<"OK">>}]),
+    Pid ! {message, Username, Message};
+
+event(logout) -> 
+    wf:logout(),
+    <<"/ws/",X,_/binary>> = wf:path(?REQ),
+    case X of
+        $i -> wf:redirect("/login");
+        $l -> wf:redirect("/login");
+         _ -> wf:redirect("/static/spa/spa.htm") end;
+
+event(login) -> login:event(login);
+event(continue) -> wf:info("OK Pressed");
+event(Event) -> wf:info("Event: ~p", [Event]).
+
+chat_loop() ->
+    receive 
+        {message, Username, Message} ->
+            Terms = [ #span { body=Username }, [": "], #span { body=Message }, #br{} ],
+            wf:insert_bottom(history, Terms),
+            wf:wire("$('#chatHistory').scrollTop = $('#chatHistory').scrollHeight;"),
+            wf:flush(room);
+        Unknown -> error_logger:info_msg("Unknown Looper Message ~p",[Unknown])
+    end,
+    chat_loop().

+ 25 - 0
apps/face/src/login.erl

@@ -0,0 +1,25 @@
+-module(login).
+-compile(export_all).
+-include_lib("n2o/include/wf.hrl").
+
+title() -> [ <<"Login">> ].
+main() -> #dtl{file = "login", app=n2o_sample,bindings=[{title,title()},{body,body()}]}.
+
+body() ->
+ [ #span{id=display}, #br{},
+            #span{body="Login: "}, #textbox{id=user}, #br{},
+            #span{body="Password: "}, #password{id=pass},
+            #button{id=login,body="Login",postback=login,source=[user,pass]} ].
+%    react:x().
+
+event(init) -> [];
+event(login) ->
+    User = wf:q(user),
+    wf:update(display,User),
+    wf:user(User),
+    <<"/ws/",X/binary>> = wf:path(?REQ),
+    case X of
+        <<>> -> wf:redirect("/index");
+        <<"login">> -> wf:redirect("/index");
+         _ -> wf:redirect("/static/spa/index.htm") end;
+event(_) -> [].

+ 116 - 0
apps/face/src/n2o_game.erl

@@ -0,0 +1,116 @@
+-module(n2o_game).
+-author('Maxim Sokhatsky').
+-include_lib("n2o/include/wf.hrl").
+-export([init/4]).
+-export([stream/3]).
+-export([info/3]).
+-export([terminate/2]).
+
+-define(PERIOD, 1000).
+
+init(_Transport, Req, _Opts, _Active) ->
+    put(actions,[]),
+    Ctx = wf_context:init_context(Req),
+    NewCtx = wf_core:fold(init,Ctx#context.handlers,Ctx),
+    wf_context:context(NewCtx),
+    Res = ets:update_counter(globals,onlineusers,{2,1}),
+    wf:reg(broadcast,wf:peer(Req)),
+    wf:send(broadcast,{counter,Res}),
+    Req1 = wf:header(<<"Access-Control-Allow-Origin">>, <<"*">>, NewCtx#context.req),
+    {ok, Req1, NewCtx}.
+
+stream(<<"ping">>, Req, State) ->
+    wf:info("ping received~n"),
+    {reply, <<"pong">>, Req, State};
+stream({text,Data}, Req, State) ->
+    %wf:info("Text Received ~p",[Data]),
+    self() ! Data,
+    {ok, Req,State};
+stream({binary,Info}, Req, State) ->
+    % wf:info("Binary Received: ~p",[Info]),
+    Pro = binary_to_term(Info,[safe]),
+    wf:info("N2O Unknown Event: ~p",[Pro]),
+    Pickled = proplists:get_value(pickle,Pro),
+    Linked = proplists:get_value(linked,Pro),
+    Depickled = wf:depickle(Pickled),
+    % wf:info("Depickled: ~p",[Depickled]),
+    case Depickled of
+        #ev{module=Module,name=Function,payload=Parameter,trigger=Trigger} ->
+            case Function of 
+                control_event   -> lists:map(fun({K,V})-> put(K,V) end,Linked),
+                                   Module:Function(Trigger, Parameter);
+                api_event       -> Module:Function(Parameter,Linked,State);
+                event           -> lists:map(fun({K,V})-> put(K,V) end,Linked),
+                                   Module:Function(Parameter);
+                UserCustomEvent -> Module:Function(Parameter,Trigger,State) end;
+          _Ev -> wf:error("N2O allows only #ev{} events") end,
+
+    Actions = get(actions),
+    wf_context:clear_actions(),
+    Render = wf:render(Actions),
+
+    GenActions = get(actions),
+    RenderGenActions = wf:render(GenActions),
+    wf_context:clear_actions(),
+
+    {reply, [Render,RenderGenActions], Req, State};
+stream(Data, Req, State) ->
+    wf:info("Data Received ~p",[Data]),
+    self() ! Data,
+    {ok, Req,State}.
+
+render_actions(InitActions) ->
+    RenderInit = wf:render(InitActions),
+    InitGenActions = get(actions),
+    RenderInitGenActions = wf:render(InitGenActions),
+    wf_context:clear_actions(),
+    [RenderInit,RenderInitGenActions].
+
+info(Pro, Req, State) ->
+    Render = case Pro of
+        {flush,Actions} ->
+            % wf:info("Comet Actions: ~p",[Actions]),
+            wf:render(Actions);
+        <<"N2O,",Rest/binary>> ->
+            Module = State#context.module, Module:event(init),
+            InitActions = get(actions),
+            wf_context:clear_actions(),
+            Pid = wf:depickle(Rest),
+            %wf:info("Transition Pid: ~p",[Pid]),
+            case Pid of
+                undefined -> 
+                    %wf:info("Path: ~p",[wf:path(Req)]),
+                    %wf:info("Module: ~p",[Module]),
+                    Elements = try Module:main() catch C:E -> wf:error_page(C,E) end,
+                    wf_core:render(Elements),
+                    render_actions(InitActions);
+
+                Transition ->
+                    X = Pid ! {'N2O',self()},
+                    R = receive Actions -> [ render_actions(InitActions) | wf:render(Actions) ]
+                    after 100 ->
+                          QS = element(14, Req),
+                          wf:redirect(case QS of <<>> -> ""; _ -> "" ++ "?" ++ wf:to_list(QS) end),
+                          []
+                    end,
+                    R
+                    end;
+        <<"PING">> -> [];
+        Unknown ->
+            M = State#context.module,
+            catch M:event(Unknown),
+            Actions = get(actions),
+            wf_context:clear_actions(),
+            wf:render(Actions) end,
+    GenActions = get(actions),
+    wf_context:clear_actions(),
+    RenderGenActions = wf:render(GenActions),
+    wf_context:clear_actions(),
+    {reply, [Render,RenderGenActions], Req, State}.
+
+terminate(_Req, _State=#context{module=Module}) ->
+    % wf:info("Bullet Terminated~n"),
+    Res = ets:update_counter(globals,onlineusers,{2,-1}),
+    wf:send(broadcast,{counter,Res}),
+    catch Module:event(terminate),
+    ok.

+ 20 - 0
apps/face/src/routes.erl

@@ -0,0 +1,20 @@
+-module (routes).
+-author('Maxim Sokhatsky').
+-include_lib("n2o/include/wf.hrl").
+-export(?ROUTING_API).
+
+finish(State, Ctx) -> {ok, State, Ctx}.
+init(State, Ctx) -> 
+    Path = wf:path(Ctx#context.req),
+    Module = route_prefix(Path),
+    {ok, State, Ctx#context{path=Path,module=Module}}.
+
+route_prefix(<<"/ws/",P/binary>>) -> route(P);
+route_prefix(<<"/",P/binary>>) -> route(P);
+route_prefix(P) -> route(P).
+
+route(<<>>)              -> index;
+route(<<"index">>)       -> index;
+route(<<"login">>)       -> login;
+route(<<"favicon.ico">>) -> static_file;
+route(_) -> index.

+ 15 - 0
apps/face/src/users.erl

@@ -0,0 +1,15 @@
+-module(users).
+-behaviour(rest).
+-compile({parse_transform, rest}).
+-include("users.hrl").
+-export([init/0, populate/1, exists/1, get/0, get/1, post/1, delete/1]).
+-rest_record(user).
+
+init() -> ets:new(users, [public, named_table, {keypos, #user.id}]).
+populate(Users) -> ets:insert(users, Users).
+exists(Id) -> ets:member(users, wf:to_list(Id)).
+get() -> ets:tab2list(users).
+get(Id) -> [User] = ets:lookup(users, wf:to_list(Id)), User. % should return record #user{}
+delete(Id) -> ets:delete(users, wf:to_list(Id)).
+post(#user{} = User) -> ets:insert(users, User);
+post(Data) -> post(from_json(Data, #user{})).

+ 20 - 0
apps/face/src/web_app.erl

@@ -0,0 +1,20 @@
+-module(web_app).
+-behaviour(application).
+-export([start/2, stop/1]).
+
+start(_StartType, _StartArgs) -> 
+    application:start(crypto),
+    application:start(sasl),
+    application:start(ranch),
+    application:start(cowboy),
+    application:start(gproc),
+    application:start(mimetypes),
+    application:start(syntax_tools),
+    application:start(compiler),
+    application:start(erlydtl),
+    application:start(rest),
+    application:start(n2o),
+
+    web_sup:start_link().
+
+stop(_State) -> ok.

+ 35 - 0
apps/face/src/web_sup.erl

@@ -0,0 +1,35 @@
+-module(web_sup).
+-behaviour(supervisor).
+-export([start_link/0, init/1]).
+-compile(export_all).
+-include_lib ("n2o/include/wf.hrl").
+-include("users.hrl").
+-define(APP, face).
+
+start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+-define(USERS, [#user{id="maxim",email="maxim@synrc.com"},
+                #user{id="doxtop",email="doxtop@synrc.com"},
+                #user{id="roman",email="roman@github.com"}]).
+
+init([]) ->
+
+    {ok, _} = cowboy:start_http(http, 100, [{port, 8000}],
+                                           [{env, [{dispatch, dispatch_rules()}]}]),
+
+    users:init(),
+    users:populate(?USERS),
+
+    {ok, {{one_for_one, 5, 10}, []}}.
+
+dispatch_rules() ->
+    cowboy_router:compile(
+        [{'_', [
+            {"/static/[...]", cowboy_static, [{directory, {priv_dir, ?APP, [<<"static">>]}},
+                                                {mimetypes, {fun mimetypes:path_to_mimes/2, default}}]},
+            {"/rest/:resource", rest_cowboy, []},
+            {"/rest/:resource/:id", rest_cowboy, []},
+            {"/ws/[...]", bullet_handler, [{handler, n2o_bullet}]},
+            {"/game/[...]", bullet_handler, [{handler, n2o_game}]},
+            {'_', n2o_cowboy, []}
+    ]}]).

+ 3 - 0
apps/rebar.config

@@ -0,0 +1,3 @@
+{sub_dirs, ["face"]}.
+{lib_dirs, ["../apps"]}.
+{deps_dir, ["../deps"]}.

+ 29 - 0
depman.erl

@@ -0,0 +1,29 @@
+#!/usr/bin/env escript
+
+-module(depman).
+-compile([export_all]).
+-define(ABORT(Str, Args), io:format(Str, Args), throw(abort)).
+
+app_exists(App,Srv) when is_tuple(App) -> app_exists(element(1,App), Srv);
+app_exists(App,Srv) when is_atom(App) -> case reltool_server:get_app(Srv,App) of {ok, _} -> true; _ -> false end.
+
+validate_rel_apps(ReltoolServer, {sys, ReltoolConfig}) ->
+    case lists:keyfind(rel, 1, ReltoolConfig) of
+        false -> ok;
+        {rel, _Name, _Vsn, Apps} ->
+            Missing = lists:sort([App || App <- Apps, app_exists(App, ReltoolServer) == false]),
+            case Missing of [] -> ok; _ -> ?ABORT("Missing Apps: ~p\n", [Missing]) end;
+        Rel -> ?ABORT("Invalid {rel, ...} section in reltool.config: ~p\n", [Rel]) end.
+
+relconfig(Apps) ->
+    LibDirs = [Dir || Dir <- ["apps", "deps"], case file:read_file_info(Dir) of {ok, _} -> true; _ -> false end],
+    {sys, [ {lib_dirs,LibDirs}, {rel,"node","1",Apps}, {boot_rel,"node"}, {app,observer,[{incl_cond,exclude}]} ]}.
+
+main([]) -> ?ABORT("usage: ./depman.erl apps", []);
+main(MainApps) ->
+    Relconfig = relconfig([list_to_atom(A) || A <- MainApps]),
+    {ok, Server} = reltool:start_server([{config, Relconfig}]),
+    validate_rel_apps(Server, Relconfig),
+    {ok, {release, _Node, _Erts, Apps}} = reltool_server:get_rel(Server, "node"),
+    Alist = [element(1, A) || A <- Apps],
+    io:format("~w~n", [Alist]).

+ 42 - 0
otp.mk

@@ -0,0 +1,42 @@
+empty :=
+ROOTS := apps deps
+space := $(empty) $(empty)
+comma := $(empty),$(empty)
+VSN   := $(shell git rev-parse HEAD | cut -c 1-6)
+DATE  := $(shell git show -s --format="%ci" HEAD | sed -e 's/\+/Z/g' -e 's/-/./g' -e 's/ /-/g' -e 's/:/./g')
+ERL_LIBS := $(subst $(space),:,$(ROOTS))
+relx  := "{release,{$(RELEASE),\"$(VER)\"},[$(subst $(space),$(comma),$(APPS))]}.\\n{include_erts,true}.\
+\\n{extended_start_script,true}.\\n{generate_start_script,true}.\\n{sys_config,\"$(SYS)\"}.\
+\\n{vm_args,\"$(VM)\"}.\\n{overlay,[{mkdir,\"log/sasl\"}]}."
+
+test: eunit ct
+compile: get-deps static-link
+delete-deps get-deps compile clean update-deps:
+	rebar $@
+.applist:
+	./depman.erl $(APPS) > $@
+$(RUN_DIR) $(LOG_DIR):
+	mkdir -p $(RUN_DIR) & mkdir -p $(LOG_DIR)
+console: .applist
+	ERL_LIBS=$(ERL_LIBS) erl $(ERL_ARGS) -eval \
+		'[ok = application:ensure_started(A, permanent) || A <- $(shell cat .applist)]'
+start: $(RUN_DIR) $(LOG_DIR) .applist
+	ERL_LIBS=$(ERL_LIBS) run_erl -daemon $(RUN_DIR)/ $(LOG_DIR)/ "exec $(MAKE) console"
+attach:
+	to_erl $(RUN_DIR)/
+release:
+	echo $(shell echo $(relx) > relx.config) & relx
+stop:
+	kill -9 `ps ax -o pid= -o command=|grep $(RELEASE)|grep $(COOKIE)|awk '{print $$1}'`
+$(PLT_NAME):
+	ERL_LIBS=deps dialyzer --build_plt --output_plt $(PLT_NAME) --apps $(APPS) || true
+dialyze: $(PLT_NAME) compile
+	dialyzer deps/*/ebin --plt $(PLT_NAME) --no_native -Werror_handling -Wunderspecs -Wrace_conditions
+tar:
+	tar zcvf $(RELEASE)-$(VSN)-$(DATE).tar.gz _rel/lib/*/ebin _rel/lib/*/priv _rel/bin _rel/releases
+eunit:
+	rebar eunit skip_deps=true
+ct:
+	rebar ct skip_deps=true verbose=1
+
+.PHONY: delete-deps get-deps compile clean console start attach release update-deps dialyze ct eunit tar

+ 10 - 0
rebar.config

@@ -0,0 +1,10 @@
+{sub_dirs,["deps","apps"]}.
+{lib_dirs,["apps"]}.
+{deps_dir,"deps"}.
+{deps, [
+    {erlydtl,       ".*",{git,"git://github.com/evanmiller/erlydtl.git",{tag,"0.8.0"}}},
+    {gproc,          ".*", {git, "git://github.com/uwiger/gproc.git", "HEAD"}},
+    {sync,          ".*", {git, "git://github.com/doxtop/sync", "HEAD"}},
+    {n2o_scripts,    ".*", {git, "git://github.com/synrc/n2o_scripts", "HEAD"}}
+]}.
+

+ 34 - 0
rels/web/files/erl

@@ -0,0 +1,34 @@
+#!/bin/sh
+
+## This script replaces the default "erl" in erts-VSN/bin. This is necessary
+## as escript depends on erl and in turn, erl depends on having access to a
+## bootscript (start.boot). Note that this script is ONLY invoked as a side-effect
+## of running escript -- the embedded node bypasses erl and uses erlexec directly
+## (as it should).
+##
+## Note that this script makes the assumption that there is a start_clean.boot
+## file available in $ROOTDIR/release/VSN.
+
+# Determine the abspath of where this script is executing from.
+ERTS_BIN_DIR=$(cd ${0%/*} && pwd)
+
+# Now determine the root directory -- this script runs from erts-VSN/bin,
+# so we simply need to strip off two dirs from the end of the ERTS_BIN_DIR
+# path.
+ROOTDIR=${ERTS_BIN_DIR%/*/*}
+
+# Parse out release and erts info
+START_ERL=`cat $ROOTDIR/releases/start_erl.data`
+ERTS_VSN=${START_ERL% *}
+APP_VSN=${START_ERL#* }
+
+BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
+EMU=beam
+PROGNAME=`echo $0 | sed 's/.*\\///'`
+CMD="$BINDIR/erlexec"
+export EMU
+export ROOTDIR
+export BINDIR
+export PROGNAME
+
+exec $CMD -boot $ROOTDIR/releases/$APP_VSN/start_clean ${1+"$@"}

+ 44 - 0
rels/web/files/install_upgrade.escript

@@ -0,0 +1,44 @@
+#!/usr/bin/env escript
+%%! -noshell -noinput
+%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ft=erlang ts=4 sw=4 et
+
+-define(TIMEOUT, 60000).
+-define(INFO(Fmt,Args), io:format(Fmt,Args)).
+
+main([NodeName, Cookie, ReleasePackage]) ->
+    TargetNode = start_distribution(NodeName, Cookie),
+    {ok, Vsn} = rpc:call(TargetNode, release_handler, unpack_release,
+                         [ReleasePackage], ?TIMEOUT),
+    ?INFO("Unpacked Release ~p~n", [Vsn]),
+    {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler,
+                                    check_install_release, [Vsn], ?TIMEOUT),
+    {ok, OtherVsn, Desc} = rpc:call(TargetNode, release_handler,
+                                    install_release, [Vsn], ?TIMEOUT),
+    ?INFO("Installed Release ~p~n", [Vsn]),
+    ok = rpc:call(TargetNode, release_handler, make_permanent, [Vsn], ?TIMEOUT),
+    ?INFO("Made Release ~p Permanent~n", [Vsn]);
+main(_) ->
+    init:stop(1).
+
+start_distribution(NodeName, Cookie) ->
+    MyNode = make_script_node(NodeName),
+    {ok, _Pid} = net_kernel:start([MyNode, shortnames]),
+    erlang:set_cookie(node(), list_to_atom(Cookie)),
+    TargetNode = make_target_node(NodeName),
+    case {net_kernel:hidden_connect_node(TargetNode),
+          net_adm:ping(TargetNode)} of
+        {true, pong} ->
+            ok;
+        {_, pang} ->
+            io:format("Node ~p not responding to pings.\n", [TargetNode]),
+            init:stop(1)
+    end,
+    TargetNode.
+
+make_target_node(Node) ->
+    [_, Host] = string:tokens(atom_to_list(node()), "@"),
+    list_to_atom(lists:concat([Node, "@", Host])).
+
+make_script_node(Node) ->
+    list_to_atom(lists:concat([Node, "_upgrader_", os:getpid()])).

+ 292 - 0
rels/web/files/node

@@ -0,0 +1,292 @@
+#!/bin/sh
+# -*- tab-width:4;indent-tabs-mode:nil -*-
+# ex: ts=4 sw=4 et
+
+RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd)
+
+CALLER_DIR=$PWD
+
+RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*}
+RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc
+# Note the trailing slash on $PIPE_DIR/
+PIPE_DIR=/tmp/$RUNNER_BASE_DIR/
+RUNNER_USER=
+
+# Make sure this script is running as the appropriate user
+if [ ! -z "$RUNNER_USER" ] && [ `whoami` != "$RUNNER_USER" ]; then
+    exec sudo -u $RUNNER_USER -i $0 $@
+fi
+
+# Identify the script name
+SCRIPT=`basename $0`
+
+# Parse out release and erts info
+START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data`
+ERTS_VSN=${START_ERL% *}
+APP_VSN=${START_ERL#* }
+
+# Use $CWD/vm.args if exists, otherwise releases/APP_VSN/vm.args, or else etc/vm.args
+if [ -e "$CALLER_DIR/vm.args" ]; then
+    VMARGS_PATH=$CALLER_DIR/vm.args
+    USE_DIR=$CALLER_DIR
+else
+    USE_DIR=$RUNNER_BASE_DIR
+    if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args" ]; then
+        VMARGS_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/vm.args"
+    else
+        VMARGS_PATH="$RUNNER_ETC_DIR/vm.args"
+    fi
+fi
+
+RUNNER_LOG_DIR=$USE_DIR/log
+# Make sure log directory exists
+mkdir -p $RUNNER_LOG_DIR
+
+# Use releases/VSN/sys.config if it exists otherwise use etc/app.config
+if [ -e "$USE_DIR/sys.config" ]; then
+    CONFIG_PATH="$USE_DIR/sys.config"
+else
+    if [ -e "$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config" ]; then
+        CONFIG_PATH="$RUNNER_BASE_DIR/releases/$APP_VSN/sys.config"
+    else
+        CONFIG_PATH="$RUNNER_ETC_DIR/app.config"
+    fi
+fi
+
+# Extract the target node name from node.args
+NAME_ARG=`egrep '^-s?name' $VMARGS_PATH`
+if [ -z "$NAME_ARG" ]; then
+    echo "vm.args needs to have either -name or -sname parameter."
+    exit 1
+fi
+
+# Extract the name type and name from the NAME_ARG for REMSH
+REMSH_TYPE=`echo $NAME_ARG | awk '{print $1}'`
+REMSH_NAME=`echo $NAME_ARG | awk '{print $2}'`
+
+# Note the `date +%s`, used to allow multiple remsh to the same node transparently
+REMSH_NAME_ARG="$REMSH_TYPE remsh`date +%s`@`echo $REMSH_NAME | awk -F@ '{print $2}'`"
+REMSH_REMSH_ARG="-remsh $REMSH_NAME"
+
+# Extract the target cookie
+COOKIE_ARG=`grep '^-setcookie' $VMARGS_PATH`
+if [ -z "$COOKIE_ARG" ]; then
+    echo "vm.args needs to have a -setcookie parameter."
+    exit 1
+fi
+
+# Make sure CWD is set to the right dir
+cd $USE_DIR
+
+# Make sure log directory exists
+mkdir -p $USE_DIR/log
+
+
+# Add ERTS bin dir to our path
+ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
+
+# Setup command to control the node
+NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
+
+# Setup remote shell command to control node
+REMSH="$ERTS_PATH/erl $REMSH_NAME_ARG $REMSH_REMSH_ARG $COOKIE_ARG"
+
+# Check the first argument for instructions
+case "$1" in
+    start|start_boot)
+        # Make sure there is not already a node running
+        RES=`$NODETOOL ping`
+        if [ "$RES" = "pong" ]; then
+            echo "Node is already running!"
+            exit 1
+        fi
+        case "$1" in
+            start)
+                shift
+                START_OPTION="console"
+                HEART_OPTION="start"
+                ;;
+            start_boot)
+                shift
+                START_OPTION="console_boot"
+                HEART_OPTION="start_boot"
+                ;;
+        esac
+        RUN_PARAM=$(printf "\'%s\' " "$@")
+        HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT $HEART_OPTION $RUN_PARAM"
+        export HEART_COMMAND
+        mkdir -p $PIPE_DIR
+        $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT $START_OPTION $RUN_PARAM" 2>&1
+        ;;
+
+    stop)
+        # Wait for the node to completely stop...
+        case `uname -s` in
+            Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
+                # PID COMMAND
+                PID=`ps ax -o pid= -o command=|\
+                    grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
+                ;;
+            SunOS)
+                # PID COMMAND
+                PID=`ps -ef -o pid= -o args=|\
+                    grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
+                ;;
+            CYGWIN*)
+                # UID PID PPID TTY STIME COMMAND
+                PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'`
+                ;;
+        esac
+        $NODETOOL stop
+        ES=$?
+        if [ "$ES" -ne 0 ]; then
+            exit $ES
+        fi
+        while `kill -0 $PID 2>/dev/null`;
+        do
+            sleep 1
+        done
+        ;;
+
+    restart)
+        ## Restart the VM without exiting the process
+        $NODETOOL restart
+        ES=$?
+        if [ "$ES" -ne 0 ]; then
+            exit $ES
+        fi
+        ;;
+
+    reboot)
+        ## Restart the VM completely (uses heart to restart it)
+        $NODETOOL reboot
+        ES=$?
+        if [ "$ES" -ne 0 ]; then
+            exit $ES
+        fi
+        ;;
+
+    ping)
+        ## See if the VM is alive
+        $NODETOOL ping
+        ES=$?
+        if [ "$ES" -ne 0 ]; then
+            exit $ES
+        fi
+        ;;
+
+    attach)
+        # Make sure a node IS running
+        RES=`$NODETOOL ping`
+        ES=$?
+        if [ "$ES" -ne 0 ]; then
+            echo "Node is not running!"
+            exit $ES
+        fi
+
+        shift
+        exec $ERTS_PATH/to_erl $PIPE_DIR
+        ;;
+
+    remote_console)
+        # Make sure a node IS running
+        RES=`$NODETOOL ping`
+        ES=$?
+        if [ "$ES" -ne 0 ]; then
+            echo "Node is not running!"
+            exit $ES
+        fi
+
+        shift
+        exec $REMSH
+        ;;
+
+    upgrade)
+        if [ -z "$2" ]; then
+            echo "Missing upgrade package argument"
+            echo "Usage: $SCRIPT upgrade {package base name}"
+            echo "NOTE {package base name} MUST NOT include the .tar.gz suffix"
+            exit 1
+        fi
+
+        # Make sure a node IS running
+        RES=`$NODETOOL ping`
+        ES=$?
+        if [ "$ES" -ne 0 ]; then
+            echo "Node is not running!"
+            exit $ES
+        fi
+
+        node_name=`echo $NAME_ARG | awk '{print $2}'`
+        erlang_cookie=`echo $COOKIE_ARG | awk '{print $2}'`
+
+        $ERTS_PATH/escript $RUNNER_BASE_DIR/bin/install_upgrade.escript $node_name $erlang_cookie $2
+        ;;
+
+    console|console_clean|console_boot)
+        # .boot file typically just $SCRIPT (ie, the app name)
+        # however, for debugging, sometimes start_clean.boot is useful.
+        # For e.g. 'setup', one may even want to name another boot script.
+        case "$1" in
+            console)        BOOTFILE=$SCRIPT ;;
+            console_clean)  BOOTFILE=start_clean ;;
+            console_boot)
+                shift
+                BOOTFILE="$1"
+                shift
+                ;;
+        esac
+        # Setup beam-required vars
+        ROOTDIR=$RUNNER_BASE_DIR
+        BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
+        EMU=beam
+        PROGNAME=`echo $0 | sed 's/.*\\///'`
+        CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $CONFIG_PATH -args_file $VMARGS_PATH"
+        export EMU
+        export ROOTDIR
+        export BINDIR
+        export PROGNAME
+
+        # Dump environment info for logging purposes
+        echo "Exec: $CMD" -- ${1+"$@"}
+        echo "Root: $ROOTDIR"
+
+        # Log the startup
+        logger -t "$SCRIPT[$$]" "Starting up"
+
+        # Start the VM
+        exec $CMD -- ${1+"$@"}
+        ;;
+
+    foreground)
+        # start up the release in the foreground for use by runit
+        # or other supervision services
+
+        BOOTFILE=$SCRIPT
+        FOREGROUNDOPTIONS="-noinput +Bd"
+
+        # Setup beam-required vars
+        ROOTDIR=$RUNNER_BASE_DIR
+        BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
+        EMU=beam
+        PROGNAME=`echo $0 | sed 's/.*\///'`
+        CMD="$BINDIR/erlexec $FOREGROUNDOPTIONS -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -config $CONFIG_PATH -args_file $VMARGS_PATH"
+        export EMU
+        export ROOTDIR
+        export BINDIR
+        export PROGNAME
+
+        # Dump environment info for logging purposes
+        echo "Exec: $CMD" -- ${1+"$@"}
+        echo "Root: $ROOTDIR"
+
+        # Start the VM
+        exec $CMD -- ${1+"$@"}
+        ;;
+    *)
+        echo "Usage: $SCRIPT {start|start_boot <file>|foreground|stop|restart|reboot|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade}"
+        exit 1
+        ;;
+esac
+
+exit 0

+ 96 - 0
rels/web/files/node.cmd

@@ -0,0 +1,96 @@
+@setlocal
+
+@set node_name=node
+
+@rem Get the absolute path to the parent directory,
+@rem which is assumed to be the node root.
+@for /F "delims=" %%I in ("%~dp0..") do @set node_root=%%~fI
+
+@set releases_dir=%node_root%\releases
+
+@rem Parse ERTS version and release version from start_erl.data
+@for /F "usebackq tokens=1,2" %%I in ("%releases_dir%\start_erl.data") do @(
+    @call :set_trim erts_version %%I
+    @call :set_trim release_version %%J
+)
+
+@set vm_args=%releases_dir%\%release_version%\vm.args
+@set sys_config=%releases_dir%\%release_version%\sys.config
+@set node_boot_script=%releases_dir%\%release_version%\%node_name%
+@set clean_boot_script=%releases_dir%\%release_version%\start_clean
+
+@rem extract erlang cookie from vm.args
+@for /f "usebackq tokens=1-2" %%I in (`findstr /b \-setcookie "%vm_args%"`) do @set erlang_cookie=%%J
+
+@set erts_bin=%node_root%\erts-%erts_version%\bin
+
+@set service_name=%node_name%_%release_version%
+
+@set erlsrv="%erts_bin%\erlsrv.exe"
+@set epmd="%erts_bin%\epmd.exe"
+@set escript="%erts_bin%\escript.exe"
+@set werl="%erts_bin%\werl.exe"
+
+@if "%1"=="usage" @goto usage
+@if "%1"=="install" @goto install
+@if "%1"=="uninstall" @goto uninstall
+@if "%1"=="start" @goto start
+@if "%1"=="stop" @goto stop
+@if "%1"=="restart" @call :stop && @goto start
+@if "%1"=="console" @goto console
+@if "%1"=="query" @goto query
+@if "%1"=="attach" @goto attach
+@if "%1"=="upgrade" @goto upgrade
+@echo Unknown command: "%1"
+
+:usage
+@echo Usage: %~n0 [install^|uninstall^|start^|stop^|restart^|console^|query^|attach^|upgrade]
+@goto :EOF
+
+:install
+@set description=Erlang node %node_name% in %node_root%
+@set start_erl=%node_root%\bin\start_erl.cmd
+@set args= ++ %node_name% ++ %node_root%
+@%erlsrv% add %service_name% -c "%description%" -sname %node_name% -w "%node_root%" -m "%start_erl%" -args "%args%" -stopaction "init:stop()."
+@goto :EOF
+
+:uninstall
+@%erlsrv% remove %service_name%
+@%epmd% -kill
+@goto :EOF
+
+:start
+@%erlsrv% start %service_name%
+@goto :EOF
+
+:stop
+@%erlsrv% stop %service_name%
+@goto :EOF
+
+:console
+@start "%node_name% console" %werl% -boot "%node_boot_script%" -config "%sys_config%" -args_file "%vm_args%" -sname %node_name%
+@goto :EOF
+
+:query
+@%erlsrv% list %service_name%
+@exit %ERRORLEVEL%
+@goto :EOF
+
+:attach
+@for /f "usebackq" %%I in (`hostname`) do @set hostname=%%I
+start "%node_name% attach" %werl% -boot "%clean_boot_script%" -remsh %node_name%@%hostname% -sname console -setcookie %erlang_cookie%
+@goto :EOF
+
+:upgrade
+@if "%2"=="" (
+    @echo Missing upgrade package argument
+    @echo Usage: %~n0 upgrade {package base name}
+    @echo NOTE {package base name} MUST NOT include the .tar.gz suffix
+    @goto :EOF
+)
+@%escript% %node_root%\bin\install_upgrade.escript %node_name% %erlang_cookie% %2
+@goto :EOF
+
+:set_trim
+@set %1=%2
+@goto :EOF

+ 138 - 0
rels/web/files/nodetool

@@ -0,0 +1,138 @@
+%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ft=erlang ts=4 sw=4 et
+%% -------------------------------------------------------------------
+%%
+%% nodetool: Helper Script for interacting with live nodes
+%%
+%% -------------------------------------------------------------------
+
+main(Args) ->
+    ok = start_epmd(),
+    %% Extract the args
+    {RestArgs, TargetNode} = process_args(Args, [], undefined),
+
+    %% See if the node is currently running  -- if it's not, we'll bail
+    case {net_kernel:hidden_connect_node(TargetNode), net_adm:ping(TargetNode)} of
+        {true, pong} ->
+            ok;
+        {_, pang} ->
+            io:format("Node ~p not responding to pings.\n", [TargetNode]),
+            halt(1)
+    end,
+
+    case RestArgs of
+        ["ping"] ->
+            %% If we got this far, the node already responsed to a ping, so just dump
+            %% a "pong"
+            io:format("pong\n");
+        ["stop"] ->
+            io:format("~p\n", [rpc:call(TargetNode, init, stop, [], 60000)]);
+        ["restart"] ->
+            io:format("~p\n", [rpc:call(TargetNode, init, restart, [], 60000)]);
+        ["reboot"] ->
+            io:format("~p\n", [rpc:call(TargetNode, init, reboot, [], 60000)]);
+        ["rpc", Module, Function | RpcArgs] ->
+            case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function),
+                          [RpcArgs], 60000) of
+                ok ->
+                    ok;
+                {badrpc, Reason} ->
+                    io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
+                    halt(1);
+                _ ->
+                    halt(1)
+            end;
+        ["rpcterms", Module, Function, ArgsAsString] ->
+            case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function),
+                          consult(ArgsAsString), 60000) of
+                {badrpc, Reason} ->
+                    io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
+                    halt(1);
+                Other ->
+                    io:format("~p\n", [Other])
+            end;
+        Other ->
+            io:format("Other: ~p\n", [Other]),
+            io:format("Usage: nodetool {ping|stop|restart|reboot}\n")
+    end,
+    net_kernel:stop().
+
+process_args([], Acc, TargetNode) ->
+    {lists:reverse(Acc), TargetNode};
+process_args(["-setcookie", Cookie | Rest], Acc, TargetNode) ->
+    erlang:set_cookie(node(), list_to_atom(Cookie)),
+    process_args(Rest, Acc, TargetNode);
+process_args(["-name", TargetName | Rest], Acc, _) ->
+    ThisNode = append_node_suffix(TargetName, "_maint_"),
+    {ok, _} = net_kernel:start([ThisNode, longnames]),
+    process_args(Rest, Acc, nodename(TargetName));
+process_args(["-sname", TargetName | Rest], Acc, _) ->
+    ThisNode = append_node_suffix(TargetName, "_maint_"),
+    {ok, _} = net_kernel:start([ThisNode, shortnames]),
+    process_args(Rest, Acc, nodename(TargetName));
+process_args([Arg | Rest], Acc, Opts) ->
+    process_args(Rest, [Arg | Acc], Opts).
+
+
+start_epmd() ->
+    [] = os:cmd(epmd_path() ++ " -daemon"),
+    ok.
+
+epmd_path() ->
+    ErtsBinDir = filename:dirname(escript:script_name()),
+    Name = "epmd",
+    case os:find_executable(Name, ErtsBinDir) of
+        false ->
+            case os:find_executable(Name) of
+                false ->
+                    io:format("Could not find epmd.~n"),
+                    halt(1);
+                GlobalEpmd ->
+                    GlobalEpmd
+            end;
+        Epmd ->
+            Epmd
+    end.
+
+
+nodename(Name) ->
+    case string:tokens(Name, "@") of
+        [_Node, _Host] ->
+            list_to_atom(Name);
+        [Node] ->
+            [_, Host] = string:tokens(atom_to_list(node()), "@"),
+            list_to_atom(lists:concat([Node, "@", Host]))
+    end.
+
+append_node_suffix(Name, Suffix) ->
+    case string:tokens(Name, "@") of
+        [Node, Host] ->
+            list_to_atom(lists:concat([Node, Suffix, os:getpid(), "@", Host]));
+        [Node] ->
+            list_to_atom(lists:concat([Node, Suffix, os:getpid()]))
+    end.
+
+
+%%
+%% Given a string or binary, parse it into a list of terms, ala file:consult/0
+%%
+consult(Str) when is_list(Str) ->
+    consult([], Str, []);
+consult(Bin) when is_binary(Bin)->
+    consult([], binary_to_list(Bin), []).
+
+consult(Cont, Str, Acc) ->
+    case erl_scan:tokens(Cont, Str, 0) of
+        {done, Result, Remaining} ->
+            case Result of
+                {ok, Tokens, _} ->
+                    {ok, Term} = erl_parse:parse_term(Tokens),
+                    consult([], Remaining, [Term | Acc]);
+                {eof, _Other} ->
+                    lists:reverse(Acc);
+                {error, Info, _} ->
+                    {error, Info}
+            end;
+        {more, Cont1} ->
+            consult(Cont1, eof, Acc)
+    end.

+ 40 - 0
rels/web/files/start_erl.cmd

@@ -0,0 +1,40 @@
+@setlocal
+
+@rem Parse arguments. erlsrv.exe prepends erl arguments prior to first ++.
+@rem Other args are position dependent.
+@set args="%*"
+@for /F "delims=++ tokens=1,2,3" %%I in (%args%) do @(
+    @set erl_args=%%I
+    @call :set_trim node_name %%J
+    @rem Trim spaces from the left of %%K (node_root), which may have spaces inside
+    @for /f "tokens=* delims= " %%a in ("%%K") do @set node_root=%%a
+)
+
+@set releases_dir=%node_root%\releases
+
+@rem parse ERTS version and release version from start_erl.dat
+@for /F "usebackq tokens=1,2" %%I in ("%releases_dir%\start_erl.data") do @(
+    @call :set_trim erts_version %%I
+    @call :set_trim release_version %%J
+)
+
+@set erl_exe="%node_root%\erts-%erts_version%\bin\erl.exe"
+@set boot_file="%releases_dir%\%release_version%\%node_name%"
+
+@if exist "%releases_dir%\%release_version%\sys.config" (
+    @set app_config="%releases_dir%\%release_version%\sys.config"
+) else (
+    @set app_config="%node_root%\etc\app.config"
+)
+
+@if exist "%releases_dir%\%release_version%\vm.args" (
+    @set vm_args="%releases_dir%\%release_version%\vm.args"
+) else (
+    @set vm_args="%node_root%\etc\vm.args"
+)
+
+@%erl_exe% %erl_args% -boot %boot_file% -config %app_config% -args_file %vm_args%
+
+:set_trim
+@set %1=%2
+@goto :EOF

+ 13 - 0
rels/web/files/sys.config

@@ -0,0 +1,13 @@
+[
+ {sync, [{sync_mode, nitrogen}]},
+ {n2o, [{route,routes}]},
+ {n2o, [{port,8000}, {transition_port, 8000}]},
+ {sasl, [
+         {sasl_error_logger, {file, "log/sasl-error.log"}},
+         {errlog_type, error},
+         {error_logger_mf_dir, "log/sasl"},      % Log directory
+         {error_logger_mf_maxbytes, 10485760},   % 10 MB max file size
+         {error_logger_mf_maxfiles, 5}           % 5 files max
+        ]}
+].
+

+ 6 - 0
rels/web/files/vm.args

@@ -0,0 +1,6 @@
+-name n2o@127.0.0.1
+-setcookie node_runner
++K true
++A 5
+-env ERL_MAX_PORTS 4096
+-env ERL_FULLSWEEP_AFTER 10

+ 46 - 0
rels/web/reltool.config

@@ -0,0 +1,46 @@
+{sys, [
+       {lib_dirs, ["../../apps","../../deps"]},
+       {erts, [{mod_cond, derived}, {app_file, strip}]},
+       {app_file, strip},
+       {rel, "node", "1",
+        [
+         kernel,
+         stdlib,
+         sasl,
+         mimetypes,
+         gproc,
+         sync,
+         n2o,
+         erlydtl,
+         face
+        ]},
+       {rel, "start_clean", "",
+        [
+         kernel,
+         stdlib
+        ]},
+       {boot_rel, "node"},
+       {profile, embedded},
+       {incl_cond, derived},
+       {mod_cond, derived},
+       {excl_archive_filters, [".*"]}, %% Do not archive built libs
+       {excl_sys_filters, ["^bin/.*", "^erts.*/bin/(dialyzer|typer)",
+                           "^erts.*/(doc|info|include|lib|man|src)"]},
+       {excl_app_filters, ["\.gitignore"]},
+       {app, hipe, [{mod_cond, app}, {incl_cond, exclude}]},
+       {app, face, [{mod_cond, app}, {incl_cond, include}]}
+      ]}.
+
+{target_dir, "node"}.
+
+{overlay, [
+           {mkdir, "log/sasl"},
+           {copy, "files/erl", "\{\{erts_vsn\}\}/bin/erl"},
+           {copy, "files/nodetool", "\{\{erts_vsn\}\}/bin/nodetool"},
+           {copy, "files/node", "bin/node"},
+           {copy, "files/node.cmd", "bin/node.cmd"},
+           {copy, "files/start_erl.cmd", "bin/start_erl.cmd"},
+           {copy, "files/install_upgrade.escript", "bin/install_upgrade.escript"},
+           {copy, "files/sys.config", "releases/\{\{rel_vsn\}\}/sys.config"},
+           {copy, "files/vm.args", "releases/\{\{rel_vsn\}\}/vm.args"}
+          ]}.

+ 5 - 0
xen.config

@@ -0,0 +1,5 @@
+kernel = "vmling"
+name = "skyline"
+memory = 512
+extra = "-ipaddr 172.16.42.108 -netmask 255.255.255.0 -gateway 172.16.42.108 -root /erlang -pz /samples/deps/erlydtl/ebin -pz /samples/apps/n2o_sample/ebin -pz /samples/deps/n2o/ebin -pz /samples/deps/cowboy/ebin -pz /samples/deps/ranch/ebin -pz /samples/deps/mimetypes/ebin -home /samples/deps/n2o_sample/ebin"
+vif =  [ 'bridge=docker0', ]