forked from unionai-oss/flytectl-setup-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflytectl.ts
More file actions
146 lines (128 loc) · 4.2 KB
/
Copy pathflytectl.ts
File metadata and controls
146 lines (128 loc) · 4.2 KB
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
import * as os from 'os';
import * as path from 'path';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import { Octokit } from "@octokit/core";
import { Error, isError } from './error';
// versionPrefix is used in Github release names, and can
// optionally be specified in the action's version parameter.
const versionPrefix = "v";
const releasesPerPage = 100;
interface ReleaseAsset {
name: string;
browser_download_url: string;
}
interface Release {
tag_name: string;
assets: ReleaseAsset[];
}
export async function getFlytectl(version: string): Promise<string | Error> {
const binaryPath = tc.find('flytectl', version, os.arch());
if (binaryPath !== '') {
core.info(`Found in cache @ ${binaryPath}`);
return binaryPath;
}
core.info(`Resolving the download URL for the current platform...`);
const downloadURL = await getDownloadURL(version);
if (isError(downloadURL)) {
return downloadURL
}
core.info(`Downloading flytectl version "${version}" from ${downloadURL}`);
const downloadPath = await tc.downloadTool(downloadURL);
core.info(`Successfully downloaded flytectl version "${version}" from ${downloadURL}`);
core.info('Extracting flytectl...');
const extractPath = await tc.extractTar(downloadPath);
core.info(`Successfully extracted flytectl to ${extractPath}`);
core.info('Adding flytectl to the cache...');
const cacheDir = await tc.cacheDir(
path.join(extractPath),
'flytectl',
version,
os.arch()
);
core.info(`Successfully cached flytectl to ${cacheDir}`);
return cacheDir;
}
// getDownloadURL resolves flytectl's Github download URL for the
// current architecture and platform.
async function getDownloadURL(version: string): Promise<string | Error> {
let architecture = '';
switch (os.arch()) {
case 'x64':
architecture = 'x86_64';
break;
default:
return {
message: `The "${os.arch()}" architecture is not supported with a flytectl release.`
};
}
let platform = '';
switch (os.platform()) {
case 'linux':
platform = 'Linux';
break;
default:
return {
message: `The "${os.platform()}" platform is not supported with a flytectl release.`
};
}
const assetName = `flytectl_${platform}_${architecture}.tar.gz`
const octokit = new Octokit();
const releases = await getAllFlyteReleases(octokit);
// Filter out releases for which the tags do not have the prefix `flytectl/`
const filteredReleases = releases.filter((release) => release.tag_name.startsWith('flytectl/'));
switch (version) {
case 'latest':
for (const asset of filteredReleases[0].assets) {
if (assetName === asset.name) {
return asset.browser_download_url;
}
}
break;
default:
for (const release of filteredReleases) {
if (releaseTagIsVersion(release.tag_name, version)) {
for (const asset of release.assets) {
if (assetName === asset.name) {
return asset.browser_download_url;
}
}
}
}
}
return {
message: `Unable to find flytectl version "${version}" for platform "${platform}" and architecture "${architecture}".`
};
}
async function getAllFlyteReleases(octokit: Octokit): Promise<Release[]> {
const releases: Release[] = [];
let page = 1;
for (;;) {
const response = await octokit.request('GET /repos/{owner}/{repo}/releases', {
owner: 'flyteorg',
repo: 'flyte',
per_page: releasesPerPage,
page,
});
const currentPageReleases = response.data as Release[];
releases.push(...currentPageReleases);
if (currentPageReleases.length < releasesPerPage) {
break;
}
page += 1;
}
return releases;
}
function releaseTagIsVersion(releaseTag: string, version: string): boolean {
// Remove the prefix `flytectl/` from releaseTag if it exists
if (releaseTag.indexOf('flytectl/') === 0) {
releaseTag = releaseTag.slice('flytectl/'.length)
}
if (releaseTag.indexOf(versionPrefix) === 0) {
releaseTag = releaseTag.slice(versionPrefix.length)
}
if (version.indexOf(versionPrefix) === 0) {
version = version.slice(versionPrefix.length)
}
return releaseTag === version
}