diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 76426dc..5403b34 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/lib/control_node/host.ex b/lib/control_node/host.ex index c04aacf..2091a06 100644 --- a/lib/control_node/host.ex +++ b/lib/control_node/host.ex @@ -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 diff --git a/lib/control_node/host/ssh.ex b/lib/control_node/host/ssh.ex index ecdaeeb..563f99f 100644 --- a/lib/control_node/host/ssh.ex +++ b/lib/control_node/host/ssh.ex @@ -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 @@ -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 @@ -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 @@ -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("; ") diff --git a/lib/control_node/release.ex b/lib/control_node/release.ex index 5325769..e91a579 100644 --- a/lib/control_node/release.ex +++ b/lib/control_node/release.ex @@ -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 diff --git a/test/control_node/host/ssh_test.exs b/test/control_node/host/ssh_test.exs index c798993..8f15df0 100644 --- a/test/control_node/host/ssh_test.exs +++ b/test/control_node/host/ssh_test.exs @@ -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 """