This repository has been archived by the owner on Nov 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from rabbitmq/rabbitmq-cli-178
Implement `rabbitmqctl hipe_compile`
- Loading branch information
Showing
13 changed files
with
195 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
## The contents of this file are subject to the Mozilla Public License | ||
## Version 1.1 (the "License"); you may not use this file except in | ||
## compliance with the License. You may obtain a copy of the License | ||
## at http://www.mozilla.org/MPL/ | ||
## | ||
## Software distributed under the License is distributed on an "AS IS" | ||
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See | ||
## the License for the specific language governing rights and | ||
## limitations under the License. | ||
## | ||
## The Original Code is RabbitMQ. | ||
## | ||
## The Initial Developer of the Original Code is Pivotal Software, Inc. | ||
## Copyright (c) 2016-2017 Pivotal Software, Inc. All rights reserved. | ||
|
||
defmodule RabbitMQ.CLI.Ctl.Commands.HipeCompileCommand do | ||
alias RabbitMQ.CLI.Core.Helpers, as: Helpers | ||
|
||
@behaviour RabbitMQ.CLI.CommandBehaviour | ||
use RabbitMQ.CLI.DefaultOutput | ||
|
||
# | ||
# API | ||
# | ||
|
||
def merge_defaults(args, opts) do | ||
{args, opts} | ||
end | ||
|
||
def switches(), do: [rabbitmq_home: :string] | ||
|
||
def usage, do: "hipe_compile <directory>" | ||
|
||
def validate([], _), do: {:validation_failure, :not_enough_args} | ||
def validate([target_dir], opts) do | ||
:ok | ||
|> Helpers.validate_step(fn() -> | ||
case acceptable_path?(target_dir) do | ||
true -> :ok | ||
false -> {:error, {:bad_argument, "Target directory path cannot be blank"}} | ||
end | ||
end) | ||
|> Helpers.validate_step(fn() -> | ||
case File.dir?(target_dir) do | ||
true -> :ok | ||
false -> | ||
case File.mkdir_p(target_dir) do | ||
:ok -> :ok | ||
{:error, :eperm} -> | ||
{:error, {:bad_argument, "Cannot create target directory #{target_dir}: insufficient permissions"}} | ||
end | ||
end | ||
end) | ||
|> Helpers.validate_step(fn() -> Helpers.require_rabbit(opts) end) | ||
end | ||
def validate(_, _), do: {:validation_failure, :too_many_args} | ||
|
||
def run([target_dir], _opts) do | ||
Code.ensure_loaded(:rabbit_hipe) | ||
hipe_compile(String.trim(target_dir)) | ||
end | ||
|
||
def banner([target_dir], _) do | ||
"Will pre-compile RabbitMQ server modules with HiPE to #{target_dir} ..." | ||
end | ||
|
||
# | ||
# Implementation | ||
# | ||
|
||
# Accepts any non-blank path | ||
defp acceptable_path?(value) do | ||
String.length(String.trim(value)) != 0 | ||
end | ||
|
||
defp hipe_compile(target_dir) do | ||
case :rabbit_hipe.can_hipe_compile() do | ||
true -> | ||
case :rabbit_hipe.compile_to_directory(target_dir) do | ||
{:ok, _, _} -> :ok | ||
{:ok, :already_compiled} -> {:ok, "already compiled"} | ||
{:error, message} -> {:error, message} | ||
end | ||
false -> | ||
{:error, "HiPE compilation is not supported"} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,5 +51,4 @@ defmodule RabbitMQ.CLI.Ctl.Validators do | |
{:error, err} -> {:validation_failure, err} | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
## The contents of this file are subject to the Mozilla Public License | ||
## Version 1.1 (the "License"); you may not use this file except in | ||
## compliance with the License. You may obtain a copy of the License | ||
## at http://www.mozilla.org/MPL/ | ||
## | ||
## Software distributed under the License is distributed on an "AS IS" | ||
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See | ||
## the License for the specific language governing rights and | ||
## limitations under the License. | ||
## | ||
## The Original Code is RabbitMQ. | ||
## | ||
## The Initial Developer of the Original Code is Pivotal Software, Inc. | ||
## Copyright (c) 2016-2017 Pivotal Software, Inc. All rights reserved. | ||
|
||
defmodule HipeCompileCommandTest do | ||
use ExUnit.Case, async: false | ||
import TestHelper | ||
|
||
require Temp | ||
|
||
@command RabbitMQ.CLI.Ctl.Commands.HipeCompileCommand | ||
@vhost "/" | ||
|
||
setup_all do | ||
RabbitMQ.CLI.Core.Distribution.start() | ||
:net_kernel.connect_node(get_rabbit_hostname()) | ||
|
||
start_rabbitmq_app() | ||
|
||
on_exit([], fn -> | ||
:erlang.disconnect_node(get_rabbit_hostname()) | ||
end) | ||
end | ||
|
||
setup do | ||
rabbitmq_home = :rabbit_misc.rpc_call(node(), :code, :lib_dir, [:rabbit]) | ||
|
||
{:ok, tmp_dir} = Temp.path | ||
|
||
{:ok, opts: %{ | ||
node: get_rabbit_hostname(), | ||
vhost: @vhost, | ||
rabbitmq_home: rabbitmq_home | ||
}, | ||
target_dir: tmp_dir | ||
} | ||
end | ||
|
||
test "validate: providing no arguments fails validation", context do | ||
assert @command.validate([], context[:opts]) == | ||
{:validation_failure, :not_enough_args} | ||
end | ||
|
||
test "validate: providing two arguments fails validation", context do | ||
assert @command.validate(["/path/one", "/path/two"], context[:opts]) == | ||
{:validation_failure, :too_many_args} | ||
end | ||
|
||
test "validate: providing three arguments fails validation", context do | ||
assert @command.validate(["/path/one", "/path/two", "/path/three"], context[:opts]) == | ||
{:validation_failure, :too_many_args} | ||
end | ||
|
||
test "validate: providing one blank directory path and required options fails", context do | ||
assert match?({:validation_failure, {:bad_argument, _}}, @command.validate([""], context[:opts])) | ||
end | ||
|
||
test "validate: providing one path argument that only contains spaces and required options fails", context do | ||
assert match?({:validation_failure, {:bad_argument, _}}, @command.validate([" "], context[:opts])) | ||
end | ||
|
||
test "validate: providing one non-blank directory path and required options succeeds", context do | ||
assert @command.validate([context[:target_dir]], context[:opts]) == :ok | ||
end | ||
|
||
test "validate: failure to load the rabbit application is reported as an error", context do | ||
assert {:validation_failure, {:unable_to_load_rabbit, _}} = | ||
@command.validate([context[:target_dir]], Map.delete(context[:opts], :rabbitmq_home)) | ||
end | ||
end |