-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79d6b7f
commit 90a3553
Showing
4 changed files
with
191 additions
and
11 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
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 |
78 changes: 78 additions & 0 deletions
78
tools/astarte_dev_tool/lib/mix/tasks/astarte_dev_tool/system/check.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,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 |
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,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.System do | ||
@field_separator "," | ||
@parse_regex ~r/^(?<id>\w+)#{@field_separator}(?:astarte-(?<name>[\w-]+)-\d)$/ | ||
|
||
def system_status(path) do | ||
command = "docker" | ||
|
||
args = | ||
~w(ps -a --no-trunc --format {{.ID}}#{@field_separator}{{.Names}} -f status=running -f label=com.docker.compose.project.working_dir=#{path}) | ||
|
||
{pids_str, 0} = System.cmd(command, args, cd: path) | ||
|
||
{:ok, | ||
pids_str | ||
|> String.split("\n", trim: true) | ||
|> Enum.map(&Regex.named_captures(@parse_regex, &1))} | ||
end | ||
end |