Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/bootleg/tasks/build/remote.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ task :remote_generate_release do

UI.info("⚡ Creating Tarball...")

time_stamp = System.os_time(:millisecond)
remote :build, cd: source_path do
"tar -czf #{app_name}.tar.gz #{app_name}/"
"mv #{app_name}/ #{time_stamp}/"
"tar -czf #{time_stamp}.tar.gz #{time_stamp}/"
end
end

Expand Down Expand Up @@ -128,24 +130,21 @@ end
task :download_release do
mix_env = config({:mix_env, "prod"})
source_path = config({:ex_path, ""})
app_name = Config.app()
app_version = Config.version()

remote_path =
Path.join(
source_path,
"_build/#{mix_env}/rel/#{app_name}.tar.gz"
"_build/#{mix_env}/rel/*.tar.gz"
)

local_archive_folder = "#{File.cwd!()}/releases"
local_path = Path.join(local_archive_folder, "#{app_version}.tar.gz")

UI.info("⚡ Downloading release archive")
File.mkdir_p!(local_archive_folder)

download(:build, remote_path, local_path)
download(:build, remote_path, local_archive_folder)

UI.info("⚡ Saved: releases/#{app_version}.tar.gz")
UI.info("⚡ Saved in releases/")
end

task :reset_remote do
Expand Down
34 changes: 21 additions & 13 deletions lib/bootleg/tasks/deploy.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,32 @@ end
task :upload_release do
remote_path = "#{Config.app()}.tar.gz"
local_archive_folder = "#{File.cwd!()}/releases"
local_path = Path.join(local_archive_folder, "#{Config.version()}.tar.gz")
UI.info("Uploading release archive")
tar_ball = "ls -t #{local_archive_folder} | head -1"
|> System.shell()
|> elem(0)
|> String.trim_trailing()
local_path = Path.join(local_archive_folder, tar_ball)
UI.info("⚡ Uploading release archive #{tar_ball}")
upload(:app, local_path, remote_path)
end

# credo:disable-for-next-line Credo.Check.Design.TagTODO
# TODO: Prepare for Rollback
# * create releases/ directory
# * fetch timestamp
# * unpack into releases/[timestamp]
# * create revisions.log file
# * create shared/ directory
#
task :unpack_release do
remote_path = "#{Config.app()}.tar.gz"
UI.info("Unpacking release archive: #{remote_path}")
app = Config.app()
remote_path = "#{app}.tar.gz"
keep_releases = Config.get_role(:app).options[:keep_releases]
UI.info("⚡ Unpacking release archive: #{remote_path}")

remote :app do
"tar -zxf #{remote_path}"
"mkdir -p releases/"
"tar -zxf #{remote_path} -C releases/"
"ls -td releases/*/ | head -1 | xargs -I{} ln -sfn {} current"
"rm #{remote_path}"
"touch --reference current/bin/#{app} current/bin/#{app}"
end

if keep_releases do
remote :app do
"ls -1dt releases/*/ | tail -n +#{keep_releases + 1} | xargs -I{} rm -rf {}"
end
end
end
2 changes: 1 addition & 1 deletion lib/bootleg/tasks/ping.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Bootleg.DSL

task :ping do
remote :app do
"bin/#{Config.app()} ping"
"current/bin/#{Config.app()} ping"
end

:ok
Expand Down
2 changes: 1 addition & 1 deletion lib/bootleg/tasks/restart.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Bootleg.DSL

task :restart do
remote :app do
"bin/#{Config.app()} restart"
"current/bin/#{Config.app()} restart"
end

UI.info("#{Config.app()} restarted")
Expand Down
14 changes: 14 additions & 0 deletions lib/bootleg/tasks/rollback.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
alias Bootleg.{Config, UI}
use Bootleg.DSL

task :rollback do
app = Config.app()

remote :app do
"test $(ls -1d releases/*/ 2>/dev/null | wc -l) -ge 2 || (echo 'No previous release to roll back to' && exit 1)"
"previous=$(ls -1dt releases/*/ | sed -n '2p') && ln -sfn $previous current"
"current/bin/#{app} restart"
end

UI.info("#{app} rolled back")
end
2 changes: 1 addition & 1 deletion lib/bootleg/tasks/start.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Bootleg.DSL

task :start do
remote :app do
"bin/#{Config.app()} start"
"current/bin/#{Config.app()} start"
end

UI.info("#{Config.app()} started")
Expand Down
2 changes: 1 addition & 1 deletion lib/bootleg/tasks/stop.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ task :stop do
app_name = Config.app()

remote :app do
"bin/#{app_name} stop"
"current/bin/#{app_name} stop"
end

UI.info("#{app_name} stopped")
Expand Down
2 changes: 1 addition & 1 deletion lib/bootleg/tasks/update.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ task :update do
end

task :stop_silent do
nodetool = "bin/#{Config.app()}"
nodetool = "current/bin/#{Config.app()}"

remote :app do
"#{nodetool} describe && (#{nodetool} stop || true)"
Expand Down
14 changes: 14 additions & 0 deletions lib/mix/tasks/rollback.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Mix.Tasks.Bootleg.Rollback do
use Bootleg.MixTask, :rollback

@shortdoc "Roll back to the previous release"

@moduledoc """
Roll back to the previous release

# Usage:

* mix bootleg.rollback

"""
end
11 changes: 5 additions & 6 deletions test/bootleg/tasks/deploy_task_functional_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Bootleg.Tasks.DeployTaskFunctionalTest do

File.cd!("test/fixtures", fn ->
capture_io(fn ->
assert_raise File.Error, fn -> invoke(:deploy) end
assert_raise SSHError, fn -> invoke(:deploy) end
end)
end)
end
Expand All @@ -49,18 +49,17 @@ defmodule Bootleg.Tasks.DeployTaskFunctionalTest do
end)
end

@tag role_opts: %{release_workspace: "/fixtures"}
@tag role_opts: %{release_workspace: "/project/test/fixtures/releases"}
test "deploy/1 deploys the release to the target hosts from a remote release_workspace path" do
alias Bootleg.Config

File.cd!("test/fixtures", fn ->
capture_io(fn ->
release_name = "#{Config.version()}.tar.gz"
app_name = "#{Config.app()}.tar.gz"
assert [{:ok, _, 0, _}] = remote(:app, "[ -f /fixtures/#{release_name} ]")
assert [{:ok, _, 0, _}] = remote(:app, "[ -f /project/test/fixtures/releases/#{release_name} ]")
invoke(:deploy)
assert [{:ok, _, 0, _}] = remote(:app, "[ -f #{app_name} ]")
assert [{:ok, _, 0, _}] = remote(:app, "[ -f release.txt ]")
assert [{:ok, _, 0, _}] = remote(:app, "[ -L current ]")
assert [{:ok, _, 0, _}] = remote(:app, "[ -f current/release.txt ]")
end)
end)
end
Expand Down
1 change: 1 addition & 0 deletions test/bootleg/tasks/manage_tasks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Bootleg.Tasks.ManageTasksTest do
capture_io(fn ->
conn = SSH.init(:app)
SSH.run!(conn, "install-app build_me")
SSH.run!(conn, "ln -sfn . current")
send(self(), {:connection, conn})
end)

Expand Down
Binary file modified test/fixtures/releases/valid_archive.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion test/support/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM bitwalker/alpine-elixir:latest
# Set up an Alpine Linux machine running an SSH server.
# Autogenerate missing host keys.

RUN apk add --update --no-cache openssh sudo git perl-utils bash
RUN apk add --update --no-cache openssh sudo git perl-utils bash tar
RUN ssh-keygen -A
RUN printf "PermitUserEnvironment yes\n" >> /etc/ssh/sshd_config

Expand Down
Binary file modified test/support/docker/fixtures/valid_archive.tar.gz
Binary file not shown.