in pipe-cli/src/utilities/ssh_operations.py [0:0]
def check_existing_tunnels(host_id, local_ports, remote_ports,
ssh, ssh_path, ssh_host, ssh_users, direct, log_file, timeout_stop,
keep_existing, keep_same, replace_existing, replace_different, ignore_owner,
region, retries, parse_tunnel_args):
for existing_tunnel in find_tunnels(parse_tunnel_args):
existing_tunnel_args = TunnelArgs.from_args(existing_tunnel.parsed_args)
creating_tunnel_args = TunnelArgs(host_id=host_id, local_ports=local_ports, remote_ports=remote_ports,
ssh=ssh, ssh_path=ssh_path, ssh_host=ssh_host, ssh_users=ssh_users,
direct=direct)
if not any(local_port in creating_tunnel_args.local_ports for local_port in existing_tunnel_args.local_ports):
logging.debug('Skipping tunnel process #%s '
'because it has %s local ports but %s local ports are required...',
existing_tunnel.pid, stringify_ports(existing_tunnel_args.local_ports),
stringify_ports(local_ports))
continue
logging.info('Comparing the existing tunnel with the creating tunnel...')
is_same_tunnel = creating_tunnel_args.compare(existing_tunnel_args)
existing_tunnel_run_id = parse_run_identifier(existing_tunnel_args.host_id)
existing_tunnel_remote_host = existing_tunnel_args.ssh_host \
or 'pipeline-{}'.format(existing_tunnel_args.host_id)
if replace_existing:
logging.info('Trying to replace the existing tunnel...')
if not ignore_owner and has_different_owner(existing_tunnel.owner):
if is_same_tunnel:
raise TunnelError('Same tunnel already exists '
'and it cannot be replaced because it was launched by {tunnel_owner} user '
'which is not the same as the current user {current_owner}. \n\n'
'Usually there is no need to replace the same tunnel '
'but if it is required then you can either '
'specify other local ports to use for the tunnel '
'or stop the existing tunnel if you have sufficient permissions. '
'In order to stop the existing tunnel '
'execute the following command once. \n\n'
'{pipe_command} tunnel stop -lp {local_ports} --ignore-owner \n'
.format(tunnel_owner=existing_tunnel.owner,
current_owner=get_current_user(),
pipe_command=get_current_pipe_command(),
local_ports=stringify_ports(local_ports)))
else:
raise TunnelError('Different tunnel already exists on {local_ports} local ports '
'and it cannot be replaced because it was launched by {tunnel_owner} user '
'which is not the same as the current user {current_owner}. \n\n'
'You can either specify other local ports to use for the tunnel '
'or stop the existing tunnel if you have sufficient permissions. '
'In order to stop the existing tunnel '
'execute the following command once. \n\n'
'{pipe_command} tunnel stop -lp {local_ports} --ignore-owner \n'
.format(tunnel_owner=existing_tunnel.owner,
current_owner=get_current_user(),
pipe_command=get_current_pipe_command(),
local_ports=stringify_ports(existing_tunnel_args.local_ports)))
kill_tunnel(existing_tunnel.proc, existing_tunnel_args.local_ports, timeout_stop)
continue
if keep_existing:
logging.info('Skipping tunnel establishing because the tunnel already exists...')
if existing_tunnel_args.ssh and has_different_owner(existing_tunnel.owner):
configure_ssh(existing_tunnel_run_id,
existing_tunnel_args.local_ports[0], existing_tunnel_args.remote_ports[0],
existing_tunnel_args.ssh_path, ssh_users, existing_tunnel_remote_host,
log_file, retries)
sys.exit(0)
if replace_different and not is_same_tunnel:
logging.info('Trying to replace the existing tunnel because it is different...')
if not ignore_owner and has_different_owner(existing_tunnel.owner):
raise TunnelError('Different tunnel already exists on {local_ports} local ports '
'and it cannot be replaced because it was launched by {tunnel_owner} user '
'which is not the same as the current user {current_owner}. \n\n'
'You can either specify other local ports to use for the tunnel '
'or stop the existing tunnel if you have sufficient permissions. '
'In order to stop the existing tunnel '
'execute the following command once. \n\n'
'{pipe_command} tunnel stop -lp {local_ports} --ignore-owner \n'
.format(tunnel_owner=existing_tunnel.owner,
current_owner=get_current_user(),
pipe_command=get_current_pipe_command(),
local_ports=stringify_ports(existing_tunnel_args.local_ports)))
kill_tunnel(existing_tunnel.proc, existing_tunnel_args.local_ports, timeout_stop)
continue
if keep_same and is_same_tunnel:
logging.info('Skipping tunnel establishing because the same tunnel already exists...')
if existing_tunnel_args.ssh and has_different_owner(existing_tunnel.owner):
configure_ssh(existing_tunnel_run_id,
existing_tunnel_args.local_ports[0], existing_tunnel_args.remote_ports[0],
existing_tunnel_args.ssh_path, ssh_users, existing_tunnel_remote_host,
log_file, retries)
sys.exit(0)
if has_different_owner(existing_tunnel.owner):
if is_same_tunnel:
raise TunnelError('Same tunnel already exists on {local_ports} local ports. '
'It was launched by {tunnel_owner} user '
'which is not the same as the current user {current_owner}. \n\n'
'Usually there is no need to replace the same tunnel '
'but if it is required then you can either '
'specify other local ports to use for the tunnel '
'or stop the existing tunnel if you have sufficient permissions. '
'In order to stop the existing tunnel '
'execute the following command once. \n\n'
'{pipe_command} tunnel stop -lp {local_ports} --ignore-owner \n'
.format(tunnel_owner=existing_tunnel.owner,
current_owner=get_current_user(),
pipe_command=get_current_pipe_command(),
local_ports=stringify_ports(existing_tunnel_args.local_ports)))
else:
raise TunnelError('Different tunnel already exists on {local_ports} local ports. '
'It was launched by {tunnel_owner} user '
'which is not the same as the current user {current_owner}. \n\n'
'You can either specify other local ports to use for the tunnel '
'or stop the existing tunnel if you have sufficient permissions. '
'In order to stop the existing tunnel '
'execute the following command once. \n\n'
'{pipe_command} tunnel stop -lp {local_ports} --ignore-owner \n'
.format(tunnel_owner=existing_tunnel.owner,
current_owner=get_current_user(),
pipe_command=get_current_pipe_command(),
local_ports=stringify_ports(existing_tunnel_args.local_ports)))
else:
if is_same_tunnel:
raise TunnelError('Same tunnel already exists. \n\n'
'Usually there is no need to replace the same tunnel '
'but if it is required then you can either '
'specify other local ports to use for the tunnel '
'or stop the existing tunnel. '
'In order to stop the existing tunnel '
'execute the following command once. \n\n'
'{pipe_command} tunnel stop -lp {local_ports} \n'
.format(pipe_command=get_current_pipe_command(),
local_ports=stringify_ports(existing_tunnel_args.local_ports)))
else:
raise TunnelError('Different tunnel already exists on {local_ports} local ports. \n\n'
'You can either specify other local ports to use for the tunnel '
'or stop the existing tunnel. '
'In order to stop the existing tunnel '
'execute the following command once. \n\n'
'{pipe_command} tunnel stop -lp {local_ports} \n'
.format(pipe_command=get_current_pipe_command(),
local_ports=stringify_ports(existing_tunnel_args.local_ports)))