mirror of
https://github.com/werf/actions.git
synced 2026-02-05 02:46:23 +03:00
feat: download binaries directly from the trdl repository
This commit is contained in:
64318
build/index.js
64318
build/index.js
File diff suppressed because one or more lines are too long
64318
cleanup/index.js
64318
cleanup/index.js
File diff suppressed because one or more lines are too long
64316
converge/index.js
64316
converge/index.js
File diff suppressed because one or more lines are too long
64288
dismiss/index.js
64288
dismiss/index.js
File diff suppressed because one or more lines are too long
66202
install/index.js
66202
install/index.js
File diff suppressed because one or more lines are too long
64548
run/index.js
64548
run/index.js
File diff suppressed because one or more lines are too long
115
src/manager.ts
115
src/manager.ts
@@ -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()
|
||||||
this.os = 'windows'
|
switch (platform) {
|
||||||
} else {
|
case 'linux':
|
||||||
this.os = process.platform.toString()
|
case 'darwin':
|
||||||
|
this.os = platform
|
||||||
|
break
|
||||||
|
case 'win32':
|
||||||
|
this.os = 'windows'
|
||||||
|
break
|
||||||
|
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> {
|
||||||
|
if (this.version !== '') {
|
||||||
|
const version = this.version.slice('v'.length)
|
||||||
|
return this._constructReleaseUrl(version)
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${WERF_TUF_SERVER_URL}/targets/channels/${werf.MAJOR_MINOR_GROUP}/${this.channel}`
|
||||||
try {
|
try {
|
||||||
let url: string
|
const resp = await request
|
||||||
let query: {}
|
.get(url)
|
||||||
|
.buffer(true)
|
||||||
if (this.version !== '') {
|
.parse(request.parse['application/octet-stream'])
|
||||||
url = WERF_API_GET_VERSION_URL_METHOD
|
const version = resp.body.toString().trim()
|
||||||
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)
|
|
||||||
|
|
||||||
return resp.body.data.toString()
|
|
||||||
} 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)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user