• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / deployments / 898138042

pending completion
898138042

Pull #867

gitlab-ci

kjaskiewiczz
chore: check database version only once

Signed-off-by: Krzysztof Jaskiewicz <krzysztof.jaskiewicz@northern.tech>
Pull Request #867: feat: make releases persistent in the database

94 of 258 new or added lines in 5 files covered. (36.43%)

55 existing lines in 5 files now uncovered.

7187 of 9193 relevant lines covered (78.18%)

34.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.84
/server.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package main
16

17
import (
18
        "context"
19
        "encoding/base64"
20
        "net/http"
21
        "strings"
22
        "time"
23

24
        "github.com/ant0ine/go-json-rest/rest"
25
        "github.com/pkg/errors"
26

27
        "github.com/mendersoftware/go-lib-micro/config"
28

29
        api "github.com/mendersoftware/deployments/api/http"
30
        "github.com/mendersoftware/deployments/app"
31
        "github.com/mendersoftware/deployments/client/reporting"
32
        dconfig "github.com/mendersoftware/deployments/config"
33
        "github.com/mendersoftware/deployments/storage"
34
        "github.com/mendersoftware/deployments/storage/azblob"
35
        "github.com/mendersoftware/deployments/storage/manager"
36
        "github.com/mendersoftware/deployments/storage/s3"
37
        mstore "github.com/mendersoftware/deployments/store/mongo"
38
)
39

40
func SetupS3(ctx context.Context, defaultOptions *s3.Options) (storage.ObjectStorage, error) {
1✔
41
        c := config.Config
1✔
42

1✔
43
        // Copy / merge defaultOptions
1✔
44
        options := s3.NewOptions(defaultOptions).
1✔
45
                SetForcePathStyle(c.GetBool(dconfig.SettingAwsS3ForcePathStyle)).
1✔
46
                SetUseAccelerate(c.GetBool(dconfig.SettingAwsS3UseAccelerate))
1✔
47

1✔
48
        // Compute the buffer size
1✔
49
        bucket := c.GetString(dconfig.SettingStorageBucket)
1✔
50

1✔
51
        // The following parameters falls back on AWS_* environment if not set
1✔
52
        if c.IsSet(dconfig.SettingAwsS3Region) {
2✔
53
                options.SetRegion(c.GetString(dconfig.SettingAwsS3Region))
1✔
54
        }
1✔
55
        if c.IsSet(dconfig.SettingsAwsAuth) ||
1✔
56
                (c.IsSet(dconfig.SettingAwsAuthKeyId) &&
1✔
57
                        c.IsSet(dconfig.SettingAwsAuthSecret)) {
2✔
58
                options.SetStaticCredentials(
1✔
59
                        c.GetString(dconfig.SettingAwsAuthKeyId),
1✔
60
                        c.GetString(dconfig.SettingAwsAuthSecret),
1✔
61
                        c.GetString(dconfig.SettingAwsAuthToken),
1✔
62
                )
1✔
63
        }
1✔
64
        if c.IsSet(dconfig.SettingAwsURI) {
2✔
65
                options.SetURI(c.GetString(dconfig.SettingAwsURI))
1✔
66
        }
1✔
67
        if c.IsSet(dconfig.SettingAwsExternalURI) {
2✔
68
                options.SetExternalURI(c.GetString(dconfig.SettingAwsExternalURI))
1✔
69
        }
1✔
70

71
        storage, err := s3.New(ctx, bucket, options)
1✔
72
        return storage, err
1✔
73
}
74

75
func SetupBlobStorage(
76
        ctx context.Context,
77
        defaultOptions *azblob.Options,
UNCOV
78
) (storage.ObjectStorage, error) {
×
UNCOV
79
        c := config.Config
×
UNCOV
80

×
81
        // Copy / merge options
×
82
        options := azblob.NewOptions(defaultOptions)
×
83

×
84
        if c.IsSet(dconfig.SettingAzureConnectionString) {
×
85
                options.SetConnectionString(c.GetString(dconfig.SettingAzureConnectionString))
×
86
        } else if c.IsSet(dconfig.SettingAzureSharedKeyAccount) &&
×
87
                c.IsSet(dconfig.SettingAzureSharedKeyAccountKey) {
×
88
                creds := azblob.SharedKeyCredentials{
×
89
                        AccountName: c.GetString(dconfig.SettingAzureSharedKeyAccount),
×
90
                        AccountKey:  c.GetString(dconfig.SettingAzureSharedKeyAccountKey),
×
91
                }
×
92
                if c.IsSet(dconfig.SettingAzureSharedKeyURI) {
×
93
                        uri := c.GetString(dconfig.SettingAzureSharedKeyURI)
×
94
                        creds.URI = &uri
×
95
                }
×
96
                options.SetSharedKey(creds)
×
97
        }
98
        return azblob.New(ctx, c.GetString(dconfig.SettingStorageBucket), options)
×
99
}
100

101
func SetupObjectStorage(ctx context.Context) (objManager storage.ObjectStorage, err error) {
1✔
102
        c := config.Config
1✔
103

1✔
104
        // Calculate s3 multipart buffer size: the minimum buffer size that
1✔
105
        // covers the maximum image size aligned to multiple of 5MiB.
1✔
106
        maxImageSize := c.GetInt64(dconfig.SettingStorageMaxImageSize)
1✔
107
        bufferSize := (((maxImageSize - 1) /
1✔
108
                (s3.MultipartMaxParts * s3.MultipartMinSize)) + 1) *
1✔
109
                s3.MultipartMinSize
1✔
110
        var (
1✔
111
                s3Options = s3.NewOptions().
1✔
112
                                SetContentType(app.ArtifactContentType).
1✔
113
                                SetBufferSize(int(bufferSize))
1✔
114
                azOptions = azblob.NewOptions().
1✔
115
                                SetContentType(app.ArtifactContentType)
1✔
116
        )
1✔
117
        var defaultStorage storage.ObjectStorage
1✔
118
        switch defType := c.GetString(dconfig.SettingDefaultStorage); defType {
1✔
119
        case dconfig.StorageTypeAWS:
1✔
120
                defaultStorage, err = SetupS3(ctx, s3Options)
1✔
UNCOV
121
        case dconfig.StorageTypeAzure:
×
UNCOV
122
                defaultStorage, err = SetupBlobStorage(ctx, azOptions)
×
UNCOV
123
        default:
×
124
                err = errors.Errorf(
×
125
                        `storage type must be one of %q or %q, received value %q`,
×
126
                        dconfig.StorageTypeAWS, dconfig.StorageTypeAzure, defType,
×
127
                )
×
128
        }
129
        if err != nil {
1✔
130
                return nil, err
×
UNCOV
131
        }
×
132
        return manager.New(ctx, defaultStorage, s3Options, azOptions)
1✔
133
}
134

135
func RunServer(ctx context.Context) error {
1✔
136
        c := config.Config
1✔
137
        dbClient, err := mstore.NewMongoClient(ctx, c)
1✔
138
        if err != nil {
1✔
UNCOV
139
                return err
×
UNCOV
140
        }
×
141
        defer func() {
1✔
142
                _ = dbClient.Disconnect(context.Background())
×
143
        }()
×
144

145
        ds := mstore.NewDataStoreMongoWithClient(dbClient)
1✔
146

1✔
147
        // Storage Layer
1✔
148
        objStore, err := SetupObjectStorage(ctx)
1✔
149
        if err != nil {
1✔
UNCOV
150
                return errors.WithMessage(err, "main: failed to setup storage client")
×
UNCOV
151
        }
×
152

153
        app := app.NewDeployments(ds, objStore)
1✔
154
        if addr := c.GetString(dconfig.SettingReportingAddr); addr != "" {
2✔
155
                c := reporting.NewClient(addr)
1✔
156
                app = app.WithReporting(c)
1✔
157
        }
1✔
158

159
        // Setup API Router configuration
160
        base64Repl := strings.NewReplacer("-", "+", "_", "/", "=", "")
1✔
161
        expireSec := c.GetDuration(dconfig.SettingPresignExpireSeconds)
1✔
162
        apiConf := api.NewConfig().
1✔
163
                SetPresignExpire(time.Second * expireSec).
1✔
164
                SetPresignHostname(c.GetString(dconfig.SettingPresignHost)).
1✔
165
                SetPresignScheme(c.GetString(dconfig.SettingPresignScheme)).
1✔
166
                SetMaxImageSize(c.GetInt64(dconfig.SettingStorageMaxImageSize)).
1✔
167
                SetEnableDirectUpload(c.GetBool(dconfig.SettingStorageEnableDirectUpload)).
1✔
168
                SetEnableDirectUploadSkipVerify(c.GetBool(dconfig.SettingStorageDirectUploadSkipVerify))
1✔
169
        if key, err := base64.RawStdEncoding.DecodeString(
1✔
170
                base64Repl.Replace(
1✔
171
                        c.GetString(dconfig.SettingPresignSecret),
1✔
172
                ),
1✔
173
        ); err == nil {
2✔
174
                apiConf.SetPresignSecret(key)
1✔
175
        }
1✔
176
        router, err := api.NewRouter(ctx, app, ds, apiConf)
1✔
177
        if err != nil {
1✔
UNCOV
178
                return err
×
UNCOV
179
        }
×
180

181
        api := rest.NewApi()
1✔
182
        SetupMiddleware(c, api)
1✔
183
        api.SetApp(router)
1✔
184

1✔
185
        listen := c.GetString(dconfig.SettingListen)
1✔
186

1✔
187
        if c.IsSet(dconfig.SettingHttps) {
1✔
UNCOV
188

×
UNCOV
189
                cert := c.GetString(dconfig.SettingHttpsCertificate)
×
UNCOV
190
                key := c.GetString(dconfig.SettingHttpsKey)
×
191

×
192
                return http.ListenAndServeTLS(listen, cert, key, api.MakeHandler())
×
193
        }
×
194

195
        return http.ListenAndServe(listen, api.MakeHandler())
1✔
196
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc