-
Notifications
You must be signed in to change notification settings - Fork 90
/
airgap_store.go
115 lines (95 loc) · 3.19 KB
/
airgap_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
package kotsstore
import (
"fmt"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/airgap/types"
airgaptypes "github.com/replicatedhq/kots/pkg/airgap/types"
"github.com/replicatedhq/kots/pkg/persistence"
"github.com/rqlite/gorqlite"
)
func (s *KOTSStore) GetPendingAirgapUploadApp() (*airgaptypes.PendingApp, error) {
db := persistence.MustGetDBSession()
query := `select id from app where install_state in ('airgap_upload_pending', 'airgap_upload_in_progress', 'airgap_upload_error') order by created_at desc limit 1`
rows, err := db.QueryOne(query)
if err != nil {
return nil, fmt.Errorf("failed to query pending app: %v: %v", err, rows.Err)
}
if !rows.Next() {
return nil, ErrNotFound
}
id := ""
if err := rows.Scan(&id); err != nil {
return nil, errors.Wrap(err, "failed to scan pending app id")
}
query = `select id, slug, name, license from app where id = ?`
rows, err = db.QueryOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{id},
})
if err != nil {
return nil, fmt.Errorf("failed to query app: %v: %v", err, rows.Err)
}
if !rows.Next() {
return nil, ErrNotFound
}
pendingApp := airgaptypes.PendingApp{}
if err := rows.Scan(&pendingApp.ID, &pendingApp.Slug, &pendingApp.Name, &pendingApp.LicenseData); err != nil {
return nil, errors.Wrap(err, "failed to scan pending app")
}
return &pendingApp, nil
}
func (s *KOTSStore) GetAirgapInstallStatus(appID string) (*airgaptypes.InstallStatus, error) {
db := persistence.MustGetDBSession()
query := `SELECT slug, install_state FROM app WHERE id = ?`
rows, err := db.QueryOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{appID},
})
if err != nil {
return nil, fmt.Errorf("failed to query: %v: %v", err, rows.Err)
}
if !rows.Next() {
return &types.InstallStatus{
InstallStatus: "not_installed",
CurrentMessage: "",
}, nil
}
var slug string
var installState gorqlite.NullString
if err := rows.Scan(&slug, &installState); err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
_, message, err := s.GetTaskStatus(fmt.Sprintf("airgap-install-slug-%s", slug))
if err != nil {
return nil, errors.Wrap(err, "failed to get task status")
}
status := &airgaptypes.InstallStatus{
InstallStatus: installState.String,
CurrentMessage: message,
}
return status, nil
}
func (s *KOTSStore) ResetAirgapInstallInProgress(appID string) error {
db := persistence.MustGetDBSession()
query := `update app set install_state = 'airgap_upload_in_progress' where id = ?`
wr, err := db.WriteOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{appID},
})
if err != nil {
return fmt.Errorf("failed to set update airgap install status: %v: %v", err, wr.Err)
}
return nil
}
func (s *KOTSStore) SetAppIsAirgap(appID string, isAirgap bool) error {
db := persistence.MustGetDBSession()
query := `update app set is_airgap = ? where id = ?`
wr, err := db.WriteOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{isAirgap, appID},
})
if err != nil {
return fmt.Errorf("failed to set app airgap flag: %v: %v", err, wr.Err)
}
return nil
}