@@ -11,18 +11,24 @@ import cockpit from 'cockpit';
11
11
import { fsinfo } from 'cockpit/fsinfo' ;
12
12
import { v4 as uuidv4 } from 'uuid' ;
13
13
14
- import { Blueprint } from './composerCloudApi' ;
15
14
// We have to work around RTK query here, since it doesn't like splitting
16
15
// out the same api into two separate apis. So, instead, we can just
17
16
// inherit/import the `contentSourcesApi` and build on top of that.
18
17
// This is fine since all the api endpoints for on-prem should query
19
18
// the same unix socket. This allows us to split out the code a little
20
19
// bit so that the `cockpitApi` doesn't become a monolith.
21
20
import { contentSourcesApi } from './contentSourcesApi' ;
21
+ import {
22
+ getBlueprintsPath ,
23
+ getCloudConfigs ,
24
+ lookupDatastreamDistro ,
25
+ mapToOnpremRequest ,
26
+ paginate ,
27
+ readComposes ,
28
+ } from './helpers' ;
22
29
import type {
23
30
CockpitCreateBlueprintApiArg ,
24
31
CockpitCreateBlueprintRequest ,
25
- CockpitImageRequest ,
26
32
CockpitUpdateBlueprintApiArg ,
27
33
UpdateWorkerConfigApiArg ,
28
34
WorkerConfigFile ,
@@ -33,7 +39,6 @@ import {
33
39
mapHostedToOnPrem ,
34
40
mapOnPremToHosted ,
35
41
} from '../../Components/Blueprints/helpers/onPremToHostedBlueprintMapper' ;
36
- import { BLUEPRINTS_DIR } from '../../constants' ;
37
42
import {
38
43
BlueprintItem ,
39
44
ComposeBlueprintApiArg ,
@@ -64,104 +69,6 @@ import {
64
69
UpdateBlueprintApiResponse ,
65
70
} from '../service/imageBuilderApi' ;
66
71
67
- const lookupDatastreamDistro = ( distribution : string ) => {
68
- if ( distribution . startsWith ( 'fedora' ) ) {
69
- return 'fedora' ;
70
- }
71
-
72
- if ( distribution === 'centos-9' ) {
73
- return 'cs9' ;
74
- }
75
-
76
- if ( distribution === 'centos-10' ) {
77
- return 'cs10' ;
78
- }
79
-
80
- if ( distribution === 'rhel-9' ) {
81
- return 'rhel9' ;
82
- }
83
-
84
- if ( distribution === 'rhel-10' ) {
85
- return 'rhel10' ;
86
- }
87
-
88
- throw 'Unknown distribution' ;
89
- } ;
90
-
91
- const getBlueprintsPath = async ( ) => {
92
- const user = await cockpit . user ( ) ;
93
-
94
- // we will use the user's `.local` directory
95
- // to save blueprints used for on-prem
96
- return `${ user . home } /${ BLUEPRINTS_DIR } ` ;
97
- } ;
98
-
99
- const readComposes = async ( bpID : string ) => {
100
- const blueprintsDir = await getBlueprintsPath ( ) ;
101
- let composes : ComposesResponseItem [ ] = [ ] ;
102
- const bpInfo = await fsinfo (
103
- path . join ( blueprintsDir , bpID ) ,
104
- [ 'entries' , 'mtime' ] ,
105
- {
106
- superuser : 'try' ,
107
- } ,
108
- ) ;
109
- const bpEntries = Object . entries ( bpInfo ?. entries || { } ) ;
110
- for ( const entry of bpEntries ) {
111
- if ( entry [ 0 ] === `${ bpID } .json` ) {
112
- continue ;
113
- }
114
- const composeReq = await cockpit
115
- . file ( path . join ( blueprintsDir , bpID , entry [ 0 ] ) )
116
- . read ( ) ;
117
- composes = [
118
- ...composes ,
119
- {
120
- id : entry [ 0 ] ,
121
- request : JSON . parse ( composeReq ) ,
122
- created_at : new Date ( entry [ 1 ] ! . mtime * 1000 ) . toString ( ) ,
123
- blueprint_id : bpID ,
124
- } ,
125
- ] ;
126
- }
127
- return composes ;
128
- } ;
129
-
130
- const getCloudConfigs = async ( ) => {
131
- try {
132
- const worker_config = cockpit . file (
133
- '/etc/osbuild-worker/osbuild-worker.toml' ,
134
- ) ;
135
- const contents = await worker_config . read ( ) ;
136
- const parsed = TOML . parse ( contents ) ;
137
- return Object . keys ( parsed ) . filter ( ( k ) => k === 'aws' ) ;
138
- } catch {
139
- return [ ] ;
140
- }
141
- } ;
142
-
143
- const mapToOnpremRequest = (
144
- blueprint : Blueprint ,
145
- distribution : string ,
146
- image_requests : CockpitImageRequest [ ] ,
147
- ) => {
148
- return {
149
- blueprint,
150
- distribution,
151
- image_requests : image_requests . map ( ( ir ) => ( {
152
- architecture : ir . architecture ,
153
- image_type : ir . image_type ,
154
- repositories : [ ] ,
155
- upload_targets : [
156
- {
157
- type : ir . upload_request . type ,
158
- upload_options : ir . upload_request . options ,
159
- } ,
160
- ] ,
161
- } ) ) ,
162
- } ;
163
- } ;
164
-
165
72
export const cockpitApi = contentSourcesApi . injectEndpoints ( {
166
73
endpoints : ( builder ) => {
167
74
return {
@@ -278,31 +185,7 @@ export const cockpitApi = contentSourcesApi.injectEndpoints({
278
185
return true ;
279
186
} ) ;
280
187
281
- let paginatedBlueprints = blueprints ;
282
- if ( offset !== undefined && limit !== undefined ) {
283
- paginatedBlueprints = blueprints . slice ( offset , offset + limit ) ;
284
- }
285
-
286
- let first = '' ;
287
- let last = '' ;
288
-
289
- if ( blueprints . length > 0 ) {
290
- first = blueprints [ 0 ] . id ;
291
- last = blueprints [ blueprints . length - 1 ] . id ;
292
- }
293
-
294
- return {
295
- data : {
296
- meta : { count : blueprints . length } ,
297
- links : {
298
- // These are kind of meaningless for the on-prem
299
- // version
300
- first : first ,
301
- last : last ,
302
- } ,
303
- data : paginatedBlueprints ,
304
- } ,
305
- } ;
188
+ return paginate ( blueprints , offset , limit ) ;
306
189
} catch ( error ) {
307
190
return { error } ;
308
191
}
@@ -546,19 +429,8 @@ export const cockpitApi = contentSourcesApi.injectEndpoints({
546
429
for ( const entry of entries ) {
547
430
composes = composes . concat ( await readComposes ( entry [ 0 ] ) ) ;
548
431
}
549
- return {
550
- data : {
551
- meta : {
552
- count : composes . length ,
553
- } ,
554
- links : {
555
- first : composes . length > 0 ? composes [ 0 ] . id : '' ,
556
- last :
557
- composes . length > 0 ? composes [ composes . length - 1 ] . id : '' ,
558
- } ,
559
- data : composes ,
560
- } ,
561
- } ;
432
+
433
+ return paginate ( composes ) ;
562
434
} catch ( error ) {
563
435
return { error } ;
564
436
}
@@ -571,19 +443,7 @@ export const cockpitApi = contentSourcesApi.injectEndpoints({
571
443
queryFn : async ( queryArgs ) => {
572
444
try {
573
445
const composes = await readComposes ( queryArgs . id ) ;
574
- return {
575
- data : {
576
- meta : {
577
- count : composes . length ,
578
- } ,
579
- links : {
580
- first : composes . length > 0 ? composes [ 0 ] . id : '' ,
581
- last :
582
- composes . length > 0 ? composes [ composes . length - 1 ] . id : '' ,
583
- } ,
584
- data : composes ,
585
- } ,
586
- } ;
446
+ return paginate ( composes ) ;
587
447
} catch ( error ) {
588
448
return { error } ;
589
449
}
0 commit comments