Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First bits of the "Ensure Protocol", seems to work

  • Loading branch information...
commit 0ea64ab35211dcc0ced6f07b2801781aafa67309 1 parent cd06bcf
Yurii Rashkovskii authored committed
View
8 apps/htoad/include/stdlib.hrl
@@ -20,18 +20,14 @@
{
type = file :: file | dir,
path :: undefined | string(),
- ensure = present :: present | absent,
mode,
- content = "",
- %% special flag denoting file request
- producer :: undefined | fs
+ content = ""
}).
-record(package,
{
name,
- version,
- ensure = present :: present | absent
+ version
}).
-record(shell,
View
4 apps/htoad/src/htoad_host.erl
@@ -11,13 +11,13 @@ init_with_hostname_overriden(Engine, #init{}, {htoad_argument, {host, Hostname}}
init(Engine, #init{}) when not {rule, [{htoad_argument, {host, _}}]} ->
initialize(Engine, hostname()).
-linux_lsb(Engine, #file{ path="/etc/lsb-release", producer = fs, content = Content }, {operating_system_name, linux}) ->
+linux_lsb(Engine, #file{ path="/etc/lsb-release", content = Content }, {operating_system_name, linux}) ->
{match, [Dist]} = re:run(Content,".*DISTRIB_ID=(.*).*",[{capture,[1],list}]),
lager:debug("Linux distribution: ~s", [Dist]),
htoad:assert(Engine, {linux_distribution, Dist}).
-linux_redhat(Engine, #file{ path="/etc/redhat-release", producer = fs, content = Content }, {operating_system_name, linux}) ->
+linux_redhat(Engine, #file{ path="/etc/redhat-release", content = Content }, {operating_system_name, linux}) ->
{match, [Dist]} = re:run(Content,".*(CentOS|RedHat).*",[{capture,[1],list}]),
lager:debug("Linux distribution: ~s", [Dist]),
htoad:assert(Engine, {linux_distribution, Dist}).
View
56 apps/htoad/src/htoad_lfs.erl
@@ -6,35 +6,34 @@
-include_lib("kernel/include/file.hrl").
-rules([init, ensure_file_present, ensure_dir_present,
- fs_file_present, fs_file_absent, fs_dir_present, fs_dir_absent]).
+ fs_file_present, fs_dir_present]).
init(Engine, #init{}) ->
lager:debug("Initialized htoad_lfs"),
Engine.
-ensure_file_present(Engine, #file{ ensure = present,
- type = file,
- producer = undefined
- } = File) ->
+ensure_file_present(Engine, {ensure, present,
+ #file{
+ type = file
+ } = File}) ->
filelib:ensure_dir(File#file.path),
file:write_file(File#file.path, File#file.content),
lager:debug("Ensured file ~s",[File#file.path]),
chmod(File#file.path, File#file.mode),
- Engine.
+ htoad:assert(Engine, File).
-ensure_dir_present(Engine, #file{ ensure = present,
- type = dir,
- producer = undefined
- } = Dir) ->
+ensure_dir_present(Engine, {ensure, present,
+ #file{
+ type = dir
+ } = Dir}) ->
ok = filelib:ensure_dir(Dir#file.path ++ "/"),
lager:debug("Ensured directory ~s",[Dir#file.path]),
chmod(Dir#file.path, Dir#file.mode),
- Engine.
+ htoad:assert(Engine, Dir).
%% file system
-fs_file_present(Engine, {file_request, #file{ ensure = present,
- type = file,
- producer = fs
+fs_file_present(Engine, {file_request, #file{
+ type = file
} = File}) ->
case filelib:is_regular(File#file.path) of
true ->
@@ -44,21 +43,8 @@ fs_file_present(Engine, {file_request, #file{ ensure = present,
Engine
end.
-fs_file_absent(Engine, {file_request, #file{ ensure = absent,
- type = file,
- producer = fs
- } = File}) ->
- case filelib:is_regular(File#file.path) of
- false ->
- lager:debug("File ~s does not exist",[File#file.path]),
- htoad:assert(Engine, File);
- true ->
- Engine
- end.
-
-fs_dir_present(Engine, {file_request, #file{ ensure = present,
- type = dir,
- producer = fs
+fs_dir_present(Engine, {file_request, #file{
+ type = dir
} = File}) ->
case filelib:is_dir(File#file.path) of
true ->
@@ -68,18 +54,6 @@ fs_dir_present(Engine, {file_request, #file{ ensure = present,
Engine
end.
-fs_dir_absent(Engine, {file_request, #file{ ensure = absent,
- type = dir,
- producer = fs
- } = File}) ->
- case filelib:is_dir(File#file.path) of
- false ->
- lager:debug("Directory ~s does not exist",[File#file.path]),
- htoad:assert(Engine, File);
- true ->
- Engine
- end.
-
%% private
chmod(Path, Mode) ->
View
9 apps/htoad/src/htoad_pkg.erl
@@ -9,12 +9,12 @@
-rules([package_present]).
package_present(Engine, {package_check,
- #package{ ensure = present } = Package,
+ #package{} = Package,
"present"}) ->
lager:debug("Package ~s is present, no action needed",[format_package(Package)]),
Engine.
-ensure_package(Engine, #package{ ensure = present } = Package, Command) ->
+ensure_package(Engine, #package{} = Package, Command) ->
lager:debug("Checking if package ~s is present",[format_package(Package)]),
htoad:assert(Engine,
[
@@ -28,7 +28,10 @@ ensure_package(Engine, #package{ ensure = present } = Package, Command) ->
package_not_present(Engine, #package{} = Package, Command) ->
lager:debug("Package ~s is absent, installing",[format_package(Package)]),
- htoad:assert(Engine, Command).
+ htoad:assert(Engine, [Command, htoad_utils:on({match,
+ [{{output, Command, "installed"},
+ [],
+ [true]}]}, Package)]).
format_package(#package{ name = Name, version = undefined }) ->
Name;
View
4 apps/htoad/src/htoad_pkg_apt.erl
@@ -34,10 +34,10 @@ initialize(Engine) ->
run_as = superuser }
end).
-ensure_package(Engine, #package{ ensure = present } = Package, {package_manager, apt}) ->
+ensure_package(Engine, {ensure, present, #package{} = Package}, {package_manager, apt}) ->
htoad_pkg:ensure_package(Engine, Package, ?APT_SHELL_CHECK(Package)).
package_not_present(Engine, {package_check,
- #package{ ensure = present } = Package,
+ #package{} = Package,
"missing"}, {package_manager, apt}) ->
htoad_pkg:package_not_present(Engine, Package, ?APT_SHELL_INSTALL(Package)).
View
4 apps/htoad/src/htoad_pkg_brew.erl
@@ -34,10 +34,10 @@ init(Engine, #init{}, {operating_system_name, darwin}) ->
"|| printf not_installed" }
end).
-ensure_package(Engine, #package{ ensure = present } = Package, {package_manager, brew}) ->
+ensure_package(Engine, {ensure, present, #package{} = Package}, {package_manager, brew}) ->
htoad_pkg:ensure_package(Engine, Package, ?BREW_SHELL_CHECK(Package)).
package_not_present(Engine, {package_check,
- #package{ ensure = present } = Package,
+ #package{} = Package,
"missing"}, {package_manager, brew}) ->
htoad_pkg:package_not_present(Engine, Package, ?BREW_SHELL_INSTALL(Package)).
View
2  apps/htoad/src/htoad_shell.erl
@@ -23,7 +23,7 @@ command_run_in_superuser(Engine, #shell{ run_as = superuser } = Shell,
command(Engine, Shell).
command_run_as_superuser(Engine, #shell{ cmd = Cmd, run_as = superuser } = Shell,
- #file{ path = "/usr/bin/sudo", producer = fs,
+ #file{ path = "/usr/bin/sudo",
content = dontread }) when not {rule, [{?MODULE, superuser}]} ->
lager:debug("Executing shell command as a super user (via sudo): ~s", [Cmd]),
Result = string:strip(os:cmd("/usr/bin/sudo -n " ++ Cmd), right, $\n),
View
8 apps/htoad/src/htoad_transform.erl
@@ -79,13 +79,13 @@ do_transform(_Type, Form, _Context, State) ->
clause_scanner(record_expr, {record, _L, file, Fields} = Form, _Context,
#state{ file_requests = FReqs } = State) ->
case scan_file_record(Fields) of
- #file{ producer = fs, path = Path } = File when is_list(Path) ->
- {Form, true, State#state{ file_requests = [File|FReqs] }};
+ #file{ path = Path } = File when is_list(Path) ->
+ {Form, false, State#state{ file_requests = [File|FReqs] }};
_ ->
- {Form, true, State}
+ {Form, false, State}
end;
clause_scanner(_Type, Form, _Context, State) ->
- {Form, true, State}.
+ {Form, false, State}.
scan_file_record(Fields) ->
View
5 apps/htoad/src/htoad_utils.erl
@@ -1,5 +1,8 @@
-module(htoad_utils).
--export([on/2, load/1, module/1, file/1, render/2, render/3, render/4]).
+-export([ensure/2, on/2, load/1, module/1, file/1, render/2, render/3, render/4]).
+
+ensure(What, Object) ->
+ {ensure, What, Object}.
on([], Plan) ->
Plan;
View
10 examples/simple.htd
@@ -1,13 +1,15 @@
-define(BASE_DIR, "/tmp/srv").
build_server() ->
+ File = #file{ type = dir,
+ path = filename:join([?BASE_DIR, "build_dir"])
+ },
+
[
- #file{ type = dir,
- path = filename:join([?BASE_DIR, "build_dir"])
- },
+ ensure(present, File),
on([ {operating_system_name, darwin},
{package_manager, brew} ],
- #package{ name = "git1" })
+ ensure(present, #package{ name = "git" }))
].
Please sign in to comment.
Something went wrong with that request. Please try again.