blob: 28585dddba870e074f379a28f4e08d5fe4bc3fae [file] [log] [blame]
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package node
18
19import (
20 "git.monogon.dev/source/nexantic.git/core/internal/common"
21
22 "errors"
23 "go.uber.org/zap"
24)
25
26var (
27 ErrConsensusAlreadyProvisioned = errors.New("consensus is already provisioned; make sure the data folder is empty")
28 ErrAlreadySetup = errors.New("node is already set up")
29 ErrNotInJoinMode = errors.New("node is not in the cluster join mode")
30 ErrTrustNotInitialized = errors.New("trust backend not initialized")
31 ErrStorageNotInitialized = errors.New("storage not initialized")
32)
33
34func (s *SmalltownNode) CurrentState() common.SmalltownState {
35 return s.state
36}
37
38func (s *SmalltownNode) GetJoinClusterToken() string {
39 return s.joinToken
40}
41
42func (s *SmalltownNode) SetupNewCluster(name string, externalHost string) error {
43 if s.state == common.StateConfigured {
44 return ErrAlreadySetup
45 }
46 dataPath, err := s.Storage.GetPathInPlace(common.PlaceData, "etcd")
47 if err == common.ErrNotInitialized {
48 return ErrStorageNotInitialized
49 } else if err != nil {
50 return err
51 }
52
53 s.logger.Info("Setting up a new cluster", zap.String("name", name), zap.String("external_host", externalHost))
54
55 s.logger.Info("Provisioning consensus")
56
57 // Make sure etcd is not yet provisioned
58 if s.Consensus.IsProvisioned() {
59 return ErrConsensusAlreadyProvisioned
60 }
61
62 // Spin up etcd
63 config := s.Consensus.GetConfig()
64 config.NewCluster = true
65 config.Name = name
66 config.ExternalHost = externalHost
67 config.DataDir = dataPath
68 s.Consensus.SetConfig(config)
69
70 err = s.Consensus.Start()
71 if err != nil {
72 return err
73 }
74
75 // Change system state
76 s.state = common.StateConfigured
77
78 s.logger.Info("New Cluster set up. Node is now fully operational")
79
80 return nil
81}
82
83func (s *SmalltownNode) EnterJoinClusterMode() error {
84 if s.state == common.StateConfigured {
85 return ErrAlreadySetup
86 }
87 s.state = common.StateClusterJoinMode
88
89 s.logger.Info("Node is now in the cluster join mode")
90
91 return nil
92}
93
94func (s *SmalltownNode) JoinCluster(name string, clusterString string, externalHost string) error {
95 if s.state != common.StateClusterJoinMode {
96 return ErrNotInJoinMode
97 }
98
99 s.logger.Info("Joining cluster", zap.String("cluster", clusterString), zap.String("name", name))
100
101 err := s.SetupBackend()
102 if err != nil {
103 return err
104 }
105
106 config := s.Consensus.GetConfig()
107 config.Name = name
108 config.InitialCluster = clusterString
109 config.ExternalHost = externalHost
110 s.Consensus.SetConfig(config)
111
112 // Start consensus
113 err = s.Consensus.Start()
114 if err != nil {
115 return err
116 }
117
118 s.state = common.StateConfigured
119
120 s.logger.Info("Joined cluster. Node is now syncing.")
121
122 return nil
123}