blob: 5d8953db1e830e64324b6aea2ec35efb60ba24dc [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 (
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010020 "git.monogon.dev/source/nexantic.git/core/generated/api"
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020021 "git.monogon.dev/source/nexantic.git/core/internal/common"
22
23 "errors"
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010024
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020025 "go.uber.org/zap"
26)
27
28var (
29 ErrConsensusAlreadyProvisioned = errors.New("consensus is already provisioned; make sure the data folder is empty")
30 ErrAlreadySetup = errors.New("node is already set up")
31 ErrNotInJoinMode = errors.New("node is not in the cluster join mode")
32 ErrTrustNotInitialized = errors.New("trust backend not initialized")
33 ErrStorageNotInitialized = errors.New("storage not initialized")
34)
35
36func (s *SmalltownNode) CurrentState() common.SmalltownState {
37 return s.state
38}
39
40func (s *SmalltownNode) GetJoinClusterToken() string {
41 return s.joinToken
42}
43
44func (s *SmalltownNode) SetupNewCluster(name string, externalHost string) error {
45 if s.state == common.StateConfigured {
46 return ErrAlreadySetup
47 }
48 dataPath, err := s.Storage.GetPathInPlace(common.PlaceData, "etcd")
49 if err == common.ErrNotInitialized {
50 return ErrStorageNotInitialized
51 } else if err != nil {
52 return err
53 }
54
55 s.logger.Info("Setting up a new cluster", zap.String("name", name), zap.String("external_host", externalHost))
56
57 s.logger.Info("Provisioning consensus")
58
59 // Make sure etcd is not yet provisioned
60 if s.Consensus.IsProvisioned() {
61 return ErrConsensusAlreadyProvisioned
62 }
63
64 // Spin up etcd
65 config := s.Consensus.GetConfig()
66 config.NewCluster = true
67 config.Name = name
68 config.ExternalHost = externalHost
69 config.DataDir = dataPath
70 s.Consensus.SetConfig(config)
71
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010072 if err := s.Consensus.PrecreateCA(); err != nil {
73 return err
74 }
75
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020076 err = s.Consensus.Start()
77 if err != nil {
78 return err
79 }
80
Lorenz Bruna4ea9d02019-10-31 11:40:30 +010081 if err := s.Consensus.InjectCA(); err != nil {
82 return err
83 }
84
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +020085 // Change system state
86 s.state = common.StateConfigured
87
88 s.logger.Info("New Cluster set up. Node is now fully operational")
89
90 return nil
91}
92
93func (s *SmalltownNode) EnterJoinClusterMode() error {
94 if s.state == common.StateConfigured {
95 return ErrAlreadySetup
96 }
97 s.state = common.StateClusterJoinMode
98
99 s.logger.Info("Node is now in the cluster join mode")
100
101 return nil
102}
103
Lorenz Bruna4ea9d02019-10-31 11:40:30 +0100104func (s *SmalltownNode) JoinCluster(name string, clusterString string, externalHost string, certs *api.ConsensusCertificates) error {
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +0200105 if s.state != common.StateClusterJoinMode {
106 return ErrNotInJoinMode
107 }
108
109 s.logger.Info("Joining cluster", zap.String("cluster", clusterString), zap.String("name", name))
110
111 err := s.SetupBackend()
112 if err != nil {
113 return err
114 }
115
116 config := s.Consensus.GetConfig()
117 config.Name = name
118 config.InitialCluster = clusterString
119 config.ExternalHost = externalHost
120 s.Consensus.SetConfig(config)
Lorenz Bruna4ea9d02019-10-31 11:40:30 +0100121 if err := s.Consensus.SetupCertificates(certs); err != nil {
122 return err
123 }
Hendrik Hofstadt0d7c91e2019-10-23 21:44:47 +0200124
125 // Start consensus
126 err = s.Consensus.Start()
127 if err != nil {
128 return err
129 }
130
131 s.state = common.StateConfigured
132
133 s.logger.Info("Joined cluster. Node is now syncing.")
134
135 return nil
136}