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 Added creating Realm feature #993

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ services:
watch:
- path: apps/astarte_trigger_engine/lib
action: rebuild
scylla:
ports:
- "9042:9042"
14 changes: 14 additions & 0 deletions tools/astarte_dev_tool/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Config

config :logger, :console,
format: {PrettyLog.LogfmtFormatter, :format},
metadata: [:realm, :datacenter, :replication_factor, :module, :function, :tag]

config :astarte_dev_tool, :xandra,
nodes: [
"#{System.get_env("CASSANDRA_DB_HOST") || "localhost"}:#{System.get_env("CASSANDRA_DB_PORT") || 9042}"
],
sync_connect: 5000,
log: :info,
stacktrace: true,
pool_size: 10
54 changes: 54 additions & 0 deletions tools/astarte_dev_tool/lib/commands/realm/create.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# 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.Realm.Create do
@moduledoc false
alias Astarte.DataAccess.Database
alias Astarte.DataAccess.Realm, as: RealmDataAccess

@start_apps [
:logger,
:crypto,
:ssl,
:xandra,
:astarte_data_access
]
def exec(
[{_, _} | _] = nodes,
realm_name,
replication \\ 1,
max_retention \\ 1,
public_key_pem \\ "@@@@",
device_registration_limit \\ nil,
realm_schema_version \\ 10
) do
with :ok <- Enum.each(@start_apps, &Application.ensure_all_started/1),
{:ok, _client} <- Database.connect(cassandra_nodes: nodes),
:ok <-
RealmDataAccess.create_realm(
realm_name,
replication,
max_retention,
public_key_pem,
device_registration_limit,
realm_schema_version
) do
:ok
end
end
end
54 changes: 54 additions & 0 deletions tools/astarte_dev_tool/lib/commands/system/check.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# 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.Check do
@moduledoc false
require Logger
alias AstarteDevTool.Utilities.System, as: SystemUtilities

@astarte_services [
"astarte-dashboard",
"astarte-appengine-api",
"astarte-housekeeping",
"astarte-housekeeping-api",
"astarte-grafana",
"astarte-realm-management",
"astarte-realm-management-api",
"astarte-data-updater-plant",
"astarte-trigger-engine",
"astarte-pairing",
"astarte-pairing-api",
"vernemq",
"scylla",
"traefik",
"rabbitmq",
"cfssl"
]

def exec(path) do
case SystemUtilities.system_status(path) do
{:ok, list} ->
current_list = list |> Enum.map(fn %{"name" => name} -> name end) |> MapSet.new()

case Enum.filter(@astarte_services, &(not MapSet.member?(current_list, &1))) do
[] -> :ok
rest -> {:error, rest}
end
end
end
end
4 changes: 2 additions & 2 deletions tools/astarte_dev_tool/lib/commands/system/watch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ defmodule AstarteDevTool.Commands.System.Watch do
@moduledoc false
require Logger
alias AstarteDevTool.Constants.System, as: Constants
alias AstarteDevTool.Utilities.Process, as: AstarteProcess
alias AstarteDevTool.Utilities.Process, as: ProcessUtilities

def exec(path) do
case AstarteProcess.check_process(Constants.command(), Constants.command_watch_args(), path) do
case ProcessUtilities.process_check(Constants.command(), Constants.command_watch_args(), path) do
{:ok, pid} when not is_nil(pid) -> kill_zombie_process(pid)
_ -> :ok
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#
# 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.Realm.Create do
use Mix.Task
alias Astarte.Core.Realm
alias AstarteDevTool.Utilities.Node
alias AstarteDevTool.Commands.Realm.Create
alias AstarteDevTool.Utilities.Path

@shortdoc "Create realm/s into the running Astarte"

@aliases [
p: :path,
n: :node
]

@switches [
path: :string,
node: :keep,
log_level: :string
]

@moduledoc """
Create realm into the running Astarte platform.

## Examples

$ mix astarte_dev_tool.realm.create -n localhost:12345 realm1
$ mix astarte_dev_tool.realm.create -n localhost:12345 -n cassandra:54321 realm1

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

* `-n` `--node` - (at least one is required) Cassandra/Scylla cluster node.
Every node has format **host/ip:port**

* `--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, args} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)

unless Keyword.has_key?(opts, :path), do: Mix.raise("The --path argument is required")

unless Keyword.has_key?(opts, :node),
do: Mix.raise("At least one --node argument is required")

unless Enum.count(args) === 1,
do: Mix.raise("The command required one argument - the Realm name")

realm_name = Enum.at(args, 0)

unless Realm.valid_name?(realm_name),
do: Mix.raise("Invalid Realm name provided")

nodes =
case(opts |> Keyword.get_values(:node) |> Node.parse_nodes()) do
{:ok, nodes} -> nodes
{:error, _} -> Mix.raise("--node argument must be in <host/ip>:<port> format")
end

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),
:ok <- Mix.Tasks.AstarteDevTool.System.Check.run(["-p", abs_path]),
:ok <- Create.exec(nodes, realm_name) do
Mix.shell().info("Realms created successfully.")
:ok
else
{:error, output} ->
Mix.raise("Failed to create Astarte's realms. Output: #{output}")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# 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.Check do
use Mix.Task
alias AstarteDevTool.Commands.System.Check
alias AstarteDevTool.Utilities.Path

@shortdoc "Check if the current Astarte is up & running"

@aliases [
p: :path
]

@switches [
path: :string,
log_level: :string
]

@moduledoc """
Check if the current Astarte is up & running.

## Examples

$ mix astarte_dev_tool.system.check -p /absolute/path/astarte
$ mix astarte_dev_tool.system.check -p ../../relative/to/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) do
case Check.exec(abs_path) do
:ok ->
Mix.shell().info("All Astarte's services are up & ready")
:ok

{:error, list} = data ->
Mix.raise("Some Astarte's services do not seem to be working: #{list}")
data
end
else
{:error, output} ->
Mix.raise("Failed to check Astarte's system. Output: #{output}")
end
end
end
36 changes: 36 additions & 0 deletions tools/astarte_dev_tool/lib/utilities/node.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# 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.Utilities.Node do
@host_regex ~r/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/
@ip_regex ~r/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
@port_regex ~r/^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/

def parse_nodes(list), do: parse_nodes(list, [])

defp parse_nodes([], acc), do: {:ok, acc}

defp parse_nodes([str | t], acc) do
with [host, port] <- String.split(str, ":"),
true <- (host =~ @host_regex or host =~ @ip_regex) and port =~ @port_regex do
parse_nodes(t, [{host, port} | acc])
else
_ -> {:error, "Invalid format"}
end
end
end
Loading