Skip to content

Commit

Permalink
fix: argument parsing for observer
Browse files Browse the repository at this point in the history
  • Loading branch information
gigatim committed Oct 1, 2023
1 parent 98870b5 commit ad42b82
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
12 changes: 9 additions & 3 deletions gigalixir/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ def ssh(host, app_name, ssh_opts, ssh_cmd, *args):
# used by ssh, remote_console, distillery_command
ssh_helper(host, app_name, ssh_opts, ssh_cmd, False, *args)

def ssh_add_identity_option(ssh_opts):
# use the identity file specified by environment variable
id_file = os.environ.get("GIGALIXIR_IDENTITY_FILE")
if id_file and not re.match(r'(^|[\s])-i', ssh_opts):
return (ssh_opts + " -i " + id_file).strip()

return ssh_opts

# if using this from a script, and you want the return
# value in a variable, use capture_output=True
# capture_output needs to be False for remote_console
Expand All @@ -142,9 +150,7 @@ def ssh_helper(host, app_name, ssh_opts, ssh_cmd, capture_output, *args):
raise Exception("You don't have any ssh keys yet. See `gigalixir account:ssh_keys:add --help`")

# use the identity file specified by environment variable
id_file = os.environ.get("GIGALIXIR_IDENTITY_FILE")
if id_file and not re.match(r'(^|[\s])-i', ssh_opts):
ssh_opts = (ssh_opts + " -i " + id_file).strip()
ssh_opts = ssh_add_identity_option(ssh_opts)

# if -T and -t are not specified, add the -t option
if not re.match(r'(^|[\s])-[Tt]', ssh_opts):
Expand Down
17 changes: 12 additions & 5 deletions gigalixir/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def observer(ctx, app_name, erlang_cookie, ssh_opts, ssh_cmd):
ssh_master_pid = None
control_path = "/tmp/gigalixir-cm-%s" % uuid.uuid4()
ssh_opts += " -S %s" % control_path
try:
ssh_opts = gigalixir_app.ssh_add_identity_option(ssh_opts)
try:
logging.getLogger("gigalixir-cli").info("Setting up SSH multiplexing master")
cmd = "".join(["ssh %s" % (ssh_opts), " root@%s -N -M" % (ssh_ip)])
ssh_master_pid = subprocess.Popen(cmd.split()).pid
Expand All @@ -55,15 +56,15 @@ def observer(ctx, app_name, erlang_cookie, ssh_opts, ssh_cmd):

logging.getLogger("gigalixir-cli").info("Fetching erlang cookie")
if erlang_cookie is None:
ERLANG_COOKIE = gigalixir_app.distillery_eval(host, app_name, ssh_opts, ssh_cmd, get_cookie_command).strip("'")
ERLANG_COOKIE = sanitize_respone(gigalixir_app.distillery_eval(host, app_name, ssh_opts, ssh_cmd, get_cookie_command))
else:
ERLANG_COOKIE = erlang_cookie
logging.getLogger("gigalixir-cli").info("Using erlang cookie: %s" % ERLANG_COOKIE)

logging.getLogger("gigalixir-cli").info("Fetching pod ip")
node_name = gigalixir_app.distillery_eval(host, app_name, ssh_opts, ssh_cmd, get_node_name_command)
node_name = sanitize_respone(gigalixir_app.distillery_eval(host, app_name, ssh_opts, ssh_cmd, get_node_name_command))
# node_name is surrounded with single quotes
(sname, MY_POD_IP) = node_name.strip("'").split('@')
(sname, MY_POD_IP) = node_name.split('@')
logging.getLogger("gigalixir-cli").info("Using pod ip: %s" % MY_POD_IP)
logging.getLogger("gigalixir-cli").info("Using node name: %s" % sname)
logging.getLogger("gigalixir-cli").info("Fetching epmd port and app port.")
Expand Down Expand Up @@ -137,9 +138,15 @@ def ensure_port_free(port):
# if the port is in use, then a pid is found, this "succeeds" and continues
# if the port is free, then a pid is not found, this "fails" and raises a CalledProcessError
pid = call("lsof -wni tcp:%(port)s -t" % {"port": port})
# If multiplexing gets supported later, on Windows this command would be:
# If multiplexing gets supported later, on Windows this command would be:
# pid = call("netstat -p tcp -n | find \"\"\":%(port)s\"\"\" % {"port": port})
raise Exception("It looks like process %s is using port %s on your local machine. We need this port to be able to connect observer. Please kill this process on your local machine and try again. e.g. `kill %s`" % (pid, port, pid))
except subprocess.CalledProcessError:
# success! continue
pass

def sanitize_respone(text):
match = re.match(r"^(~c\"|')(.+)[\"']$", text)
if match:
return match.groups()[1]
return text
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
url='https://github.com/gigalixir/gigalixir-cli',
author='Tim Day',
author_email='tim@gigalixir.com',
version='1.8.0',
version='1.8.1',
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down

0 comments on commit ad42b82

Please sign in to comment.