File size: 5,220 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package graphqlapp

import (
	"context"
	"fmt"
	"sort"
	"time"

	"github.com/target/goalert/graphql2"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/swo"
	"github.com/target/goalert/swo/swogrp"
	"github.com/target/goalert/swo/swoinfo"
	"github.com/target/goalert/validation"
)

func (m *Mutation) SwoAction(ctx context.Context, action graphql2.SWOAction) (bool, error) {
	if m.SWO == nil {
		return false, validation.NewGenericError("not in SWO mode")
	}

	err := permission.LimitCheckAny(ctx, permission.Admin)
	if err != nil {
		return false, err
	}

	switch action {
	case graphql2.SWOActionReset:
		err = m.SWO.Reset(ctx)
	case graphql2.SWOActionExecute:
		err = m.SWO.StartExecute(ctx)
	default:
		return false, validation.NewGenericError("invalid SWO action")
	}

	return err == nil, err
}

// validateSWOGrpNode validates that the node has the correct DB urls.
func validateSWOGrpNode(s swo.Status, node swogrp.Node) error {
	if node.NewID != s.NextDBID {
		return fmt.Errorf("next-db-url is invalid")
	}
	if node.OldID != s.MainDBID {
		return fmt.Errorf("db-url is invalid")
	}

	return nil
}

// gwlSWOConnFromConnName maps a DB connection count to a GraphQL type.
func gqlSWOConnFromConnName(countInfo swoinfo.ConnCount) (nodeID string, conn graphql2.SWOConnection) {
	var connType, version string
	idStr := "unknown-" + countInfo.Name
	info, _ := swo.ParseConnInfo(countInfo.Name)
	if info != nil {
		version = info.Version
		connType = string(info.Type)
		idStr = info.ID.String()
	}

	return idStr, graphql2.SWOConnection{
		Name:    countInfo.Name,
		IsNext:  countInfo.IsNext,
		Version: version,
		Type:    string(connType),
		Count:   countInfo.Count,
	}
}

// validateNodeConnections ensures that the node has the correct number of connections, identified as the correct & expected type(s).
func validateNodeConnections(n graphql2.SWONode) error {
	if len(n.Connections) == 0 {
		return fmt.Errorf("node is not connected to any DB")
	}

	version := n.Connections[0].Version
	for _, conn := range n.Connections {
		if conn.Version != version {
			return fmt.Errorf("node has multiple versions: %s and %s", version, conn.Version)
		}

		if len(conn.Type) != 1 {
			return fmt.Errorf("invalid connection type: %s", conn.Type)
		}

		if conn.IsNext != swo.ConnType(conn.Type[0]).IsNext() {
			return fmt.Errorf("node has invalid connection type: %s", conn.Type)
		}
	}

	return nil
}

// gqlStateFromSWOState maps a SWO state to a GraphQL type.
func gqlStateFromSWOState(st swogrp.ClusterState) (graphql2.SWOState, error) {
	switch st {
	case swogrp.ClusterStateUnknown:
		return graphql2.SWOStateUnknown, nil
	case swogrp.ClusterStateResetting:
		return graphql2.SWOStateResetting, nil
	case swogrp.ClusterStateIdle:
		return graphql2.SWOStateIdle, nil
	case swogrp.ClusterStateSyncing:
		return graphql2.SWOStateSyncing, nil
	case swogrp.ClusterStatePausing:
		return graphql2.SWOStatePausing, nil
	case swogrp.ClusterStateExecuting:
		return graphql2.SWOStateExecuting, nil
	case swogrp.ClusterStateDone:
		return graphql2.SWOStateDone, nil
	}

	return "", fmt.Errorf("invalid state: %d", st)
}

// gqlSWOStatus maps a SWO status and connection list to a GraphQL type.
func gqlSWOStatus(s swo.Status, conns []swoinfo.ConnCount) (*graphql2.SWOStatus, error) {
	nodes := make(map[string]*graphql2.SWONode)

	// sort connections by name to ensure consistent ordering
	sort.Slice(conns, func(i, j int) bool {
		return conns[i].Name < conns[j].Name
	})
	// map connections to nodes
	for _, conn := range conns {
		idStr, c := gqlSWOConnFromConnName(conn)

		n := nodes[idStr]
		if n == nil {
			n = &graphql2.SWONode{ID: idStr}
			nodes[idStr] = n
		}
		n.Connections = append(n.Connections, c)
	}

	// update nodes from switchover_log and validate
	for _, node := range s.Nodes {
		n := nodes[node.ID.String()]
		if n == nil {
			n = &graphql2.SWONode{ID: node.ID.String()}
			nodes[node.ID.String()] = n
		}
		n.IsLeader = node.ID == s.LeaderID
		n.CanExec = node.CanExec
		n.Uptime = time.Since(node.StartedAt).Truncate(time.Second).String()

		err := validateSWOGrpNode(s, node)
		if err != nil {
			n.ConfigError = err.Error()
			continue
		}

		err = validateNodeConnections(*n)
		if err != nil {
			n.ConfigError = err.Error()
			continue
		}
	}

	// convert to list, sort by ID (for consistency)
	var nodeList []graphql2.SWONode
	for _, n := range nodes {
		nodeList = append(nodeList, *n)
	}
	sort.Slice(nodeList, func(i, j int) bool {
		return nodeList[i].ID < nodeList[j].ID
	})

	// map state to GraphQL type
	state, err := gqlStateFromSWOState(s.State)
	if err != nil {
		return nil, err
	}

	return &graphql2.SWOStatus{
		State: state,

		LastStatus: s.LastStatus,
		LastError:  s.LastError,
		Nodes:      nodeList,

		NextDBVersion: s.NextDBVersion,
		MainDBVersion: s.MainDBVersion,
	}, nil
}

func (q *Query) SwoStatus(ctx context.Context) (*graphql2.SWOStatus, error) {
	if q.SWO == nil {
		return nil, validation.NewGenericError("not in SWO mode")
	}

	err := permission.LimitCheckAny(ctx, permission.Admin)
	if err != nil {
		return nil, err
	}

	conns, err := q.SWO.ConnInfo(ctx)
	if err != nil {
		return nil, err
	}

	return gqlSWOStatus(q.SWO.Status(), conns)
}