feat: download binaries directly from the trdl repository

This commit is contained in:
Alexey Igrychev
2021-10-27 21:39:40 +01:00
parent f507c4f344
commit 6acc213146
11 changed files with 227036 additions and 354072 deletions
+1 -1
View File
@@ -38,6 +38,6 @@ jobs:
- name: Install - name: Install
uses: ./install uses: ./install
with: with:
version: v1.1.23+fix49 version: v1.1.23+fix50
- run: werf version - run: werf version
+24777 -38900
View File
File diff suppressed because one or more lines are too long
+24816 -38932
View File
File diff suppressed because one or more lines are too long
+24824 -38940
View File
File diff suppressed because one or more lines are too long
+24825 -38941
View File
File diff suppressed because one or more lines are too long
+24824 -38947
View File
File diff suppressed because one or more lines are too long
+24809 -38925
View File
File diff suppressed because one or more lines are too long
+25720 -39836
View File
File diff suppressed because one or more lines are too long
+24828 -38951
View File
File diff suppressed because one or more lines are too long
+24886 -39000
View File
File diff suppressed because one or more lines are too long
+68 -41
View File
@@ -12,9 +12,9 @@ import * as dotenv from 'dotenv'
import * as werf from './werf' import * as werf from './werf'
import {ValidateWerfVersion} from './common' import {ValidateWerfVersion} from './common'
const WERF_API_GET_CHANNEL_VERSION_URL_METHOD = const WERF_TUF_SERVER_URL = 'https://tuf.werf.io'
'https://werf.io/api/getChannelVersionURL' const CACHE_TOOL_NAME = 'werf'
const WERF_API_GET_VERSION_URL_METHOD = 'https://werf.io/api/getVersionURL' const CACHE_TOOL_DIR = 'werf-tools'
export class Manager { export class Manager {
private readonly channel: string private readonly channel: string
@@ -32,24 +32,44 @@ export class Manager {
ValidateWerfVersion(this.version) ValidateWerfVersion(this.version)
} }
if (process.platform.toString() === 'win32') { const platform = process.platform.toString()
switch (platform) {
case 'linux':
case 'darwin':
this.os = platform
break
case 'win32':
this.os = 'windows' this.os = 'windows'
} else { break
this.os = process.platform.toString() default:
throw new Error(String.Format(`The platform ${platform} not supported`))
} }
this.arch = process.arch const arch = process.arch
switch (arch) {
case 'x64':
this.arch = 'amd64'
break
case 'arm64':
this.arch = 'arm64'
break
default:
throw new Error(String.Format(`The architecture ${arch} not supported`))
}
} }
public async Install(): Promise<void> { public async Install(): Promise<void> {
const actualBinaryUrl = await this._getActualBinaryUrl() const actualBinaryUrl = await this._getActualBinaryUrl()
const binaryName = actualBinaryUrl.substring(
actualBinaryUrl.lastIndexOf('/') + 1
)
const cachedPath = cache.find( const cachedPath = cache.find(
'werf', CACHE_TOOL_NAME,
Manager._toolVersionCacheID(actualBinaryUrl) Manager._toolVersionCacheID(actualBinaryUrl)
) )
if (cachedPath) { if (cachedPath) {
this.binaryPath = path.join(cachedPath, 'werf') this.binaryPath = path.join(cachedPath, binaryName)
} else { } else {
this.binaryPath = await this._downloadAndCache(actualBinaryUrl) this.binaryPath = await this._downloadAndCache(actualBinaryUrl)
} }
@@ -95,35 +115,29 @@ export class Manager {
} }
private async _getActualBinaryUrl(): Promise<string> { private async _getActualBinaryUrl(): Promise<string> {
try {
let url: string
let query: {}
if (this.version !== '') { if (this.version !== '') {
url = WERF_API_GET_VERSION_URL_METHOD const version = this.version.slice('v'.length)
query = { return this._constructReleaseUrl(version)
version: this.version,
os: this.os,
arch: this.arch
}
} else {
url = WERF_API_GET_CHANNEL_VERSION_URL_METHOD
query = {
group: werf.MAJOR_MINOR_GROUP,
channel: this.channel,
os: this.os,
arch: this.arch
}
} }
const resp = await request.get(url).query(query) const url = `${WERF_TUF_SERVER_URL}/targets/channels/${werf.MAJOR_MINOR_GROUP}/${this.channel}`
try {
return resp.body.data.toString() const resp = await request
.get(url)
.buffer(true)
.parse(request.parse['application/octet-stream'])
const version = resp.body.toString().trim()
return this._constructReleaseUrl(version)
} catch (err) { } catch (err) {
if (err.response && err.response.error) { if (err.response && err.response.error) {
let errMessage = err.response.error.message let errMessage = err.response.error.message
if (err.response.text) { if (err.response.text) {
errMessage = String.Format('{0}\n{1}', errMessage, err.response.text) errMessage = String.Format(
'{0}: {1}\n{2}',
url,
errMessage,
err.response.text
)
} }
throw Error(errMessage) throw Error(errMessage)
@@ -133,19 +147,32 @@ export class Manager {
} }
} }
private _constructReleaseUrl(version: string): string {
let ext = ''
if (this.os === 'windows') {
ext = '.exe'
}
return String.Format(
'{0}/targets/releases/{1}/{2}-{3}/bin/werf{4}',
WERF_TUF_SERVER_URL,
version,
this.os,
this.arch,
ext
)
}
private async _downloadAndCache(binaryUrl: string): Promise<string> { private async _downloadAndCache(binaryUrl: string): Promise<string> {
const binaryName = binaryUrl.substring(binaryUrl.lastIndexOf('/') + 1)
const downloadedBinaryPath = await cache.downloadTool(binaryUrl) const downloadedBinaryPath = await cache.downloadTool(binaryUrl)
const parsedDownloadedBinaryPath = path.parse(downloadedBinaryPath) const cacheDownloadToolDir = path.dirname(downloadedBinaryPath)
const cacheDownloadToolDir = parsedDownloadedBinaryPath.dir const tmpWerfVersionBinaryPath = path.join(
const tmpWerfVersionBinaryPath = path.join(cacheDownloadToolDir, 'werf.tmp')
const werfVersionDir = path.join(
cacheDownloadToolDir, cacheDownloadToolDir,
parsedDownloadedBinaryPath.name `${binaryName}.tmp`
)
const werfVersionBinaryPath = path.join(
werfVersionDir,
String.Format('werf{0}', parsedDownloadedBinaryPath.ext)
) )
const werfVersionDir = path.join(cacheDownloadToolDir, CACHE_TOOL_DIR)
const werfVersionBinaryPath = path.join(werfVersionDir, binaryName)
// werf-x.x.x -> werf.tmp // werf-x.x.x -> werf.tmp
// werf.tmp -> werf-x.x.x/werf // werf.tmp -> werf-x.x.x/werf
@@ -159,7 +186,7 @@ export class Manager {
await cache.cacheDir( await cache.cacheDir(
werfVersionDir, werfVersionDir,
'werf', CACHE_TOOL_NAME,
Manager._toolVersionCacheID(binaryUrl) Manager._toolVersionCacheID(binaryUrl)
) )