Skip to content

Commit 975906b

Browse files
committed
migrate env with importing stack
1 parent aa08261 commit 975906b

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/lib/services/docker/stack-service.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ export async function importExternalStack(stackId: string): Promise<Stack> {
10591059

10601060
// 3. Read the compose file if available, or create a new one
10611061
let composeContent = '';
1062+
let envContent: string | undefined = undefined; // Variable to store .env content
10621063
let actualComposeFilePathUsed = ''; // For logging the path that was attempted
10631064

10641065
const configFilesLabel = labels['com.docker.compose.project.config_files'];
@@ -1094,6 +1095,21 @@ export async function importExternalStack(stackId: string): Promise<Stack> {
10941095
console.log(`Attempting to read compose file for import from: ${actualComposeFilePathUsed}`);
10951096
composeContent = await fs.readFile(actualComposeFilePathUsed, 'utf8');
10961097
console.log(`Successfully read compose file: ${actualComposeFilePathUsed}. Content length: ${composeContent.length}`);
1098+
1099+
// Attempt to read .env file from the same directory
1100+
const composeFileDir = path.dirname(actualComposeFilePathUsed);
1101+
const envFilePath = path.join(composeFileDir, '.env');
1102+
try {
1103+
envContent = await fs.readFile(envFilePath, 'utf8');
1104+
console.log(`Successfully read .env file from: ${envFilePath}`);
1105+
} catch (envErr) {
1106+
const nodeEnvErr = envErr as NodeJS.ErrnoException;
1107+
if (nodeEnvErr.code === 'ENOENT') {
1108+
console.log(`.env file not found at ${envFilePath}, proceeding without it.`);
1109+
} else {
1110+
console.warn(`Could not read .env file at ${envFilePath} during import:`, envErr);
1111+
}
1112+
}
10971113
} catch (err) {
10981114
console.warn(`Could not read compose file at ${actualComposeFilePathUsed} during import:`, err);
10991115
// composeContent will remain empty, leading to generation logic below
@@ -1117,7 +1133,7 @@ export async function importExternalStack(stackId: string): Promise<Stack> {
11171133
const serviceName = containerLabels['com.docker.compose.service'] || cont.Names[0]?.replace(`/${stackId}_`, '').replace(/_\d+$/, '') || `service_${cont.Id.substring(0, 8)}`;
11181134

11191135
// Inspect the container to get more details
1120-
const containerDetails = await docker.getContainer(cont.Id).inspect();
1136+
// const containerDetails = await docker.getContainer(cont.Id).inspect(); // Uncomment if more details are needed
11211137

11221138
services[serviceName] = {
11231139
image: cont.Image
@@ -1135,11 +1151,12 @@ export async function importExternalStack(stackId: string): Promise<Stack> {
11351151
11361152
services:
11371153
${yaml.dump({ services }, { indent: 2 }).substring('services:'.length).trimStart()}`;
1154+
// Note: If compose file is generated, we don't have a path to look for an associated .env file.
1155+
// envContent will remain undefined in this case.
11381156
}
11391157

11401158
// 5. Create a new stack in Arcane's managed stacks
1141-
// The createStack function will set hasArcaneMeta to true.
1142-
return await createStack(stackId, composeContent);
1159+
return await createStack(stackId, composeContent, envContent); // Pass envContent here
11431160
}
11441161

11451162
/**

0 commit comments

Comments
 (0)