<?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>Forem: Nick Lavrov</title>
    <description>The latest articles on Forem by Nick Lavrov (@nicklavrov).</description>
    <link>https://forem.com/nicklavrov</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%2F95537%2Fc2fd674f-a03e-405c-849e-954fbfa51661.jpeg</url>
      <title>Forem: Nick Lavrov</title>
      <link>https://forem.com/nicklavrov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nicklavrov"/>
    <language>en</language>
    <item>
      <title>Calculating Prime Numbers with CircleCI</title>
      <dc:creator>Nick Lavrov</dc:creator>
      <pubDate>Sat, 25 Aug 2018 01:01:14 +0000</pubDate>
      <link>https://forem.com/nicklavrov/calculating-prime-numbers-with-circleci-243e</link>
      <guid>https://forem.com/nicklavrov/calculating-prime-numbers-with-circleci-243e</guid>
      <description>&lt;p&gt;If you're looking for a list of prime numbers, &lt;a href="https://en.wikipedia.org/wiki/List_of_prime_numbers"&gt;you&lt;/a&gt; &lt;a href="https://www.mathsisfun.com/numbers/prime-numbers-to-10k.html"&gt;don't&lt;/a&gt; &lt;a href="http://math.info/Arithmetic/Prime_Numbers_List/"&gt;have&lt;/a&gt; &lt;a href="https://primes.utm.edu/lists/small/10000.txt"&gt;to&lt;/a&gt; &lt;a href="http://compoasso.free.fr/primelistweb/page/prime/liste_online_en.php"&gt;look&lt;/a&gt; &lt;a href="http://www.primos.mat.br/indexen.html"&gt;very&lt;/a&gt; &lt;a href="http://www.tsm-resources.com/alists/prim.html"&gt;far&lt;/a&gt;. Nevertheless, here's my small, completely useful way to add to this list.&lt;/p&gt;

&lt;p&gt;I decided the best way for me to do this was to create a continuously growing list of prime numbers maintained in a Github repo. I would use CircleCI to compute the next prime number and update the list. &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i3JOwpme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/NickLavrov"&gt;
        NickLavrov
      &lt;/a&gt; / &lt;a href="https://github.com/NickLavrov/circleci-primes"&gt;
        circleci-primes
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Create a list of prime numbers using CircleCI
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
circleci-primes&lt;/h1&gt;
&lt;p&gt;&lt;a href="https://circleci.com/gh/NickLavrov/circleci-primes" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/16dc9fd6832868e24536b2d33e74cd600ed03fda6c99023c5164b98bd8258106/68747470733a2f2f636972636c6563692e636f6d2f67682f4e69636b4c6176726f762f636972636c6563692d7072696d65732e7376673f7374796c653d736869656c64" alt="CircleCI"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Create a list of prime numbers using CircleCI!&lt;/p&gt;
&lt;p&gt;This repo keeps a running list of prime numbers, found in &lt;code&gt;primes.txt&lt;/code&gt;. A commit to the master branch triggers a build on CircleCI, calculates the next prime number, adds it to the list, commits the new list to the master branch, triggers a build on CircleCI, calculates the next prime number, adds it to the list... you get the idea.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://circleci.com/gh/NickLavrov/circleci-primes" title="Builds on CircleCI" rel="nofollow"&gt;Watch the builds here!&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/NickLavrov/circleci-primes"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;The structure of the repo is simple enough. There is a list of primes, initialized with 2 and 3; a script called nextprime.sh that, unsurprisingly, finds the next prime; and a CircleCI config file that appends the next prime to the list and commits the new list.&lt;/p&gt;

&lt;p&gt;Here's the fun part - builds on CircleCI are triggered by commits to the master branch, and since the build commits the new prime list to the master branch, a new build will calculate the next prime number. &lt;a href="https://circleci.com/gh/NickLavrov/circleci-primes"&gt;Watch the builds in real time here!&lt;/a&gt; This will happen until I cancel a build before the commit happens, or until the kind folks at CircleCI decide enough is enough.&lt;/p&gt;

&lt;p&gt;Are there better, faster ways to do this? Of course. Is this a complete waste of time? Debatable. When you constrain yourself to a certain set of tools, you're forced to come up with creative solutions. Because of this, I learned some new things about shell scripting and about how CircleCI handles SSH and Github communications.&lt;/p&gt;

&lt;p&gt;If you're interested, below are some of the challenges I faced:&lt;/p&gt;

&lt;h3&gt;
  
  
  • Doing math with shell scripts
&lt;/h3&gt;

&lt;p&gt;I decided I wanted to use a shell script for two main reasons. Firstly, I wanted to use the tiny &lt;a href="https://hub.docker.com/_/alpine/"&gt;Docker alpine image&lt;/a&gt; for my builds.  I don't want any extra stuff! (Although you'll see below that I needed to add the &lt;code&gt;bc&lt;/code&gt; (Basic Calculator) tool anyway) Secondly, I love shell scripting. Yeah, I'm serious.&lt;/p&gt;

&lt;p&gt;I was originally using &lt;code&gt;expr&lt;/code&gt; to do math, but after trial and error, I realized that there was an upper limit to numbers &lt;code&gt;expr&lt;/code&gt; could handle. Specifically, from the man page:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Arithmetic operations are performed using signed integer math with a range according to the C intmax_t data type (the largest signed integral type available).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So once I started dealing with numbers greater than 2^63, there would be issues. Try running the following commands in your terminal to see for yourself:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expr 42 + 1&lt;/code&gt;&lt;br&gt;
&lt;code&gt;expr 9223372036854775808 + 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since I fully expect this to calculate prime numbers past 2^63, this limit of &lt;code&gt;expr&lt;/code&gt; is &lt;a href="https://www.youtube.com/watch?v=b-t8JmyCvWg"&gt;unacceptable&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I found a tool called &lt;code&gt;bc&lt;/code&gt; that does not have this limit because it "supports arbitrary precision numbers with interactive execution of statements." It's also easier to use, in my opinion. With a little work after that, my prime number generation script was finished. The nice thing about having a list of prime numbers is that when finding the next prime number, you have a nice list of divisors to test it!&lt;/p&gt;
&lt;h3&gt;
  
  
  • Pushing to a Github repo with CircleCI
&lt;/h3&gt;

&lt;p&gt;It's easy enough to pull from a Github repo with Circle. Pushing, on the other hand, is a bit harder. One way is to use your own Github token (stored securely as an env var within CircleCI, of course!) to push, with something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git push -q https://${GITHUB_TOKEN}@github.com/NickLavrov/circleci-primes.git HEAD&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But I didn't want to go that route. I wanted to push on behalf of CircleCI and not have any risk of exposing my personal Github token (that &lt;code&gt;-q&lt;/code&gt; is very, very important).&lt;/p&gt;

&lt;p&gt;So I created a new deploy key for the repo on Github, gave it write access, added the SSH key on CircleCI, and thought I was done. Unfortunately, when it came time to push, I was hit with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/bin/sh: git: not found&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So I installed &lt;code&gt;git&lt;/code&gt;. Then I was hit with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fatal: You are not currently on a branch.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So I changed the command to &lt;code&gt;git push origin HEAD:master&lt;/code&gt; and was hit with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fatal: cannot run ssh: No such file or directory&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So I added a step to install &lt;code&gt;openssh&lt;/code&gt; and was hit with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;The authenticity of host 'github.com (192.30.253.112)' can't be established.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is where I found a solution that I really don't like. I added a step in the config to add github.com to the ~/.ssh/known_hosts file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '
github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
' &amp;gt;&amp;gt; ~/.ssh/known_hosts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I didn't want to install any other tools besides bc, git, and openssh, so I stayed with this solution. If you can think of something a bit more elegant, I'd love to know!&lt;/p&gt;

</description>
      <category>circleci</category>
      <category>git</category>
    </item>
  </channel>
</rss>
