Skip to content

Commit

Permalink
Add --env-file and --env-json options. Fixes #608
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhay Saxena committed May 10, 2018
1 parent 2c1641b commit e898d90
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
19 changes: 19 additions & 0 deletions telepresence/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,25 @@ def parse_args(args=None) -> argparse.Namespace:
)
)

parser.add_argument(
"--env-json",
metavar="FILENAME",
default=None,
help="Also emit the remote environment to a file as a JSON blob."
)

parser.add_argument(
"--env-file",
metavar="FILENAME",
default=None,
help=(
"Also emit the remote environment to an env file in Docker "
"Compose format. "
"See https://docs.docker.com/compose/env-file/ for more "
"information on the limitations of this format."
)
)

group = parser.add_mutually_exclusive_group()
group.add_argument(
"--run-shell",
Expand Down
5 changes: 4 additions & 1 deletion telepresence/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from telepresence.output import Output
from telepresence.proxy import start_proxy, connect
from telepresence.mount import mount_remote
from telepresence.remote_env import get_remote_env
from telepresence.remote_env import get_remote_env, write_env_files
from telepresence.startup import analyze_args
from telepresence.usage_tracking import call_scout

Expand Down Expand Up @@ -89,6 +89,9 @@ def shutdown(signum, frame):
# Handle filesystem stuff (pod name, ssh object)
mount_dir = mount_remote(session)

# Maybe write environment files
write_env_files(session)

# Set up outbound networking (pod name, ssh object)
# Launch user command with the correct environment (...)
if args.method == "container":
Expand Down
53 changes: 51 additions & 2 deletions telepresence/remote_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# limitations under the License.

import argparse
from json import loads
from json import loads, dump
from subprocess import CalledProcessError
from time import time, sleep
from typing import Dict
from typing import Dict, Tuple, List

from telepresence.remote import RemoteInfo
from telepresence.runner import Runner
Expand Down Expand Up @@ -80,3 +80,52 @@ def get_remote_env(
else:
return exit("Error: Failed to get environment variables")
return env


def serialize_as_env_file(env: Dict[str, str]) -> Tuple[str, List[str]]:
"""
Render an env file as defined by Docker Compose
https://docs.docker.com/compose/env-file/
- Compose expects each line in an env file to be in VAR=VAL format.
- Lines beginning with # are processed as comments and ignored.
- Blank lines are ignored.
- There is no special handling of quotation marks.
This means that they are part of the VAL.
Unstated but implied is that values cannot include newlines.
"""
res = []
skipped = []
for key, value in sorted(env.items()):
if "\n" in value:
skipped.append(key)
else:
res.append("{}={}\n".format(key, value))
return "".join(res), skipped


def write_env_files(session) -> None:
args = session.args
env = session.env
if args.env_json:
try:
with open(args.env_json, "w") as env_json_file:
dump(env, env_json_file, sort_keys=True, indent=4)
except IOError as exc:
print("Failed to write environment as JSON: {}".format(exc))

if args.env_file:
try:
data, skipped = serialize_as_env_file(env)
with open(args.env_file, "w") as env_file:
env_file.write(data)
if skipped:
print(
"Skipped these environment keys when writing env "
"file because the associated values have newlines:"
)
for key in skipped:
print(key)
except IOError as exc:
print("Failed to write environment as env file: {}".format(exc))

0 comments on commit e898d90

Please sign in to comment.