Skip to content

Commit

Permalink
Add support for yaml configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
arturogonzalez58 committed Jun 2, 2022
1 parent 34ba1f5 commit 292e488
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
17 changes: 12 additions & 5 deletions configure/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@ const installTelepresence = require('../src/install');
const cache = require('@actions/cache');

const telepresenceConfiguring = async function () {
const isInstalled = await installTelepresence.telepresenceInstall();
if(!isInstalled)
const telepresenceCacheKey = await installTelepresence.telepresenceInstall();
if(!telepresenceCacheKey)
return;

const path = configure.getTelepresenceConfigPath();
const telepresenceConfigDir = [path];

try {
await io.mkdirP(path);
await cache.restoreCache(telepresenceConfigDir, configure.TELEPRESENCE_CACHE_KEY);
await cache.restoreCache(telepresenceConfigDir, telepresenceCacheKey);
} catch (error) {
core.warning(`Unable to find the telepresence id: ${error}`);
}
// Create telepresence configuration file if provided
try {
await exec.exec(`${installTelepresence.TP_PATH}/telepresence`, ['connect']);
await configure.createClientConfigFile();
} catch(err) {
core.setFailed(err);
return;
}
try {
await exec.exec('telepresence', ['connect']);
} catch (error) {
core.setFailed(error.message);
return;
}
try {
const cacheKey = await cache.saveCache(telepresenceConfigDir, configure.TELEPRESENCE_CACHE_KEY);
const cacheKey = await cache.saveCache(telepresenceConfigDir, telepresenceCacheKey);
if (!cacheKey)
core.setFailed('Unable to save the telepresence key cache');
} catch (error) {
Expand Down
61 changes: 56 additions & 5 deletions src/configure/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const core = require('@actions/core');
const io = require('@actions/io');
const cache = require('@actions/cache');
const fs = require('fs');

exports.fileExists = async function (filePath) {
try {
const stats = await fs.promises.stat(filePath);
if (!stats.isFile()) {
throw new Error('client_values_file must be a file.');
}
return true;
} catch (err) {
if (err.code === 'ENOENT') {
return false;
}
throw err;
}
};

exports.getTelepresenceConfigPath = () => {
switch (process.platform) {
Expand All @@ -15,25 +31,60 @@ exports.getTelepresenceConfigPath = () => {
};

exports.getConfiguration = async () => {
const telepresenceCacheKey = process.env.TELEPRESENCE_CACHE_KEY;
if (!telepresenceCacheKey)
return false;
const path = this.getTelepresenceConfigPath();
try {
await io.mkdirP(path);
const cacheid = await cache.restoreCache([path], this.TELEPRESENCE_CACHE_KEY,)
if (!cacheid){
const cacheid = await cache.restoreCache([path], telepresenceCacheKey);
if (!cacheid) {
core.setFailed('Unable to find a telepresence install id stored');
return false;
}

} catch (error) {
core.setFailed(error);
return false;
}
return true;
};

/**
* Copies the given client configuration file to the user's Telepresence configuration directory
*/
exports.createClientConfigFile = async function () {
let fileExists = false;
try {
fileExists = await this.fileExists(this.TELEPRESENCE_CONFIG_FILE_PATH);
} catch (err) {
core.warning('Error accessing telepresence config file. ' + err);
return;
}
if (!fileExists) {
return;
}

const telepresenceConfigDir = this.getTelepresenceConfigPath();
await exec.exec('cp', [this.TELEPRESENCE_CONFIG_FILE_PATH, telepresenceConfigDir + '/config.yml']);
}

exports.checksumConfigFile = function (algorithm) {
const filePath = process.env.GITHUB_WORKSPACE + this.TELEPRESENCE_CONFIG_FILE_PATH;
return new Promise(function (resolve, reject) {
let crypto = require('crypto');

let hash = crypto.createHash(algorithm).setEncoding('hex');
fs.createReadStream(filePath)
.once('error', reject)
.pipe(hash)
.once('finish', function () {
resolve(hash.read());
});
});
}

exports.TELEPRESENCE_ID_STATE = 'telepresence-id-state';
exports.TELEPRESENCE_ID_SAVES = 'telepresence-saves';
exports.TELEPRESENCE_ID_SAVED = 'telepresence-saved';
exports.TELEPRESENCE_CACHE_KEY = 'telepresence_cache_key';

exports.TELEPRESENCE_CONFIG_FILE_PATH = '/.github/telepresence-config/config.yml'

18 changes: 14 additions & 4 deletions src/install/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const core = require('@actions/core');
const toolCache = require('@actions/tool-cache');
const cache = require('@actions/cache');
const configure = require('../configure');
const exec = require('@actions/exec');

const TP_INSTALL_CACHE_ID = 'telepresence-install-id';
Expand Down Expand Up @@ -37,20 +38,29 @@ const unixInstall = async (version) => {
};

exports.telepresenceInstall = async () => {
const version = core.getInput('version');
let configFileSha = '00000';
try {
configFileSha = await configure.checksumConfigFile('sha1');
} catch (err) {
core.info('No telepresence configuration file found.');
}
const telepresenceCacheKey = `TELEPRESENCE-${version}-${configFileSha}`;
core.exportVariable('TELEPRESENCE_CACHE_KEY', telepresenceCacheKey);
try {
const version = core.getInput('version');
switch (process.platform) {
case "win32":
return await windowsInstall(version);
return await windowsInstall(version) && telepresenceCacheKey;
case "linux":
case "darwin":
return await unixInstall(version);
return await unixInstall(version) && telepresenceCacheKey;
default:
core.setFailed("Invalid runner platform");
return false;
return undefined;
}
} catch (error) {
core.setFailed(error.message);
return undefined;
}
};

Expand Down

0 comments on commit 292e488

Please sign in to comment.