@jorgeceballos/mcp-server-oci
Version:
Model Context Protocol server for Oracle Cloud Infrastructure
142 lines (141 loc) • 5.2 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCIClient = void 0;
const oci_sdk_1 = require("oci-sdk");
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
class OCIClient {
constructor(profileName = 'DEFAULT') {
// Initialize OCI clients using the default config file
const configPath = path.join(os.homedir(), '.oci', 'config');
if (!fs.existsSync(configPath)) {
throw new Error(`OCI config file not found at ${configPath}`);
}
this.provider = new oci_sdk_1.common.ConfigFileAuthenticationDetailsProvider(configPath, profileName);
this.computeClient = new oci_sdk_1.core.ComputeClient({
authenticationDetailsProvider: this.provider
});
this.identityClient = new oci_sdk_1.identity.IdentityClient({
authenticationDetailsProvider: this.provider
});
}
/**
* List available compartments
*/
async listCompartments() {
const tenantId = this.provider.getTenantId();
const request = {
compartmentId: tenantId,
compartmentIdInSubtree: true,
lifecycleState: oci_sdk_1.identity.models.Compartment.LifecycleState.Active
};
const response = await this.identityClient.listCompartments(request);
return response.items;
}
/**
* List compute instances in a compartment
*/
async listInstances(compartmentId) {
const request = {
compartmentId: compartmentId
};
const response = await this.computeClient.listInstances(request);
return response.items;
}
/**
* Get instance details
*/
async getInstance(instanceId) {
const response = await this.computeClient.getInstance({
instanceId: instanceId
});
return response.instance;
}
/**
* Start a compute instance
*/
async startInstance(instanceId) {
const response = await this.computeClient.instanceAction({
instanceId: instanceId,
action: 'START'
});
// Wait for the operation to complete
await this.waitForInstanceState(instanceId, 'RUNNING');
}
/**
* Stop a compute instance
*/
async stopInstance(instanceId) {
const response = await this.computeClient.instanceAction({
instanceId: instanceId,
action: 'STOP'
});
// Wait for the operation to complete
await this.waitForInstanceState(instanceId, 'STOPPED');
}
/**
* Restart a compute instance
*/
async restartInstance(instanceId) {
const response = await this.computeClient.instanceAction({
instanceId: instanceId,
action: 'RESET'
});
// Wait for the operation to complete
await this.waitForInstanceState(instanceId, 'RUNNING');
}
/**
* Wait for an instance to reach a specific state
*/
async waitForInstanceState(instanceId, targetState, maxWaitTimeMs = 300000) {
const startTime = Date.now();
let currentState = '';
while (currentState !== targetState && (Date.now() - startTime) < maxWaitTimeMs) {
const instance = await this.getInstance(instanceId);
currentState = instance.lifecycleState || '';
if (currentState === targetState) {
return;
}
// Wait 5 seconds before checking again
await new Promise(resolve => setTimeout(resolve, 5000));
}
if (currentState !== targetState) {
throw new Error(`Timeout waiting for instance ${instanceId} to reach state ${targetState}`);
}
}
}
exports.OCIClient = OCIClient;