forked from juju/juju
/
updateseries.go
206 lines (182 loc) · 6.55 KB
/
updateseries.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
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
206
// Copyright 2021 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package application
import (
"context"
"fmt"
"github.com/juju/charm/v9"
"github.com/juju/errors"
"github.com/juju/names/v4"
apiservererrors "github.com/DavinZhang/juju/apiserver/errors"
"github.com/DavinZhang/juju/apiserver/params"
"github.com/DavinZhang/juju/charmhub"
"github.com/DavinZhang/juju/charmhub/transport"
corecharm "github.com/DavinZhang/juju/core/charm"
coreseries "github.com/DavinZhang/juju/core/series"
)
// CharmhubClient represents a way for querying the charmhub api for information
// about the application charm.
type CharmhubClient interface {
Refresh(ctx context.Context, config charmhub.RefreshConfig) ([]transport.RefreshResponse, error)
}
// UpdateSeries defines an interface for interacting with updating a series.
type UpdateSeries interface {
// UpdateSeries attempts to update an application series for deploying new
// units.
UpdateSeries(string, string, bool) error
}
// UpdateSeriesState defines a common set of functions for retrieving state
// objects.
type UpdateSeriesState interface {
// Application returns a list of all the applications for a
// given machine. This includes all the subordinates.
Application(string) (Application, error)
}
// UpdateSeriesValidator defines an application validator.
type UpdateSeriesValidator interface {
// ValidateApplication attempts to validate an application for
// a given series. Using force to allow the overriding of the error to
// ensure all application validate.
//
// I do question if you actually need to validate anything if force is
// employed here?
ValidateApplication(application Application, series string, force bool) error
}
// UpdateSeriesAPI provides the update series API facade for any given version.
// It is expected that any API parameter changes should be performed before
// entering the API.
type UpdateSeriesAPI struct {
state UpdateSeriesState
validator UpdateSeriesValidator
}
// NewUpdateSeriesAPI creates a new UpdateSeriesAPI
func NewUpdateSeriesAPI(
state UpdateSeriesState,
validator UpdateSeriesValidator,
) *UpdateSeriesAPI {
return &UpdateSeriesAPI{
state: state,
validator: validator,
}
}
func (a *UpdateSeriesAPI) UpdateSeries(tag, series string, force bool) error {
if series == "" {
return errors.BadRequestf("series missing from args")
}
applicationTag, err := names.ParseApplicationTag(tag)
if err != nil {
return errors.Trace(err)
}
app, err := a.state.Application(applicationTag.Id())
if err != nil {
return errors.Trace(err)
}
if !app.IsPrincipal() {
return ¶ms.Error{
Message: fmt.Sprintf("%q is a subordinate application, update-series not supported", applicationTag.Id()),
Code: params.CodeNotSupported,
}
}
if err := a.validator.ValidateApplication(app, series, force); err != nil {
return errors.Trace(err)
}
return app.UpdateApplicationSeries(series, force)
}
type updateSeriesValidator struct {
localValidator UpdateSeriesValidator
removeValidator UpdateSeriesValidator
}
func makeUpdateSeriesValidator(client CharmhubClient) updateSeriesValidator {
return updateSeriesValidator{
localValidator: stateSeriesValidator{},
removeValidator: charmhubSeriesValidator{
client: client,
},
}
}
func (s updateSeriesValidator) ValidateApplication(app Application, series string, force bool) error {
// This is not a charmhub charm, so we can fallback to querying state
// for the supported series.
if origin := app.CharmOrigin(); origin == nil || !corecharm.CharmHub.Matches(origin.Source) {
return s.localValidator.ValidateApplication(app, series, force)
}
return s.removeValidator.ValidateApplication(app, series, force)
}
// stateSeriesValidator validates an application using the state (database)
// version of the charm.
// NOTE: stateSeriesValidator also exists in apiserver/facades/client/machinemanager/upgrade_series.go,
// When making changes here, review the copy for required changes as well.
type stateSeriesValidator struct{}
// ValidateApplications attempts to validate a series of applications for
// a given series.
func (s stateSeriesValidator) ValidateApplication(application Application, series string, force bool) error {
ch, _, err := application.Charm()
if err != nil {
return errors.Trace(err)
}
supportedSeries, err := corecharm.ComputedSeries(ch)
if err != nil {
return errors.Trace(err)
}
if len(supportedSeries) == 0 {
supportedSeries = append(supportedSeries, ch.URL().Series)
}
_, seriesSupportedErr := charm.SeriesForCharm(series, supportedSeries)
if seriesSupportedErr != nil && !force {
// TODO (stickupkid): Once all commands are placed in this API, we
// should relocate these to the API server.
return apiservererrors.NewErrIncompatibleSeries(supportedSeries, series, ch.String())
}
return nil
}
// NOTE: charmhubSeriesValidator also exists in apiserver/facades/client/machinemanager/upgrade_series.go,
// When making changes here, review the copy for required changes as well.
type charmhubSeriesValidator struct {
client CharmhubClient
}
// ValidateApplications attempts to validate a series of applications for
// a given series.
func (s charmhubSeriesValidator) ValidateApplication(application Application, series string, force bool) error {
// We can be assured that the charm origin is not nil, because we
// guarded against it before.
origin := application.CharmOrigin()
rev := origin.Revision
if rev == nil {
return errors.Errorf("no revision found for application %q", application.Name())
}
cfg, err := charmhub.DownloadOneFromRevision(origin.ID, *rev)
if err != nil {
return errors.Trace(err)
}
refreshResp, err := s.client.Refresh(context.TODO(), cfg)
if err != nil {
return errors.Trace(err)
}
if len(refreshResp) != 1 {
return errors.Errorf("unexpected number of responses %d for applications 1", len(refreshResp))
}
for _, resp := range refreshResp {
if err := resp.Error; err != nil && !force {
return errors.Annotatef(err, "unable to locate application with series %s", series)
}
}
// DownloadOneFromRevision does not take a base, however the response contains the bases
// supported by the given revision. Validate against provided series.
channelToValidate, err := coreseries.SeriesVersion(series)
if err != nil {
return errors.Trace(err)
}
for _, resp := range refreshResp {
var found bool
for _, base := range resp.Entity.Bases {
if channelToValidate == base.Channel || force {
found = true
break
}
}
if !found {
return errors.Errorf("charm %q does not support %s, force not used", resp.Name, series)
}
}
return nil
}