-
Notifications
You must be signed in to change notification settings - Fork 55
/
LayerCache.ts
306 lines (247 loc) · 10 KB
/
LayerCache.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import * as path from 'path'
import * as exec from 'actions-exec-listener'
import crypto from 'crypto'
import * as core from '@actions/core'
import * as cache from '@actions/cache'
import { ExecOptions } from '@actions/exec/lib/interfaces'
import { promises as fs } from 'fs'
import recursiveReaddir from 'recursive-readdir'
import { Manifest, loadManifests, loadRawManifests } from './Tar'
import format from 'string-format'
import PromisePool from 'native-promise-pool'
class LayerCache {
ids: string[] = []
unformattedSaveKey: string = ''
restoredRootKey: string = ''
imagesDir: string = path.resolve(`${__dirname}/../.adlc`)
enabledParallel = true
concurrency: number = 4
static ERROR_CACHE_ALREAD_EXISTS_STR = `Cache already exists`
static ERROR_LAYER_CACHE_NOT_FOUND_STR = `Layer cache not found`
constructor(ids: string[]) {
this.ids = ids
}
async exec(command: string, args?: string[], options?: ExecOptions) {
const result = await exec.exec(command, args, options)
return result
}
async store(key: string) {
this.unformattedSaveKey = key
await this.saveImageAsUnpacked()
if (this.enabledParallel) {
await this.separateAllLayerCaches()
}
if (await this.storeRoot() === undefined) {
core.info(`cache key already exists, aborting.`)
return false
}
await Promise.all(this.enabledParallel ? await this.storeLayers() : [])
return true
}
private async saveImageAsUnpacked() {
await fs.mkdir(this.getSavedImageTarDir(), { recursive: true })
await this.exec(`sh -c`, [`docker save '${(await this.makeRepotagsDockerSaveArgReady(this.ids)).join(`' '`)}' | tar xf - -C .`], { cwd: this.getSavedImageTarDir() })
}
private async makeRepotagsDockerSaveArgReady(repotags: string[]): Promise<string[]> {
const getMiddleIdsWithRepotag = async (id: string): Promise<string[]> => {
return [id, ...(await this.getAllImageIdsFrom(id))]
}
return (await Promise.all(repotags.map(getMiddleIdsWithRepotag))).flat()
}
private async getAllImageIdsFrom(repotag: string): Promise<string[]> {
const { stdoutStr: rawHistoryIds } = await this.exec(`docker history -q`, [repotag], { silent: true, listeners: { stderr: console.warn }})
const historyIds = rawHistoryIds.split(`\n`).filter(id => id !== `<missing>` && id !== ``)
return historyIds
}
private async getManifests() {
return loadManifests(this.getUnpackedTarDir())
}
private async storeRoot() {
const rootKey = await this.generateRootSaveKey()
const paths = [
this.getUnpackedTarDir(),
]
core.info(`Start storing root cache, key: ${rootKey}, dir: ${paths}`)
const cacheId = await LayerCache.dismissError(cache.saveCache(paths, rootKey), LayerCache.ERROR_CACHE_ALREAD_EXISTS_STR, -1)
core.info(`Stored root cache, key: ${rootKey}, id: ${cacheId}`)
return cacheId !== -1 ? cacheId : undefined
}
private async separateAllLayerCaches() {
await this.moveLayerTarsInDir(this.getUnpackedTarDir(), this.getLayerCachesDir())
}
private async joinAllLayerCaches() {
await this.moveLayerTarsInDir(this.getLayerCachesDir(), this.getUnpackedTarDir())
}
private async moveLayerTarsInDir(fromDir: string, toDir: string) {
const layerTars = (await recursiveReaddir(fromDir))
.filter(path => path.endsWith(`/layer.tar`))
.map(path => path.replace(`${fromDir}/`, ``))
const moveLayer = async (layer: string) => {
const from = path.resolve(`${fromDir}/${layer}`)
const to = path.resolve(`${toDir}/${layer}`)
core.debug(`Moving layer tar from ${from} to ${to}`)
await fs.mkdir(`${path.dirname(to)}`, { recursive: true })
await fs.rename(from, to)
}
await Promise.all(layerTars.map(moveLayer))
}
private async storeLayers(): Promise<number[]> {
const pool = new PromisePool(this.concurrency)
const result = Promise.all(
(await this.getLayerIds()).map(
layerId => {
return pool.open(() => this.storeSingleLayerBy(layerId))
}
)
)
return result
}
static async dismissError<T>(promise: Promise<T>, dismissStr: string, defaultResult: T): Promise<T> {
try {
return await promise
} catch (e) {
core.debug(`catch error: ${e.toString()}`)
if (typeof e.message !== 'string' || !e.message.includes(dismissStr)) {
core.error(`Unexpected error: ${e.toString()}`)
throw e
}
core.info(`${dismissStr}: ${e.toString()}`)
core.debug(e)
return defaultResult
}
}
private async storeSingleLayerBy(layerId: string): Promise<number> {
const path = this.genSingleLayerStorePath(layerId)
const key = await this.generateSingleLayerSaveKey(layerId)
core.info(`Start storing layer cache: ${JSON.stringify({ layerId, key })}`)
const cacheId = await LayerCache.dismissError(cache.saveCache([path], key), LayerCache.ERROR_CACHE_ALREAD_EXISTS_STR, -1)
core.info(`Stored layer cache: ${JSON.stringify({ key, cacheId })}`)
core.debug(JSON.stringify({ log: `storeSingleLayerBy`, layerId, path, key, cacheId}))
return cacheId
}
// ---
async restore(primaryKey: string, restoreKeys?: string[]) {
const restoredCacheKey = await this.restoreRoot(primaryKey, restoreKeys)
if (restoredCacheKey === undefined) {
core.info(`Root cache could not be found. aborting.`)
return undefined
}
if (this.enabledParallel) {
const hasRestoredAllLayers = await this.restoreLayers()
if (!hasRestoredAllLayers) {
core.info(`Some layer cache could not be found. aborting.`)
return undefined
}
await this.joinAllLayerCaches()
}
await this.loadImageFromUnpacked()
return restoredCacheKey
}
private async restoreRoot(primaryKey: string, restoreKeys?: string[]): Promise<string | undefined> {
core.debug(`Trying to restore root cache: ${ JSON.stringify({ restoreKeys, dir: this.getUnpackedTarDir() }) }`)
const restoredRootKey = await cache.restoreCache([this.getUnpackedTarDir()], primaryKey, restoreKeys)
core.debug(`restoredRootKey: ${restoredRootKey}`)
if (restoredRootKey === undefined) {
return undefined
}
this.restoredRootKey = restoredRootKey
return restoredRootKey
}
private async restoreLayers(): Promise<boolean> {
const pool = new PromisePool(this.concurrency)
const tasks = (await this.getLayerIds()).map(
layerId => pool.open(() => this.restoreSingleLayerBy(layerId))
)
try {
await Promise.all(tasks)
} catch (e) {
if (typeof e.message === `string` && e.message.includes(LayerCache.ERROR_LAYER_CACHE_NOT_FOUND_STR)) {
core.info(e.message)
// Avoid UnhandledPromiseRejectionWarning
tasks.map(task => task.catch(core.info))
return false
}
throw e
}
return true
}
private async restoreSingleLayerBy(id: string): Promise<string> {
const path = this.genSingleLayerStorePath(id)
const key = await this.recoverSingleLayerKey(id)
const dir = path.replace(/[^/\\]+$/, ``)
core.debug(JSON.stringify({ log: `restoreSingleLayerBy`, id, path, dir, key }))
await fs.mkdir(dir, { recursive: true })
const result = await cache.restoreCache([path], key)
if (result == null) {
throw new Error(`${LayerCache.ERROR_LAYER_CACHE_NOT_FOUND_STR}: ${JSON.stringify({ id })}`)
}
return result
}
private async loadImageFromUnpacked() {
await exec.exec(`sh -c`, [`tar cf - . | docker load`], { cwd: this.getUnpackedTarDir() })
}
async cleanUp() {
await fs.rmdir(this.getImagesDir(), { recursive: true })
}
// ---
getImagesDir(): string {
return this.imagesDir
}
getUnpackedTarDir(): string {
return path.resolve(`${this.getImagesDir()}/${this.getCurrentTarStoreDir()}`)
}
getLayerCachesDir() {
return `${this.getUnpackedTarDir()}-layers`
}
getSavedImageTarDir(): string {
return path.resolve(`${this.getImagesDir()}/${this.getCurrentTarStoreDir()}`)
}
getCurrentTarStoreDir(): string {
return 'image'
}
genSingleLayerStorePath(id: string) {
return path.resolve(`${this.getLayerCachesDir()}/${id}/layer.tar`)
}
async generateRootHashFromManifest(): Promise<string> {
const manifest = await loadRawManifests(this.getUnpackedTarDir())
return crypto.createHash(`sha256`).update(manifest, `utf8`).digest(`hex`)
}
async generateRootSaveKey(): Promise<string> {
const rootHash = await this.generateRootHashFromManifest()
const formatted = await this.getFormattedSaveKey(rootHash)
core.debug(JSON.stringify({ log: `generateRootSaveKey`, rootHash, formatted }))
return `${formatted}-root`
}
async generateSingleLayerSaveKey(id: string) {
const formatted = await this.getFormattedSaveKey(id)
core.debug(JSON.stringify({ log: `generateSingleLayerSaveKey`, formatted, id }))
return `layer-${formatted}`
}
async recoverSingleLayerKey(id: string) {
const unformatted = await this.recoverUnformattedSaveKey()
return format(`layer-${unformatted}`, { hash: id })
}
async getFormattedSaveKey(hash: string) {
const result = format(this.unformattedSaveKey, { hash })
core.debug(JSON.stringify({ log: `getFormattedSaveKey`, hash, result }))
return result
}
async recoverUnformattedSaveKey() {
const hash = await this.generateRootHashFromManifest()
core.debug(JSON.stringify({ log: `recoverUnformattedSaveKey`, hash}))
return this.restoredRootKey.replace(hash, `{hash}`).replace(/-root$/, ``)
}
async getLayerTarFiles(): Promise<string[]> {
const getTarFilesFromManifest = (manifest: Manifest) => manifest.Layers
const tarFilesThatMayDuplicate = (await this.getManifests()).flatMap(getTarFilesFromManifest)
const tarFiles = [...new Set(tarFilesThatMayDuplicate)]
return tarFiles
}
async getLayerIds(): Promise<string[]> {
const getIdfromLayerRelativePath = (path: string) => path.replace('/layer.tar', '')
const layerIds = (await this.getLayerTarFiles()).map(getIdfromLayerRelativePath);
core.debug(JSON.stringify({ log: `getLayerIds`, layerIds }))
return layerIds
}
}
export { LayerCache }