Skip to content
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: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ RUN for binary in /app/bin/* /usr/bin/pg_dump /usr/bin/pg_restore /usr/bin/psql;
xargs -I % sh -c 'mkdir -p $(dirname deps%); cp % deps%;'; \
done

RUN git clone https://github.com/PlaceOS/models
# PPT-526: the tenancy migrations live on this branch until models #332 merges
RUN git clone --branch PPT-526-partner-client https://github.com/PlaceOS/models

# Create tmp directory with proper permissions
RUN rm -rf /tmp && mkdir -p /tmp && chmod 1777 /tmp
Expand Down
6 changes: 3 additions & 3 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ shards:

place_calendar:
git: https://github.com/placeos/calendar.git
version: 4.30.0
version: 4.30.1

placeos-log-backend:
git: https://github.com/place-labs/log-backend.git
version: 0.13.0

placeos-models:
placeos-models: # Overridden
git: https://github.com/placeos/models.git
version: 9.118.1
version: 9.118.1+git.commit.e21d7904b1d9fe12b7852e9e835a50489647e5f6

sam:
git: https://github.com/imdrasil/sam.cr.git
Expand Down
5 changes: 5 additions & 0 deletions shard.override.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
dependencies:
# PPT-526: build against the multi-tenancy data layer until models #332 merges
placeos-models:
github: placeos/models
branch: PPT-526-partner-client

retriable:
github: Sija/retriable.cr
db:
Expand Down
4 changes: 3 additions & 1 deletion src/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ namespace "create" do

desc "Creates a representative set of documents in PostgreSQL DB"
task "placeholders" do
PlaceOS::Tasks.create_placeholders
# placeholders belong to the staff organisation when one exists
staff = PlaceOS::Model::Organisation.where(partner_staff: true).first?
PlaceOS::Tasks.create_placeholders(staff.try(&.id))
end

desc "Creates an authority"
Expand Down
44 changes: 42 additions & 2 deletions src/tasks/entities.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,41 @@ module PlaceOS::Tasks::Entities
raise e
end

# The platform operator's partner and staff organisation. The first domain
# belongs to that organisation, so its admins have cluster reach once
# tenancy enforcement is on.
def create_management_organisation(
authority : Model::Authority,
partner_name : String = "PlaceOS",
) : Model::Organisation
partner = upsert_document(Model::Partner.where(management: true)) do
Log.info { {message: "creating management Partner", name: partner_name} }
Model::Partner.new(name: partner_name, description: "Platform operator (management partner)").tap do |new_partner|
new_partner.management = true
end
end

organisation = upsert_document(Model::Organisation.where(partner_id: partner.id, partner_staff: true)) do
Log.info { {message: "creating staff Organisation", name: authority.name} }
Model::Organisation.new(name: "#{partner_name} staff", description: "Staff of the management partner").tap do |new_org|
new_org.partner_id = partner.id
new_org.partner_staff = true
new_org.payer = Model::Organisation::PAYER_ORGANISATION
end
end

if authority.organisation_id.nil?
authority.organisation_id = organisation.id
authority.save!
Log.info { {message: "attached Authority to staff Organisation", authority: authority.id, organisation: organisation.id.to_s} }
end

organisation
rescue e
log_fail("Organisation", e)
raise e
end

def create_interface(
name : String,
folder_name : String,
Expand Down Expand Up @@ -141,7 +176,7 @@ module PlaceOS::Tasks::Entities
raise e
end

def create_placeholders
def create_placeholders(organisation_id : UUID? = nil)
version = UUID.random.to_s.split('-').first

private_repository_uri = "https://github.com/placeos/private-drivers"
Expand Down Expand Up @@ -201,6 +236,7 @@ module PlaceOS::Tasks::Entities
Model::Zone.new.tap do |zone|
zone.name = "Zone-#{tag}-#{version}"
zone.tags = Set{tag}
zone.organisation_id = organisation_id
end
end
end
Expand All @@ -213,7 +249,9 @@ module PlaceOS::Tasks::Entities
zones[2].save!

control_system = upsert_document(Model::ControlSystem.all) do
Model::ControlSystem.new(name: "System-#{version}")
Model::ControlSystem.new(name: "System-#{version}").tap do |sys|
sys.organisation_id = organisation_id
end
end

upsert_document(Model::Settings.for_parent(control_system.id.as(String))) do
Expand All @@ -227,6 +265,7 @@ module PlaceOS::Tasks::Entities
mod = upsert_document(Model::Module.where(driver_id: driver.id.as(String), control_system_id: control_system.id.as(String))) do
Model::Generator.module(driver: driver, control_system: control_system).tap do |new_module|
new_module.custom_name = "Module-#{version}"
new_module.organisation_id = organisation_id
end
end

Expand All @@ -241,6 +280,7 @@ module PlaceOS::Tasks::Entities
trigger_description = "An automatically generated Trigger."
new_trigger = Model::Trigger.new(name: trigger_name, description: trigger_description)
new_trigger.control_system = control_system
new_trigger.organisation_id = organisation_id
new_trigger
end

Expand Down
3 changes: 2 additions & 1 deletion src/tasks/initialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module PlaceOS::Tasks::Initialization

start_lock.synchronize do
authority = Entities.create_authority(name: application_base, domain: application_base, config: metrics_config)
organisation = Entities.create_management_organisation(authority)

Entities.create_user(authority: authority, name: username, email: email, password: password, sys_admin: true)
Entities.create_application(authority: authority, name: application_name, base: application_base)
Expand All @@ -53,7 +54,7 @@ module PlaceOS::Tasks::Initialization

unless PlaceOS::Tasks.production? || PlaceOS::SKIP_PLACEHOLDERS
Log.info { "creating placeholder documents" }
Entities.create_placeholders
Entities.create_placeholders(organisation.id)
end
end
end
Expand Down
Loading