Debian Codegraph @

pkg/server/services.go

raw
1 // Copyright 2024 The Archivista Contributors
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 // A note: this follows a pattern followed by network service mesh.
16 // The pattern was copied from the Network Service Mesh Project
17 // and modified for use here. The original code was published under the
18 // Apache License V2.
19
20 package server
21
22 import (
23 "context"
24 "fmt"
25 "strings"
26 "time"
27
28 "github.com/in-toto/archivista/pkg/artifactstore"
29 "github.com/in-toto/archivista/pkg/config"
30 "github.com/in-toto/archivista/pkg/metadatastorage/sqlstore"
31 "github.com/in-toto/archivista/pkg/objectstorage/blobstore"
32 "github.com/in-toto/archivista/pkg/objectstorage/filestore"
33 "github.com/in-toto/archivista/pkg/publisherstore"
34 "github.com/minio/minio-go/v7/pkg/credentials"
35 "github.com/sirupsen/logrus"
36 )
37
38 // Service is the interface for the Archivista service
39 type Service interface {
40 Setup() (Server, error)
41 GetConfig() *config.Config
42 GetFileStoreCh() chan error
43 GetSQLStoreCh() chan error
44 }
45
46 // ArchivistaService is the implementation of the Archivista service
47 type ArchivistaService struct {
48 Ctx context.Context // context for the service
49 Cfg *config.Config // configuration for the service (if none it uses environment variables)
50 fileStoreCh <-chan error
51 sqlStoreCh <-chan error
52 }
53
54 // Setup Archivista Service
55 func (a *ArchivistaService) Setup() (*Server, error) {
56 var (
57 level logrus.Level
58 err error
59 sqlStore *sqlstore.Store
60 fileStore StorerGetter
61 publisherStore []publisherstore.Publisher
62 )
63 serverOpts := make([]Option, 0)
64
65 startTime := time.Now()
66 now := time.Now()
67 if a.Cfg == nil {
68
69 logrus.Infof("executing: get config from environment (time since start: %s)", time.Since(startTime))
70
71 a.Cfg = new(config.Config)
72 if err := a.Cfg.Process(); err != nil {
73 logrus.Fatal(err)
74 }
75 level, err = logrus.ParseLevel(a.Cfg.LogLevel)
76 if err != nil {
77 logrus.Fatalf("invalid log level %s", a.Cfg.LogLevel)
78 }
79 logrus.WithField("duration", time.Since(now)).Infof("completed phase: get config from environment")
80 } else {
81 logrus.Infof("executing: load given config (time since start: %s)", time.Since(startTime))
82 level, err = logrus.ParseLevel(a.Cfg.LogLevel)
83 if err != nil {
84 logrus.Fatalf("invalid log level %s", a.Cfg.LogLevel)
85 }
86 logrus.WithField("duration", time.Since(now)).Infof("completed phase: load given config")
87 }
88 logrus.SetLevel(level)
89
90 // ********************************************************************************
91 logrus.Infof("executing phase: initializing storage clients (time since start: %s)", time.Since(startTime))
92 // ********************************************************************************
93 now = time.Now()
94 fileStore, a.fileStoreCh, err = a.initObjectStore()
95 if err != nil {
96 logrus.Fatalf("could not create object store: %+v", err)
97 }
98 serverOpts = append(serverOpts, WithObjectStore(fileStore))
99
100 if a.Cfg.EnableSQLStore {
101 entClient, err := sqlstore.NewEntClient(
102 a.Cfg.SQLStoreBackend,
103 a.Cfg.SQLStoreConnectionString,
104 sqlstore.ClientWithMaxIdleConns(a.Cfg.SQLStoreMaxIdleConnections),
105 sqlstore.ClientWithMaxOpenConns(a.Cfg.SQLStoreMaxOpenConnections),
106 sqlstore.ClientWithConnMaxLifetime(a.Cfg.SQLStoreConnectionMaxLifetime),
107 )
108 if err != nil {
109 logrus.Fatalf("could not create ent client: %+v", err)
110 }
111
112 // Continue with the existing setup code for the SQLStore
113 sqlStore, a.sqlStoreCh, err = sqlstore.New(context.Background(), entClient)
114 if err != nil {
115 logrus.Fatalf("error initializing new SQLStore: %+v", err)
116 }
117 serverOpts = append(serverOpts, WithMetadataStore(sqlStore))
118
119 // Add SQL client for ent
120 sqlClient := sqlStore.GetClient()
121 serverOpts = append(serverOpts, WithEntSqlClient(sqlClient))
122 } else {
123 sqlStoreChan := make(chan error)
124 a.sqlStoreCh = sqlStoreChan
125 go func() {
126 <-a.Ctx.Done()
127 close(sqlStoreChan)
128 }()
129 }
130
131 // initialize the artifact store
132 if a.Cfg.EnableArtifactStore {
133 wds, err := artifactstore.New(artifactstore.WithConfigFile(a.Cfg.ArtifactStoreConfig))
134 if err != nil {
135 logrus.Fatalf("could not create the artifact store: %+v", err)
136 }
137
138 serverOpts = append(serverOpts, WithArtifactStore(wds))
139 }
140
141 if a.Cfg.Publisher != nil {
142 publisherStore = publisherstore.New(a.Cfg)
143 serverOpts = append(serverOpts, WithPublishers(publisherStore))
144 }
145 // Create the Archivista server with all options
146 server, err := New(a.Cfg, serverOpts...)
147 if err != nil {
148 logrus.Fatalf("could not create archivista server: %+v", err)
149 }
150
151 logrus.WithField("duration", time.Since(now)).Infof("completed phase: initializing storage clients")
152 return &server, nil
153 }
154
155 // GetFileStoreCh returns the file store channel
156 func (a *ArchivistaService) GetFileStoreCh() <-chan error {
157 return a.fileStoreCh
158 }
159
160 // GetSQLStoreCh returns the SQL store channel
161 func (a *ArchivistaService) GetSQLStoreCh() <-chan error {
162 return a.sqlStoreCh
163 }
164
165 func (a *ArchivistaService) initObjectStore() (StorerGetter, <-chan error, error) {
166 switch strings.ToUpper(a.Cfg.StorageBackend) {
167 case "FILE":
168 return filestore.New(a.Ctx, a.Cfg.FileDir, a.Cfg.FileServeOn)
169
170 case "BLOB":
171 var creds *credentials.Credentials
172
173 switch a.Cfg.BlobStoreCredentialType {
174 case "IAM":
175 creds = credentials.NewIAM("")
176 case "ACCESS_KEY":
177 creds = credentials.NewStaticV4(a.Cfg.BlobStoreAccessKeyId, a.Cfg.BlobStoreSecretAccessKeyId, "")
178 default:
179 logrus.Fatalf("invalid blob store credential type: %s", a.Cfg.BlobStoreCredentialType)
180 }
181 return blobstore.New(
182 a.Ctx,
183 a.Cfg.BlobStoreEndpoint,
184 creds,
185 a.Cfg.BlobStoreBucketName,
186 a.Cfg.BlobStoreUseTLS,
187 )
188
189 case "":
190 errCh := make(chan error)
191 go func() {
192 <-a.Ctx.Done()
193 close(errCh)
194 }()
195 return nil, errCh, nil
196
197 default:
198 return nil, nil, fmt.Errorf("unknown storage backend: %s", a.Cfg.StorageBackend)
199 }
200 }