<commit_before>require Logger, as: L
require Bottler.Helpers, as: H

defmodule Bottler.Install do
  alias Bottler.SSH

  @moduledoc """
    Functions to install an already shipped release on remote servers.
  """

  @doc """
    Install previously shipped release on remote servers, making it _current_
    release. Actually running release is not touched. Next restart will run
    the new release.

    Returns `{:ok, details}` when done, `{:error, details}` if anything fails.
  """
  def install(servers) do
    :ssh.start
    servers |> Keyword.values |> H.in_tasks( fn(args) -> on_server(args) end )
  end

  defp on_server(args) do
    ip = args[:ip] |> to_char_list
    user = args[:user] |> to_char_list

    {:ok, conn} = :ssh.connect(ip, 22,
                        [{:user,user},{:silently_accept_hosts,true}], 5000)

    L.info "Installing #{Mix.Project.get!.project[:version]}..."

    place_files conn, user
    make_current conn, user
    cleanup_old_releases conn, user
    :ok
  end

  # Decompress release file, put it in place, and make needed movements
  #
  defp place_files(conn, user) do
    L.info "Settling files..."
    vsn = Mix.Project.get!.project[:version]
    app = Mix.Project.get!.project[:app]
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/releases/#{vsn}'
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/pipes'
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/log'
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/tmp'
    {:ok, _, 0} = SSH.run conn,
          'tar --directory /home/#{user}/#{app}/releases/#{vsn}/ ' <>
          '-xf /tmp/#{app}.tar.gz'
    SSH.cmd! conn, 'ln -sfn /home/#{user}/#{app}/tmp ' <>
                   '/home/#{user}/#{app}/releases/#{vsn}/tmp'
    SSH.cmd! conn,
          'ln -sfn /home/#{user}/#{app}/releases/#{vsn}/releases/#{vsn} ' <>
          '/home/#{user}/#{app}/releases/#{vsn}/boot'
  end
<commit_msg>Log server installation before actually start installing<commit_after>require Logger, as: L
require Bottler.Helpers, as: H

defmodule Bottler.Install do
  alias Bottler.SSH

  @moduledoc """
    Functions to install an already shipped release on remote servers.
  """

  @doc """
    Install previously shipped release on remote servers, making it _current_
    release. Actually running release is not touched. Next restart will run
    the new release.

    Returns `{:ok, details}` when done, `{:error, details}` if anything fails.
  """
  def install(servers) do
    :ssh.start
    servers |> Keyword.values |> H.in_tasks( fn(args) -> on_server(args) end )
  end

  defp on_server(args) do
    ip = args[:ip] |> to_char_list
    user = args[:user] |> to_char_list

    L.info "Installing #{Mix.Project.get!.project[:version]} on #{ip}..."

    {:ok, conn} = :ssh.connect(ip, 22,
                        [{:user,user},{:silently_accept_hosts,true}], 5000)

    place_files conn, user
    make_current conn, user
    cleanup_old_releases conn, user
    :ok
  end

  # Decompress release file, put it in place, and make needed movements
  #
  defp place_files(conn, user) do
    L.info "Settling files..."
    vsn = Mix.Project.get!.project[:version]
    app = Mix.Project.get!.project[:app]
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/releases/#{vsn}'
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/pipes'
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/log'
    SSH.cmd! conn, 'mkdir -p /home/#{user}/#{app}/tmp'
    {:ok, _, 0} = SSH.run conn,
          'tar --directory /home/#{user}/#{app}/releases/#{vsn}/ ' <>
          '-xf /tmp/#{app}.tar.gz'
    SSH.cmd! conn, 'ln -sfn /home/#{user}/#{app}/tmp ' <>
                   '/home/#{user}/#{app}/releases/#{vsn}/tmp'
    SSH.cmd! conn,
          'ln -sfn /home/#{user}/#{app}/releases/#{vsn}/releases/#{vsn} ' <>
          '/home/#{user}/#{app}/releases/#{vsn}/boot'
  end

{'fraction_non_alphanumeric': 0.17476489028213166, 'fraction_numerical': 0.0036572622779519333, 'mean_word_length': 2.7798617966436328, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1421974', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1328, 'n_words': 334}