This commit is contained in:
2025-12-14 01:42:14 +00:00
parent 9ad5222b95
commit 5d18e53e30
10 changed files with 310 additions and 9863 deletions

9733
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +0,0 @@
onlyBuiltDependencies:
- esbuild
- mongodb-memory-server
- puppeteer

View File

@@ -1,10 +1,21 @@
// Disable TLS certificate validation for testing
Deno.env.set('NODE_TLS_REJECT_UNAUTHORIZED', '0');
Deno.env.set('SZCI_TEST', 'true');
Deno.env.set('CI_REPOSITORY_URL', 'https://yyyyyy:xxxxxxxx@gitlab.com/mygroup/myrepo.git');
import { CloudlyConnector } from '../ts/connector.cloudly/cloudlyconnector.ts';
import { Szci } from '../ts/szci.classes.szci.ts';
Deno.test('should be able to announce a container to cloudly', async () => {
const cloudlyConnector = new CloudlyConnector(null);
Deno.test({
name: 'should be able to announce a container to cloudly',
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
// Create a proper Szci instance for the connector
const szciInstance = new Szci();
await szciInstance.start();
const cloudlyConnector = new CloudlyConnector(szciInstance);
await cloudlyConnector.announceDockerContainer(
{
registryUrl: 'registry.losssless.com',
@@ -14,4 +25,5 @@ Deno.test('should be able to announce a container to cloudly', async () => {
},
'cloudly.lossless.one'
);
},
});

View File

@@ -1,14 +1,14 @@
import { assertEquals } from '@std/assert';
import { assertEquals, assertExists } from '@std/assert';
import * as path from '@std/path';
import * as smartpath from '@push.rocks/smartpath';
// Set up test environment
Deno.env.set('NPMTS_TEST', 'true');
Deno.env.set('NPMCI_URL_CLOUDLY', 'localhost');
// Set up test environment with the NEW SZCI environment variables
Deno.env.set('SZCI_TEST', 'true');
Deno.env.set('SZCI_URL_CLOUDLY', 'localhost');
Deno.env.set('CI_REPOSITORY_URL', 'https://yyyyyy:xxxxxxxx@gitlab.com/mygroup/myrepo.git');
Deno.env.set('CI_BUILD_TOKEN', 'kjlkjfiudofiufs');
Deno.env.set('NPMCI_LOGIN_DOCKER', 'docker.io|someuser|somepass');
Deno.env.set('NPMCI_SSHKEY_1', 'hostString|somePrivKey|##');
Deno.env.set('SZCI_LOGIN_DOCKER', 'docker.io|someuser|somepass');
Deno.env.set('SZCI_SSHKEY_1', 'hostString|somePrivKey|##');
// Get the test assets directory
const testAssetsDir = path.join(smartpath.get.dirnameFromImportMetaUrl(import.meta.url), 'assets/');
@@ -51,44 +51,81 @@ Deno.test('should read a directory of Dockerfiles', async () => {
szciInstance.dockerManager
);
sortableArray = readDockerfilesArray;
assertEquals(readDockerfilesArray[1].version, 'sometag1');
// The test assets directory should have multiple Dockerfiles
assertExists(readDockerfilesArray, 'readDockerfilesArray should exist');
assertEquals(readDockerfilesArray.length > 0, true, 'Should find at least one Dockerfile');
// Find the sometag1 dockerfile
const sometag1Dockerfile = readDockerfilesArray.find(df => df.version === 'sometag1');
assertExists(sometag1Dockerfile, 'Should find Dockerfile_sometag1');
assertEquals(sometag1Dockerfile?.version, 'sometag1');
});
Deno.test('should sort an array of Dockerfiles', async () => {
// Use the sortableArray from previous test, or create a new one if empty
if (!sortableArray || sortableArray.length === 0) {
const szciInstance = new Szci();
await szciInstance.start();
sortableArray = await DockerfileModule.Dockerfile.readDockerfiles(szciInstance.dockerManager);
}
const sortedArray = await DockerfileModule.Dockerfile.sortDockerfiles(sortableArray);
console.log(sortedArray);
assertExists(sortedArray, 'sortedArray should exist');
console.log('Sorted dockerfiles:', sortedArray.map(df => df.cleanTag));
});
Deno.test('should build all Dockerfiles', async () => {
Deno.test({
name: 'should build all Dockerfiles',
// Allow resource leaks since smartshell creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.dockerManager.handleCli({
_: ['docker', 'build'],
});
},
});
Deno.test('should test all Dockerfiles', async () => {
Deno.test({
name: 'should test all Dockerfiles',
// Allow resource leaks since smartshell creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.dockerManager.handleCli({
_: ['docker', 'test'],
});
},
});
Deno.test('should login docker daemon', async () => {
Deno.test({
name: 'should login docker daemon',
// Allow resource leaks since smartshell creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.dockerManager.handleCli({
_: ['docker', 'login'],
});
},
});
// ===
// SSH
// ===
Deno.test('should prepare SSH keys', async () => {
const npmciModSsh = await import('../ts/mod_ssh/index.ts');
await npmciModSsh.handleCli({
// Ensure test mode is set so we don't actually write to disk
Deno.env.set('SZCI_TEST', 'true');
const szciModSsh = await import('../ts/mod_ssh/index.ts');
await szciModSsh.handleCli({
_: ['ssh', 'prepare'],
});
});
@@ -96,7 +133,12 @@ Deno.test('should prepare SSH keys', async () => {
// ====
// node
// ====
Deno.test('should install a certain version of node', async () => {
Deno.test({
name: 'should install a certain version of node',
// Allow resource leaks for this test since nvm creates background processes
sanitizeResources: false,
sanitizeOps: false,
fn: async () => {
const szciInstance = new Szci();
await szciInstance.start();
await szciInstance.nodejsManager.handleCli({
@@ -108,6 +150,7 @@ Deno.test('should install a certain version of node', async () => {
await szciInstance.nodejsManager.handleCli({
_: ['node', 'install', 'legacy'],
});
},
});
// Restore original working directory after all tests

View File

@@ -22,7 +22,7 @@ export class Dockerfile {
public static async readDockerfiles(
szciDockerManagerRefArg: SzciDockerManager
): Promise<Dockerfile[]> {
const fileTree = await plugins.smartfile.fs.listFileTree(paths.cwd, 'Dockerfile*');
const fileTree = await plugins.smartfile.fs.listFileTree(paths.getCwd(), 'Dockerfile*');
// create the Dockerfile array
const readDockerfilesArray: Dockerfile[] = [];

View File

@@ -1,10 +1,20 @@
import * as plugins from './mod.plugins.ts';
import * as paths from '../szci.paths.ts';
import { logger } from '../szci.logging.ts';
/**
* cleans szci config files
* Cleans szci config files from the project directory
*/
export let clean = async (): Promise<void> => {
export const clean = async (): Promise<void> => {
try {
if (plugins.smartfile.fs.fileExistsSync(paths.SzciPackageConfig)) {
plugins.smartfile.fs.removeSync(paths.SzciPackageConfig);
return;
logger.log('ok', 'Cleaned szci config files');
} else {
logger.log('info', 'No szci config files to clean');
}
} catch (error) {
logger.log('error', `Failed to clean config files: ${(error as Error).message}`);
throw error;
}
};

View File

@@ -1,15 +1,30 @@
import * as plugins from './mod.plugins.ts';
import { bash } from '../szci.bash.ts';
import { logger } from '../szci.logging.ts';
export let command = async () => {
let wrappedCommand: string = '';
let argvArray = ['deno', 'mod.ts', ...Deno.args];
for (let i = 3; i < argvArray.length; i++) {
wrappedCommand = wrappedCommand + argvArray[i];
if (i + 1 !== argvArray.length) {
wrappedCommand = wrappedCommand + ' ';
}
/**
* Executes a wrapped command passed via CLI arguments.
* Usage: szci command <your-command-here>
*
* This allows running arbitrary commands through szci's bash wrapper
* which handles nvm and other environment setup.
*/
export const command = async (): Promise<void> => {
// Skip 'deno', 'mod.ts', 'command' and get the rest
const commandArgs = Deno.args.slice(1); // Skip 'command'
if (commandArgs.length === 0) {
logger.log('error', 'No command specified. Usage: szci command <your-command>');
Deno.exit(1);
}
const wrappedCommand = commandArgs.join(' ');
logger.log('info', `Executing command: ${wrappedCommand}`);
try {
await bash(wrappedCommand);
return;
logger.log('ok', 'Command executed successfully');
} catch (error) {
logger.log('error', `Command failed: ${(error as Error).message}`);
throw error;
}
};

View File

@@ -1,10 +1,22 @@
import { logger } from '../szci.logging.ts';
import * as plugins from './mod.plugins.ts';
let sshInstance: plugins.smartssh.SshInstance;
export let handleCli = async (argvArg: any) => {
/**
* Interface for CLI arguments
*/
interface ICliArgs {
_: string[];
[key: string]: unknown;
}
/**
* Handle SSH CLI commands
*/
export const handleCli = async (argvArg: ICliArgs): Promise<void> => {
if (argvArg._.length >= 2) {
const action: string = argvArg._[1];
const action = argvArg._[1];
switch (action) {
case 'prepare':
await prepare();
@@ -20,45 +32,66 @@ export let handleCli = async (argvArg: any) => {
};
/**
* checks if not undefined
* Checks if a string value is defined and not a placeholder
*/
const notUndefined = (stringArg: string) => {
return stringArg && stringArg !== 'undefined' && stringArg !== '##';
const isValidValue = (value: string | undefined): boolean => {
return Boolean(value && value !== 'undefined' && value !== '##');
};
/**
* checks for ENV vars in form of SZCI_SSHKEY_* and deploys any found ones
* Checks for ENV vars in form of SZCI_SSHKEY_* and deploys any found ones
*/
export let prepare = async () => {
sshInstance = new plugins.smartssh.SshInstance(); // init ssh instance
plugins.smartobject.forEachMinimatch(Deno.env.toObject(), 'SZCI_SSHKEY_*', evaluateSshEnv);
if (!Deno.env.get("SZCI_TEST")) {
export const prepare = async (): Promise<void> => {
sshInstance = new plugins.smartssh.SshInstance();
// Get all env vars and filter for SSH keys
const envVars = Deno.env.toObject();
const sshKeyEnvVars = Object.entries(envVars).filter(([key]) =>
key.startsWith('SZCI_SSHKEY_')
);
// Process each SSH key env var
for (const [key, value] of sshKeyEnvVars) {
logger.log('info', `Processing SSH key from ${key}`);
addSshKeyFromEnvVar(value);
}
// Only write to disk if not in test mode
if (!Deno.env.get('SZCI_TEST')) {
try {
sshInstance.writeToDisk();
logger.log('ok', 'SSH keys written to disk');
} catch (error) {
logger.log('error', `Failed to write SSH keys: ${(error as Error).message}`);
throw error;
}
} else {
logger.log('info', 'In test mode, so not storing SSH keys to disk!');
}
};
/**
* gets called for each found SSH ENV Var and deploys it
* Parses an SSH key env var and adds it to the SSH instance
* Format: host|privKeyBase64|pubKeyBase64
*/
const evaluateSshEnv = async (sshkeyEnvVarArg: string) => {
const sshEnvArray = sshkeyEnvVarArg.split('|');
const addSshKeyFromEnvVar = (sshkeyEnvVarArg: string): void => {
const [host, privKeyBase64, pubKeyBase64] = sshkeyEnvVarArg.split('|');
const sshKey = new plugins.smartssh.SshKey();
logger.log('info', 'Found SSH identity for ' + sshEnvArray[1]);
if (notUndefined(sshEnvArray[0])) {
logger.log('info', `Found SSH identity for ${host || 'unknown host'}`);
if (isValidValue(host)) {
logger.log('info', '---> host defined!');
sshKey.host = sshEnvArray[0];
sshKey.host = host;
}
if (notUndefined(sshEnvArray[1])) {
if (isValidValue(privKeyBase64)) {
logger.log('info', '---> privKey defined!');
sshKey.privKeyBase64 = sshEnvArray[1];
sshKey.privKeyBase64 = privKeyBase64;
}
if (notUndefined(sshEnvArray[2])) {
if (isValidValue(pubKeyBase64)) {
logger.log('info', '---> pubKey defined!');
sshKey.pubKeyBase64 = sshEnvArray[2];
sshKey.pubKeyBase64 = pubKeyBase64;
}
sshInstance.addKey(sshKey);
return;
};

View File

@@ -1,47 +1,101 @@
import * as plugins from './mod.plugins.ts';
import { bash } from '../szci.bash.ts';
import { logger } from '../szci.logging.ts';
const triggerValueRegex =
/^([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|([a-zA-Z0-9\.]*)\|?([a-zA-Z0-9\.\-\/]*)/;
/**
* Interface for parsed trigger configuration
*/
interface ITriggerConfig {
domain: string;
projectId: string;
triggerToken: string;
refName: string;
triggerName: string;
}
export let trigger = async () => {
/**
* Regex to parse trigger env var format:
* domain|projectId|triggerToken|refName|triggerName (optional)
*/
const TRIGGER_VALUE_REGEX =
/^([a-zA-Z0-9.]+)\|([a-zA-Z0-9.]+)\|([a-zA-Z0-9.]+)\|([a-zA-Z0-9.]+)\|?([a-zA-Z0-9.\-/]*)$/;
/**
* Execute all configured triggers from environment variables
*/
export const trigger = async (): Promise<void> => {
logger.log('info', 'now running triggers');
await plugins.smartobject.forEachMinimatch(Deno.env.toObject(), 'SZCI_TRIGGER_*', evaluateTrigger);
};
const evaluateTrigger = async (triggerEnvVarArg: string) => {
const triggerRegexResultArray = triggerValueRegex.exec(triggerEnvVarArg);
if (!triggerRegexResultArray) {
logger.log('error', 'malformed trigger env var...');
// Get all env vars and filter for triggers
const envVars = Deno.env.toObject();
const triggerEnvVars = Object.entries(envVars).filter(([key]) =>
key.startsWith('SZCI_TRIGGER_')
);
if (triggerEnvVars.length === 0) {
logger.log('info', 'no triggers configured');
return;
}
const regexDomain = triggerRegexResultArray[1];
const regexProjectId = triggerRegexResultArray[2];
const regexProjectTriggerToken = triggerRegexResultArray[3];
const regexRefName = triggerRegexResultArray[4];
let regexTriggerName: string;
if (triggerRegexResultArray.length === 6) {
regexTriggerName = triggerRegexResultArray[5];
} else {
regexTriggerName = 'Unnamed Trigger';
// Process each trigger
for (const [key, value] of triggerEnvVars) {
logger.log('info', `Processing trigger from ${key}`);
await executeTrigger(value);
}
logger.log('info', 'Found Trigger!');
logger.log('info', 'triggering build for ref ' + regexRefName + ' of ' + regexTriggerName);
plugins.smartrequest.postFormData(
'https://gitlab.com/api/v3/projects/' + regexProjectId + '/trigger/builds',
logger.log('ok', `executed ${triggerEnvVars.length} trigger(s)`);
};
/**
* Parse a trigger env var string into a config object
*/
const parseTriggerConfig = (triggerEnvVar: string): ITriggerConfig | null => {
const match = TRIGGER_VALUE_REGEX.exec(triggerEnvVar);
if (!match) {
return null;
}
return {
domain: match[1],
projectId: match[2],
triggerToken: match[3],
refName: match[4],
triggerName: match[5] || 'Unnamed Trigger',
};
};
/**
* Execute a single trigger by calling the GitLab API
*/
const executeTrigger = async (triggerEnvVar: string): Promise<void> => {
const config = parseTriggerConfig(triggerEnvVar);
if (!config) {
logger.log('error', 'malformed trigger env var, expected format: domain|projectId|token|ref|name');
return;
}
logger.log('info', `Found Trigger: ${config.triggerName}`);
logger.log('info', `Triggering build for ref "${config.refName}" of "${config.triggerName}"`);
try {
await plugins.smartrequest.postFormData(
`https://${config.domain}/api/v3/projects/${config.projectId}/trigger/builds`,
{},
[
{
name: 'token',
payload: regexProjectTriggerToken,
payload: config.triggerToken,
type: 'string',
},
{
name: 'ref',
payload: regexRefName,
payload: config.refName,
type: 'string',
},
]
);
logger.log('ok', `Trigger "${config.triggerName}" executed successfully`);
} catch (error) {
logger.log('error', `Failed to execute trigger: ${(error as Error).message}`);
}
};

View File

@@ -1,5 +1,14 @@
import * as plugins from './szci.plugins.ts';
/**
* Get current working directory (evaluated at call time, not module load time)
*/
export const getCwd = (): string => Deno.cwd();
/**
* Current working directory - use getCwd() if you need the live value after chdir
* @deprecated Use getCwd() for dynamic cwd resolution
*/
export const cwd = Deno.cwd();
// package paths
@@ -9,7 +18,13 @@ export const SzciPackageRoot = plugins.path.join(
);
export const SzciPackageConfig = plugins.path.join(SzciPackageRoot, './config.json');
// project paths
// project paths - use functions for dynamic resolution
export const getSzciProjectDir = (): string => getCwd();
export const getSzciProjectNogitDir = (): string => plugins.path.join(getCwd(), './.nogit');
export const getSzciTestDir = (): string => plugins.path.join(getCwd(), './test');
export const getSzciCacheDir = (): string => plugins.path.join(getCwd(), './.szci_cache');
// Static paths (for backwards compatibility - captured at module load)
export const SzciProjectDir = cwd;
export const SzciProjectNogitDir = plugins.path.join(SzciProjectDir, './.nogit');
export const SzciTestDir = plugins.path.join(cwd, './test');