Pass .env to commands being executed to always match new changes
This commit is contained in:
parent
a78926dd37
commit
d8c7120b5f
1 changed files with 47 additions and 3 deletions
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import subprocess
|
import os
|
||||||
|
import re
|
||||||
import time
|
import time
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
@ -121,6 +122,35 @@ class ContainerManager:
|
||||||
self._compose_search_log += f"\n 3. Falling back to: {cwd_path.absolute()}"
|
self._compose_search_log += f"\n 3. Falling back to: {cwd_path.absolute()}"
|
||||||
return Path(filename)
|
return Path(filename)
|
||||||
|
|
||||||
|
def _get_env_from_file(self) -> Dict[str, str]:
|
||||||
|
"""Read environment variables from .env file, prioritizing file values over os.environ.
|
||||||
|
|
||||||
|
This ensures Docker Compose commands use the latest values from .env file,
|
||||||
|
even if os.environ has stale values.
|
||||||
|
"""
|
||||||
|
env = dict(os.environ) # Start with current environment
|
||||||
|
env_file = Path(".env")
|
||||||
|
|
||||||
|
if env_file.exists():
|
||||||
|
try:
|
||||||
|
from ..utils.validation import sanitize_env_value
|
||||||
|
with open(env_file, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
if "=" in line:
|
||||||
|
key, value = line.split("=", 1)
|
||||||
|
key = key.strip()
|
||||||
|
value = sanitize_env_value(value)
|
||||||
|
# Override os.environ with .env file values
|
||||||
|
# This ensures Docker Compose uses the latest .env values
|
||||||
|
env[key] = value
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Error reading .env file for Docker Compose: {e}")
|
||||||
|
|
||||||
|
return env
|
||||||
|
|
||||||
def is_available(self) -> bool:
|
def is_available(self) -> bool:
|
||||||
"""Check if container runtime with compose is available."""
|
"""Check if container runtime with compose is available."""
|
||||||
return (self.runtime_info.runtime_type != RuntimeType.NONE and
|
return (self.runtime_info.runtime_type != RuntimeType.NONE and
|
||||||
|
|
@ -153,7 +183,6 @@ class ContainerManager:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import re
|
|
||||||
content = compose_file.read_text()
|
content = compose_file.read_text()
|
||||||
current_service = None
|
current_service = None
|
||||||
in_ports_section = False
|
in_ports_section = False
|
||||||
|
|
@ -245,11 +274,15 @@ class ContainerManager:
|
||||||
cmd.extend(args)
|
cmd.extend(args)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Get environment variables from .env file to ensure latest values
|
||||||
|
env = self._get_env_from_file()
|
||||||
|
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
*cmd,
|
*cmd,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
stderr=asyncio.subprocess.PIPE,
|
stderr=asyncio.subprocess.PIPE,
|
||||||
cwd=Path.cwd(),
|
cwd=Path.cwd(),
|
||||||
|
env=env,
|
||||||
)
|
)
|
||||||
|
|
||||||
stdout, stderr = await process.communicate()
|
stdout, stderr = await process.communicate()
|
||||||
|
|
@ -287,11 +320,15 @@ class ContainerManager:
|
||||||
cmd.extend(args)
|
cmd.extend(args)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Get environment variables from .env file to ensure latest values
|
||||||
|
env = self._get_env_from_file()
|
||||||
|
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
*cmd,
|
*cmd,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
stderr=asyncio.subprocess.STDOUT,
|
stderr=asyncio.subprocess.STDOUT,
|
||||||
cwd=Path.cwd(),
|
cwd=Path.cwd(),
|
||||||
|
env=env,
|
||||||
)
|
)
|
||||||
|
|
||||||
if process.stdout:
|
if process.stdout:
|
||||||
|
|
@ -356,11 +393,15 @@ class ContainerManager:
|
||||||
cmd.extend(args)
|
cmd.extend(args)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Get environment variables from .env file to ensure latest values
|
||||||
|
env = self._get_env_from_file()
|
||||||
|
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
*cmd,
|
*cmd,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
stderr=asyncio.subprocess.STDOUT,
|
stderr=asyncio.subprocess.STDOUT,
|
||||||
cwd=Path.cwd(),
|
cwd=Path.cwd(),
|
||||||
|
env=env,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
success_flag["value"] = False
|
success_flag["value"] = False
|
||||||
|
|
@ -926,7 +967,6 @@ class ContainerManager:
|
||||||
|
|
||||||
async for message, replace_last in self._stream_compose_command(["up", "-d"], up_success, cpu_mode):
|
async for message, replace_last in self._stream_compose_command(["up", "-d"], up_success, cpu_mode):
|
||||||
# Detect error patterns in the output
|
# Detect error patterns in the output
|
||||||
import re
|
|
||||||
lower_msg = message.lower()
|
lower_msg = message.lower()
|
||||||
|
|
||||||
# Check for common error patterns
|
# Check for common error patterns
|
||||||
|
|
@ -1110,11 +1150,15 @@ class ContainerManager:
|
||||||
cmd.extend(["logs", "-f", service_name])
|
cmd.extend(["logs", "-f", service_name])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Get environment variables from .env file to ensure latest values
|
||||||
|
env = self._get_env_from_file()
|
||||||
|
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
*cmd,
|
*cmd,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
stderr=asyncio.subprocess.STDOUT,
|
stderr=asyncio.subprocess.STDOUT,
|
||||||
cwd=Path.cwd(),
|
cwd=Path.cwd(),
|
||||||
|
env=env,
|
||||||
)
|
)
|
||||||
|
|
||||||
if process.stdout:
|
if process.stdout:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue