-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker-demo.js
More file actions
165 lines (139 loc) Β· 5.79 KB
/
docker-demo.js
File metadata and controls
165 lines (139 loc) Β· 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
// Docker Demo Script for OpenAgent
import { shellTools } from './dist/shell-execution/shell-tools.js';
import { DockerValidator } from './dist/shell-execution/docker-validator.js';
console.log('π³ OpenAgent Docker Integration Demo');
console.log('=====================================\n');
async function runDemo() {
try {
// 1. Check Docker availability
console.log('1. Checking Docker availability...');
const dockerCheck = await DockerValidator.checkDockerAvailability();
if (dockerCheck.available) {
console.log(`β
Docker is available (version: ${dockerCheck.version})`);
} else {
console.log(`β Docker is not available: ${dockerCheck.error}`);
console.log('Please install Docker to continue with the demo.');
return;
}
// 2. Test Docker command validation
console.log('\n2. Testing Docker command validation...');
const testCommands = [
'docker --version',
'docker ps',
'docker images',
'docker run --rm hello-world',
'docker run --privileged ubuntu', // Should trigger warning
'docker system prune -f', // Should trigger critical warning
'docker run -v /:/host ubuntu' // Should trigger security warning
];
for (const cmd of testCommands) {
console.log(`\n Testing: ${cmd}`);
const validation = DockerValidator.validateDockerCommand(cmd);
console.log(` Risk Level: ${validation.riskLevel.toUpperCase()}`);
console.log(` Safe: ${validation.safe ? 'β
' : 'β'}`);
if (validation.issues.length > 0) {
console.log(' Issues:');
validation.issues.forEach(issue => {
const icon = issue.severity === 'critical' ? 'π¨' :
issue.severity === 'error' ? 'β' :
issue.severity === 'warning' ? 'β οΈ' : 'βΉοΈ';
console.log(` ${icon} ${issue.message}`);
});
}
if (validation.suggestions.length > 0) {
console.log(' Suggestions:');
validation.suggestions.forEach(suggestion => {
console.log(` π‘ ${suggestion}`);
});
}
}
// 3. Test safe Docker commands execution
console.log('\n3. Testing safe Docker command execution...');
const safeCommands = [
'docker --version',
'docker ps -a',
'docker images'
];
for (const cmd of safeCommands) {
console.log(`\n Executing: ${cmd}`);
try {
const result = await shellTools.executeShellCommand(cmd, {
safetyLevel: 'moderate',
enableLearning: false
});
if (result.success) {
console.log(' β
Success');
console.log(` Output: ${result.output?.trim() || 'No output'}`);
} else {
console.log(' β Failed');
console.log(` Error: ${result.error}`);
}
} catch (error) {
console.log(' β Exception');
console.log(` Error: ${error.message}`);
}
}
// 4. Test Docker tools (if available)
console.log('\n4. Testing Docker tools...');
try {
// Import Docker tools
const { dockerTools } = await import('./dist/tools/docker-tools.js');
console.log(` Available Docker tools: ${dockerTools.length}`);
dockerTools.forEach(tool => {
console.log(` - ${tool.name}: ${tool.description}`);
});
// Test list containers tool
console.log('\n Testing list_docker_containers tool...');
const listContainersResult = await dockerTools.find(t => t.name === 'list_docker_containers')?.fn({
all: true
});
if (listContainersResult?.success) {
console.log(' β
List containers successful');
console.log(` Output: ${listContainersResult.output?.trim() || 'No containers'}`);
} else {
console.log(' β List containers failed');
console.log(` Error: ${listContainersResult?.error}`);
}
} catch (error) {
console.log(` β Docker tools not available: ${error.message}`);
}
// 5. Test Docker Compose detection
console.log('\n5. Testing Docker Compose detection...');
try {
const composeResult = await shellTools.executeShellCommand('docker-compose --version', {
safetyLevel: 'moderate'
});
if (composeResult.success) {
console.log(' β
Docker Compose is available');
console.log(` Version: ${composeResult.output?.trim()}`);
} else {
console.log(' β Docker Compose not available');
}
} catch (error) {
console.log(' β Docker Compose check failed');
}
// 6. Show Docker best practices
console.log('\n6. Docker Best Practices for OpenAgent:');
console.log(' π Recommended practices:');
console.log(' β’ Always specify image tags (avoid :latest)');
console.log(' β’ Use memory limits for containers');
console.log(' β’ Avoid privileged mode unless necessary');
console.log(' β’ Use specific volume mounts instead of root');
console.log(' β’ Prefer bridge networking over host networking');
console.log(' β’ Use multi-stage builds for smaller images');
console.log(' β’ Scan images for vulnerabilities');
console.log(' β’ Use .dockerignore files');
console.log('\nβ
Docker integration demo completed successfully!');
console.log('\nπ You can now use Docker commands in OpenAgent with:');
console.log(' β’ Enhanced safety validation');
console.log(' β’ Automatic risk assessment');
console.log(' β’ Helpful suggestions');
console.log(' β’ Specialized Docker tools');
} catch (error) {
console.error('β Demo failed:', error.message);
console.error('Stack trace:', error.stack);
}
}
// Run the demo
runDemo().catch(console.error);