Skip to content

Commit

Permalink
added basic functionalities
Browse files Browse the repository at this point in the history
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
shinnokdisengir committed Jul 18, 2024
1 parent 284935b commit 19e8140
Show file tree
Hide file tree
Showing 12 changed files with 485 additions and 7 deletions.
45 changes: 42 additions & 3 deletions tools/astarte_dev_tool/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# AstarteDevTool

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
Expand All @@ -15,7 +13,48 @@ def deps do
end
```

## Usage

Actually, the functionalities of `astarte_dev_tool` are stateless and based on the `mix task` mechanism.

### Starting the System

To start `docker compose` in development mode, use:

```bash
mix astarte_dev_tool.system.up --path <astarte-path>
```

### Stopping the System

To stop `docker compose` in development mode, use:

```bash
mix astarte_dev_tool.system.down --path <astarte-path>
```

### Watching for Changes

To enable watch mode for hot-code-reloading, use:

```bash
mix astarte_dev_tool.system.watch --path <astarte-path>
```

This command will start the system in watch mode and keep running in the foreground, allowing for real-time code reloading during development.

---

### Example

1. Start the system: `mix astarte_dev_tool.system.up --path ../../../astarte`
2. Enable watch mode: `mix astarte_dev_tool.system.watch --path ../../../astarte`
3. Stop the system: `mix astarte_dev_tool.system.down --path ../../../astarte`

These commands will help you manage the development environment for Astarte efficiently.

---

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/astarte_dev_tool>.

35 changes: 35 additions & 0 deletions tools/astarte_dev_tool/lib/commands/system/down.ex
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
34 changes: 34 additions & 0 deletions tools/astarte_dev_tool/lib/commands/system/up.ex
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
53 changes: 53 additions & 0 deletions tools/astarte_dev_tool/lib/commands/system/watch.ex
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
31 changes: 31 additions & 0 deletions tools/astarte_dev_tool/lib/constants/system.ex
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
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 tools/astarte_dev_tool/lib/mix/tasks/astarte_dev_tool/system/up.ex
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
Loading

0 comments on commit 19e8140

Please sign in to comment.