Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kansi committed Sep 6, 2024
1 parent 37bf694 commit f019c0f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
run: ls -al
- name: Change permission for test keys
run: make permissions
- name: Install docker compose
run: |
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
- name: Build docker-compose stack
run: docker-compose -f docker-compose.yml up -d
- name: Check running containers
Expand Down
2 changes: 1 addition & 1 deletion lib/control_node/host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule ControlNode.Host do
@spec init_release(SSH.t(), binary, atom) :: :ok | :failure | {:error, any}
def init_release(%SSH{} = host_spec, init_file, command) do
with {:ok, %SSH.ExecStatus{exit_code: 0}} <-
SSH.exec(host_spec, "nohup #{init_file} #{command} &", true) do
SSH.exec(host_spec, "#{init_file} #{command}", true) do
:ok
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/control_node/host/ssh.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule ControlNode.Host.SSH do
conn: nil,
hostname: nil,
via_ssh_agent: false,
env_vars: %{}
env_vars: nil

@typedoc """
SSH spec defines a host which shall be used to connect and deploy releases. Following fields should be
Expand Down Expand Up @@ -129,6 +129,7 @@ defmodule ControlNode.Host.SSH do
@spec exec(t, list | binary) :: {:ok, ExecStatus.t()} | :failure | {:error, any}
def exec(ssh_config, commands, skip_eof \\ false) do
env_vars = to_shell_env_vars(ssh_config.env_vars, :inline)
Logger.debug("Processed env var", env_vars: env_vars)
do_exec(ssh_config, "#{env_vars} #{commands}", skip_eof)
end

Expand All @@ -139,6 +140,8 @@ defmodule ControlNode.Host.SSH do
end

defp do_exec(ssh_config, script, skip_eof) when is_binary(script) do
Logger.debug("Executing script on host", host: ssh_config.host, script: script)

with {:ok, conn} <- connect_host(ssh_config),
{:ok, channel_id} <- :ssh_connection.session_channel(conn, @timeout),
:success <- :ssh_connection.exec(conn, channel_id, to_list(script), @timeout) do
Expand All @@ -151,18 +154,18 @@ defmodule ControlNode.Host.SSH do
end
end

@spec to_shell_env_vars(Map.t(), :inline | :export) :: String.t()
defp to_shell_env_vars(%{}, _), do: ""
@spec to_shell_env_vars(Map.t() | nil, :inline | :export) :: String.t()
defp to_shell_env_vars(nil, _), do: ""

defp to_shell_env_vars(env_vars, :inline) do
Enum.map(env_vars, "", fn {key, value}, acc ->
Enum.map(env_vars, fn {key, value} ->
"#{key}=#{value}"
end)
|> Enum.join(" ")
end

defp to_shell_env_vars(env_vars, :export) do
Enum.map(env_vars, "", fn {key, value}, acc ->
Enum.map(env_vars, fn {key, value} ->
"export #{key}=#{value}"
end)
|> Enum.join("; ")
Expand Down
2 changes: 1 addition & 1 deletion lib/control_node/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ defmodule ControlNode.Release do
:ok <- Host.upload_file(host_spec, host_release_path, tar_file),
:ok <- Host.extract_tar(host_spec, host_release_path, host_release_dir) do
init_file = Path.join(host_release_dir, "bin/#{release_spec.name}")
Host.init_release(host_spec, init_file, :start)
Host.init_release(host_spec, init_file, :daemon)
end
end

Expand Down
9 changes: 4 additions & 5 deletions test/control_node/host/ssh_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ defmodule ControlNode.Host.SSHTest do
end

test "run list of commands on remote SSH server", %{ssh_config: ssh_config} do
ssh_config = Map.put(ssh_config, :env_vars, %{"ENV_TEST" => "hello world"})
assert {:ok, %SSH.ExecStatus{exit_status: :success}} =
SSH.exec(ssh_config, [
"export ENV_TEST='hello world'",
"echo $ENV_TEST > /tmp/config.txt"
])
SSH.exec(ssh_config, ["echo $ENV_TEST > /tmp/config.txt"])

assert {:ok, "hello world\n"} = File.read("/tmp/config.txt")
end

test "runs script on remote SSH server", %{ssh_config: ssh_config} do
ssh_config = Map.put(ssh_config, :env_vars, %{"ENV_TEST" => "hello world"})

script = """
#!/bin/sh
export ENV_TEST='hello world';
echo $ENV_TEST > /tmp/config.txt
"""

Expand Down

0 comments on commit f019c0f

Please sign in to comment.