Implement monorepo layout
Implemented the nexantic monorepo.
Smalltown code was moved to `core`. From now on all code will live in top level directories named after the projects with the exception for general purpose libraries which should go to `<lang>libs`.
General build and utility folders are underscore prefixed.
The repo name will from now on be rNXT (nexantic). I think this change makes sense since components in this repo will not all be part of Smalltown, the Smalltown brand has been claimed by Signon GmbH so we need to change it anyway and the longer we wait the harder it will be to change/move it.
Test Plan: Launched Smalltown using `./scripts/bin/bazel run //core/scripts:launch`
X-Origin-Diff: phab/D210
GitOrigin-RevId: fa5a7f08143d2ead2cb7206b4c63ab641794162c
diff --git a/core/internal/common/BUILD.bazel b/core/internal/common/BUILD.bazel
new file mode 100644
index 0000000..4312b88
--- /dev/null
+++ b/core/internal/common/BUILD.bazel
@@ -0,0 +1,20 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_library")
+
+go_library(
+ name = "go_default_library",
+ srcs = [
+ "grpc.go",
+ "service.go",
+ "setup.go",
+ "storage.go",
+ "util.go",
+ ],
+ importpath = "git.monogon.dev/source/nexantic.git/core/internal/common",
+ visibility = ["//core:__subpackages__"],
+ deps = [
+ "//core/api/api:go_default_library",
+ "//core/api/common:go_default_library",
+ "@org_golang_google_grpc//:go_default_library",
+ "@org_uber_go_zap//:go_default_library",
+ ],
+)
diff --git a/core/internal/common/grpc.go b/core/internal/common/grpc.go
new file mode 100644
index 0000000..5512a5c
--- /dev/null
+++ b/core/internal/common/grpc.go
@@ -0,0 +1,52 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// 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.
+
+package common
+
+import (
+ "git.monogon.dev/source/nexantic.git/core/generated/api"
+
+ "google.golang.org/grpc"
+)
+
+type (
+ SmalltownClient struct {
+ conn *grpc.ClientConn
+
+ Cluster api.ClusterManagementClient
+ Setup api.SetupServiceClient
+ }
+)
+
+func NewSmalltownAPIClient(address string) (*SmalltownClient, error) {
+ s := &SmalltownClient{}
+
+ conn, err := grpc.Dial(address, grpc.WithInsecure())
+ if err != nil {
+ return nil, err
+ }
+ s.conn = conn
+
+ // Setup all client connections
+ s.Cluster = api.NewClusterManagementClient(conn)
+ s.Setup = api.NewSetupServiceClient(conn)
+
+ return s, nil
+}
+
+func (s *SmalltownClient) Close() error {
+ return s.conn.Close()
+}
diff --git a/core/internal/common/service.go b/core/internal/common/service.go
new file mode 100644
index 0000000..3bdc1f9
--- /dev/null
+++ b/core/internal/common/service.go
@@ -0,0 +1,104 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// 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.
+
+package common
+
+import (
+ "errors"
+ "go.uber.org/zap"
+ "sync"
+)
+
+var (
+ ErrAlreadyRunning = errors.New("service is already running")
+ ErrNotRunning = errors.New("service is not running")
+)
+
+type (
+ // Service represents a subsystem of an application that can be used with a BaseService.
+ Service interface {
+ OnStart() error
+ OnStop() error
+ }
+
+ // BaseService implements utility functionality around a service.
+ BaseService struct {
+ impl Service
+ name string
+
+ Logger *zap.Logger
+
+ mutex sync.Mutex
+ running bool
+ }
+)
+
+func NewBaseService(name string, logger *zap.Logger, impl Service) *BaseService {
+ return &BaseService{
+ Logger: logger,
+ name: name,
+ impl: impl,
+ }
+}
+
+// Start starts the service. This is an atomic operation and should not be called on an already running service.
+func (b *BaseService) Start() error {
+ b.mutex.Lock()
+ defer b.mutex.Unlock()
+
+ if b.running {
+ return ErrAlreadyRunning
+ }
+
+ err := b.impl.OnStart()
+ if err != nil {
+ b.Logger.Error("Failed to start service", zap.String("service", b.name), zap.Error(err))
+ return err
+ }
+
+ b.running = true
+ b.Logger.Info("Started service", zap.String("service", b.name))
+ return nil
+}
+
+// Stop stops the service. THis is an atomic operation and should only be called on a running service.
+func (b *BaseService) Stop() error {
+ b.mutex.Lock()
+ defer b.mutex.Unlock()
+
+ if !b.running {
+ return ErrNotRunning
+ }
+
+ err := b.impl.OnStart()
+ if err != nil {
+ b.Logger.Error("Failed to stop service", zap.String("service", b.name), zap.Error(err))
+
+ return err
+ }
+
+ b.running = false
+ b.Logger.Info("Stopped service", zap.String("service", b.name))
+ return nil
+}
+
+// IsRunning returns whether the service is currently running.
+func (b *BaseService) IsRunning() bool {
+ b.mutex.Lock()
+ defer b.mutex.Unlock()
+
+ return b.running
+}
diff --git a/core/internal/common/setup.go b/core/internal/common/setup.go
new file mode 100644
index 0000000..fd70d0a
--- /dev/null
+++ b/core/internal/common/setup.go
@@ -0,0 +1,35 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// 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.
+
+package common
+
+type (
+ SetupService interface {
+ CurrentState() SmalltownState
+ GetJoinClusterToken() string
+ SetupNewCluster(name string, externalHost string) error
+ EnterJoinClusterMode() error
+ JoinCluster(name string, clusterString string, externalHost string) error
+ }
+
+ SmalltownState string
+)
+
+const (
+ StateSetupMode SmalltownState = "setup"
+ StateClusterJoinMode SmalltownState = "join"
+ StateConfigured SmalltownState = "configured"
+)
diff --git a/core/internal/common/storage.go b/core/internal/common/storage.go
new file mode 100644
index 0000000..caaa155
--- /dev/null
+++ b/core/internal/common/storage.go
@@ -0,0 +1,37 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// 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.
+
+package common
+
+import "errors"
+
+type DataPlace uint32
+
+const (
+ PlaceESP DataPlace = 0
+ PlaceData = 1
+)
+
+var (
+ // ErrNotInitialized will be returned when trying to access a place that's not yet initialized
+ ErrNotInitialized = errors.New("This place is not initialized")
+ // ErrUnknownPlace will be returned when trying to access a place that's not known
+ ErrUnknownPlace = errors.New("This place is not known")
+)
+
+type StorageManager interface {
+ GetPathInPlace(place DataPlace, path string) (string, error)
+}
diff --git a/core/internal/common/util.go b/core/internal/common/util.go
new file mode 100644
index 0000000..fc8a72b
--- /dev/null
+++ b/core/internal/common/util.go
@@ -0,0 +1,33 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// 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.
+
+package common
+
+import "git.monogon.dev/source/nexantic.git/core/generated/common"
+
+func MapToKVs(input map[string]string) []*common.KV {
+ kvs := make([]*common.KV, len(input))
+
+ i := 0
+ for key, item := range input {
+ kvs[i] = &common.KV{
+ Key: key,
+ Value: []byte(item),
+ }
+ }
+
+ return kvs
+}