-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition of `astarte_dev_tool` functionalities: the functionalities are stateless and based on the `mix task` mechanism - [X] `mix astarte_dev_tool.system.up`: start `docker compose` in dev mode - [x] `mix astarte_dev_tool.system.down`: stop `docker compose` in dev mode - [X] `mix astarte_dev_tool.system.watch`: watch mode to enable hot-code-reloading Signed-off-by: Gabriele Ghio <gabriele.ghio@secomind.com>
- Loading branch information
1 parent
284935b
commit f4c1f04
Showing
12 changed files
with
486 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule AstarteDevTool.Commands.System.Down do | ||
@moduledoc false | ||
alias AstarteDevTool.Constants.System, as: Constants | ||
|
||
def exec(path, volumes \\ false) do | ||
args = | ||
if volumes, | ||
do: Constants.command_down_args() ++ ["-v"], | ||
else: Constants.command_down_args() | ||
|
||
case System.cmd(Constants.command(), args, Constants.options() ++ [cd: path]) do | ||
{_result, 0} -> :ok | ||
{:error, reason} -> {:error, "System is not up and running: #{reason}"} | ||
{result, exit_code} -> {:error, "Cannot exec system.down: #{result}, #{exit_code}"} | ||
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule AstarteDevTool.Commands.System.Up do | ||
@moduledoc false | ||
alias AstarteDevTool.Constants.System, as: Constants | ||
|
||
def exec(path) do | ||
case System.cmd( | ||
Constants.command(), | ||
Constants.command_up_args(), | ||
Constants.options() ++ [cd: path] | ||
) do | ||
{_result, 0} -> :ok | ||
{:process, _} -> {:error, "The system is already running"} | ||
{result, exit_code} -> {:error, "Cannot exec system.up: #{result}, #{exit_code}"} | ||
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule AstarteDevTool.Commands.System.Watch do | ||
@moduledoc false | ||
require Logger | ||
alias AstarteDevTool.Constants.System, as: Constants | ||
alias AstarteDevTool.Utilities.Process, as: AstarteProcess | ||
|
||
def exec(path) do | ||
case AstarteProcess.check_process(Constants.command(), Constants.command_watch_args(), path) do | ||
{:ok, pid} when not is_nil(pid) -> kill_zombie_process(pid) | ||
_ -> :ok | ||
end | ||
|
||
case System.cmd( | ||
Constants.command(), | ||
Constants.command_watch_args(), | ||
Constants.options() ++ [cd: path] | ||
) do | ||
{_result, 0} -> :ok | ||
{:error, reason} -> {:error, "Cannot run system watching: #{reason}"} | ||
{result, exit_code} -> {:error, "Cannot exec system.watch: #{result}, #{exit_code}"} | ||
end | ||
end | ||
|
||
defp kill_zombie_process(pid) do | ||
# Kill zombie watching process | ||
# The function is required to terminate the previous zombie | ||
# process if closed by closing the mix shell | ||
# TODO: to be implemented differently with https://github.com/alco/porcelain | ||
|
||
case System.cmd("kill", [pid]) do | ||
{_result, 0} -> Logger.info("Watching zombie process ##{pid} killed") | ||
_ -> {:error, "Cannot kill zombie process"} | ||
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule AstarteDevTool.Constants.System do | ||
@command "docker" | ||
@command_up_args ~w(compose -f docker-compose.yml -f docker-compose.dev.yml up --build --watch -d) | ||
@command_down_args ~w(compose -f docker-compose.yml -f docker-compose.dev.yml down) | ||
@command_watch_args ~w(compose -f docker-compose.yml -f docker-compose.dev.yml watch --no-up) | ||
@options [stderr_to_stdout: true, into: IO.stream(:stdio, :line)] | ||
|
||
def command, do: @command | ||
def command_up_args, do: @command_up_args | ||
def command_down_args, do: @command_down_args | ||
def command_watch_args, do: @command_watch_args | ||
def options, do: @options | ||
end |
75 changes: 75 additions & 0 deletions
75
tools/astarte_dev_tool/lib/mix/tasks/astarte_dev_tool/system/down.ex
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,75 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule Mix.Tasks.AstarteDevTool.System.Down do | ||
use Mix.Task | ||
alias AstarteDevTool.Commands.System.Down | ||
alias AstarteDevTool.Utilities.Path | ||
|
||
@shortdoc "Down the local Astarte system" | ||
|
||
@aliases [ | ||
p: :path, | ||
v: :volumes | ||
] | ||
|
||
@switches [ | ||
path: :string, | ||
volumes: :boolean, | ||
log_level: :string | ||
] | ||
|
||
@moduledoc """ | ||
Down the local Astarte system. | ||
## Examples | ||
$ mix system.down -p /path/astarte | ||
$ mix system.down -v | ||
## Command line options | ||
* `-p` `--path` - (required) working Astarte project directory | ||
* `--log-level` - the level to set for `Logger`. This task | ||
does not start your application, so whatever level you have configured in | ||
your config files will not be used. If this is not provided, no level | ||
will be set, so that if you set it yourself before calling this task | ||
then this won't interfere. Can be any of the `t:Logger.level/0` levels | ||
""" | ||
|
||
@impl true | ||
def run(args) do | ||
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases) | ||
|
||
unless Keyword.has_key?(opts, :path), do: Mix.raise("The --path argument is required") | ||
|
||
if log_level = opts[:log_level], | ||
do: Logger.configure(level: String.to_existing_atom(log_level)) | ||
|
||
with path <- opts[:path], | ||
{:ok, abs_path} <- Path.directory_path_from(path), | ||
_ = Mix.shell().info("Stopping astarte system..."), | ||
:ok <- Down.exec(abs_path, opts[:volumes]) do | ||
Mix.shell().info("Astarte's system stopped successfully.") | ||
:ok | ||
else | ||
{:error, output} -> | ||
Mix.raise("Failed to stop Astarte's system. Output: #{output}") | ||
end | ||
end | ||
end |
72 changes: 72 additions & 0 deletions
72
tools/astarte_dev_tool/lib/mix/tasks/astarte_dev_tool/system/up.ex
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,72 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule Mix.Tasks.AstarteDevTool.System.Up do | ||
use Mix.Task | ||
alias AstarteDevTool.Commands.System.Up | ||
alias AstarteDevTool.Utilities.Path | ||
|
||
@shortdoc "Up the local Astarte system" | ||
|
||
@aliases [ | ||
p: :path | ||
] | ||
|
||
@switches [ | ||
path: :string, | ||
log_level: :string | ||
] | ||
|
||
@moduledoc """ | ||
Up the local Astarte system. | ||
## Examples | ||
$ mix system.up -p /path/astarte | ||
## Command line options | ||
* `-p` `--path` - (required) working Astarte project directory | ||
* `--log-level` - the level to set for `Logger`. This task | ||
does not start your application, so whatever level you have configured in | ||
your config files will not be used. If this is not provided, no level | ||
will be set, so that if you set it yourself before calling this task | ||
then this won't interfere. Can be any of the `t:Logger.level/0` levels | ||
""" | ||
|
||
@impl true | ||
def run(args) do | ||
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases) | ||
|
||
unless Keyword.has_key?(opts, :path), do: Mix.raise("The --path argument is required") | ||
|
||
if log_level = opts[:log_level], | ||
do: Logger.configure(level: String.to_existing_atom(log_level)) | ||
|
||
with path <- opts[:path], | ||
{:ok, abs_path} <- Path.directory_path_from(path), | ||
_ = Mix.shell().info("Starting system in dev mode..."), | ||
:ok <- Up.exec(abs_path) do | ||
Mix.shell().info("Astarte's system started successfully") | ||
:ok | ||
else | ||
{:error, output} -> | ||
Mix.raise("Failed to start Astarte's system. Output: #{output}") | ||
end | ||
end | ||
end |
Oops, something went wrong.