<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jostein Leira on Medium]]></title>
        <description><![CDATA[Stories by Jostein Leira on Medium]]></description>
        <link>https://medium.com/@josteinleira?source=rss-dbdba8526170------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vI2w1IZeXMilmeeO</url>
            <title>Stories by Jostein Leira on Medium</title>
            <link>https://medium.com/@josteinleira?source=rss-dbdba8526170------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 10:26:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@josteinleira/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Automatically run Black in PyCharm on Windows (December 2022)]]></title>
            <link>https://medium.com/@josteinleira/automatically-run-black-in-pycharm-on-windows-december-2022-746d7b06ef52?source=rss-dbdba8526170------2</link>
            <guid isPermaLink="false">https://medium.com/p/746d7b06ef52</guid>
            <category><![CDATA[windows]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[pycharm]]></category>
            <dc:creator><![CDATA[Jostein Leira]]></dc:creator>
            <pubDate>Wed, 21 Dec 2022 09:42:08 GMT</pubDate>
            <atom:updated>2022-12-21T11:03:27.682Z</atom:updated>
            <content:encoded><![CDATA[<p>Updated 2022–12–21</p><p>This is an updated version of <a href="https://medium.com/compendium/automatically-run-black-in-pycharm-on-windows-d2eab855a918">Automatically run black in PyCharm on Windows</a> (December 2021)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*We31j4B45IR6YXDiags0_A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*kuqin0qa9IwM1XITX0brSg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*TMeuta6WbWhKAjm2g-pbmA.png" /></figure><p>I often forget to run the code formatter before I commit code to git. This is bad since the following commits will contain irrelevant formatting changes.</p><p>I want the code formatter to run automatically. I have heard about git pre-commit hooks but never used them on Windows.</p><p>These are the installation steps:</p><ol><li>Install black in your project’s virtual environment</li><li>Enable the File Watchers plugin in PyCharm</li></ol><h3>Install black in your project’s virtual environment</h3><p>Install black in your current project’s virtual environment.</p><h3>Enable the File Watchers plugin in PyCharm</h3><p>The File Watchers plugin is bundled with PyCharm. First, we need to enable the plugin. Choose the menu option Settings | Plugins | File Watchers | Enable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rZ8RFF1rnvPxVZQ8vzuwnA.png" /></figure><p>Then create a new file watcher by going to the menu option Settings | Tools | File Watchers | + (Add &lt;custom&gt; template).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*rAXd_JfLbyHLn6-1ECTpDQ.png" /></figure><p>Fill in the fields and replace the Program with your correct path to the Scripts folder. Add <em>black</em> at the end:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*l6zcR5TUvOU0pb5YML7vMg.png" /></figure><pre>Name: Black<br>File type: Python<br>Scope: Project Files<br>Program: $PyInterpreterDirectory$/black<br>Arguments: &quot;$FilePath$&quot;<br>Output paths to refresh: &quot;$FilePath$&quot;<br>Uncheck all advanced options.</pre><p>The File Watchers plugin will run black on your files when they change.</p><h3>Sources</h3><p>Black documentation: <a href="https://black.readthedocs.io/en/stable/integrations/editors.html#pycharm-intellij-idea">Black Editor integration</a></p><p>The PyCharm logo Copyright © 2000–2022 JetBrains s.r.o. JetBrains and the JetBrains logo are registered trademarks of JetBrains s.r.o.</p><p>The Black logo Copyright © 2018 Łukasz Langa</p><p>The Windows logo Copyright © 2021 Microsoft</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=746d7b06ef52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatically run Black in PyCharm on Windows]]></title>
            <link>https://medium.com/compendium/automatically-run-black-in-pycharm-on-windows-d2eab855a918?source=rss-dbdba8526170------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2eab855a918</guid>
            <category><![CDATA[linter]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[pycharm]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[windows]]></category>
            <dc:creator><![CDATA[Jostein Leira]]></dc:creator>
            <pubDate>Tue, 02 Nov 2021 12:38:21 GMT</pubDate>
            <atom:updated>2022-12-21T09:42:38.029Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*0a7JKgPTxuz6HWm_6yl4wA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iwMjk9rEJ5PwsVYF2RJvBg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JqugFrVbN_e9V_h_avBRJw.png" /></figure><h3>(December 2021)</h3><p>An updated and shorter version from December 2022 is <a href="https://medium.com/@josteinleira/automatically-run-black-in-pycharm-on-windows-december-2022-746d7b06ef52">here</a>.</p><p>I often forget to run the code formatter before I commit code to git. This is bad since the following commits will contain irrelevant formatting changes.</p><p>I want the code formatter to run automatically. I have heard about git pre-commit hooks but never used them on Windows.</p><p>These are the installation steps:</p><h3>Overview</h3><ol><li>Install a system Python on Windows</li><li>Create a virtual environment for the Black formatter</li><li>Add Black in PyCharm’s External tools (optional step)</li><li>Enable the File Watchers plugin in PyCharm</li></ol><h3>Install a system Python on Windows</h3><p>You need Python installed on your system. Go to <a href="https://python.org/">python.org</a> and download and install if not already OK. You need to know the path to the python.exe file for the next step (create virtual environment below).</p><p>On Windows I never let any installation program add itself to the path if I can control it. Also valid for Python, so we must find the system python.exe file.</p><p>Open a Command Prompt window and run “dir \python.exe /s”:</p><pre>C:\&gt;dir \python.exe /s</pre><pre>(ignore any virtual environment ones)</pre><pre>(ignore Windows apps)</pre><pre>This is the one:</pre><pre>Directory of <strong>C:\Users\&lt;user&gt;\AppData\Local\Programs\Python\Python39</strong></pre><pre>30.08.2021  20:36           101 608 <strong>python.exe</strong></pre><pre>1 File(s)        101 608 bytes</pre><p>Note the <strong>&lt;full path&gt;</strong> to python.exe, with your actual user name instead of &lt;user&gt;.</p><pre><strong>C:\Users\&lt;user&gt;\AppData\Local\Programs\Python\Python39</strong></pre><p>Check that the python.exe is the one you want by running it with the version option “-V”.</p><pre><strong>&lt;full path&gt;\python -V<br>Python 3.9.7</strong></pre><h3>Create a virtual environment for the Black formatter</h3><p>I always create a new virtual environment for new applications, not to mess with either the system Python or other applications.</p><p>Change the target path at the end to the one you want and run the command:</p><pre>&lt;full path&gt;\python -m venv C:\Users\<strong>&lt;user&gt;</strong>\venv\black</pre><p>Then install the black package in the new virtual environment.</p><pre><strong>C:\Users\&lt;user&gt;\venv\black</strong>\Scripts\pip install black</pre><h3>Add Black in PyCharm’s External tools</h3><p>This is an optional step if you only want automatic formatting and not the option to run black from the PyCharm menu.</p><p>Add Balck to PyCharm’s External tools by going to Settings | Tools | External Tools. Choose + to create a new tool entry:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*gONpEHyRrwOlHR73oTvfMA.png" /><figcaption>Settings | Tools | External Tools | + (Add)</figcaption></figure><p>Fill in the fields:</p><pre>Name: Black<br>Program: C:\Users\&lt;user&gt;\venv\black\Scripts\black.exe<br>Arguments: &quot;$FilePath$&quot;</pre><p>The new tool is now available from the main menu: Tools | External Tools | Black, if you want to reformat the current file.</p><h3>Enable the File Watchers plugin in PyCharm</h3><p>The File Watchers plugin is bundled with PyCharm. First, we need to enable the plugin. Choose the menu option Settings | Plugins | File Watchers | Enable.</p><figure><img alt="Settings | Plugins | File Watchers | Enable" src="https://cdn-images-1.medium.com/max/984/1*mVHBZE5XAXbSuosEhUF5LA.png" /><figcaption>Settings | Plugins | File Watchers | Enable</figcaption></figure><p>Then create a new file watcher by going to the menu option Settings | Tools | File Watchers | + (Add &lt;custom&gt; template).</p><figure><img alt="New File Watchers dialog" src="https://cdn-images-1.medium.com/max/703/1*IzyXK4NGk1fzBZbNfUB_Fg.png" /><figcaption>Settings | Tools | File Watchers | + (Add)</figcaption></figure><p>Fill in the fields:</p><pre>Name: Black<br>File type: Python<br>Scope: Project Files<br>Program: C:\Users\&lt;user&gt;\venv\black\Scripts\black<br>Arguments: &quot;$FilePath$&quot;<br>Output paths to refresh: &quot;$FilePath$&quot;<br>Uncheck all advanced options.</pre><p>The File Watchers plugin will run black on your files when they change.</p><h3>Summary</h3><p>I experienced that some PyCharm restarts were necessary during the setup process. I also got the following warning after the installation:</p><figure><img alt="Run File Watchers? Running File Watchers may execute potentially malicious code. Skip running if you don’t trust the source. Trust project and run. Skip. Review…" src="https://cdn-images-1.medium.com/max/341/1*mjX4QBMfXVDFNf4K4N-Vwg.png" /></figure><h3>Sources</h3><p>Black documentation: <a href="https://black.readthedocs.io/en/stable/integrations/editors.html#pycharm-intellij-idea">Black Editor integration</a></p><p>The PyCharm logo Copyright © 2000–2021 JetBrains s.r.o. JetBrains and the JetBrains logo are registered trademarks of JetBrains s.r.o.</p><p>The Black logo Copyright © 2018 Łukasz Langa</p><p>The Windows logo Copyright © 2021 Microsoft</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2eab855a918" width="1" height="1" alt=""><hr><p><a href="https://medium.com/compendium/automatically-run-black-in-pycharm-on-windows-d2eab855a918">Automatically run Black in PyCharm on Windows</a> was originally published in <a href="https://medium.com/compendium">Compendium</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Install an SFTP server on OpenShift]]></title>
            <link>https://medium.com/compendium/install-an-sftp-server-on-openshift-818ea30a4319?source=rss-dbdba8526170------2</link>
            <guid isPermaLink="false">https://medium.com/p/818ea30a4319</guid>
            <category><![CDATA[sftp]]></category>
            <category><![CDATA[openshift]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[okd]]></category>
            <category><![CDATA[okc]]></category>
            <dc:creator><![CDATA[Jostein Leira]]></dc:creator>
            <pubDate>Wed, 11 Nov 2020 19:39:44 GMT</pubDate>
            <atom:updated>2020-11-18T12:16:52.936Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*VjgXd3DnUwAmHoboqC0U0g.png" /></figure><h3>Background</h3><p>At work, we needed an <a href="https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol">SFTP</a> server for testing our software. I volunteered to install one in our OpenShift test environment, but ran into lots of problems underway and used way too much time on this project. I’m new to OpenShift and really underestimated this task.</p><p>We use OpenShift/okd version 3.11. As our source, I found <a href="https://github.com/atmoz/sftp">https://github.com/atmoz/sftp</a> which is also on Docker Hub with over 100 million downloads.</p><p>The GitHub repository contains several branches, and I chose to use the alpine-3.7 branch since it’s size is only 16 MB.</p><p>I soon ran into problems trying to modify the atmoz/sftp project to be able to run as a non-root user. It did not go well, and I abandoned this attempt.</p><p>Another problem was line feeds in config files CRLF (Windows) vs LF (Linux).</p><p>I hope this installation log may be helpful to you.</p><h3>Security considerations</h3><p>Do you trust the source? The SFTP server must run as root, which is against the OpenShift <a href="https://docs.openshift.com/container-platform/3.11/install/prerequisites.html#security-warning">recommendations</a>. We only use this server internally, not exposed to the internet. Made a fork (copy) of the GitHub source project and use that.</p><h3>Installation overview</h3><p>The installation consists of the following:</p><ol><li>Container source from docker hub or GitHub.</li><li>Config map for the users:<br>/etc/sftp/users.conf</li><li>Config map for ssh keys and config:<br>/etc/ssh/ssh_host_ed25519_key<br>/etc/ssh/ssh_host_ed25519_key.pub<br>/etc/ssh/ssh_host_rsa_key<br>/etc/ssh/ssh_host_rsa_key.pub<br>/etc/ssh/sshd_config</li><li>NodePort allowing external traffic to port 30022 -&gt; internal port 22</li><li>A Persistent Volume (PV) storage for the user&#39;s upload directory:<br>/home/user/upload</li></ol><h3>Project</h3><p>Log in to your OpenShift server and either create a new project/namespace for the SFTP service or select an existing one.</p><p>either create a new project:</p><pre>oc new-project int-sftp --display-name=&quot;Internal sftp server&quot;</pre><p>or select an existing project:</p><pre>oc project int-sftp</pre><blockquote>Throughout this installation, it is assumed that you are logged in to the same project/namespace.</blockquote><h3>New application</h3><p>Create a new application:</p><p>From docker hub:</p><pre>oc new-app atmoz/sftp:alpine-3.7</pre><p>or from a GitHub repository:</p><pre>oc new-app <a href="https://github.com/atmoz/sftp#alpine-3.7">https://github.com/atmoz/sftp#alpine-3.7</a></pre><p>Take a look at the pod’s log. Expect error message:</p><pre>mkdir: can&#39;t create directory &#39;/var/run/sftp&#39;: Permission denied</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*6E8a1bi_wOT4odbj2xqWKw.png" /></figure><h3>Allow running as root</h3><p>Will change the policy for the default user in your current project/namespace (int-sftp) to run as any user ID (root):</p><pre>oc adm policy add-scc-to-user anyuid -z default<br>scc “anyuid” added to: [“system:serviceaccount:int-sftp:default”]</pre><p>To check which users have the ability to run as root:</p><pre>oc edit scc anyuid</pre><pre>users:</pre><pre>- system:serviceaccount:int-sftp:default</pre><p>To see that the “run as root” setting had the desired effect, scale down and up again the sftp deployment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*fELsrt0XcR--E--5Q4SRMA.png" /></figure><p>See that you get the expected error message:</p><pre>[entrypoint] FATAL: No users provided!</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*nNnOZ8p6NTNqkEDIWdxqgw.png" /></figure><h3>SFTP users config map</h3><p>Create a new config map with the name sftp-etc-sftp and key users.conf and content:</p><pre>foo:123:1001:100:upload<br>bar:abc:1002:100:upload<br>baz:xyz:1003:100:upload</pre><p>The format of the user config file is username:password:UID:GUI:directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*dGz_lrIEGWuwTRxy5Hn95A.png" /></figure><p>I am on Windows and get CRLF at the end of each line, but you must remove the extra CR characters. Remove by Edit YAML:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*qB0DUrXPEKXq0mKfHge2zg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*Xk4xTvN3sSfqi4FaYmmk9A.png" /></figure><p>Remove the ‘\r’ characters otherwise, it will be part of the upload directory name and cause problems.</p><h3>Mount the sftp config map</h3><p>Mount the sftp-etc-sftp config map to the file system path /etc/sftp by going to your Deployments &gt; sftp &gt; Configuration &gt; Add config files:</p><p>Source: sftp-etc-sftp<br>Mount path: /etc/sftp</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*m18fIzdurwzEIgtth-a6Sw.png" /></figure><p>Hit the Add button.</p><h3>Test the config map</h3><p>To test the new config map, ensure that your pod is redeployed and scaled to one. You may not need to do anything if automatic redeploy on config changes are turned on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*fELsrt0XcR--E--5Q4SRMA.png" /></figure><p>Startup log:</p><pre>[entrypoint] Parsing user data: &quot;foo:123:1001:100:upload&quot;<br>Creating mailbox file: No such file or directory<br>[entrypoint] Creating directory: /home/foo/upload<br>[entrypoint] Parsing user data: &quot;bar:abc:1002:100:upload&quot;<br>Creating mailbox file: No such file or directory<br>[entrypoint] Creating directory: /home/bar/upload<br>[entrypoint] Parsing user data: &quot;baz:xyz:1003:100:upload&quot;<br>Creating mailbox file: No such file or directory<br>[entrypoint] Creating directory: /home/baz/upload<br>Generating public/private ed25519 key pair.<br>Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.<br>Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.<br>The key fingerprint is:<br>SHA256:qNWATmxbbYyHinTx5n10eIml7UG5K2vana9XHcqr3rY root@sftp-3-kwg2c<br>The key&#39;s randomart image is:<br>+--[ED25519 256]--+<br>|    .       o.   |<br>|   . + =   B..   |<br>|  . * B = = *.   |<br>| . * * B . +.. . |<br>|  . + + S . o.. o|<br>|     o   .. .o  o|<br>|    .      o  .. |<br>|         .o..+.  |<br>|        .ooo*Eo  |<br>+----[SHA256]-----+<br>Generating public/private rsa key pair.<br>Your identification has been saved in /etc/ssh/ssh_host_rsa_key.<br>Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.<br>The key fingerprint is:<br>SHA256:aTIlNkPOyXYDXiZ4Yixz2poRwYrmQJ4DuCIRqxljuxU root@sftp-3-kwg2c<br>The key&#39;s randomart image is:<br>+---[RSA 4096]----+<br>|oo.o .+ o        |<br>|+o= **.*         |<br>|O=.E o% +        |<br>|XO+ oo * o       |<br>|O..=  o S        |<br>| .=    +         |<br>| .               |<br>|                 |<br>|                 |<br>+----[SHA256]-----+<br>[entrypoint] Executing sshd<br>Server listening on 0.0.0.0 port 22.<br>Server listening on :: port 22.</pre><p>The SFTP server is now up and running!</p><p>What is still missing is ensuring that the server ssh keys (generated during startup) and files uploaded by the users are permanently stored. Now they will be gone in case of a pod restart. In addition, we can not access the SFTP server from the outside of our pod.</p><h3>SSH config map</h3><p>Without persistent ssh keys, the end-users will get login errors after a pod restart due to changed host keys.</p><p>We must ensure that the file access permissions to the ssh keys are restricted to only be readable by the root user.</p><p>Create a new config map with the name sftp-etc-ssh and add the following keys:</p><pre>ssh_host_ed25519_key<br>ssh_host_ed25519_key.pub<br>ssh_host_rsa_key<br>ssh_host_rsa_key.pub<br>sshd_config</pre><p>The content/value of these files could be lifted from the running pod via Pods &gt; sftp-N-AAAA &gt; Terminal and the cat-commands:</p><pre>cat /etc/ssh/ssh_host_ed25519_key<br>cat /etc/ssh/ssh_host_ed25519_key.pub<br>cat /etc/ssh/ssh_host_rsa_key<br>cat /etc/ssh/ssh_host_rsa_key.pub<br>cat /etc/ssh/sshd_config</pre><p>Or you can generate new keys with the following two commands. Use empty passphrases. Then cat their content:</p><pre>ssh-keygen -t ed25519 -f ssh_host_ed25519_key &lt; /dev/null<br>ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key &lt; /dev/null</pre><p>The sshd_config content is:</p><pre>Protocol 2<br>HostKey /etc/ssh/ssh_host_ed25519_key<br>HostKey /etc/ssh/ssh_host_rsa_key</pre><pre># Faster connection<br># See: <a href="https://github.com/atmoz/sftp/issues/11">https://github.com/atmoz/sftp/issues/11</a><br>UseDNS no</pre><pre># Limited access<br>PermitRootLogin no<br>X11Forwarding no<br>AllowTcpForwarding no</pre><pre># Force sftp and chroot jail<br>Subsystem sftp internal-sftp<br>ForceCommand internal-sftp<br>ChrootDirectory %h</pre><p>You should end up with something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*3rjwjJJGYVsCH7lCQEcusw.png" /></figure><h3>Mount ssh config map</h3><p>Map the sftp-etc-ssh config map to the file system path /etc/ssh by going to your Deployments &gt; sftp &gt; Configuration &gt; Add Config Files:</p><p>Source: sftp-etc-ssh<br>Mount path: /etc/ssh</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/617/1*3vcOh0mXHVNWt2hEmW-atw.png" /></figure><p>Ensure that your pod is restarted (redeployed or scaled down to 0 and up to 1 again).</p><p>Next, you should get an error message like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*J-pLgqFWVqdGE8oe7cXAKw.png" /></figure><p>We need to restrict the permissions for the ssh key files. Go to Deployments &gt; sftp &gt; Edit YAML:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*0LoSZbHcZL7iAZ1TiEVdjg.png" /></figure><p>Change to <strong>defaultMode: 384</strong> setting for sftp-stc-ssh:</p><pre>spec:<br>  template:<br>    spec:<br>      volumes:<br>        - configMap:<br>          defaultMode: 384<br>          name: sftp-etc-ssh</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/617/1*PM3ctlr3WhKGKrtbJoaNUQ.png" /></figure><blockquote>384 decimal = 600 octal = user +rw <br>(See <a href="http://permissions-calculator.org/)">http://permissions-calculator.org/)</a></blockquote><h3>Test the SSH config map</h3><p>Ensure the pod is restarted. The new startup log should now look something like this:</p><pre>[entrypoint] Parsing user data: “foo:123:1001:100:upload”<br>Creating mailbox file: No such file or directory<br>[entrypoint] Creating directory: /home/foo/upload<br>[entrypoint] Parsing user data: “bar:abc:1002:100:upload”<br>Creating mailbox file: No such file or directory<br>[entrypoint] Creating directory: /home/bar/upload<br>[entrypoint] Parsing user data: “baz:xyz:1003:100:upload”<br>Creating mailbox file: No such file or directory<br>[entrypoint] Creating directory: /home/baz/upload<br>[entrypoint] Executing sshd<br>Server listening on 0.0.0.0 port 22.<br>Server listening on :: port 22.</pre><h3>Expose service port</h3><p>To be able to access the SFTP server from an external machine we need to create a NodePort. A default is already automatically created, but we need to make some changes. Go to Services &gt; sftp &gt; Edit YAML:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*-Slxg5vkbOsEItzjwUkt8w.png" /></figure><p>Do two modifications:</p><ol><li>Change type from ClusterIP to <strong>NodePort</strong>:</li><li>Add ‘<strong>nodePort: 30022</strong>’ to the port definition section</li></ol><p>You should end up with something like this:</p><pre>spec:<br>  ports:<br>    - name: 22-tcp<br>      <strong>nodePort: 30022</strong><br>      port: 22<br>      protocol: TCP<br>      targetPort: 22<br>  type: <strong>NodePort</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*lwNJ4uFUDU72wm0PJ-yrGA.png" /></figure><h3>Test the exposed port</h3><p>It should now be possible to access the OpenShift node server’s external address with SFTP.</p><pre>sftp -P 30022 bar@myopenshiftserver.com<br>bar@myopenshiftserver’s password:<br>Connected to bar@myopenshiftserver.com.<br>sftp&gt;<br>sftp&gt; ls<br>upload<br>sftp&gt; cd upload<br>sftp&gt; lls<br>…</pre><h3>Persistent volume</h3><p>We need to make a Persistent Volume (PV) claim to permanently store files uploaded by the users. Until now all uploaded files would disappear after a pod restart.</p><p>For now, I only create a PV claim for one user (mounted on: /home/bar/upload). You need to repeat this procedure for each user.</p><p>Go to Storage &gt; Create Storage:</p><p>Name: sftp-bar-storage<br>Access Mode: Shared Access (RWX)<br>Size: 10 GiB</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*WCZUbeZxUx-DglgYrSrPag.png" /></figure><p>Hit the Create button.</p><p>Then go to Deployments &gt; sftp &gt; Add Storage:</p><p>Select:<br>Storage: sftp-bar-storage<br>Mount path: /home/bar/upload</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*GTxlqf3KJeulSfNAC9NTrw.png" /></figure><p>Press the Add button.</p><p>Ensure pod restart. Upload a file to the directory /home/bar/upload and restart the pod again. Confirm that the file is still there.</p><h3>Conclusion</h3><p>This is a complicated installation, and I especially struggled with CRLF characters sneaking into my config and script files, giving strange error messages. I hope this document is helpful.</p><h3>Future work</h3><p>Use secrets instead of config map.</p><p>Encrypt users’ passwords.</p><p>A single Persistent Volume (PV) for all users.</p><p>Remove showing password during startup. Is a security problem if a centralized logging system is used.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=818ea30a4319" width="1" height="1" alt=""><hr><p><a href="https://medium.com/compendium/install-an-sftp-server-on-openshift-818ea30a4319">Install an SFTP server on OpenShift</a> was originally published in <a href="https://medium.com/compendium">Compendium</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Airflow on GCP (May 2020)]]></title>
            <link>https://medium.com/compendium/airflow-on-gcp-may-2020-cdcdfe594019?source=rss-dbdba8526170------2</link>
            <guid isPermaLink="false">https://medium.com/p/cdcdfe594019</guid>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[etl]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Jostein Leira]]></dc:creator>
            <pubDate>Fri, 15 May 2020 17:08:06 GMT</pubDate>
            <atom:updated>2020-05-19T07:32:50.975Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a complete guide to install Apache Airflow on a Google Cloud Platform (GCP) Virtual Machine (VM) from scratch.</p><p>An alternative is to use Cloud Composer, the managed version that Google offers.</p><p>This is an updated version of my post on <a href="https://medium.com/grensesnittet/install-apache-airflow-on-a-google-cloud-platform-virtual-machine-f9a5b01b6c33">installing Airflow on a GCP VM</a> published in April 2019. A year has passed and I have picked up some new tricks since then. New versions of Airflow, Ubuntu, PostgreSQL, and Python have arrived and I have decided to replaced Nginx with Google’s load balancer.</p><h3>Modules</h3><ul><li>Apache Airflow — version 1.10.10</li><li>PostgreSQL 10 — Managed Cloud SQL version</li><li>Python 3.7 — The latest version that Airflow works with.</li><li>Ubuntu 20.04 LTS</li><li>Google’s load balancer with managed HTTPS certificate</li><li>Systemd services — Automatic startup</li></ul><h3>Architecture</h3><figure><img alt="A sketch of the components used and their connections. Static IP, load balancer, HTTPS certificate, VM, and Cloud SQL." src="https://cdn-images-1.medium.com/max/1024/1*xVO3bfm78nF7hXtPtbumDw.png" /></figure><p>The core of the system is the VM, on which we install Airflow. We use a Cloud SQL instance as our database.</p><p>The load balancer is only used as an HTTPS terminator, serving and automatically renewing certificates. If you prefer to use Nginx with Let’s Encrypt instead, see <a href="https://medium.com/grensesnittet/install-apache-airflow-on-a-google-cloud-platform-virtual-machine-f9a5b01b6c33">Installing Airflow on a GCP VM (April 2019)</a>.</p><p>To be able to connect the Cloud load balancer with our virtual machine, the VM must reside in an instance group. You may only connect a GCP load balancer to an instance group, and not directly to a specific VM.</p><h3>Installation overview</h3><ol><li>Network setup (virtual private cloud)</li><li>Virtual Machine</li><li>Firewall rules (allow ssh access)</li><li>Instance group</li><li>Install the required packages</li><li>Installing the database (Cloud SQL)</li><li>Configure Airflow</li><li>Automatic startup</li><li>Static IP address</li><li>Load balancer</li></ol><h3>Network setup</h3><p>If you already have your infrastructure in place or just want to use the default network, skip this part.</p><p>The reason for setting up a new Virtual Private Cloud (network) to isolate our VM.</p><figure><img alt="Screenshot of the Google console, showing VPC networks and the “Create VPC network” button." src="https://cdn-images-1.medium.com/max/815/1*eKSlYHgqx_6RkylOeY7p8g.png" /><figcaption>Google Cloud Console — VPC network — Create VPC Network</figcaption></figure><p>In the GCP Console, choose VPC Networks and + Create VPC Network.</p><figure><img alt="Partial screenshot of Create a VPC network form. Enter airflow-network in the Name field." src="https://cdn-images-1.medium.com/max/754/1*-jSK3NxRO4_540TkMAnWdA.png" /><figcaption>Create a VPC network — Enter a name</figcaption></figure><p>Name your new network <em>airflow-network</em>.</p><figure><img alt="Partial screenshot of Create a VPC network form. New subnet. Name: airflow-subnet. Region:europe-west1. Address: 10.0.0.0/8" src="https://cdn-images-1.medium.com/max/486/1*exkcUmYl8etwBT5mjW_sXw.png" /><figcaption>Create a VPC network — New subnet</figcaption></figure><p>I have used the names <em>airflow-network</em> and <em>airflow-subnet</em>. Names do not matter, as long as you are able to identify them later.</p><p>Select a region near your end-users. Be aware that there are two types of network tiers; standard and premium. Standard is optimized for cost and premium for speed. Standard is unavailable in some regions. See <a href="https://www.google.com/url?q=https://cloud.google.com/network-tiers/docs/overview&amp;sa=D&amp;ust=1589539163946000">https://cloud.google.com/network-tiers/docs/overview</a>.</p><figure><img alt="Screenshot of the newly created network and subnet." src="https://cdn-images-1.medium.com/max/650/1*1ZP6aXP7j7_dCQ6YNSE0tw.png" /><figcaption>The new airflow-network and airflow-subnet.</figcaption></figure><h3>Virtual Machine</h3><p>Create a new Virtual Machine from the “Compute Engine” | “VM instances” menu with the following settings:</p><p>Name: airflow-vm<br>Region/Zone: europe-west1/europe-west1-b (same region as your network)<br>Machine type: g1-small (it is easy to change later if you need bigger)<br>Boot disk: Ubuntu 20.04 LTS minimal image<br>Disk size: 10 GB (you may increase disks later, but never shrink)<br>Firewall: Allow HTTP traffic (will change this later)<br>Networking: Select the airflow-network and airflow-subnet</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*z_xvCq6Yj25S_wG27uInmA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*kbSgUpjb6Mr-bn2-iyt0Hg.png" /><figcaption>Create an instance form</figcaption></figure><h3>Firewall rules</h3><p>For administration and installation, I need ssh access to the new VM. Since we create our own custom VPC, we need to add a firewall rule to allow access.</p><p>Choose “VPC network” | “Firewall rules” | “Create Firewall rule”.</p><p>Use:<br>Name: allow-ssh-access<br>Network: airflow-network<br>Targets: All instances in the network<br>Source IP-ranges: 0.0.0.0/0 (all)<br>Specified protocols and ports: tcp 22</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*ccHghRQhFvtKvJzdFhuzvg.png" /></figure><p>Hit the “Create” button. You may later disable/enable this rule depending on access needs.</p><p>The plan is to serve airflow on TCP port 8080. During setup and testing, I modify the existing firewall rule <em>airflow-network-allow-http</em>, and add port 8080.</p><figure><img alt="Partial screenshot of modifying existing firewall rule. Add port 8080." src="https://cdn-images-1.medium.com/max/541/1*mROks6YBEoM2fvTr2LTO3g.png" /><figcaption>Modify the existing firewall rule. Add TCP port 8080.</figcaption></figure><p>Create another firewall rule to allow traffic from the load balancer to the backend. Traffic from the load balancer may come from addresses in the IP ranges 130.211.0.0/22 and 35.191.0.0/16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*_l9e_8-YKJ4Yzxjr-p-HCA.png" /></figure><p>You should end up with the following rules:</p><figure><img alt="Screenshot of new and modified firewall rules." src="https://cdn-images-1.medium.com/max/1023/1*Oaid3HynTgph4ymTsUkbOw.png" /><figcaption>Resulting firewall rules for the airflow-network.</figcaption></figure><p>For now, both <em>airflow-network-allow-http</em> and <em>allow-loadbalancer</em> overlap, since both allow traffic to port 8080 on the VM. The idea is to disable <em>airflow-network-allow-http</em> when we are finished.</p><h3>Instance group</h3><p>We create an unmanaged instance group and add our newly created VM. Select “Create instance group” under the menu element “Compute Engine” | “Instance groups”:</p><p>Enter<br>Name: airflow-instance-group<br>Region/zone: europe-west1/europe-west1-b<br>Network/subnet: airflow-network/airflow-subnet<br>Expand “Specify port name mapping” (see below)<br>VM instances: airflow-vm</p><figure><img alt="Screenshot of “Create instance group” form." src="https://cdn-images-1.medium.com/max/874/1*ZPEYUavgs5hm-tQdV3h23g.png" /><figcaption>Compute Engine | Instance groups | Create instance group</figcaption></figure><p>Expand the “Specify port name mapping” and define the port name “http8080” with port numbers “8080”:</p><figure><img alt="Partial screenshot of expanded form to specify port name." src="https://cdn-images-1.medium.com/max/471/1*q4Q9bzs8vMWqvcPbBPtmwQ.png" /><figcaption>Specify a new port name “http8080”.</figcaption></figure><p>This port name is used by the load balancer later.</p><p>Hit the “Create” button. You should end up with a new instance group containing one instance.</p><figure><img alt="Screenshot of the result after creating a new instance group." src="https://cdn-images-1.medium.com/max/696/1*TBqfFbvlT2U0lbD2AIN0sA.png" /><figcaption>Result after creating a new instance group.</figcaption></figure><h3>Install the required packages</h3><p>The commands below install the needed packages, creates a Python virtual environment under <em>/srv</em>, creates an airflow user that the server will run as, and set owner and permissions.</p><pre>sudo su<br>apt update<br>apt upgrade</pre><pre>apt install software-properties-common<br>add-apt-repository ppa:deadsnakes/ppa</pre><pre>apt install python3.7 python3.7-venv python3.7-dev</pre><pre>adduser airflow --disabled-login --disabled-password --gecos &quot;Airflow system user&quot;</pre><pre>cd /srv<br>python3.7 -m venv airflow<br>cd airflow<br>source bin/activate</pre><pre># With an activated virtual environment<br>pip install --upgrade pip<br>pip install wheel<br>pip install apache-airflow[postgres,crypto]==1.10.10</pre><pre>chown airflow.airflow . -R<br>chmod g+rwx . -R</pre><h3>Installing the database</h3><p>Using a managed Cloud SQL database without a public IP address. Keeping<br>costs down, choosing all minimum resources (1 CPU, 3.75 GB RAM, 10 GB<br>HDD).</p><p>We want to end up with:<br>database instance (server) with instance ID: airflow-db<br>database name: airflow<br>database user name: airflow-user<br>database password for the airflow-user: &lt;db-password&gt;<br>database IP address: &lt;db-server-ip&gt;</p><p>From the Google console, go to “Storage” | “SQL” | “Create new<br>instance” and then “Choose PostgreSQL”.</p><p>Choose “Private IP” and associate<br>it to the airflow-network. Hit “Allocate and connect”, before hitting the “Create” button at the bottom of the form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*VK1Zc9SIzQJL2SsfjXAejw.png" /></figure><p>Wait for the new server instance being created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cGsC36ONao3dcDaKUKp9Yw.png" /></figure><p>When the new database instance is up and running, you should create a new database <em>airflow</em> and a new user <em>airflow-user</em>. Enter the <em>airflow-db</em> instance and select “Databases” and then “Create database”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*p7FzKEbcW5KC_k9FuKQ9Wg.png" /></figure><p>Enter “airflow” in the popup and hit the create button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*-wnFWaKT_hLH0ILh4pcEnw.png" /></figure><p>Then create a new user. Select the menu item Users, then the “Create user account” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*dsKNRIgkotviKUOtcHJJRw.png" /></figure><p>Enter the user name <em>airflow-user</em> and choose a new password, referred to as &lt;db-password&gt; below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*tkx4L5jxDP1JRgyo2ozErQ.png" /></figure><p>At this point, you should be able to connect to the new database. You need to locate the “Private IP address” from the database instance details overview pane.</p><pre>$ psql -h &lt;db-server-ip&gt; -U airflow-user -d airflow<br>Password for user airflow-user: &lt;db-password&gt;<br>psql (12.2 (Ubuntu 12.2-4), server 11.6)<br>SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384)<br>Type &quot;help&quot; for help</pre><pre>airflow=&gt;</pre><h3>Configure Airflow</h3><p>Running <em>airflow initdb</em> will create config files and a metadata database.<br>Since we are using PostgreSQL database we have to modify the database<br>connection string in the config file afterward and then rerun the <em>airflow<br>initdb</em> command.</p><pre>sudo su airflow<br>cd /srv/airflow<br>source bin/activate<br>export AIRFLOW_HOME=/srv/airflow</pre><pre>airflow initdb</pre><p>Review the <em>/srv/airflow/airflow.cfg</em> file and change the following:</p><pre># This is not the complete airflow.cfg<br># but only show configurations you need to change<br>sql_alchemy_conn = postgresql+psycopg2://airflow-user:&lt;db-password&gt;@&lt;db-server-ip&gt;/airflow</pre><pre>default_impersonation = airflow</pre><pre>load_examples = False</pre><pre>rbac = True</pre><pre>enable_proxy_fix = True</pre><p>After editing the config file, then initialize PostgreSQL database (activated virtual environment):</p><pre>airflow initdb</pre><p>Add a new airflow admin user (activated virtual environment):</p><pre>airflow create_user -r Admin -u jon -e jon@exampl.com -f Jon -l Snow</pre><p>You can now start the airflow webserver:</p><pre>airflow webserver -p 8080</pre><p>You may now access the webserver on http://&lt;VM’s External IP&gt;:8080</p><p>You may start the scheduler with the command:</p><pre>airflow scheduler</pre><h3>Automatic startup</h3><p>Set up the Airflow webserver and scheduler with systemd to allow for<br>automatic start on server boot.</p><pre>[Unit]<br>Description=Airflow webserver daemon<br>After=network.target</pre><pre>[Service]<br>Environment=”PATH=/srv/airflow/bin”<br>Environment=”AIRFLOW_HOME=/srv/airflow”<br>User=airflow<br>Group=airflow<br>Type=simple<br>ExecStart=/srv/airflow/bin/airflow webserver -p 8080 — pid /srv/airflow/webserver.pid<br>Restart=on-failure<br>RestartSec=5s<br>PrivateTmp=true</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><p>Create a file <em>airflow-webserver.service</em> with the content listed above. Then run the following commands:</p><pre>sudo cp airflow-webserver.service /lib/systemd/system/<br>sudo systemctl daemon-reload<br>sudo systemctl enable airflow-webserver.service<br>sudo systemctl start airflow-webserver.service<br>sudo systemctl status airflow-webserver.service</pre><p>Do the same with <em>airflow-scheduler.service</em>:</p><pre>[Unit]<br>Description=Airflow scheduler daemon<br>After=network.target</pre><pre>[Service]<br>Environment=”PATH=/srv/airflow/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin”<br>Environment=”AIRFLOW_HOME=/srv/airflow”<br>User=airflow<br>Group=airflow<br>Type=simple<br>ExecStart=/srv/airflow/bin/airflow scheduler<br>Restart=always<br>RestartSec=5s</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><p>and run the commands:</p><pre>sudo cp airflow-scheduler.service /lib/systemd/system/<br>sudo systemctl daemon-reload<br>sudo systemctl enable airflow-scheduler.service<br>sudo systemctl start airflow-scheduler.service<br>sudo systemctl status airflow-scheduler.service</pre><p>You should then be able to start and stop the services with the systemctl command:</p><pre>sudo systemctl start airflow-webserver<br>sudo systemctl stop airflow-webserver</pre><pre>sudo systemctl start airflow-scheduler<br>sudo systemctl stop airflow-scheduler</pre><p>Try to reboot your server an see that both the airflow scheduler and the webserver start automatically.</p><pre>sudo reboot now</pre><h3>Static IP address</h3><p>Reserve an external static IP address. This will be where the users connect to from the internet, and we need to set up a DNS A record pointing to this IP address. This IP address will be the front end of the load balancer and is not directly attached to the server.</p><p>In the Google console go to “VPC Networks” | “External IP addresses” | “Reserve static address”. Enter a name (<em>airflow-static-ip</em>) and region (europe-west1). Do not attach the IP address to anything yet. We will attach it to the load balancer later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*tY9TcnAKMvCw871F1WspYQ.png" /></figure><p>Hit the “Reserve” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZiW_HAbT6fB9r-Ztb3LCVw.png" /></figure><p>Create a DNS A-record pointing to the static IP address created above. This is done in your DNS provider’s setup. I’m using the DNS name airflow.leira.net:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*tfTw03hn3LRbq91imojuDg.png" /></figure><p>You should, of course, use your own IP address and DNS name. Please note that the DNS A record must be in place before we can create an HTTPS certificate.</p><p>Checking that everything works for now by running:</p><pre>ping airflow.leira.net</pre><pre>Pinging airflow.leira.net [35.206.141.120] with 32 bytes of data</pre><p>You should not get any reply, but you should see the correct IP address.</p><h3>Load Balancer</h3><p>In the Google console, select “Network services” | “Load balancing” | “Create load balancer”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ZJGqsWPGPcve0BbYHg9R3w.png" /></figure><p>Select “Start configuration” in the box “HTTP(S) Load Balancing”.</p><p>Select “From Internet to my VMs” in the first step:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*-WPT8MW-jrG-JUIPdlZmCA.png" /></figure><p>Then give the load balancer a name of your choice, and click on the first bullet point “Backend configuration”, then “Backend services” | “Create a backend service”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*_x7_Tzh-3EwLASX7g5iZwg.png" /></figure><p>While entering the health check drop-down, another form is shown.</p><p>Named port: http8080<br>New backend:<br>Instance group: airflow-instance-group<br>Port numbers: 8080</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*ZFR1PEjnoHAFG3ifwo2KBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*JUUNmfWgaxJcDbjhjQ7-sA.png" /></figure><p>We do not have to do anything in the “Host and path rules”, so skip this now.</p><p>Enter the “Frontend configuration”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*lM7aRnxZ8qOPgmkTxSC3kA.png" /></figure><p>Choose a name and select:</p><p>Protocol: HTTPS<br>IP address: airflow-static-id (that we created above)<br>Certificate: Create a new certificate (again, before you do this, your domain name must have an A record pointing to the static IP address).</p><p>A new pop-up:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*xMw0FLOEIHxYa6Syo8Fuuw.png" /></figure><p>Enter a name, and choose “Create Google-managed certificate”.</p><p>Enter your DNS name pointing to the static IP address and hit the “Create” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*EGu3jU4rE2dw4cCyISiciQ.png" /></figure><p>Hit the “Done” button in the frontend configuration form.</p><p>Then hit the “Create” button in the “New HTTP(S) load balancer” form.</p><p>You should then end up with a load balancer like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*bo35zKlbW_LPanUkvskZSg.png" /></figure><p>Entering the newly created load balancer, we can see that the certificate is not ready yet. In this case, it took about 10 minutes before it was ready.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*z6e1hC2c5nlKKCyQ8OmHUQ.png" /></figure><h3>Conclusion</h3><p>I hope this post will help you.</p><p>Not solved. How to automatically redirect from HTTP (port 80) to HTTPS (port 443). It should be a setting in the load balancer, and I hope it will come in the future.</p><p>Please give me feedback.</p><h3>Miscellaneous</h3><p>I use the Environment systemd config file to set the path used by the scheduler. I only use the BashOperator and DummyOperator, and set up my Python jobs with a path to the wanted python version for my operators:</p><pre>task = BashOperator(<br> task_id=’my_task’,<br> bash_command=f’/srv/&lt;virtualenv&gt;/bin/python3 src/transfer.py’,<br> …<br>)</pre><p>I do not need to run my operators with the same Python version or virtual environment. Each may use their own, or share.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cdcdfe594019" width="1" height="1" alt=""><hr><p><a href="https://medium.com/compendium/airflow-on-gcp-may-2020-cdcdfe594019">Airflow on GCP (May 2020)</a> was originally published in <a href="https://medium.com/compendium">Compendium</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Install Apache Airflow on a Google Cloud Platform Virtual Machine]]></title>
            <link>https://medium.com/compendium/install-apache-airflow-on-a-google-cloud-platform-virtual-machine-f9a5b01b6c33?source=rss-dbdba8526170------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9a5b01b6c33</guid>
            <category><![CDATA[systemd]]></category>
            <category><![CDATA[teknologi]]></category>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[nginx]]></category>
            <dc:creator><![CDATA[Jostein Leira]]></dc:creator>
            <pubDate>Wed, 24 Apr 2019 13:21:15 GMT</pubDate>
            <atom:updated>2020-05-29T12:55:22.242Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PxafgMVye18LlDGWNfW59w.jpeg" /><figcaption>Three Wind Turbines — Photo: Vera Kratochvil (<a href="http://creativecommons.org/publicdomain/zero/1.0/">CC0 Public Domain</a>)</figcaption></figure><p>Installing Airflow from scratch is an alternative to the managed version Cloud Composer that Google offers. Here are my installation notes.</p><p>A new, updated version can be found here: <a href="https://medium.com/grensesnittet/airflow-on-gcp-may-2020-cdcdfe594019">Airflow on GCP (May 2020)</a></p><h3>Modules</h3><p>Ubuntu 18.04 LTS — already up and running, not covered<br>Python 3.6 — Default for Ubuntu 18.04 LTS<br>Apache Airflow 1.10.1<br>PostgreSQL 9.6 — Managed Cloud SQL version<br>Nginx 1.14.0 — Used as front-end and TLS termination<br>Let’s encrypt HTTPS certificate<br>Systemd services — Automatic startup</p><h3>Installing Airflow</h3><p>The commands below install the needed packages, creates a Python virtual environment under /srv, creates an airflow user that the server will run as and set owner and permissions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f22c43c2e136ef8cac1ef87caa66e00f/href">https://medium.com/media/f22c43c2e136ef8cac1ef87caa66e00f/href</a></iframe><h3>Installing Database</h3><p>Using a managed Cloud SQL database without a public IP address. Keeping costs down, choosing all minimum resources (1 CPU, 3.75 GB RAM, 10 GB HDD).</p><p>We want to end up with:</p><ul><li>database instance (server) with instance ID: airflow-db</li><li>database name: airflow</li><li>database user name: airflow-user</li><li>database password for the airflow-user: &lt;db-password&gt;</li><li>database IP address: &lt;db-server-ip&gt;</li></ul><p>From the Google console, go to STORAGE | SQL | select “Create new instance” and then “Choose PostgreSQL”. Choose “Private IP” and associate to an existing network that your VM can access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*A0JnvX1R1fOmOV24jhMQvQ.png" /><figcaption>Database installation. Note the password and select an associated network that your VM can access.</figcaption></figure><p>After you finish the database wizard you should create a new database “airflow” and a new user “airflow-user”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uI_6g63um511j0SFzRTAPw.png" /><figcaption>Create the new “airflow” database in your airflow-db instance.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aZxJeZA0gFnF3JMFWSMFLg.png" /><figcaption>Create a new user account “airflow-user” and take note of the password.</figcaption></figure><p>You need to locate the “Private IP address” from the instance details overview pane.</p><p>At this point, you should be able to connect the new database.</p><pre>$ psql -h <strong>&lt;db-server-ip&gt;</strong> -U airflow-user -d airflow<br>Password for user airflow-user:<br>psql (10.6 (Ubuntu 10.6–0ubuntu0.18.04.1), server 9.6.10)<br>SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128, compression: off)<br>Type “help” for help.</pre><pre>airflow=&gt;</pre><pre>airflow=&gt; grant usage on schema public to “airflow-user”;</pre><h4>Initialize the database and config file</h4><p>Running airflow initdbwill create config files and a metadata database. Since we are using PostgreSQL database we have to modify the database connection string in the config file afterward and then rerun the airflow initdb command.</p><pre>sudo su airflow<br>cd /srv/airflow<br>source bin/activate<br>export AIRFLOW_HOME=/srv/airflow<br>export AIRFLOW__WEBSERVER__RBAC=true</pre><pre>airflow initdb</pre><p>Review the /srv/airflow/airflow.cfgfile and change the connection string and impersonation user:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfda126404f3fc29603a26f0ba7279b0/href">https://medium.com/media/bfda126404f3fc29603a26f0ba7279b0/href</a></iframe><p>Review the file /srv/airflow/webserver_config.pyand ensure the following is set:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f3a3da30fe73ceb6c80f6852da8fd2a/href">https://medium.com/media/7f3a3da30fe73ceb6c80f6852da8fd2a/href</a></iframe><p>After editing the config files, then initialize PostgreSQL database (activated virtual environment):</p><pre>airflow initdb</pre><p>Add a new airflow admin user (activated virtual environment):</p><pre>airflow create_user -r Admin -u jon -e jon@exampl.com -f Jon -l Snow</pre><p>You can now start the airflow web server:</p><pre>airflow webserver -p 8080</pre><p>and scheduler:</p><pre>airflow scheduler</pre><h3>Nginx and Let’s encrypt certificates</h3><p>To use Nginx as a web front-end with HTTPS termination and Let’s encrypt certificates your Airflow server need to have:</p><ul><li>a public IP address</li><li>a DNS entry for your servers domain name (airflow.example.com) pointing to the public IP address</li><li>a firewall that allows ingress traffic from anywhere to port 80 and 443</li></ul><p>If all the above is true, then you can install the following repositories and packages:</p><pre>sudo su</pre><pre># Nginx as TLS terminator and reverse proxy for Airflow<br><em>apt </em>install nginx</pre><pre># Needed added repositories and packages for certbot<br><em>apt </em>update<br><em>apt </em>install software-properties-common -y<br><em>add-apt-repository </em>ppa:certbot/certbot -y<br><em>apt update<br>apt upgrade<br>apt </em>install python-certbot-nginx -y</pre><p>Run once on a new machine to create a new dhparam.pem file:</p><pre>sudo su<br>cd /etc/ssl/private<br>openssl dhparam -out dhparam.pem 4096<br>chmod o-rwx dhparam.pem</pre><p>Edit /etc/nginx/nginx.conf and add/edit the following in the SSL Settings in the http section:</p><pre>ssl_session_timeout 10m;<br>ssl_session_cache shared:SSL:10m;<br>ssl_protocols TLSv1.2;<br>ssl_ciphers ‘EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH’;<br>ssl_dhparam /etc/ssl/private/dhparam.pem;<br>ssl_prefer_server_ciphers on;</pre><p>Create a new Nginx config file for your airflow site<br>sudo nano /etc/nginx/sites-available/airflow.example.comshown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac7ce7fd5498c2c28b4edd2b58a48c5d/href">https://medium.com/media/ac7ce7fd5498c2c28b4edd2b58a48c5d/href</a></iframe><p>This configuration is depending on Airflow web server listening on 127.0.0.1:8080. Apply your configuration change by reloading nginx:</p><pre>sudo service nginx reload</pre><p>Then run certbotin interactive mode:</p><pre>sudo certbot --nginx</pre><p>After Certbot has run successfully, you should end up with two server sections, one for HTTP and one for HTTPS. You need to add the location section:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18aaac08359304885b99fa87b0087367/href">https://medium.com/media/18aaac08359304885b99fa87b0087367/href</a></iframe><p>If successful, check the nginx config and reload nginx.</p><pre>sudo nginx -t<br>sudo service nginx reload</pre><p>You should now be able to access your web site with HTTPS. Check your test score here: <a href="https://www.ssllabs.com/ssltest/analyze.html?d=bigdata.bbl.no&amp;hideResults=on&amp;latest">https://www.ssllabs.com/ssltest/analyze.html?d=airflow.example.com&amp;hideResults=on&amp;latest</a></p><h3>Systemd services</h3><p>Set up the Airflow web server and scheduler with systemd to allow for automatic start on server boot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e13b207eadc7a8669e4f75409c3fa95/href">https://medium.com/media/0e13b207eadc7a8669e4f75409c3fa95/href</a></iframe><p>Copy the above file to /lib/systemd/system:</p><pre>sudo cp airflow-webserver.service /lib/systemd/system/<br>sudo systemctl daemon-reload<br>sudo systemctl enable airflow-webserver.service<br>sudo systemctl start airflow-webserver.service<br>sudo systemctl status airflow-webserver.service</pre><p>To the same with airflow-scheduler.service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/371099c2da20d092e72db50bfe5f78c7/href">https://medium.com/media/371099c2da20d092e72db50bfe5f78c7/href</a></iframe><p>You should then be able to start and stop the services with the systemctlcommand:</p><pre>sudo systemctl stop airflow-webserver<br>sudo systemctl stop airflow-scheduler<br>sudo systemctl stop nginx</pre><p>I use the Environment to set the path used by the scheduler. I only use the BashOperator and DummyOperator, and set up my Python jobs with:</p><pre>task = BashOperator(<br>    task_id=&#39;my_task&#39;,<br>    bash_command=f’/srv/&lt;virtualenv&gt;/bin/python3 src/transfer.py’,<br>    ...<br>)</pre><h3>Misc</h3><p>Please give me feedback. It is a complicated installation and with many moving parts.</p><h4>Notes/Problems</h4><p>Had to create the directory /home/airflow, even if I used the installation directory /srv/airflow, some service always wanted to check the /home/airflow directory. I never found a way to make airflow not check that directory.</p><p>You should add a firewall rule to only allow traffic from your own, known, sites.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9a5b01b6c33" width="1" height="1" alt=""><hr><p><a href="https://medium.com/compendium/install-apache-airflow-on-a-google-cloud-platform-virtual-machine-f9a5b01b6c33">Install Apache Airflow on a Google Cloud Platform Virtual Machine</a> was originally published in <a href="https://medium.com/compendium">Compendium</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>