Skip to content

Commit

Permalink
check feature
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnokdisengir committed Oct 3, 2024
1 parent 79d6b7f commit 90a3553
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 11 deletions.
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
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
34 changes: 23 additions & 11 deletions tools/astarte_dev_tool/lib/utilities/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ defmodule AstarteDevTool.Utilities.Process do
end
end

defp clean_version(version), do: {:ok, version |> String.trim() |> String.trim("'")}

def check_process(command, args, path) when is_list(args) do
command = "#{command} #{Enum.join(args, "")}"
check_process(command, path)
Expand All @@ -58,17 +56,18 @@ defmodule AstarteDevTool.Utilities.Process do
end
end

defp find_pids_by_command(command) do
case System.cmd("pgrep", ["-f", command]) do
{pids_str, 0} -> {:ok, String.trim(pids_str) |> String.split("\n")}
_ -> {:ok, []}
end
end
defp clean_version(version), do: {:ok, version |> String.trim() |> String.trim("'")}

defp find_pid_by_path([], _path), do: {:ok, nil}
def process_check(command, args, path) when is_list(args) do
command = "#{command} #{Enum.join(args, "")}"
process_check(command, path)
end

defp find_pid_by_path([pid | rest], path) do
if process_matches_path?(pid, path), do: {:ok, pid}, else: find_pid_by_path(rest, path)
def process_check(command, path) when is_bitstring(command) and is_bitstring(path) do
with {:ok, pids} <- find_pids_by_command(command),
{:ok, pid} <- find_pid_by_path(pids, path) do
{:ok, pid}
end
end

def process_matches_path?(pid, path) do
Expand All @@ -85,4 +84,17 @@ defmodule AstarteDevTool.Utilities.Process do

cwd == path
end

defp find_pids_by_command(command) do
case System.cmd("pgrep", ["-f", command]) do
{pids_str, 0} -> {:ok, String.trim(pids_str) |> String.split("\n")}
_ -> {:ok, []}
end
end

defp find_pid_by_path([], _path), do: {:ok, nil}

defp find_pid_by_path([pid | rest], path) do
if process_matches_path?(pid, path), do: {:ok, pid}, else: find_pid_by_path(rest, path)
end
end
36 changes: 36 additions & 0 deletions tools/astarte_dev_tool/lib/utilities/system.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.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

0 comments on commit 90a3553

Please sign in to comment.