Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

astarte_dev_tool: setup and features #978

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tools/astarte_dev_tool/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions tools/astarte_dev_tool/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
astarte_dev_tool-*.tar

# Temporary files, for example, from tests.
/tmp/
60 changes: 60 additions & 0 deletions tools/astarte_dev_tool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# AstarteDevTool

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `astarte_dev_tool` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:astarte_dev_tool, "~> 0.1.0"}
]
end
```

## Usage

Currently, 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.base_opts() ++ [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.base_opts() ++ [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.base_opts() ++ [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
shinnokdisengir marked this conversation as resolved.
Show resolved Hide resolved
# 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
shinnokdisengir marked this conversation as resolved.
Show resolved Hide resolved
def command, do: "docker"

def command_up_args,
do: ~w(compose -f docker-compose.yml -f docker-compose.dev.yml up --build --watch -d)

def command_down_args, do: ~w(compose -f docker-compose.yml -f docker-compose.dev.yml down)

def command_watch_args,
do: ~w(compose -f docker-compose.yml -f docker-compose.dev.yml watch --no-up)

def base_opts, do: [stderr_to_stdout: true, into: IO.stream(:stdio, :line)]
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#
# 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 /absolute/path/astarte
$ mix system.down -p ../../relative/to/astarte -v

## Command line options
* `-p` `--path` - (required) working Astarte project directory

* `-v` `--volumes` - remove volumes after switching off

* `--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")
shinnokdisengir marked this conversation as resolved.
Show resolved Hide resolved

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..."),
shinnokdisengir marked this conversation as resolved.
Show resolved Hide resolved
:ok <- Down.exec(abs_path, opts[:volumes]) do
Mix.shell().info("Astarte's system stopped successfully.")
:ok
else
{:error, output} ->
shinnokdisengir marked this conversation as resolved.
Show resolved Hide resolved
Mix.raise("Failed to stop Astarte's system. Output: #{output}")
end
end
end
Loading