This code defines a function getIdentityServer
that automates the process of building, running, and managing a Docker container for an identity server, ensuring a fresh container is used each time.
npm run import -- "set up identity server"
var path = require('path');
// TODO: fix mono build step
var execSync = require('child_process').execSync;
var getIdentityServer = (name = 'act-identity') => {
var DOCKERFILE = path.resolve(__dirname);
return identityDockerfile(DOCKERFILE)
.then(r => execSync('docker build -t ' + name + ' "' + DOCKERFILE + '"').toString())
.then(r => {
var ps = execSync('docker ps -a').toString();
if (ps.indexOf(name) !== -1) {
return r
+ execSync('docker stop ' + name).toString()
+ execSync('docker rm ' + name).toString();
}
return '';
})
.then(r => r + execSync('docker run --name ' + name + ' ' + name).toString())
// wait for process to start
.then(() => new Promise((resolve, reject) =>
setTimeout(resolve, 5000)));
};
(getIdentityServer);
const path = require('path');
const { execSync } = require('child_process');
/**
* Retrieves the identity server information for a given Docker container.
*
* @param {string} [name='act-identity'] The name of the Docker container.
* @returns {Promise} A promise resolving to the identity server information.
*/
async function getIdentityServer(name = 'act-identity') {
const dockerfilePath = path.resolve(__dirname);
const dockerfile = identityDockerfile(dockerfilePath);
try {
await dockerfile;
} catch (error) {
// TODO: Improve error handling for failed Docker build
throw error;
}
try {
await buildDockerImage(name, dockerfilePath);
} catch (error) {
// TODO: Improve error handling for failed Docker image build
throw error;
}
const containerName = await getRunningContainerName(name);
if (containerName) {
await stopAndRemoveContainer(name);
}
await runDockerContainer(name);
// Wait for the process to start
await new Promise((resolve) => {
setTimeout(resolve, 5000);
});
return `Identity server started for container ${name}`;
}
// Helper functions
async function identityDockerfile(dockerfilePath) {
// Return a promise that resolves with no value (simulating the return value of identityDockerfile)
return Promise.resolve();
}
async function buildDockerImage(name, dockerfilePath) {
const output = execSync(`docker build -t ${name} "${dockerfilePath}"`).toString();
return output;
}
async function getRunningContainerName(name) {
const output = execSync('docker ps -a').toString();
const containerName = output.indexOf(name)!== -1? name : null;
return containerName;
}
async function stopAndRemoveContainer(name) {
execSync(`docker stop ${name}`).toString();
execSync(`docker rm ${name}`).toString();
}
async function runDockerContainer(name) {
execSync(`docker run --name ${name} ${name}`).toString();
}
// Call the function
getIdentityServer().then((output) => {
console.log(output);
});
This code defines a function getIdentityServer
that builds and runs a Docker container for an identity server.
Here's a breakdown:
Dependencies:
path
: Used for working with file paths.child_process
: Used to execute shell commands.getIdentityServer
Function:
name
parameter (defaults to 'act-identity') for the container name.DOCKERFILE
: Sets the path to the Dockerfile.identityDockerfile(DOCKERFILE)
: Calls a function (not shown) likely to perform some preparation based on the Dockerfile.Docker Build:
execSync('docker build -t ' + name + ' "' + DOCKERFILE + '"')
: Builds the Docker image with the specified name.Container Management:
Container Run:
execSync('docker run --name ' + name + ' ' + name)
: Runs the container with the specified name.Wait for Startup:
setTimeout
to wait for 5 seconds to allow the container to start before resolving the promise.Return Value:
Function Call:
(getIdentityServer)
: Immediately calls the getIdentityServer
function.In essence, this code automates the process of building, running, and managing a Docker container for an identity server.