Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for check latest Helm version #6969

Merged
merged 3 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Tasks/Common/utility-common/downloadutility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ var https = require('https');
var fs = require('fs');
import * as tl from "vsts-task-lib/task";

export async function download(url: string, downloadPath: string): Promise<void> {
export async function download(url: any, downloadPath: string, printData: boolean = false): Promise<void> {
var file = fs.createWriteStream(downloadPath);
var body = ''
return new Promise<void>((resolve, reject) => {
var req = https.request(url, res => {
tl.debug("statusCode: " + res.statusCode);
res.pipe(file);
res.on("error", err => reject(err));
res.on('data', d => body += d);

res.on("end", () => {
file.end(null, null, file.close);
if(printData) {
tl.debug(body);
}

if(res.statusCode < 200 || res.statusCode >= 300) {
tl.debug("File download failed");
reject(new Error('Failed to download file status code: ' + res.statusCode));
Expand All @@ -23,12 +30,11 @@ export async function download(url: string, downloadPath: string): Promise<void>
}
});
});

req.on("error", err => {
tl.debug(err);
reject(err);
});

req.end();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"loc.group.displayName.prerequisite": "Prerequisite",
"loc.input.label.helmVersion": "Helm Version Spec",
"loc.input.help.helmVersion": "Specify the version of Helm to install",
"loc.input.label.checkLatestHelmVersion": "Check for latest version of Helm",
"loc.input.help.checkLatestHelmVersion": "Check for latest version of Helm.",
"loc.input.label.installKubeCtl": "Install Kubectl",
"loc.input.help.installKubeCtl": "Install Kubectl.",
"loc.input.label.kubectlVersion": "Kubectl Version Spec",
Expand All @@ -16,5 +18,6 @@
"loc.messages.DownloadKubectlFailedFromLocation": "Failed to download kubectl from location %s. Error %s",
"loc.messages.NotAValidSemverVersion": "Version not specified in correct format. E.g: 1.8.2, v1.8.2, 2.8.2, v2.8.2.",
"loc.messages.HelmNotFoundInFolder": "Helm executable not found in path %s",
"loc.messages.HelmDownloadFailed": "Failed to download helm from location %s. Error %s"
"loc.messages.HelmDownloadFailed": "Failed to download helm from location %s. Error %s",
"loc.messages.HelmLatestNotKnown": "Can not get lateast helm info from %s. Error %s. Using default helm version %s."
}
48 changes: 48 additions & 0 deletions Tasks/HelmInstaller/src/helminstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ import * as utils from './utils';
import * as os from "os";
import * as util from "util";
const uuidV4 = require('uuid/v4');
import downloadutility = require("utility-common/downloadutility");
const helmToolName = "helm"
const helmLatestReleaseUrl = "https://api.github.com/repos/kubernetes/helm/releases/latest";
const stableHelmVersion = "v2.8.2"

export async function getHelmVersion(): Promise<string> {
var checkLatestHelmVersion = tl.getBoolInput('checkLatestHelmVersion', false);
if(checkLatestHelmVersion) {
return await getStableHelmVersion();
}

return utils.sanitizeVersionString(tl.getInput("helmVersion", true));
}

Expand Down Expand Up @@ -60,10 +68,50 @@ function getHelmDownloadURL(version: string) : string {
}
}

async function getStableHelmVersion() : Promise<string>{
var downloadPath = path.join(getTempDirectory(), uuidV4() +".json");
var options = {
hostname: 'api.github.com',
port: 443,
path: '/repos/kubernetes/helm/releases/latest',
method: 'GET',
secureProtocol: "TLSv1_2_method",
headers: {
'User-Agent' : 'vsts'
}
}

try{
await downloadutility.download(options, downloadPath, true);
var version = await getReleaseVersion(downloadPath);
return version;
} catch(error) {
tl.warning(tl.loc("HelmLatestNotKnown", helmLatestReleaseUrl, error, stableHelmVersion));
}

return stableHelmVersion;
}

function getExecutableExtention(): string {
if(os.type().match(/^Win/)){
return ".exe";
}

return "";
}

function getTempDirectory(): string {
return tl.getVariable('agent.tempDirectory') || os.tmpdir();
}

function getReleaseVersion(jsonFilePath): Promise<string> {
return new Promise(function (fulfill, reject){
fs.readFile(jsonFilePath, {encoding: 'utf8'} ,function(err,data) {
if(err) {
reject(err);
}
var latestVersionInfo = JSON.parse(data);
fulfill(latestVersionInfo.tag_name);
})
});
}
12 changes: 10 additions & 2 deletions Tasks/HelmInstaller/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 2
"Patch": 3
},
"demands": [],
"satisfies": ["Helm"],
Expand All @@ -35,6 +35,13 @@
"helpMarkDown": "Specify the version of Helm to install",
"defaultValue": "2.8.2"
},
{
"name": "checkLatestHelmVersion",
"type": "boolean",
"label": "Check for latest version of Helm",
"defaultValue": "true",
"helpMarkDown": "Check for latest version of Helm."
},
{
"name": "installKubeCtl",
"aliases": ["installKubectl"],
Expand Down Expand Up @@ -76,6 +83,7 @@
"DownloadKubectlFailedFromLocation": "Failed to download kubectl from location %s. Error %s",
"NotAValidSemverVersion": "Version not specified in correct format. E.g: 1.8.2, v1.8.2, 2.8.2, v2.8.2.",
"HelmNotFoundInFolder": "Helm executable not found in path %s",
"HelmDownloadFailed": "Failed to download helm from location %s. Error %s"
"HelmDownloadFailed": "Failed to download helm from location %s. Error %s",
"HelmLatestNotKnown": "Can not get lateast helm info from %s. Error %s. Using default helm version %s."
}
}
12 changes: 10 additions & 2 deletions Tasks/HelmInstaller/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 2
"Patch": 3
},
"demands": [],
"satisfies": [
Expand All @@ -37,6 +37,13 @@
"helpMarkDown": "ms-resource:loc.input.help.helmVersion",
"defaultValue": "2.8.2"
},
{
"name": "checkLatestHelmVersion",
"type": "boolean",
"label": "ms-resource:loc.input.label.checkLatestHelmVersion",
"defaultValue": "true",
"helpMarkDown": "ms-resource:loc.input.help.checkLatestHelmVersion"
},
{
"name": "installKubeCtl",
"aliases": [
Expand Down Expand Up @@ -82,6 +89,7 @@
"DownloadKubectlFailedFromLocation": "ms-resource:loc.messages.DownloadKubectlFailedFromLocation",
"NotAValidSemverVersion": "ms-resource:loc.messages.NotAValidSemverVersion",
"HelmNotFoundInFolder": "ms-resource:loc.messages.HelmNotFoundInFolder",
"HelmDownloadFailed": "ms-resource:loc.messages.HelmDownloadFailed"
"HelmDownloadFailed": "ms-resource:loc.messages.HelmDownloadFailed",
"HelmLatestNotKnown": "ms-resource:loc.messages.HelmLatestNotKnown"
}
}