removed references to az login and copied execute az command func

This commit is contained in:
Atharva Mulmuley
2021-05-04 12:58:58 +05:30
parent 542c3fbc66
commit f095a22138
2 changed files with 40 additions and 14 deletions

View File

@ -12,11 +12,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = require("@actions/core"); const core = require("@actions/core");
const client_1 = require("./client"); const client_1 = require("./client");
const querystring = require("querystring"); const querystring = require("querystring");
const az_login = require("./main");
const path = require("path"); const path = require("path");
const child_process_1 = require("child_process"); const child_process_1 = require("child_process");
const fs = require("fs"); const fs = require("fs");
const io = require("@actions/io"); const io = require("@actions/io");
const exec = require("@actions/exec");
var azPath;
const kubeconfig_timeout = 120; //timeout in seconds const kubeconfig_timeout = 120; //timeout in seconds
function getAzureAccessToken(servicePrincipalId, servicePrincipalKey, tenantId, authorityUrl, managementEndpointUrl) { function getAzureAccessToken(servicePrincipalId, servicePrincipalKey, tenantId, authorityUrl, managementEndpointUrl) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@ -70,19 +71,18 @@ function getArcKubeconfig() {
if (!clusterName) { if (!clusterName) {
throw Error("'clusterName' is not passed for arc cluster."); throw Error("'clusterName' is not passed for arc cluster.");
} }
//await az_login.main(); azPath = yield io.which("az", true);
yield az_login.executeAzCliCommand(`account show`, false); yield executeAzCliCommand(`account show`, false);
try { try {
yield az_login.executeAzCliCommand(`extension remove -n connectedk8s`, false); yield executeAzCliCommand(`extension remove -n connectedk8s`, false);
} }
catch (_a) { catch (_a) {
//ignore if this causes an error //ignore if this causes an error
} }
yield az_login.executeAzCliCommand(`extension add -n connectedk8s`, false); yield executeAzCliCommand(`extension add -n connectedk8s`, false);
yield az_login.executeAzCliCommand(`extension list`, false); yield executeAzCliCommand(`extension list`, false);
const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated
const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`); const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`);
let azPath = yield io.which("az", true);
if (method == 'service-account') { if (method == 'service-account') {
let saToken = core.getInput('token'); let saToken = core.getInput('token');
if (!saToken) { if (!saToken) {
@ -118,3 +118,14 @@ exports.getArcKubeconfig = getArcKubeconfig;
function sleep(ms) { function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
function executeAzCliCommand(command, silent, execOptions = {}, args = []) {
return __awaiter(this, void 0, void 0, function* () {
execOptions.silent = !!silent;
try {
yield exec.exec(`"${azPath}" ${command}`, args, execOptions);
}
catch (error) {
throw new Error(error);
}
});
}

View File

@ -2,11 +2,13 @@ import * as core from '@actions/core';
import { rejects } from 'assert'; import { rejects } from 'assert';
import { WebRequest, WebRequestOptions, WebResponse, sendRequest } from './client'; import { WebRequest, WebRequestOptions, WebResponse, sendRequest } from './client';
import * as querystring from 'querystring'; import * as querystring from 'querystring';
import * as az_login from './main';
import * as path from 'path'; import * as path from 'path';
import {spawn} from 'child_process'; import {spawn} from 'child_process';
import * as fs from 'fs'; import * as fs from 'fs';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as exec from '@actions/exec';
var azPath: string;
const kubeconfig_timeout = 120;//timeout in seconds const kubeconfig_timeout = 120;//timeout in seconds
async function getAzureAccessToken(servicePrincipalId, servicePrincipalKey, tenantId, authorityUrl, managementEndpointUrl: string): Promise<string> { async function getAzureAccessToken(servicePrincipalId, servicePrincipalKey, tenantId, authorityUrl, managementEndpointUrl: string): Promise<string> {
@ -65,19 +67,18 @@ export async function getArcKubeconfig(): Promise<string> {
if(!clusterName){ if(!clusterName){
throw Error("'clusterName' is not passed for arc cluster.") throw Error("'clusterName' is not passed for arc cluster.")
} }
//await az_login.main(); azPath = await io.which("az", true);
await az_login.executeAzCliCommand(`account show`, false); await executeAzCliCommand(`account show`, false);
try{ try{
await az_login.executeAzCliCommand(`extension remove -n connectedk8s`, false); await executeAzCliCommand(`extension remove -n connectedk8s`, false);
} }
catch{ catch{
//ignore if this causes an error //ignore if this causes an error
} }
await az_login.executeAzCliCommand(`extension add -n connectedk8s`, false); await executeAzCliCommand(`extension add -n connectedk8s`, false);
await az_login.executeAzCliCommand(`extension list`, false); await executeAzCliCommand(`extension list`, false);
const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated
const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`); const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`);
let azPath = await io.which("az", true);
if (method == 'service-account'){ if (method == 'service-account'){
let saToken = core.getInput('token'); let saToken = core.getInput('token');
if(!saToken){ if(!saToken){
@ -110,3 +111,17 @@ export async function getArcKubeconfig(): Promise<string> {
function sleep(ms) { function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
async function executeAzCliCommand(
command: string,
silent?: boolean,
execOptions: any = {},
args: any = []) {
execOptions.silent = !!silent;
try {
await exec.exec(`"${azPath}" ${command}`, args, execOptions);
}
catch (error) {
throw new Error(error);
}
}