]>
Commit | Line | Data |
---|---|---|
1 | import { randomInt } from 'crypto' | |
2 | import { HttpStatusCode } from '@shared/models' | |
3 | import { makePostBodyRequest } from '../requests' | |
4 | ||
5 | export class ObjectStorageCommand { | |
6 | static readonly DEFAULT_SCALEWAY_BUCKET = 'peertube-ci-test' | |
7 | ||
8 | private readonly bucketsCreated: string[] = [] | |
9 | private readonly seed: number | |
10 | ||
11 | // --------------------------------------------------------------------------- | |
12 | ||
13 | constructor () { | |
14 | this.seed = randomInt(0, 10000) | |
15 | } | |
16 | ||
17 | static getMockCredentialsConfig () { | |
18 | return { | |
19 | access_key_id: 'AKIAIOSFODNN7EXAMPLE', | |
20 | secret_access_key: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY' | |
21 | } | |
22 | } | |
23 | ||
24 | static getMockEndpointHost () { | |
25 | return 'localhost:9444' | |
26 | } | |
27 | ||
28 | static getMockRegion () { | |
29 | return 'us-east-1' | |
30 | } | |
31 | ||
32 | getDefaultMockConfig () { | |
33 | return { | |
34 | object_storage: { | |
35 | enabled: true, | |
36 | endpoint: 'http://' + ObjectStorageCommand.getMockEndpointHost(), | |
37 | region: ObjectStorageCommand.getMockRegion(), | |
38 | ||
39 | credentials: ObjectStorageCommand.getMockCredentialsConfig(), | |
40 | ||
41 | streaming_playlists: { | |
42 | bucket_name: this.getMockStreamingPlaylistsBucketName() | |
43 | }, | |
44 | ||
45 | videos: { | |
46 | bucket_name: this.getMockWebVideosBucketName() | |
47 | } | |
48 | } | |
49 | } | |
50 | } | |
51 | ||
52 | getMockWebVideosBaseUrl () { | |
53 | return `http://${this.getMockWebVideosBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/` | |
54 | } | |
55 | ||
56 | getMockPlaylistBaseUrl () { | |
57 | return `http://${this.getMockStreamingPlaylistsBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/` | |
58 | } | |
59 | ||
60 | async prepareDefaultMockBuckets () { | |
61 | await this.createMockBucket(this.getMockStreamingPlaylistsBucketName()) | |
62 | await this.createMockBucket(this.getMockWebVideosBucketName()) | |
63 | } | |
64 | ||
65 | async createMockBucket (name: string) { | |
66 | this.bucketsCreated.push(name) | |
67 | ||
68 | await this.deleteMockBucket(name) | |
69 | ||
70 | await makePostBodyRequest({ | |
71 | url: ObjectStorageCommand.getMockEndpointHost(), | |
72 | path: '/ui/' + name + '?create', | |
73 | expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 | |
74 | }) | |
75 | ||
76 | await makePostBodyRequest({ | |
77 | url: ObjectStorageCommand.getMockEndpointHost(), | |
78 | path: '/ui/' + name + '?make-public', | |
79 | expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 | |
80 | }) | |
81 | } | |
82 | ||
83 | async cleanupMock () { | |
84 | for (const name of this.bucketsCreated) { | |
85 | await this.deleteMockBucket(name) | |
86 | } | |
87 | } | |
88 | ||
89 | getMockStreamingPlaylistsBucketName (name = 'streaming-playlists') { | |
90 | return this.getMockBucketName(name) | |
91 | } | |
92 | ||
93 | getMockWebVideosBucketName (name = 'web-videos') { | |
94 | return this.getMockBucketName(name) | |
95 | } | |
96 | ||
97 | getMockBucketName (name: string) { | |
98 | return `${this.seed}-${name}` | |
99 | } | |
100 | ||
101 | private async deleteMockBucket (name: string) { | |
102 | await makePostBodyRequest({ | |
103 | url: ObjectStorageCommand.getMockEndpointHost(), | |
104 | path: '/ui/' + name + '?delete', | |
105 | expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307 | |
106 | }) | |
107 | } | |
108 | ||
109 | // --------------------------------------------------------------------------- | |
110 | ||
111 | static getDefaultScalewayConfig (options: { | |
112 | serverNumber: number | |
113 | enablePrivateProxy?: boolean // default true | |
114 | privateACL?: 'private' | 'public-read' // default 'private' | |
115 | }) { | |
116 | const { serverNumber, enablePrivateProxy = true, privateACL = 'private' } = options | |
117 | ||
118 | return { | |
119 | object_storage: { | |
120 | enabled: true, | |
121 | endpoint: this.getScalewayEndpointHost(), | |
122 | region: this.getScalewayRegion(), | |
123 | ||
124 | credentials: this.getScalewayCredentialsConfig(), | |
125 | ||
126 | upload_acl: { | |
127 | private: privateACL | |
128 | }, | |
129 | ||
130 | proxy: { | |
131 | proxify_private_files: enablePrivateProxy | |
132 | }, | |
133 | ||
134 | streaming_playlists: { | |
135 | bucket_name: this.DEFAULT_SCALEWAY_BUCKET, | |
136 | prefix: `test:server-${serverNumber}-streaming-playlists:` | |
137 | }, | |
138 | ||
139 | videos: { | |
140 | bucket_name: this.DEFAULT_SCALEWAY_BUCKET, | |
141 | prefix: `test:server-${serverNumber}-videos:` | |
142 | } | |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | static getScalewayCredentialsConfig () { | |
148 | return { | |
149 | access_key_id: process.env.OBJECT_STORAGE_SCALEWAY_KEY_ID, | |
150 | secret_access_key: process.env.OBJECT_STORAGE_SCALEWAY_ACCESS_KEY | |
151 | } | |
152 | } | |
153 | ||
154 | static getScalewayEndpointHost () { | |
155 | return 's3.fr-par.scw.cloud' | |
156 | } | |
157 | ||
158 | static getScalewayRegion () { | |
159 | return 'fr-par' | |
160 | } | |
161 | ||
162 | static getScalewayBaseUrl () { | |
163 | return `https://${this.DEFAULT_SCALEWAY_BUCKET}.${this.getScalewayEndpointHost()}/` | |
164 | } | |
165 | } |