This article is participating in a contest from Wunsh.ru - the Russian-speaking community of Elixir. Practices and just sympathizers - join us !
The article describes the process of setting up an application for release on a remote server. For such a not easy business in the world of Elixir, there are two good projects, the first is Distillery
, which makes the application build and the second is Edeliver
, which allows hot-swappable code. Below are the basic instructions for using these two libraries on the example of the simplest Elixir application. As well as the article will tell how to improve deploy through the use of docker
containers.
Distillery
designed to automate the generation of releases of Elixir projects! He is an Exrm heir from the same author. Very easy to use.
The first thing you need to add distillery
in the project. And then run mix deps.get
.
def application do [ mod: {Single, []}, applications: [:logger, :cowboy, :plug] ] end defp deps do [{:cowboy, "~> 1.1.2"}, {:plug, "~> 1.3.0"}, {:distillery, "~> 1.0"}] end
To create a configuration file for a distillery
, the following command should be executed in the project directory:
mix release.init
This command will create a rel
directory with the config.exs
file. Distillery
has a large number of customizable parameters . The file generated automatically came right away.
In order to make a build application for deployment in "combat" mode, you must run the following command:
MIX_ENV=prod mix release --env=prod
This command will create a _build/prod/rel/<name>/releases/<version>/<name>.tar.gz
with the compiled application. Further, this archive must be delivered to the prod. server where you need to unzip and run the application. This can be done using a simple shell
script, but this is not the best option, since it will be downtime.
#!/bin/sh REMOTE_USER="www" REMOTE_HOST="host" APP_NAME="single" APP_FOLDER="/home/$REMOTE_USER/$APP_NAME" SERVER=$REMOTE_USER@$REMOTE_HOST echo "Enter the version of the application" read VERSION if !(ssh $SERVER "[ -d $APP_FOLDER/tmp/ ]") then echo "Preparing directory structure" ssh $SERVER "mkdir -p $APP_FOLDER/tmp/" fi echo "Copying release to server in $APP_FOLDER" scp _build/prod/rel/$APP_NAME/releases/$VERSION/$APP_NAME.tar.gz $SERVER:$APP_FOLDER/tmp/ echo "Stopping the old version" ssh $SERVER "cd $APP_FOLDER && bin/$APP_NAME stop" echo "Extracting archive with release" ssh $SERVER "cd $APP_FOLDER/tmp/ && tar -xzf $APP_NAME.tar.gz -C $APP_FOLDER" echo "Running the new version" ssh $SERVER "cd $APP_FOLDER && PORT=80 bin/$APP_NAME start"
It remains only to check the availability of the offer.
curl -i http://host HTTP/1.1 200 OK server: Cowboy date: Thu, 09 Feb 2017 11:28:08 GMT content-length: 9 cache-control: max-age=0, private, must-revalidate content-type: text/plain; charset=utf-8 Single v1
You can simplify the delivery of the application on the prod. server using Edeliver
.
Edeliver
is essentially an add-on written on Elixir over a set of bash scripts that can build and deploy Elixir and Erlang applications, as well as perform hot code updates.
The first step is to add edeliver
to the project dependencies. And then run mix deps.get
.
def application do [ mod: {Single, []}, applications: [:logger, :cowboy, :plug, :edeliver] ] end defp deps do [{:cowboy, "~> 1.1.2"}, {:plug, "~> 1.3.0"}, {:distillery, "~> 1.0"}, {:edeliver, "~> 1.4.2"}] end
All settings for Edeliver
are stored in a .deliver/config
file. In it, you can specify all sorts of environments. For example BUILD, STAGING and PRODUCTION.
APP="single" BUILD_HOST="host" BUILD_USER="www" BUILD_AT="/home/www/single/tmp" PRODUCTION_HOSTS="host" PRODUCTION_USER="www" DELIVER_TO="/home/www/single"
In order to make a build application for deployment in "combat" mode, you must run the following command:
env MIX_ENV=prod mix edeliver build release
Then, to deliver the build to the right environment (in this case, production), you need to do:
mix edeliver deploy release to production --version=0.1.0
This command will only download the collected release to a remote server, but will not launch the application itself.
To launch an application, use the following command:
mix edeliver start production
If you did a build in a non-production environment, you can get a non-working application. This is due to the fact that when assembling an application, NIFs (native implemented functions) are used, which on different machines may be ... different.
It remains only to check the availability of the offer.
curl -i http://host HTTP/1.1 200 OK server: Cowboy date: Thu, 09 Feb 2017 11:28:08 GMT content-length: 9 cache-control: max-age=0, private, must-revalidate content-type: text/plain; charset=utf-8 Single v2
I have nothing to tell here, better read the excellent translation from Wunsh of this article http://teamon.eu/2017/deploying-phoenix-to-production-using-docker/
Deploying Elixir applications on Heroku is very simple, all you need is to add a buildpack to an existing application.
heroku buildpacks:set https://github.com/HashNuke/heroku-buildpack-elixir
Or create a new application with this buildpack.
heroku create --buildpack "https://github.com/HashNuke/heroku-buildpack-elixir.git"
This buildpack can be configured using the example from the official repository .
Then, as you are used to, just do:
git push heroku master
If you are interested in the functional programming language Elixir or you are just sympathetic, then I advise you to join the Telegram channel https://telegram.me/proelixir about Elixir.
In the domestic Elixir community, a single platform begins to appear in the face of the Wunsh.ru project. Now the project has a thematic newsletter, in which there is nothing illegal, once a week will receive a letter with a collection of articles about Elixir in Russian.
http://bitwalker.org/posts/2016-07-21-distillery-vs-exrm-vs-relx/
https://shovik.com/blog/6-deploying-phoenix-apps-for-rails-developers
https://github.com/bitwalker/distillery
https://github.com/boldpoker/edeliver
https://github.com/HashNuke/heroku-buildpack-elixir
http://blog.plataformatec.com.br/2016/06/deploying-elixir-applications-with-edeliver/
Source: https://habr.com/ru/post/320096/
All Articles