<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tyaakow</title>
    <description>The latest articles on DEV Community by tyaakow (@tyaakow).</description>
    <link>https://dev.to/tyaakow</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F189621%2Fa5f9e833-fb93-4b58-9e5a-ca0152d93c78.jpg</url>
      <title>DEV Community: tyaakow</title>
      <link>https://dev.to/tyaakow</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tyaakow"/>
    <language>en</language>
    <item>
      <title>Deploying Python Web App to Alibaba ECS with Fabric</title>
      <dc:creator>tyaakow</dc:creator>
      <pubDate>Mon, 22 Jun 2020 14:28:29 +0000</pubDate>
      <link>https://dev.to/tyaakow/deploying-python-web-app-to-alibaba-ecs-with-fabric-2ld1</link>
      <guid>https://dev.to/tyaakow/deploying-python-web-app-to-alibaba-ecs-with-fabric-2ld1</guid>
      <description>&lt;p&gt;Fabric is a Python library  used to streamline and automate ssh workflows. It can be very useful in automating and scripting tedious, repetitive or complex tasks, deployments, installations and configurations on remote servers.&lt;/p&gt;

&lt;p&gt;In this article we introduce Fabric, and show how to automate deployment of a Flask web app to Alibaba Cloud Elastic Compute Service.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fabric - the introduction
&lt;/h2&gt;

&lt;p&gt;Fabric is a tool which we can use to deploy or sync our local projects to remote server. We can also automate all operations we can achieve through SSH - we can configure our server, install or update our software stack, start, stop, update or restart our application.&lt;/p&gt;

&lt;p&gt;This article presumes that the reader already created and configured a server on Alibaba Cloud to serve a Flask web app, and that it is operational.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://www.alibabacloud.com/blog/setting-up-a-flask-application-on-alibaba-cloud-ecs-ubuntu-16-04_594502?spm=5176.11423382.detail.11.146c3f5dc4fEDd"&gt;this blog post&lt;/a&gt; we can find a full guide to set up Ubuntu server on Alibaba ECS to serve a Flask app. If we follow this guide, our app will live under /var/www/flaskapp (we will use &lt;code&gt;flaskapp&lt;/code&gt; for the directory name here, but it can be changed to anything else).&lt;/p&gt;

&lt;p&gt;According to the linked guide, our flaskapp will contain two files - uwsgi.ini and app.py.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment Setup
&lt;/h2&gt;

&lt;p&gt;App.py file will contain something basic, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;
  &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;'Hello World!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, to streamline our deployment, we will presume the same document structure both on our local development machine and on the server.  We also presume that on our local machine we are using bash command line or something similar - on MS Windows we will use WSL shell.&lt;/p&gt;

&lt;p&gt;If we start our (very basic) app locally, by doing &lt;code&gt;flask run&lt;/code&gt; in our &lt;code&gt;flaskapp&lt;/code&gt; directory, we will get a simple &lt;code&gt;Hello World!&lt;/code&gt; web page on our localhost:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fuLZIj4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xnh1u3ly2o2b69cg59bc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fuLZIj4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xnh1u3ly2o2b69cg59bc.png" alt="hello world!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to install fabric - we need only to install it locally, our production or development servers don't need anything installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;fabric
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Depending on the OS we are running, we may also be able to install it with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;fabric
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Fabric dependencies are the Python versions 2.7 or 3.4+; Invoke task-execution library;&lt;br&gt;
and Parmiko SSH library. Once we install fabric, we should be able to run the  &lt;code&gt;fab&lt;/code&gt; command in our terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zo2as3e0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fsueqr1ipwa9z3vifx4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zo2as3e0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fsueqr1ipwa9z3vifx4e.png" alt="Fab Command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, &lt;code&gt;fab&lt;/code&gt; command will immediately complain about the (lack of) presence of the &lt;code&gt;fabfile&lt;/code&gt; in the current directory, which should contain configuration and instructions for fabric to run its commands.&lt;/p&gt;

&lt;p&gt;We will &lt;code&gt;cd&lt;/code&gt; into the parent directory of our &lt;code&gt;flaskapp&lt;/code&gt; project, and create our &lt;code&gt;fabfile&lt;/code&gt; . Fabfile is a simple python file with some imports, variables, and function definitions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;fabric2&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;fabric2.config&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt;

&lt;span class="n"&gt;PROJECT_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"fabtut"&lt;/span&gt;
&lt;span class="n"&gt;REMOTE_PATH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/var/www/flaskapp"&lt;/span&gt;
&lt;span class="n"&gt;LOCAL_PATH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/var/www/flaskapp"&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;connect_kwargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect_kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;staging&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"root"&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"xxx.xxx.xxx.xxx"&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect_kwargs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key_filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/home/user/.ssh/id_rsa"&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;production&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"root"&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yyy.yyy.yyy.yyy"&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect_kwargs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key_filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/home/user/.ssh/id_rsa_2"&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"rsync -avzh --exclude '.git' -e 'ssh -i {}' {}/ {}@{}:{}/"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect_kwargs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key_filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LOCAL_PATH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;REMOTE_PATH&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"rsync -avzh -e 'ssh -i {}' {}@{}:{}/ {}/"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connect_kwargs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key_filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;REMOTE_PATH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LOCAL_PATH&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once we save this file,  in the same directory we can run  &lt;code&gt;fab staging push&lt;/code&gt; and our local flaskapp directory will be synced with the one on our staging ECS server  from Alibaba.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some Notes About the Setup
&lt;/h3&gt;

&lt;p&gt;For this guide to work, our &lt;code&gt;fabfile&lt;/code&gt; will need to contain real IP addresses in &lt;code&gt;ctx.host&lt;/code&gt; variables in the &lt;code&gt;staging&lt;/code&gt; and &lt;code&gt;production&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;In this fabfile example we show one of the Fabric features - we can set up multiple environments, from development to staging and production.&lt;/p&gt;

&lt;p&gt;We can also use multiple SSH keys for multiple server environments.&lt;/p&gt;

&lt;p&gt;This setup also demonstrates how we can chain and combine commands, so in our case we can use &lt;code&gt;staging&lt;/code&gt; and &lt;code&gt;production&lt;/code&gt; interchangeably and combine them with &lt;code&gt;pull&lt;/code&gt; or &lt;code&gt;push&lt;/code&gt; as needed.&lt;/p&gt;

&lt;p&gt;We could also define other tasks (in the fabfile we decorate them with &lt;code&gt;@task&lt;/code&gt; decorator), whether to preform some preliminary tasks localy or on the server, or to do some cleanup after the main task.&lt;/p&gt;

&lt;p&gt;If we were working with Django framework, we could here define tasks to perform migrations, or, if our tasks alter the web / application server  configuration, we could restart or reload the server after our task is finished.&lt;/p&gt;

&lt;p&gt;Chaining of tasks comes handy here, and allows for defining very declarative and high-level workflows.&lt;/p&gt;

&lt;p&gt;Since we rely on ssh authentication in this guide, we will need to have loaded our ssh public key  on the ECS server. Alibaba Cloud has more documentation on how to set up SSH authentication &lt;a href="https://www.alibabacloud.com/help/doc-detail/51792.htm"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this short guide, we set up Fabric deployment tool to define deployment workflows to Alibaba ECS server, and we explain how to work with Fabric. We use bash syntax combined with &lt;code&gt;rsync&lt;/code&gt; here, but we could be using any other linux commands like awk, sed, etc.&lt;/p&gt;

&lt;p&gt;Fabric itself allows even more declarative workflows, but we find that using linux &amp;amp; bash commands allows for much greater flexibility and more sophisticated workflows.&lt;/p&gt;

&lt;p&gt;Do you have anything to add to this? Let us know in the comments.&lt;/p&gt;

</description>
      <category>python</category>
      <category>fabric</category>
      <category>flask</category>
    </item>
  </channel>
</rss>
