-
Notifications
You must be signed in to change notification settings - Fork 88
/
cluster_store.go
155 lines (124 loc) · 4.3 KB
/
cluster_store.go
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
package kotsstore
import (
"database/sql"
"fmt"
"time"
"github.com/gosimple/slug"
"github.com/pkg/errors"
downstreamtypes "github.com/replicatedhq/kots/pkg/api/downstream/types"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/persistence"
"github.com/replicatedhq/kots/pkg/rand"
"go.uber.org/zap"
)
func (s *KOTSStore) ListClusters() ([]*downstreamtypes.Downstream, error) {
db := persistence.MustGetDBSession()
query := `select id, slug, title, snapshot_schedule, snapshot_ttl from cluster` // TODO the current sequence
rows, err := db.Query(query)
if err != nil {
return nil, errors.Wrap(err, "failed to query clusters")
}
defer rows.Close()
clusters := []*downstreamtypes.Downstream{}
for rows.Next() {
cluster := downstreamtypes.Downstream{}
var snapshotSchedule sql.NullString
var snapshotTTL sql.NullString
if err := rows.Scan(&cluster.ClusterID, &cluster.ClusterSlug, &cluster.Name, &snapshotSchedule, &snapshotTTL); err != nil {
return nil, errors.Wrap(err, "failed to scan row")
}
cluster.SnapshotSchedule = snapshotSchedule.String
cluster.SnapshotTTL = snapshotTTL.String
clusters = append(clusters, &cluster)
}
return clusters, nil
}
func (s *KOTSStore) GetClusterIDFromSlug(slug string) (string, error) {
db := persistence.MustGetDBSession()
query := `select id from cluster where slug = $1`
row := db.QueryRow(query, slug)
var clusterID string
if err := row.Scan(&clusterID); err != nil {
return "", errors.Wrap(err, "failed to scan")
}
return clusterID, nil
}
func (s *KOTSStore) GetClusterIDFromDeployToken(deployToken string) (string, error) {
db := persistence.MustGetDBSession()
query := `select id from cluster where token = $1`
row := db.QueryRow(query, deployToken)
var clusterID string
if err := row.Scan(&clusterID); err != nil {
return "", errors.Wrap(err, "failed to scan")
}
return clusterID, nil
}
func (s *KOTSStore) CreateNewCluster(userID string, isAllUsers bool, title string, token string) (string, error) {
clusterID := rand.StringWithCharset(32, rand.LOWER_CASE)
clusterSlug := slug.Make(title)
db := persistence.MustGetDBSession()
foundUniqueSlug := false
for i := 0; !foundUniqueSlug; i++ {
slugProposal := clusterSlug
if i > 0 {
slugProposal = fmt.Sprintf("%s-%d", clusterSlug, i)
}
query := `select count(1) as count from cluster where slug = $1`
row := db.QueryRow(query, slugProposal)
var count int
if err := row.Scan(&count); err != nil {
return "", errors.Wrap(err, "failed to scan")
}
if count == 0 {
clusterSlug = slugProposal
foundUniqueSlug = true
break
}
}
if token == "" {
token = rand.StringWithCharset(32, rand.LOWER_CASE)
}
tx, err := db.Begin()
if err != nil {
return "", errors.Wrap(err, "failed to begin transaction")
}
defer tx.Rollback()
query := `insert into cluster (id, title, slug, created_at, updated_at, cluster_type, is_all_users, token) values ($1, $2, $3, $4, $5, $6, $7, $8)`
_, err = db.Exec(query, clusterID, title, clusterSlug, time.Now(), nil, "ship", isAllUsers, token)
if err != nil {
return "", errors.Wrap(err, "failed to insert cluster row")
}
if userID != "" {
query := `insert into user_cluster (user_id, cluster_id) values ($1, $2)`
_, err := db.Exec(query, userID, clusterID)
if err != nil {
return "", errors.Wrap(err, "failed to insert user_cluster row")
}
}
if err := tx.Commit(); err != nil {
return "", errors.Wrap(err, "failed to commit transaction")
}
return clusterID, nil
}
func (s *KOTSStore) SetInstanceSnapshotTTL(clusterID string, snapshotTTL string) error {
logger.Debug("Setting instance snapshot TTL",
zap.String("clusterID", clusterID))
db := persistence.MustGetDBSession()
query := `update cluster set snapshot_ttl = $1 where id = $2`
_, err := db.Exec(query, snapshotTTL, clusterID)
if err != nil {
return errors.Wrap(err, "failed to exec db query")
}
return nil
}
func (s *KOTSStore) SetInstanceSnapshotSchedule(clusterID string, snapshotSchedule string) error {
logger.Debug("Setting instance snapshot Schedule",
zap.String("clusterID", clusterID))
db := persistence.MustGetDBSession()
query := `update cluster set snapshot_schedule = $1 where id = $2`
_, err := db.Exec(query, snapshotSchedule, clusterID)
if err != nil {
return errors.Wrap(err, "failed to exec db query")
}
return nil
}