Skip to content

Commit 958aa2b

Browse files
committed
chore: forward-port release-1.3
Signed-off-by: Francesco Noacco <francesco.noacco@secomind.com>
2 parents ee3fde3 + 97699cf commit 958aa2b

File tree

27 files changed

+216
-271
lines changed

27 files changed

+216
-271
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ RUN apt-get update -y && \
6868
ARG BUILD_ENV=prod
6969
ARG SERVICE
7070

71+
ENV SERVICE_CMD="./bin/$SERVICE start"
72+
7173
COPY --from=builder --chown=nobody:nogroup /app/_build/$BUILD_ENV/rel/$SERVICE .
72-
COPY --from=builder --chown=nobody:nogroup /app/entrypoint.sh .
7374

7475
# Change to non-root user
7576
USER nobody
7677

77-
ENTRYPOINT ["/bin/bash", "entrypoint.sh"]
78-
CMD ["start"]
78+
CMD $SERVICE_CMD

apps/astarte_appengine_api/entrypoint.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

apps/astarte_appengine_api/mix.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ defmodule Astarte.AppEngine.API.Mixfile do
8383
{:guardian, "~> 2.3.2"},
8484
{:uuid, "~> 2.0", hex: :uuid_erl},
8585
# Required by :phoenix_swagger, otherwise it fails finding ex_json_schema.app
86-
{:ex_json_schema, "~> 0.7"},
86+
{:ex_json_schema, "~> 0.9"},
8787
{:current_rabbit_pool, "~> 1.1"},
8888
{:phoenix_swagger, "~> 0.8"},
89-
{:xandra, "~> 0.13"},
9089
{:exandra, "~> 0.13"},
9190
{:typed_ecto_schema, "~> 0.4"},
9291
{:pretty_log, "~> 0.1"},
@@ -108,7 +107,8 @@ defmodule Astarte.AppEngine.API.Mixfile do
108107
# Test section
109108
{:excoveralls, "~> 0.15", only: :test},
110109
{:mox, "~> 0.5", only: :test},
111-
{:mimic, "~> 1.11", only: :test}
110+
{:mimic, "~> 1.11", only: :test},
111+
{:ecto, "~> 3.13", override: true}
112112
]
113113
end
114114

apps/astarte_appengine_api/test/astarte_appengine_api_web/controllers/interface_controller_test.exs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -413,33 +413,21 @@ defmodule Astarte.AppEngine.APIWeb.InterfaceControllerTest do
413413
property "forbids deletion if `allow_unset` is false", context do
414414
%{realm_name: realm_name, interfaces: interfaces, device: device, auth_conn: conn} = context
415415

416-
valid_interfaces_to_unset =
416+
invalid_interfaces_to_unset =
417417
interfaces
418418
|> Enum.filter(fn interface ->
419-
allow_unset? = Enum.any?(interface.mappings, &(!&1.allow_unset))
419+
doesnt_allow_unset? = Enum.any?(interface.mappings, &(!&1.allow_unset))
420420

421421
server? = interface.ownership == :server
422422
property? = interface.type == :properties
423423

424-
property? && server? && allow_unset?
424+
property? && server? && doesnt_allow_unset?
425425
end)
426426

427-
check all interface <- member_of(valid_interfaces_to_unset),
427+
check all interface <- member_of(invalid_interfaces_to_unset),
428428
mapping_to_unset <-
429429
member_of(interface.mappings |> Enum.filter(&(!&1.allow_unset))),
430430
path <- InterfaceUpdateGenerator.path_from_endpoint(mapping_to_unset.endpoint) do
431-
if mapping_to_unset.allow_unset do
432-
path_tokens = path_tokens(path)
433-
expected_token = [realm_name, device.encoded_id, interface.name | path_tokens]
434-
435-
publish_result_ok(interface, mapping_to_unset, fn args ->
436-
assert %{payload: payload, topic_tokens: topic_tokens, qos: qos} = args
437-
assert topic_tokens == expected_token
438-
assert qos == 2
439-
assert payload == ""
440-
end)
441-
end
442-
443431
request_path =
444432
interface_values_path(
445433
conn,

apps/astarte_appengine_api/test/support/cases/device.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ defmodule Astarte.Cases.Device do
169169
individual_downsampable: individual_downsampable(),
170170
object_downsampable: object_downsampable(),
171171
properties_server_allow_unset: properties_server_allow_unset(),
172+
properties_server_without_unset: properties_server_without_unset(),
172173
explicit_timestamps: explicit_timestamps(),
173174
other_interfaces: other_interfaces()
174175
}
@@ -231,6 +232,14 @@ defmodule Astarte.Cases.Device do
231232
|> customize_mappings(allow_unset: true)
232233
end
233234

235+
defp properties_server_without_unset do
236+
[ownership: :server, type: :properties]
237+
|> InterfaceGenerator.interface()
238+
|> list_of(min_length: 1)
239+
|> Enum.at(0)
240+
|> customize_mappings(allow_unset: false)
241+
end
242+
234243
defp fallible_interfaces do
235244
[ownership: :server]
236245
|> InterfaceGenerator.interface()

apps/astarte_data_updater_plant/entrypoint.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

apps/astarte_housekeeping/entrypoint.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

apps/astarte_housekeeping/lib/astarte_housekeeping/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ defmodule Astarte.Housekeeping.Application do
2222

2323
alias Astarte.DataAccess.Config, as: DataAccessConfig
2424
alias Astarte.Housekeeping.Config
25+
alias Astarte.Housekeeping.ReleaseTasks
2526

2627
require Logger
2728

@@ -45,6 +46,7 @@ defmodule Astarte.Housekeeping.Application do
4546

4647
# Define workers and child supervisors to be supervised
4748
children = [
49+
ReleaseTasks,
4850
Astarte.HousekeepingWeb.Telemetry,
4951
Astarte.HousekeepingWeb.Endpoint
5052
]

apps/astarte_housekeeping/lib/astarte_housekeeping/migrator.ex

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818

1919
defmodule Astarte.Housekeeping.Migrator do
2020
@moduledoc false
21-
import Ecto.Query
2221

2322
alias Astarte.DataAccess.Consistency
2423
alias Astarte.DataAccess.CSystem
2524
alias Astarte.DataAccess.KvStore
2625
alias Astarte.DataAccess.Realms.Realm
27-
alias Astarte.DataAccess.Repo
2826
alias Astarte.Events.AMQP.Vhost
2927
alias Astarte.Housekeeping.Realms.Queries
3028
alias Astarte.Housekeeping.Realms.Realm, as: HKRealm
@@ -36,8 +34,7 @@ defmodule Astarte.Housekeeping.Migrator do
3634
def run_astarte_keyspace_migrations do
3735
_ = Logger.info("Starting to migrate Astarte keyspace.", tag: "astarte_migration_started")
3836

39-
with :ok <- ensure_astarte_kv_store(),
40-
{:ok, astarte_schema_version} <- get_astarte_schema_version() do
37+
with {:ok, astarte_schema_version} <- get_astarte_schema_version() do
4138
migrate_astarte_keyspace_from_version(astarte_schema_version)
4239
end
4340
end
@@ -88,50 +85,6 @@ defmodule Astarte.Housekeeping.Migrator do
8885
end
8986
end
9087

91-
defp ensure_astarte_kv_store do
92-
keyspace_name = Realm.astarte_keyspace_name()
93-
94-
query =
95-
from t in "system_schema.tables",
96-
where:
97-
t.keyspace_name == ^keyspace_name and
98-
t.table_name == "kv_store",
99-
select: t.table_name
100-
101-
consistency = Consistency.domain_model(:read)
102-
103-
case Repo.safe_fetch_one(query, consistency: consistency) do
104-
{:ok, _item} ->
105-
:ok
106-
107-
{:error, :not_found} ->
108-
create_astarte_kv_store()
109-
110-
{:error, reason} ->
111-
Logger.warning("Error checking Astarte kv_store existence: #{inspect(reason)}.",
112-
tag: "database_error"
113-
)
114-
115-
{:error, reason}
116-
end
117-
end
118-
119-
defp create_astarte_kv_store do
120-
query = """
121-
CREATE TABLE #{Realm.astarte_keyspace_name()}.kv_store (
122-
group varchar,
123-
key varchar,
124-
value blob,
125-
126-
PRIMARY KEY ((group), key)
127-
);
128-
"""
129-
130-
with {:ok, %{rows: nil, num_rows: 1}} <- CSystem.execute_schema_change(query) do
131-
:ok
132-
end
133-
end
134-
13588
defp get_astarte_schema_version do
13689
get_schema_version(Realm.astarte_keyspace_name())
13790
end

apps/astarte_housekeeping/lib/astarte_housekeeping/realms/queries.ex

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ defmodule Astarte.Housekeeping.Realms.Queries do
218218
if opts[:async] do
219219
{:ok, _pid} =
220220
Task.start(fn ->
221-
do_create_realm(
221+
do_execute_realm_creation(
222222
realm_name,
223223
keyspace_name,
224224
public_key_pem,
@@ -230,7 +230,7 @@ defmodule Astarte.Housekeeping.Realms.Queries do
230230

231231
{:ok, :started}
232232
else
233-
do_create_realm(
233+
do_execute_realm_creation(
234234
realm_name,
235235
keyspace_name,
236236
public_key_pem,
@@ -241,6 +241,26 @@ defmodule Astarte.Housekeeping.Realms.Queries do
241241
end
242242
end
243243

244+
defp do_execute_realm_creation(
245+
realm_name,
246+
keyspace_name,
247+
public_key_pem,
248+
replication_map_str,
249+
device_limit,
250+
max_retention
251+
) do
252+
Repo.checkout(fn ->
253+
do_create_realm(
254+
realm_name,
255+
keyspace_name,
256+
public_key_pem,
257+
replication_map_str,
258+
device_limit,
259+
max_retention
260+
)
261+
end)
262+
end
263+
244264
defp do_create_realm(
245265
realm_name,
246266
keyspace_name,
@@ -1127,6 +1147,8 @@ defmodule Astarte.Housekeeping.Realms.Queries do
11271147
end
11281148

11291149
def initialize_database do
1150+
Logger.info("Starting Astarte keyspace initialization")
1151+
11301152
with :ok <- create_astarte_keyspace(),
11311153
:ok <- create_realms_table(),
11321154
:ok <- create_astarte_kv_store(),
@@ -1194,25 +1216,21 @@ defmodule Astarte.Housekeeping.Realms.Queries do
11941216
opts = [consistency: consistency]
11951217

11961218
case Repo.query(tablets_query, [], opts) do
1197-
{:ok, %{num_rows: 1}} ->
1219+
{:ok, _} ->
1220+
Logger.info("Astarte keyspace initialized")
11981221
:ok
11991222

1200-
{:ok, res} ->
1201-
"Unexpected ok result from database while creating astarte keyspace: #{inspect(res)}"
1202-
|> Logger.warning()
1203-
1204-
{:error, :astarte_keyspace_creation_failed}
1223+
{:error, %Xandra.Error{reason: :already_exists}} ->
1224+
:ok
12051225

12061226
_ ->
12071227
case Repo.query(base_query, [], opts) do
1208-
{:ok, %{num_rows: 1}} ->
1228+
{:ok, _} ->
1229+
Logger.info("Astarte keyspace initialized")
12091230
:ok
12101231

1211-
{:ok, res} ->
1212-
"Unexpected ok result from database while creating astarte keyspace: #{inspect(res)}"
1213-
|> Logger.warning()
1214-
1215-
{:error, :astarte_keyspace_creation_failed}
1232+
{:error, %Xandra.Error{reason: :already_exists}} ->
1233+
:ok
12161234

12171235
error ->
12181236
error
@@ -1230,10 +1248,18 @@ defmodule Astarte.Housekeeping.Realms.Queries do
12301248
"""
12311249

12321250
consistency = Consistency.domain_model(:write)
1251+
opts = [consistency: consistency]
12331252

1234-
with {:ok, %{rows: nil, num_rows: 1}} <-
1235-
Repo.query(query, [], consistency: consistency) do
1236-
:ok
1253+
case Repo.query(query, [], opts) do
1254+
{:ok, _} ->
1255+
Logger.info("Created Astarte realms table")
1256+
:ok
1257+
1258+
{:error, %Xandra.Error{reason: :already_exists}} ->
1259+
:ok
1260+
1261+
error ->
1262+
error
12371263
end
12381264
end
12391265

@@ -1249,10 +1275,18 @@ defmodule Astarte.Housekeeping.Realms.Queries do
12491275
"""
12501276

12511277
consistency = Consistency.domain_model(:write)
1278+
opts = [consistency: consistency]
12521279

1253-
with {:ok, %{rows: nil, num_rows: 1}} <-
1254-
Repo.query(query, [], consistency: consistency) do
1255-
:ok
1280+
case Repo.query(query, [], opts) do
1281+
{:ok, _} ->
1282+
Logger.info("Initialized Astarte KV Store")
1283+
:ok
1284+
1285+
{:error, %Xandra.Error{reason: :already_exists}} ->
1286+
:ok
1287+
1288+
error ->
1289+
error
12561290
end
12571291
end
12581292

@@ -1275,19 +1309,4 @@ defmodule Astarte.Housekeeping.Realms.Queries do
12751309

12761310
KvStore.insert(kv_store_map, opts)
12771311
end
1278-
1279-
def astarte_keyspace_existing? do
1280-
keyspace_name = Realm.astarte_keyspace_name()
1281-
1282-
query =
1283-
from k in "system_schema.keyspaces",
1284-
where: k.keyspace_name == ^keyspace_name,
1285-
select: count()
1286-
1287-
consistency = Consistency.domain_model(:read)
1288-
1289-
with {:ok, count} <- Repo.safe_fetch_one(query, consistency: consistency) do
1290-
{:ok, count > 0}
1291-
end
1292-
end
12931312
end

0 commit comments

Comments
 (0)