forked from medyagh/setup-minikube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.ts
106 lines (96 loc) · 3.05 KB
/
cache.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
import {
restoreCache as restoreCacheAction,
saveCache as saveCacheAction,
} from '@actions/cache'
import {getInput as getInputAction} from '@actions/core'
import {exec} from '@actions/exec'
import {existsSync} from 'fs'
import {arch, homedir} from 'os'
import {join} from 'path'
type CacheHits = {
iso: boolean
kic: boolean
preload: boolean
}
export const restoreCaches = async (): Promise<CacheHits> => {
const cacheHits: CacheHits = {iso: true, kic: true, preload: true}
if (!useCache()) {
return cacheHits
}
const minikubeVersion = await getMinikubeVersion()
const isoCacheKey = restoreCache('iso', minikubeVersion)
const kicCacheKey = restoreCache('kic', minikubeVersion)
const preloadCacheKey = restoreCache('preloaded-tarball', minikubeVersion)
cacheHits.iso = typeof (await isoCacheKey) !== 'undefined'
cacheHits.kic = typeof (await kicCacheKey) !== 'undefined'
cacheHits.preload = typeof (await preloadCacheKey) !== 'undefined'
return cacheHits
}
export const getMinikubeVersion = async (): Promise<string> => {
let version = ''
const options: any = {}
options.listeners = {
stdout: (data: Buffer) => {
version += data.toString()
},
}
await exec('minikube', ['version', '--short'], options)
return version.trim()
}
export const saveCaches = async (cacheHits: CacheHits): Promise<void> => {
if (!useCache()) {
return
}
const minikubeVersion = await getMinikubeVersion()
await Promise.all([
saveCache('iso', cacheHits.iso, minikubeVersion),
saveCache('kic', cacheHits.kic, minikubeVersion),
saveCache('preloaded-tarball', cacheHits.preload, minikubeVersion),
])
}
const restoreCache = async (
name: string,
minikubeVersion: string
): Promise<string | undefined> => {
return restoreCacheAction(
getCachePaths(name),
getCacheKey(name, minikubeVersion)
)
}
const saveCache = async (
name: string,
cacheHit: boolean,
minikubeVersion: string
): Promise<void> => {
if (cacheHit) {
return
}
const cachePaths = getCachePaths(name)
if (!existsSync(cachePaths[0])) {
return
}
try {
await saveCacheAction(cachePaths, getCacheKey(name, minikubeVersion))
} catch (error) {
console.log(name + error)
}
}
const getCachePaths = (folderName: string): string[] => {
return [join(homedir(), '.minikube', 'cache', folderName)]
}
const getCacheKey = (name: string, minikubeVersion: string): string => {
let cacheKey = `${name}-${minikubeVersion}-${arch()}`
if (name === 'preloaded-tarball') {
const kubernetesVersion = getInput('kubernetes-version', 'stable')
const containerRuntime = getInput('container-runtime', 'docker')
cacheKey += `-${kubernetesVersion}-${containerRuntime}`
}
return cacheKey
}
// getInput gets the specified value from the users workflow yaml
// if the value is empty the default value it returned
const getInput = (name: string, defaultValue: string): string => {
const value = getInputAction(name).toLowerCase()
return value !== '' ? value : defaultValue
}
const useCache = (): boolean => getInputAction('cache').toLowerCase() === 'true'