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/node/setup.go b/core/internal/node/setup.go
new file mode 100644
index 0000000..28585dd
--- /dev/null
+++ b/core/internal/node/setup.go
@@ -0,0 +1,123 @@
+// 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 node
+
+import (
+ "git.monogon.dev/source/nexantic.git/core/internal/common"
+
+ "errors"
+ "go.uber.org/zap"
+)
+
+var (
+ ErrConsensusAlreadyProvisioned = errors.New("consensus is already provisioned; make sure the data folder is empty")
+ ErrAlreadySetup = errors.New("node is already set up")
+ ErrNotInJoinMode = errors.New("node is not in the cluster join mode")
+ ErrTrustNotInitialized = errors.New("trust backend not initialized")
+ ErrStorageNotInitialized = errors.New("storage not initialized")
+)
+
+func (s *SmalltownNode) CurrentState() common.SmalltownState {
+ return s.state
+}
+
+func (s *SmalltownNode) GetJoinClusterToken() string {
+ return s.joinToken
+}
+
+func (s *SmalltownNode) SetupNewCluster(name string, externalHost string) error {
+ if s.state == common.StateConfigured {
+ return ErrAlreadySetup
+ }
+ dataPath, err := s.Storage.GetPathInPlace(common.PlaceData, "etcd")
+ if err == common.ErrNotInitialized {
+ return ErrStorageNotInitialized
+ } else if err != nil {
+ return err
+ }
+
+ s.logger.Info("Setting up a new cluster", zap.String("name", name), zap.String("external_host", externalHost))
+
+ s.logger.Info("Provisioning consensus")
+
+ // Make sure etcd is not yet provisioned
+ if s.Consensus.IsProvisioned() {
+ return ErrConsensusAlreadyProvisioned
+ }
+
+ // Spin up etcd
+ config := s.Consensus.GetConfig()
+ config.NewCluster = true
+ config.Name = name
+ config.ExternalHost = externalHost
+ config.DataDir = dataPath
+ s.Consensus.SetConfig(config)
+
+ err = s.Consensus.Start()
+ if err != nil {
+ return err
+ }
+
+ // Change system state
+ s.state = common.StateConfigured
+
+ s.logger.Info("New Cluster set up. Node is now fully operational")
+
+ return nil
+}
+
+func (s *SmalltownNode) EnterJoinClusterMode() error {
+ if s.state == common.StateConfigured {
+ return ErrAlreadySetup
+ }
+ s.state = common.StateClusterJoinMode
+
+ s.logger.Info("Node is now in the cluster join mode")
+
+ return nil
+}
+
+func (s *SmalltownNode) JoinCluster(name string, clusterString string, externalHost string) error {
+ if s.state != common.StateClusterJoinMode {
+ return ErrNotInJoinMode
+ }
+
+ s.logger.Info("Joining cluster", zap.String("cluster", clusterString), zap.String("name", name))
+
+ err := s.SetupBackend()
+ if err != nil {
+ return err
+ }
+
+ config := s.Consensus.GetConfig()
+ config.Name = name
+ config.InitialCluster = clusterString
+ config.ExternalHost = externalHost
+ s.Consensus.SetConfig(config)
+
+ // Start consensus
+ err = s.Consensus.Start()
+ if err != nil {
+ return err
+ }
+
+ s.state = common.StateConfigured
+
+ s.logger.Info("Joined cluster. Node is now syncing.")
+
+ return nil
+}