<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title></title>
    <description>Risa writes about Rails, Postgres, DevOps, and other things. She loves pairing, internet memes, cats, and all the cute things.
</description>
    <link>http://risaonrails.com/</link>
    <atom:link href="http://risaonrails.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 03 Jul 2017 10:01:56 -0700</pubDate>
    <lastBuildDate>Mon, 03 Jul 2017 10:01:56 -0700</lastBuildDate>
    <generator>Jekyll v3.1.6</generator>
    
      <item>
        <title>Regex and Ruby</title>
        <description>&lt;h2 id=&quot;regex&quot;&gt;Regex&lt;/h2&gt;

&lt;p&gt;I wanted to give a quick tip on something I recently discovered about regex and Ruby, while trying to look for some regex to use.&lt;/p&gt;

&lt;p&gt;This is a journey of self discovery... Wait, no, that&amp;#39;s not right. It was definitely an adventure. And it started when the search for a particular regex got mashed up with knowing Rubocop preferring string literals to be immutable with &lt;code&gt;.freeze&lt;/code&gt;.  Oh, how we dive into rabbit holes.&lt;/p&gt;

&lt;h3 id=&quot;immutability&quot;&gt;Immutability&lt;/h3&gt;

&lt;p&gt;As mentioned above, string literals should be made immutable with &lt;code&gt;.freeze&lt;/code&gt;. It&amp;#39;s a thing that Rubocop picks up on and alerts you.  &lt;code&gt;.freeze&lt;/code&gt; does 2 things: one, the object can&amp;#39;t be changed (immutable), and two, it doesn&amp;#39;t create as many objects, so in terms of memory use, it&amp;#39;s hella beneficial.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.honeybadger.io/when-to-use-freeze-and-frozen-in-ruby/&quot;&gt;Click here&lt;/a&gt; to read a fantastic article that talks about this exact thing in a much better way than I can.&lt;/p&gt;

&lt;p&gt;Now, knowing this, I wondered if I could do something similar with regex. While Googling for it, I came upon a &lt;a href=&quot;https://bugs.ruby-lang.org/issues/8948&quot;&gt;discussion on exactly this topic&lt;/a&gt; in the Ruby developer forums.&lt;/p&gt;

&lt;p&gt;And I found out that Ruby has immutable regex. This is great news!&lt;/p&gt;

&lt;p&gt;But, more importantly? &lt;strong&gt;&lt;em&gt;Dynamic Regex&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;dynamic-regex&quot;&gt;Dynamic Regex&lt;/h3&gt;

&lt;p&gt;Admittedly I didn&amp;#39;t know that regex &lt;em&gt;could&lt;/em&gt; be dynamic, but given that this is Ruby we&amp;#39;re talking about, I should have assumed it. From the link above, user &lt;a href=&quot;https://bugs.ruby-lang.org/users/772&quot;&gt;Eregon&lt;/a&gt; provides examples, and they speak for themselves. But I&amp;#39;ve re-done them in my own console to confirm:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dynamic_regex&lt;/span&gt;
  &lt;span class=&quot;sr&quot;&gt;/ab&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;057&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dynamic_regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;object_id&lt;/span&gt;
 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;70175794398600&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dynamic_regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;object_id&lt;/span&gt;
 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;70175794381460&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dynamic_regex_frozen&lt;/span&gt;
  &lt;span class=&quot;sr&quot;&gt;/ab&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/o&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;052&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dynamic_regex_frozen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;object_id&lt;/span&gt;
 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;70175794516740&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mo&quot;&gt;053&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dynamic_regex_frozen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;object_id&lt;/span&gt;
 &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;70175794516740&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I never knew this was even a thing. I&amp;#39;m glad I know about it, though I don&amp;#39;t know when I&amp;#39;ll ever use it. But, it&amp;#39;s a little bit of knowledge gained.&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +2&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Mon, 03 Jul 2017 03:01:30 -0700</pubDate>
        <link>http://risaonrails.com/regex-and-ruby/</link>
        <guid isPermaLink="true">http://risaonrails.com/regex-and-ruby/</guid>
        
        <category>ruby</category>
        
        <category>regex</category>
        
        
        <category>rails</category>
        
        <category>ruby</category>
        
      </item>
    
      <item>
        <title>Optimizing an App, Part 1</title>
        <description>&lt;h2 id=&quot;jumping-right-into-it&quot;&gt;Jumping right into it&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;NB: This is a two part series on small little optimizations that had huge impacts on a client&amp;#39;s app.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A while ago, a colleague/mentor of mine was telling me about the power of &lt;code&gt;.to_sql&lt;/code&gt;, which, when used in console, outputs the SQL query to the ActiveRecord query you wrote. It&amp;#39;s a super handy tool.&lt;/p&gt;

&lt;p&gt;At the time, I didn&amp;#39;t think too much about it except that it was something nifty to keep in my toolbox.&lt;/p&gt;

&lt;p&gt;And now, last month. A client of mine had asked me to help troubleshoot some issues with his staging app. The app only had 2 active users, but memory usage was inexplicably high. As in, the app kept running at around 70-120% of the 512MB allotted RAM on Heroku&amp;#39;s hobby tier.&lt;/p&gt;

&lt;h2 id=&quot;why-not-get-a-bigger-dyno&quot;&gt;Why not get a bigger dyno?&lt;/h2&gt;

&lt;p&gt;Remember, the app only had 2 active users. For something like that, there really isn&amp;#39;t a reason for the app to be maxing out on memory usage. Luckily Heroku has a metrics tab that allows you to see the state of the dyno for the past 24 hours. At first I thought it was a question of leaky memory, but the data didn&amp;#39;t hold true. It would start at around 256MB, which is normal and great, but almost immediately would shoot up towards max capacity, then every hour it&amp;#39;d cause the dyno to restart due to OOM (out of memory) errors.  The client was baffled. So was I.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NB: a leaky app would have had a slow creep of memory usage over a longer period of time, something that&amp;#39;d look like a y = ax + b kind of graph. The current app was looking more like a y = ax^3 + b kind of graph. Exponential growth&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As the app was for staging, neither he nor I wanted to bump up the dyno to account for the RAM, even though that would immediately solve the problem.  I spoke with his developer, and he walked me through the app&amp;#39;s functionality and showed me their logs.&lt;/p&gt;

&lt;p&gt;Almost immediately I noticed a few things. One, he had his production config set to &lt;code&gt;:debug&lt;/code&gt;, which, admittedly is the default, but it makes for extremely noisy output. Two, even with all that excess noise, the app was still outputting so much stuff.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://media.giphy.com/media/1M9fmo1WAFVK0/giphy.gif&quot; alt=&quot;But why?&quot;&gt;&lt;/p&gt;

&lt;p&gt;We dove deeper.&lt;/p&gt;

&lt;h2 id=&quot;hello-ruby&quot;&gt;Hello, Ruby&lt;/h2&gt;

&lt;p&gt;I asked the dev to run the app locally so I could scrutinize the output of the logs while pouring over the code line by line. Literally, line by line. I haven&amp;#39;t had to visually parse through code like this since learning Java in Intro to CS 102.&lt;/p&gt;

&lt;p&gt;Something stood out. There was code that said something to the effect of:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;https://media.giphy.com/media/El10jTzCzQymk/giphy.gif&quot; alt=&quot;hmmm....fascinating&quot;&gt;&lt;/p&gt;

&lt;p&gt;It almost slipped by me, except the  &lt;code&gt;[-1]&lt;/code&gt; part stood out, I was a bit baffled as I&amp;#39;d never seen code like this in Rails before, even though it&amp;#39;s a common thing for Arrays in Ruby.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s break this code down. The &lt;code&gt;self&lt;/code&gt; refers to an ActiveRecord instance, e.g. &lt;code&gt;User.first&lt;/code&gt;. In this particular instance, a User has a &lt;em&gt;1:1&lt;/em&gt; relationship with Post. This Post instance object has a &lt;em&gt;1:many&lt;/em&gt; relationship with items. In case you missed this lesson when learning about arrays in Ruby, the &lt;code&gt;[-1]&lt;/code&gt; is to return the very last item in the array.  This kind of call is essentially saying in simple English: &amp;quot;I need all the Items of this Post, and then I want the last one of that list, and I want its id.&amp;quot;  Because of the &lt;em&gt;1:many&lt;/em&gt; relationship Post has with Items, the SQL query is a &lt;code&gt;where&lt;/code&gt; lookup, which returns an array of ActiveRecord objects.&lt;/p&gt;

&lt;p&gt;One thing that I&amp;#39;ve learned is that Arrays are great if there&amp;#39;s only a small number of them, but if there are many, it&amp;#39;s super sluggish. It&amp;#39;s really not ideal when you&amp;#39;re talking about a ton of records.  Not only that but when you do &lt;code&gt;post.items&lt;/code&gt;, the returned collection of AR objects gets shoved into Ruby memory.  Imagine shoving 100,000 records into memory.... No bueno.  Essentially, the code would return all the items as an array, put it into Ruby memory, then from there get the last object&amp;#39;s id.&lt;/p&gt;

&lt;p&gt;Repeat that kind of query a bunch of times, and I hope you see where this is going.&lt;/p&gt;

&lt;p&gt;To top it off, the developer &lt;em&gt;only&lt;/em&gt; wanted the last item&amp;#39;s ID. We didn&amp;#39;t need anything else. Technically we didn&amp;#39;t want all the items belonging to the post, nor did we even want the last item&amp;#39;s full AR object. In simple English, it was &amp;quot;Return the id of the last item that belongs to Post.&amp;quot; So why make Rails do this? To me, this screams &amp;quot;I can be a SQL query!&amp;quot; Asking your database to do SQL queries is &lt;em&gt;far more efficient&lt;/em&gt; than asking ActiveRecord/Rails to translate to SQL and return a bunch of objects &lt;em&gt;and then&lt;/em&gt; get other data.&lt;/p&gt;

&lt;p&gt;Now we had something we could optimize.&lt;/p&gt;

&lt;h2 id=&quot;sql-power-to-the-rescue&quot;&gt;SQL power to the rescue&lt;/h2&gt;

&lt;p&gt;We wanted the last item&amp;#39;s ID based on the current post. We wanted a SQL query that looked something like: (in pseudo code)&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;select max id from items where user.post is a number (e.g. 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And SQL-fied:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;items&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;id&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;items&#39;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;user&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;post_id&#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;#39;s see if Rails can do this. We know the &lt;code&gt;post_id&lt;/code&gt; because it&amp;#39;s been given to us as part of &lt;code&gt;user&lt;/code&gt; so...&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;post_id: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;maximum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We ran the above and appended &lt;code&gt;.to_sql&lt;/code&gt; and confirmed that the generated SQL was exactly what we wanted above. Thank you to my colleague/mentor, Adam!&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s what it looked like afterwards, because &amp;quot;pics or it didn&amp;#39;t happen&amp;quot; (which, if you think about it, is the new millennium equivalent of &amp;quot;a picture is worth a 1000 words&amp;quot;). It would have been great if I had gotten a screenshot of what it looked like before, but as you can see, none of this &amp;quot;off the chart&amp;quot; business.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://i.imgur.com/peFpxtQ.png&quot; alt=&quot;before and after pic&quot;&gt;&lt;/p&gt;

&lt;h2 id=&quot;punt-it&quot;&gt;Punt it!&lt;/h2&gt;

&lt;p&gt;Lesson learned. If you can punt the work to the database, let it do the heavy lifting of searching/sorting/finding. Don&amp;#39;t let Rails do it.&lt;/p&gt;

&lt;p&gt;This one change resulted in the memory to drop to a steady 256MB. We haven&amp;#39;t seen it bust out the OOM errors despite throwing more users and data at it. EXCELLENT.&lt;/p&gt;

&lt;p&gt;Stay tuned for Part 2, where I talk about some other small optimizations including our favourite N+1 query.&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +5&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Fri, 26 May 2017 07:46:28 -0700</pubDate>
        <link>http://risaonrails.com/optimizing-queries/</link>
        <guid isPermaLink="true">http://risaonrails.com/optimizing-queries/</guid>
        
        <category>rails</category>
        
        <category>optimization</category>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>I used vi the other day</title>
        <description>&lt;p&gt;I am really not a fan of using &lt;code&gt;vi&lt;/code&gt; or &lt;code&gt;vim&lt;/code&gt; or &lt;code&gt;emacs&lt;/code&gt; or whatever complicated built-in editor. If ever I&amp;#39;m on the server, I use &lt;code&gt;nano&lt;/code&gt; because it&amp;#39;s stupidly simple and gets the job done. Yes I realize there are a bazillion reasons as to why &lt;code&gt;vi&lt;/code&gt; is better, but the amount of time needed to fully learn everything &lt;em&gt;PLUS&lt;/em&gt; set up my working environment just the way I would want it isn&amp;#39;t worth it to me. The value add does not justify the learning/time cost.&lt;/p&gt;

&lt;p&gt;HOWEVER! As with all things, it&amp;#39;s important to still be somewhat familiar with it. I have seriously been refusing to learn how to use &lt;code&gt;vi&lt;/code&gt;, despite insistence from numerous colleagues. It wasn&amp;#39;t until recently (now a few months ago, oh how quickly time flies) that I had learned a cool new trick. And I shall impart this knowledge to you, fellow reader, or to future me.&lt;/p&gt;

&lt;h2 id=&quot;but-first&quot;&gt;But first...&lt;/h2&gt;

&lt;p&gt;Let&amp;#39;s go over some basics.&lt;/p&gt;

&lt;h3 id=&quot;super-basic-commands&quot;&gt;Super basic commands&lt;/h3&gt;

&lt;p&gt;Here&amp;#39;s how to enter and exit out of &lt;code&gt;vi&lt;/code&gt;/&lt;code&gt;vim&lt;/code&gt;. &lt;code&gt;vim&lt;/code&gt; is the &amp;quot;improved&amp;quot; version of &lt;code&gt;vi&lt;/code&gt;, you know, version 2. &lt;img class=&#39;emoji&#39; title=&#39;wink&#39; alt=&#39;wink&#39; src=&#39;https://github.global.ssl.fastly.net/images/icons/emoji/wink.png&#39; height=&#39;20&#39; width=&#39;20&#39; align=&#39;absmiddle&#39; &gt;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# to open a file with vi&lt;/span&gt;
vi file_name.text
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# to exit out of a file without saving&lt;/span&gt;
:q
&lt;span class=&quot;c&quot;&gt;# to force quit w/o saving&lt;/span&gt;
:q!

&lt;span class=&quot;c&quot;&gt;# to exit out of a file &amp;amp;&amp;amp; save&lt;/span&gt;
:wq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note, you must add the &lt;code&gt;:&lt;/code&gt; in there.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;q&lt;/code&gt; is for quit, &lt;code&gt;w&lt;/code&gt; is for write, so &lt;code&gt;:wq&lt;/code&gt; is to write (save) and exit.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s move on now. Inserting text.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# to go into &#39;typing things into editor because its technically a text editor&#39; mode&lt;/span&gt;
i  &lt;span class=&quot;c&quot;&gt;# (for insert)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# to exit out of insert mode&lt;/span&gt;
cntl-c
&lt;span class=&quot;c&quot;&gt;# or&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;lt;ESC&amp;gt;  &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# sometimes the ESC button works, sometimes it doesn&#39;t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note: the ESC seems to work on Linux distros, such as Ubuntu, so when you&amp;#39;re on the server then you can press &lt;code&gt;Esc&lt;/code&gt;. On my Mac, that isn&amp;#39;t the case and only &lt;code&gt;cntl-c&lt;/code&gt; works.&lt;/p&gt;

&lt;p&gt;And now let&amp;#39;s delete a full line of text. Or a chunk of text.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# to delete the current line&lt;/span&gt;
dd                    &lt;span class=&quot;c&quot;&gt;# =&amp;gt; cntl-k on nano&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# to delete 20 lines (+1 extra on a mac)&lt;/span&gt;
20d &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;then &lt;/span&gt;press enter&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Note: this deletes 20+1 lines from where the blinker/cursor is.&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# to delete line 5 only&lt;/span&gt;
:5d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Jumping down to a particular line number. This is the equivalent to scrolling to line 10 and inserting your cursor there.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# jump down to line 10&lt;/span&gt;
:10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;the-fun-stuff&quot;&gt;The fun stuff&lt;/h3&gt;

&lt;p&gt;Here&amp;#39;s where the power of &lt;code&gt;vi&lt;/code&gt; and &lt;code&gt;vim&lt;/code&gt; lie. You can jump down to a certain line number, you can delete multiple lines, you can find and replace text within a file. All the things you can do in your standard editor like Atom or Sublime, you can now do in &lt;code&gt;vi&lt;/code&gt;! Woo! /s&lt;/p&gt;

&lt;p&gt;But really, I happen to enjoy using my mouse and highlighting/scrolling with it. Moving my hands from the keyboard doesn&amp;#39;t make me a horrible programmer. Writing horrible code makes me a horrible programmer.  It also doesn&amp;#39;t really let me be less efficient either. Honestly, switching from keyboard to trackpad/mouse/pen allows your brain to switch gears and quietly pause and reflect on your coding decisions, such as &amp;quot;Did I really mean to write that line of code? Maybe I should delete it&amp;quot; vs going back and willy-nilly pull the trigger and delete 15 lines of code only to realize afterwards that it was probably a bad idea and you should have kept it because now you could have used it. It&amp;#39;s a built in mechanism to prevent yourself from pulling the trigger too quickly. (&lt;em&gt;Insert social commentary of how this should always be applied, mostly when actual weapons are involved...&lt;/em&gt;) However, in server-land, you don&amp;#39;t get the built-in benefits of actually having a GUI, which means text editing must be done in a GUI-less environment.&lt;/p&gt;

&lt;p&gt;I digress.&lt;/p&gt;

&lt;p&gt;The main point of this entire post was really for this next reason.  May I present to you.... &lt;strong&gt;Find and replace all!&lt;/strong&gt; This has been the one main benefit to using vi. And if I am doing batch text changes, to a config file for example, this is the best way to do it.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# find all instances of phrase and replace it with something else&lt;/span&gt;
:%s/original_word/new_word

&lt;span class=&quot;c&quot;&gt;# for example...&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# find all asdf and replace with 0000&lt;/span&gt;
:%s/asdf/0000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There is a caveat to the above. This will change all instances without confirmation, so if there is one you want to skip, this is not the command for you.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s look at the other options and iterations of the Find and Replace All command.  First, let&amp;#39;s confirm our individual find/replaces. Appending &lt;code&gt;gc&lt;/code&gt; will do a global search with confirmation. You will have to type &lt;code&gt;y&lt;/code&gt; or &lt;code&gt;n&lt;/code&gt; for replace or skip.  It will highlight the word it is looking to replace. I prefer this as it reminds me of &lt;code&gt;git add -p&lt;/code&gt; and forcibly confirming each one.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# confirm each change before replacing&lt;/span&gt;
:%s/asdf/0000/gc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What happens if the word you want to replace is kind of funky? like &lt;code&gt;http://localhost&lt;/code&gt; needs to be replaced with &lt;code&gt;$APP_URL&lt;/code&gt;? Uh oh, you can&amp;#39;t use &lt;code&gt;/&lt;/code&gt; since the command also uses it as the delimiter. What do? Simple. Change the delimiter! I prefer the &lt;code&gt;#&lt;/code&gt; for this, but it can be any symbol as long as if it&amp;#39;s consistently used across the command.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# change delimiter and confirm before replacing&lt;/span&gt;
:%s#original_word#new_word#gc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using the example (without bash code-style turned on)&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;:%s#http://localhost#$APP_URL#gc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It looks ugly but, wowza, this really works.  If I had used &lt;code&gt;nano&lt;/code&gt; to find text and then delete and replace it, that would have taken me ages. I could have also copied the text and pasted it into Atom to replace what I needed to replace, but both of these are costly. And my time is extremely expensive. In this case, the value add of this simple use case is incredibly high and far outweighs the knowledge cost.&lt;/p&gt;

&lt;p&gt;So there you have it! Possibly the best thing about &lt;code&gt;vi&lt;/code&gt;.  I am sure I will encounter other fun handy things with &lt;code&gt;vi&lt;/code&gt;, but for now, I&amp;#39;m pretty good with this list as is.&lt;/p&gt;

&lt;p&gt;Don&amp;#39;t forget to &lt;code&gt;:wq&lt;/code&gt; your changes! &lt;img class=&#39;emoji&#39; title=&#39;grinning&#39; alt=&#39;grinning&#39; src=&#39;https://github.global.ssl.fastly.net/images/icons/emoji/grinning.png&#39; height=&#39;20&#39; width=&#39;20&#39; align=&#39;absmiddle&#39; &gt;&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +5 (+10 if you&amp;#39;ve been avoiding vi as if it were the bubonic plague)&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Sun, 25 Sep 2016 10:17:17 -0700</pubDate>
        <link>http://risaonrails.com/i-used-vi-the-other-day/</link>
        <guid isPermaLink="true">http://risaonrails.com/i-used-vi-the-other-day/</guid>
        
        <category>vi</category>
        
        <category>sysadmin</category>
        
        <category>devops</category>
        
        
        <category>rails</category>
        
        <category>devops</category>
        
      </item>
    
      <item>
        <title>Testing, testing, 1...2...3</title>
        <description>&lt;p&gt;&lt;em&gt;Hello? Is anybody in there? Just nod if you can hear me...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For the past while, I&amp;#39;ve been writing a lot of tests to make up for the lack of them in some apps at work. Most of that time has been writing feature tests.  I&amp;#39;ve encountered a lot of weird little things that I&amp;#39;ve struggled with. I don&amp;#39;t quite know &lt;em&gt;why&lt;/em&gt; some things have happened, but I&amp;#39;ve figured out &lt;em&gt;how&lt;/em&gt; to get around or solve the problems I&amp;#39;ve been facing.&lt;/p&gt;

&lt;p&gt;So this is all about the tips and tricks I&amp;#39;ve learned while writing feature tests.&lt;/p&gt;

&lt;h3 id=&quot;but-first-a-digression&quot;&gt;But first, a digression&lt;/h3&gt;

&lt;p&gt;When I first learned Rails, I learned Behavior Driven Development (BDD) with Cucumber. If you&amp;#39;re not aware, Cucumber isn&amp;#39;t just a vegetable anymore! Cucumber tests use a DSL called Gherkin, which is business-readable. (Tho, what&amp;#39;s up with the pickling vegetable names?) The concept behind it is that it&amp;#39;s for the client to read and understand, as if they were created from the 3x5 index cards used to define the feature at the beginning.  The format is set in a &amp;#39;Given-When-Then&amp;#39; setup, such as: &amp;#39;Given I have some chocolate, when I open the package, then it is eaten.&amp;#39; What was familiar English on the front, was actually a bunch of regex in the back combined with Capybara to drive the actions.&lt;/p&gt;

&lt;p&gt;One of the common problems I&amp;#39;ve faced when using Cucumber was that the steps, the given/when/thens, would sometimes get repeated, not redundant, but slightly different in wording gave it its own step definition. Bloated. It was also annoying to try to find a step definition when one would say &amp;quot;When I click on a button&amp;quot; and another would say &amp;quot;When I click a button&amp;quot;. This would be even more accentuated if there were multiple devs involved in the project writing their own step definitions. I wonder if this was one of the reasons why people stopped using cukes for testing and started incorporating feature tests with RSpec.  &lt;/p&gt;

&lt;p&gt;I, for one, have embraced using RSpec feature tests with Capybara. You still have the same expectations but without the &lt;em&gt;cucumbersome&lt;/em&gt; extra layers of Gherkin (which no client actually really reads) and bloated step definitions. Instead, it&amp;#39;s straight up RSpec and Capybara for you and your dev team.&lt;/p&gt;

&lt;h2 id=&quot;tips-amp-tricks&quot;&gt;Tips &amp;amp; Tricks&lt;/h2&gt;

&lt;h4 id=&quot;0-5-save-and-open-page&quot;&gt;0.5) Save and open page&lt;/h4&gt;

&lt;p&gt;I&amp;#39;m always a bit surprised when I hear that devs have never heard of &lt;code&gt;save_and_open_page&lt;/code&gt;, a Capybara method that lets you view a simple html-only snapshot of the page at invocation. Unfortunately it renders the page without the benefit of JS so things the javascript would hide would be visible and the angular interpolations would display. As long as if you&amp;#39;re aware of the pitfalls, &lt;code&gt;save_and_open_page&lt;/code&gt; is a fantastic lil line that can help you troubleshoot. Stick it in the feature spec anywhere before your expectation. You may need the &lt;code&gt;launchy&lt;/code&gt; gem for this now.&lt;/p&gt;

&lt;h4 id=&quot;1-would-you-like-some-javascript-with-those-features&quot;&gt;1) Would you like some Javascript with those features?&lt;/h4&gt;

&lt;p&gt;Typically, our apps would have &lt;code&gt;@javascript&lt;/code&gt; at the beginning of each scenario when we wanted to test a feature that incorporates Javascript, such as Angular. I am pretty sure this is a carry-over from the Cucumber days but in RSpec land, you can get away with simply stating &lt;code&gt;js: true&lt;/code&gt; in your scenario, context, or even feature block.&lt;/p&gt;

&lt;p&gt;As a lot of our apps use Angular, it&amp;#39;s easier for me to put the &lt;code&gt;js: true&lt;/code&gt; in the feature block. Here&amp;#39;s what I mean.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;rails_helper&#39;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;feature&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Admin home page&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:devise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;js: &lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# and then continue with your scenario blocks, before blocks, and whatever else&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This gives for cleaner code, since each of your scenarios aren&amp;#39;t littered with &lt;code&gt;@javascript&lt;/code&gt; or tagged with &lt;code&gt;js: true&lt;/code&gt;. This really comes in handy if your front-end app is fairly JS heavy.&lt;/p&gt;

&lt;h4 id=&quot;2-check-response-headers-for-downloads&quot;&gt;2) Check response headers for downloads&lt;/h4&gt;

&lt;p&gt;One of our apps has a download function. I didn&amp;#39;t want to test the downloaded file, but simply that the download actually worked. Instead of creating a file and comparing that against the downloaded copy, I &amp;quot;cheated&amp;quot; a bit and decided to check the response headers. This &lt;a href=&quot;http://stackoverflow.com/a/21623939/2464546&quot;&gt;StackOverflow answer&lt;/a&gt; actually helped me out by pointing me in the direction I needed to go.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;scenario&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;it downloads a file&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;js: &lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;download_path&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;click_on&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Download&#39;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;response_headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;Content-Type&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eq&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;image/jpeg&#39;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, I also had to turn off Javascript by inserting &lt;code&gt;js: false&lt;/code&gt; just for that test since the entire feature had Javascript turned on. This has to do with Capybara using Selenium (our default) to test the Javascript. Instead, having &lt;code&gt;js: false&lt;/code&gt; forced Capybara to use its natural default of RackTest. Note that this makes the particular test go faster since it doesn&amp;#39;t render Javascript.&lt;/p&gt;

&lt;h4 id=&quot;3-responsive-web-design-and-continuous-integration&quot;&gt;3) Responsive web design and continuous integration&lt;/h4&gt;

&lt;p&gt;OK. Story time. For one of our apps, I had an awful time troubleshooting failed tests on our CI environment -- TravisCI. For whatever bizarre reason, certain tests would fail on Travis but never locally. I couldn&amp;#39;t reproduce it at all, but the errors would almost always be about Capybara saying an element couldn&amp;#39;t be found.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Failure&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;click_on&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Download&#39;&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;Capybara&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ElementNotFound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Unable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;link&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;button&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Download&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Annoying. I know. I&amp;#39;m looking straight at the page and saying &amp;quot;but there IS a download button! It&amp;#39;s right there!&amp;quot;&lt;/p&gt;

&lt;p&gt;I tried to add &lt;code&gt;id&lt;/code&gt;s; I tried renaming it; I tried a slew of different things, all using &lt;code&gt;save_and_open_page&lt;/code&gt; to see what the state of the page was. Without fail, &amp;quot;Download&amp;quot; appeared. As I surfed around the web, I had an epiphany. &lt;em&gt;What if the test is being run in a smaller browser window?&lt;/em&gt; I didn&amp;#39;t know what size &amp;quot;smaller&amp;quot; would be. I used Chrome&amp;#39;s mobile-mode to test my theory and BOOM! The buttons I had been looking for had disappeared when the browser was at around 400px. This explained why my tests would fail. Our app uses responsive web design with bootstrap, so css values like &lt;code&gt;hidden-lg&lt;/code&gt; or &lt;code&gt;visible-lg&lt;/code&gt; were being used. Was Travis running my tests at mobile size (e.g. 400-ish pixels)?&lt;/p&gt;

&lt;p&gt;The real question is, how do I get Travis to run my tests at a particular size? After some sleuthing, I found out that by default, Travis runs Firefox at around 1024x768. Ok. I tested whether or not I could see my buttons at 1024. Yup, the hamburglar menu popped up at that width and my &amp;quot;Download&amp;quot; button disappeared. Travis has in their docs that the browser size can be resized to whatever dimensions you want. I added the following &lt;code&gt;before_install&lt;/code&gt; section to my &lt;code&gt;.travis.yml&lt;/code&gt; file, keeping the xvbf start in the &lt;code&gt;before_script&lt;/code&gt;. &lt;em&gt;Note: Travis docs says having &lt;code&gt;xvfb start&lt;/code&gt; doesn&amp;#39;t play well w/ the custom dimensions but I haven&amp;#39;t seen a problem with it yet.&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-yml&quot; data-lang=&quot;yml&quot;&gt;&lt;span class=&quot;s&quot;&gt;before_install&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/sbin/start-stop-daemon&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--start&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--quiet&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--pidfile&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/tmp/custom_xvfb_99.pid&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--make-pidfile&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--background&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--exec&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/usr/bin/Xvfb&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;:99&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-ac&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-screen&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;1280x1024x16&quot;&lt;/span&gt;


&lt;span class=&quot;s&quot;&gt;before_script&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;export DISPLAY=:99.0&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;sh -e /etc/init.d/xvfb start&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Tests were rerun, but they still failed. WTF, mate? Was my setting not being accepted? I surfed the web for something. Anything. I even &lt;a href=&quot;http://stackoverflow.com/questions/38214547/travis-ci-not-recognizing-large-resolution-on-xvfb-causing-feature-specs-to-fail&quot;&gt;posted on StackOverflow&lt;/a&gt;. Finally after about a week, I found the following article: &lt;a href=&quot;https://discuss.circleci.com/t/capybara-driver-rack-test/407&quot;&gt;A circleCI conversation about Capybara testing&lt;/a&gt; which included an interesting article called &lt;a href=&quot;http://blaulabs.de/2011/11/22/acceptance-testing-with-responsive-layouts/&quot;&gt;Testing responsive layouts&lt;/a&gt;. &lt;em&gt;Voila!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The two articles gave me all the information I needed to proceed. Selenium needed a browser window size setting, so I simply gave it one.  In my &lt;code&gt;spec/spec_helper.rb&lt;/code&gt; I added the following:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set_selenium_window_size_to_large&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;browser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;manage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resize_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1280&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1280&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set_selenium_window_size_to_small&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;browser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;manage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resize_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;600&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;600&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now I could simply call &lt;code&gt;set_selenium_window_size_to_large&lt;/code&gt; (or small) from within my feature tests.&lt;/p&gt;

&lt;p&gt;And Travis? Travis was super happy to give me some green builds after that. &lt;img class=&#39;emoji&#39; title=&#39;grinning&#39; alt=&#39;grinning&#39; src=&#39;https://github.global.ssl.fastly.net/images/icons/emoji/grinning.png&#39; height=&#39;20&#39; width=&#39;20&#39; align=&#39;absmiddle&#39; &gt;  &lt;/p&gt;

&lt;h4 id=&quot;4-refreshing-the-page&quot;&gt;4) Refreshing the page&lt;/h4&gt;

&lt;p&gt;With the use of Angular, I&amp;#39;ve noticed some finickiness with some of my other tests. Once I solved the responsive-layout problem above, I encountered another issue of some tests failing, both locally and on Travis.&lt;/p&gt;

&lt;p&gt;In this particular instance, it was another &lt;code&gt;Capybara::ElementNotFound&lt;/code&gt; error, but not associated with the browser resolution problem above. The error was triggered only on certain instances. An example test that failed:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;scenario&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;editing an answer&#39;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;click_on&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Edit&#39;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;fill_in&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Answer&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;with: &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;something else&#39;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;click_on&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Save&#39;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;expect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;have_content&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;something else&#39;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The page uses Angular to display a modal/overlay that allows you to edit the answer, then saves that upon close. You could then click on &amp;#39;Edit&amp;#39; to have the overlay pop up again to confirm the change. Unfortunately the tests did not like this. I suspect it was because the answer&amp;#39;s edit page was in a tabbed view. I had to actually force the page to refresh in order to get the page overlay to display once more. So how do I refresh the page without actually saying &lt;code&gt;visit such_and_such_path&lt;/code&gt;? Selenium had the answer.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;driver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;browser&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;navigate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;refresh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This does mean that I have to add in a couple more extra clicks, but it made the headache go away.&lt;/p&gt;

&lt;h4 id=&quot;5-hunting-down-flaky-tests&quot;&gt;5) Hunting down flaky tests&lt;/h4&gt;

&lt;p&gt;A couple of apps were failing their tests periodically: flaky tests. It seemed random. Underneath it all, there were 2 issues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First one.&lt;/strong&gt; Angular was too fast. During the scenario build, the transaction to create data wasn&amp;#39;t finished when Angular asked for that data. This caused Angular to get back an empty data set causing the test to fail. No good. A quick, &lt;em&gt;but brittle&lt;/em&gt;, fix to this was to insert a &lt;code&gt;sleep 1&lt;/code&gt; in the test. After speaking to a senior dev on the team, he sold me on the fact that having to rely on &lt;code&gt;sleep&lt;/code&gt; could mean that there is actually something wrong with the code. For example, the db transaction could be taking too long because it&amp;#39;s trying to do multiple things at once.  If you have the time to investigate further, that would be ideal. Unfortunately there were not enough client hours to justify spending it to solve this issue so &lt;code&gt;sleep 1&lt;/code&gt; it was.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second one.&lt;/strong&gt; This one is a big mindboggling for me, but probably because I haven&amp;#39;t figured out why it happened in the first place. We originally were running our cukes in headless mode (aka, no firefox browser popping up). It&amp;#39;s faster and uses PhantomJS. However, everyone&amp;#39;s environment seemed to react differently. Some folks had perfectly passing tests while others didn&amp;#39;t. The environments should have been the same but were not. Worse was that the Gemfile.locks were all the same so it wasn&amp;#39;t a versioning issue either. I dared to actually try to build a Docker dev environment for everyone to use. At least with that, everyone&amp;#39;s environment would be the same. While setting this up, my coworker and I couldn&amp;#39;t get PhantomJS to load or run properly with Docker, so we opted to use Selenium instead. On a whim, we ran our tests with Selenium. Everything passed! And despite my disdain for Docker, thank you! Because had we not gone that route and encountered obstacles, we wouldn&amp;#39;t have switched to Selenium and fixed our build.&lt;/p&gt;

&lt;p&gt;Ultimately, these might be common tips and tricks for feature tests, but I hope this helps anyone who is rather rusty or new to the game.  Of course as I write this, I&amp;#39;m wondering if xvfb&amp;#39;s screen size change is for &amp;quot;monitors&amp;quot; vs &amp;quot;browsers&amp;quot;......doh! &lt;img class=&#39;emoji&#39; title=&#39;sweat_smile&#39; alt=&#39;sweat_smile&#39; src=&#39;https://github.global.ssl.fastly.net/images/icons/emoji/sweat_smile.png&#39; height=&#39;20&#39; width=&#39;20&#39; align=&#39;absmiddle&#39; &gt;&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +5&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Wed, 24 Aug 2016 15:38:56 -0700</pubDate>
        <link>http://risaonrails.com/testing-testing-1-2-3/</link>
        <guid isPermaLink="true">http://risaonrails.com/testing-testing-1-2-3/</guid>
        
        <category>testing</category>
        
        <category>capybara</category>
        
        <category>rspec</category>
        
        <category>rails</category>
        
        <category>tips</category>
        
        
        <category>rails</category>
        
        <category>testing</category>
        
        <category>tips</category>
        
      </item>
    
      <item>
        <title>Thoughts on Atom, the text editor</title>
        <description>&lt;p&gt;I want to take the time to give my opinion on Atom, the text editor. As a developer, I can choose between a plethora of options for text editors, including but not limited to vim, emacs, Sublime, and Atom. As a developer that prefers GUI and using a mouse, I&amp;#39;m going to casually ignore any terminal(ly ill) related editors. Why? I think this sums it up pretty nicely:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/iamdevloper/status/435555976687923200&quot;&gt;&lt;img src=&quot;/assets/iamdevloper.png&quot; alt=&quot;IamDevloper&amp;#39;s tweet about vim&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Probably much like every Rails developer that taught themselves, I learned Rails using Sublime. And, quite frankly, Sublime is great! It has a ton of functionality, packages to add more functionality, and a fairly decent default colour scheme. But, teaching yourself how to learn how to install new packages for whatever new functionality was a pain in the butt.&lt;/p&gt;

&lt;p&gt;Then came Atom. At first, I thought, &amp;quot;Not &lt;em&gt;another&lt;/em&gt; editor! What makes it so much better than Sublime??&amp;quot; and thought nothing of it. I continued to use Sublime. But then I started to write a lot in Markdown. In particular, Github markdown because of support docs.&lt;/p&gt;

&lt;p&gt;A colleague of mine mentioned an app that displayed a file in markdown, some app that started with a big teal M. I have no idea what it was, but having to use another app to view Markdown format was a pain.&lt;/p&gt;

&lt;p&gt;Another colleague suggested Atom. Atom had this thing called &amp;quot;Markdown preview&amp;quot;, done with a keyboard shortcut of &lt;code&gt;cntl-shift-m&lt;/code&gt;. Boom! To my left was my .md file I was working on and to my right was the preview version. Fantastic!&lt;/p&gt;

&lt;p&gt;But, after my stint at that particular job was over, I stopped using Atom and went back to Sublime. It wasn&amp;#39;t until about a month later when that very same colleague mentioned in a tweet that Atom had gotten to be considerably better than Sublime and that he himself completely switched. I decided to give Atom another go, mostly after Yet Another Popup(tm) happened on Sublime. (Yeah I didn&amp;#39;t want to shell out $70 for it, mostly because I don&amp;#39;t use all those packages they have.)&lt;/p&gt;

&lt;p&gt;I haven&amp;#39;t looked back. Admittedly I haven&amp;#39;t really added extra functionality or anything; I&amp;#39;ve added a few extra packages (such as particular syntax highlighting) and themes but outside of that, no major changes.  &lt;/p&gt;

&lt;p&gt;Here are some of Atom&amp;#39;s functionalities that I enjoy fully. It&amp;#39;s the little things!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I can change the theme and syntax highlighting super quickly and easily.&lt;/li&gt;
&lt;li&gt;It tells me which branch I&amp;#39;m currently on, how many package/theme updates are available, and if there&amp;#39;s anything wrong with the editor (like something was deprecated) at the very bottom.&lt;/li&gt;
&lt;li&gt;It changes the colours of my files in my document tree based on whether it&amp;#39;s new or modified (this might be because of a theme, but I loooove it!)&lt;/li&gt;
&lt;li&gt;Atom tells me how much added time a plugin or theme will cause Atom&amp;#39;s startup time to slow down.&lt;/li&gt;
&lt;li&gt;Adding new themes/plugins/functionality is super simple.&lt;/li&gt;
&lt;li&gt;If writing in markdown, typing &lt;code&gt;code&lt;/code&gt; then pressing tab autocompletes it to the ` triple tick marks, then you can add ruby to the end of that to get ruby syntax highlighting in your .md&amp;#39;s.&lt;/li&gt;
&lt;li&gt;Typing &lt;code&gt;pry&lt;/code&gt; then hitting tab autocompletes it to &lt;code&gt;binding.pry&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Markdown preview seriously needs a lot more love.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a heads up, I currently use One Dark / Seti as my theme/syntax highlight.&lt;/p&gt;

&lt;p&gt;Note though, that the functionalities I was used to in Sublime still worked in Atom, and that was a huge relief and made the transition much smoother.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.sublimetext.com/&quot;&gt;Sublime&lt;/a&gt; is created by crazy Aussies. &lt;a href=&quot;https://atom.io/&quot;&gt;Atom&lt;/a&gt; is created by Github (aka, crazy SFers).&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +0. It&amp;#39;s an opinionated piece, what did you expect?&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Thu, 10 Dec 2015 07:25:51 -0800</pubDate>
        <link>http://risaonrails.com/thoughts-on-Atom/</link>
        <guid isPermaLink="true">http://risaonrails.com/thoughts-on-Atom/</guid>
        
        <category>editors</category>
        
        <category>atom</category>
        
        <category>sublime</category>
        
        
        <category>rails</category>
        
        <category>developer</category>
        
        <category>opinion</category>
        
      </item>
    
      <item>
        <title>HIPAA compliance</title>
        <description>&lt;p&gt;Readers, it&amp;#39;s been a while. So much has happened. Lately I have gotten to understand HIPAA law a bit better as it pertains to servers and hosting apps that have or use medical data. Let&amp;#39;s start first with some definitions. HIPAA stands for Health Insurance Portability and Accountability Act, which was passed by Congress back in &amp;#39;96.  It is enforced by the Dept of Health and Human Services (HHS).  For the purposes of app hosting, HIPAA is important for the electronic side of things, namely, protected health information (PHI) and personal identifiable information (PII).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TLDR: You need to be HIPAA compliant and protect personal information if you&amp;#39;re gonna have a medical app or host a medical app.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order for a hosting provider to be compliant, it needs to meet certain requirements. These requirements are in the form of physical, technical, and administrative safeguards.  Let&amp;#39;s talk about each one.&lt;/p&gt;

&lt;h3 id=&quot;physical-safeguards&quot;&gt;Physical Safeguards&lt;/h3&gt;

&lt;p&gt;Servers that host PHI/PII need to have certain physical safeguards, such as facility access control, workstation access, etc. The good thing about this is that a lot of infrastructure as a service companies like Digital Ocean or Linode are already HIPAA compliant. The physical datacenters which host our lovely virtual servers have a lot of safety requirements by default, such as key card entry plus biometric login, etc.  Datacenters can also be certified at higher levels of security, such as SOC I and II certifications (Service Organization Control).&lt;/p&gt;

&lt;h3 id=&quot;technical-safeguards&quot;&gt;Technical Safeguards&lt;/h3&gt;

&lt;p&gt;Technical safeguards are things like access control, authentication systems, and secure transmissions. Thinking about technical safeguards is easiest from the app perspective. You will need unique logins, your database should be encrypted (easier to be encrypted before going into the db), and a record of who did what when.  From a server perspective, it&amp;#39;s ensuring that access to the server is limited and recorded and that the server is properly monitored for any breaches. You might also implement automatic logoff (both app and server).  This is the part where you implement services on the server like Fail2ban and have a bunch of firewall rules with all the ports turned off except the ones you absolutely need.&lt;/p&gt;

&lt;h3 id=&quot;administrative-safeguards&quot;&gt;Administrative Safeguards&lt;/h3&gt;

&lt;p&gt;Administrative safeguards isn&amp;#39;t just about writing up documentation to explain what the SOP is for when there&amp;#39;s a security breach, but it&amp;#39;s also for ensuring all personnel are properly trained to handle PHI/PII. You&amp;#39;ll have to do risk analysis and risk assessments (generally once a year) on your systems, which includes but isn&amp;#39;t limited to testing out disaster recovery plans, like server and data restoration in the event of a catastrophe. Reviewing your own policies and procedures is also required as a safeguard, such as reviewing how things are done and assessing whether or not the process in place is still effective or needs to be adjusted. Think of this as adjusting your Fail2ban settings to make sure it&amp;#39;s capturing and banning failed login attemps properly.  Finally, having a contingency plan is also part of the administrative safeguards. It makes sense to have backups of your database, but making sure you have a backup of your server is essential. It also helps to have a backup of a backup as well (the external server hosting the backup databases).  Finally, as a hosting company or as a medical app, you will most likely need a signed Business Associate Agreement between you and your client.&lt;/p&gt;

&lt;p&gt;Overall, HIPAA compliance seems like a daunting thing, but truthfully, the law is there to help us. We need to be smart about how we keep things running as a hosting company. And as long as if we keep our ducks in a row, being HIPAA compliant is actually really easy to do.&lt;/p&gt;

&lt;p&gt;The particular section we, as techies, need to be aware of with HIPAA regularion is CFR 164.308, 310, 312, 314, and 316.&lt;/p&gt;

&lt;p&gt;For more information, that section, along with the rest of the regulation can be found &lt;a href=&quot;http://www.hhs.gov/ocr/privacy/hipaa/administrative/combined/hipaa-simplification-201303.pdf&quot;&gt;here&lt;/a&gt;, in particular pages 62-69.&lt;/p&gt;

&lt;p&gt;Please note, I&amp;#39;ve over-simplified this. It&amp;#39;s good to actually read thru the regulation. It&amp;#39;s surprisingly very easy to understand. And when you&amp;#39;re dealing with engineering manuals, this is most definitely a breath of fresh air! :)&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +5&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Wed, 30 Sep 2015 11:26:17 -0700</pubDate>
        <link>http://risaonrails.com/hipaa-compliance/</link>
        <guid isPermaLink="true">http://risaonrails.com/hipaa-compliance/</guid>
        
        <category>HIPAA</category>
        
        <category>servers</category>
        
        
        <category>rails</category>
        
        <category>devops</category>
        
        <category>HIPAA</category>
        
      </item>
    
      <item>
        <title>Joomla migrations</title>
        <description>&lt;p&gt;Recently I&amp;#39;d been tasked to migrate a Joomla CMS from one hosting provider to another. As a Rails developer, I cried. A lot. Seriously, what the heck is &lt;a href=&quot;http://www.joomla.org/&quot;&gt;Joomla&lt;/a&gt;??&lt;/p&gt;

&lt;p&gt;Joomla is a CMS (content management system), much like WordPress. Apparently it&amp;#39;s the 2nd most popular CMS out there? (I thought Drupal was? What do I know, I barely know what CMS even is.)&lt;/p&gt;

&lt;p&gt;Anyway, first of all, I did the running away on your behalf. &amp;quot;Countless&amp;quot; hours pouring over tutorials and figuring out things. But...It really isn&amp;#39;t that hard to migrate your Joomla stuff from one server to another.&lt;/p&gt;

&lt;p&gt;Here is the general setup of the old server and new server. And the trials and tribulations I went thru. This was scary for me because it uses MySQL and PHP, 2 things I am rather unfamiliar with, but looking back, it wasn&amp;#39;t so bad at all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Old Server&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CentOS 6.3&lt;/li&gt;
&lt;li&gt;MySQL 5.5&lt;/li&gt;
&lt;li&gt;httpd (and not Apache2)&lt;/li&gt;
&lt;li&gt;Joomla version ??? (more on this later)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;New Server&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ubuntu 14.04 LTS&lt;/li&gt;
&lt;li&gt;MySQL 5.6&lt;/li&gt;
&lt;li&gt;Apache2 (2.4.x)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-summary-of-steps&quot;&gt;The summary of steps&lt;/h2&gt;

&lt;p&gt;Here is what is going to happen and why.&lt;/p&gt;

&lt;p&gt;Joomla pages are in their own folders. You can pretty much transfer the folders as is, with their known directory structure, from one server to the other. And then you do a database backup and shove it into the new server. There&amp;#39;s some weird finicky settings that need to be changed, but that&amp;#39;s all small stuff. So since I like bulletted lists...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transfer Joomla pages from old server to new server&lt;/li&gt;
&lt;li&gt;Export and then import database dump&lt;/li&gt;
&lt;li&gt;Change about certain settings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The task seemed a lot larger at the beginning when I first was handed it. But at the end of the day, it wasn&amp;#39;t so bad at all, only time consuming.&lt;/p&gt;

&lt;h4 id=&quot;transferring-of-joomla-files&quot;&gt;Transferring of Joomla files&lt;/h4&gt;

&lt;p&gt;The particular Joomla installation on the old server was at &lt;code&gt;/var/www/vhosts/&lt;/code&gt; with each site having their own directory. A lot of the tutorials I found had me FTPing the pages down to my local machine and then reFTP&amp;#39;ing it up. But...why put me in as middleman? That&amp;#39;s what &lt;code&gt;scp&lt;/code&gt; is for!  &lt;/p&gt;

&lt;p&gt;Save yourself a bit of time by adding in the &lt;code&gt;~/.ssh/id_rsa.pub&lt;/code&gt; key to your &lt;code&gt;~/.ssh/authorized_keys&lt;/code&gt; file so you don&amp;#39;t have to put in root password.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# on old server as root&lt;/span&gt;
cat ~/.ssh/id_rsa.pub
&lt;span class=&quot;c&quot;&gt;# copy this&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# on new server&lt;/span&gt;
sudo nano ~/.ssh/authorized_keys
&lt;span class=&quot;c&quot;&gt;# paste the id_rsa.pub key into here&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# save and exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Time to copy the files.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# on the new server&lt;/span&gt;
sudo mkdir /var/www/vhosts
&lt;span class=&quot;c&quot;&gt;# on the old server&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /var/www/vhosts
scp -r . root@123.123.123.123:/var/www/vhosts/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Depending on how many Joomla sites you&amp;#39;re moving and depending on your servers&amp;#39; connection speeds, this can take a very short time or a very very long time. I moved 5 sites and that took roughly 2 hours, which is surprising since it was going from one server to another and bypassing me. There was about 5 GB of data to be transferred though.&lt;/p&gt;

&lt;p&gt;While we&amp;#39;re at it, since the CentOS setup was using httpd (Apache), I got lazy and decided to transfer over the site.conf files for Apache. Why reinvent the wheel when you can just copy, after all?&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# on the old server&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /etc/httpd/vhost.d/
scp -r . root@123.123.123.123:/etc/apache2/sites-available/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At this point I feel like I should re-name the article &amp;quot;how to use SCP&amp;quot; hah...&lt;/p&gt;

&lt;h4 id=&quot;export-and-import-mysql-database&quot;&gt;Export and Import MySQL database&lt;/h4&gt;

&lt;p&gt;I&amp;#39;m so much more familiar with Postgres that when it came to using MySQL, I freaked out a bit, admittedly. Being unfamiliar with how things were setup, I took the easy route and created a dump directly on the older server then &lt;code&gt;scp&lt;/code&gt; the dump to the new one. There were no show-stopping differences between 5.5 and 5.6 that I saw.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# on old server&lt;/span&gt;
mysqldump --all-databases &amp;gt; all_db.sql
scp -r all_db.sql root@123.123.123.123:/home/deploy/
&lt;span class=&quot;c&quot;&gt;# put it in any directory you want, really&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# on new server&lt;/span&gt;
mysql -u root -p &amp;lt; all_db.sql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Fun fact, if you didn&amp;#39;t know this about mysql.... If you set up mysql root with a password and don&amp;#39;t have it locked down, any user can log into it, but they won&amp;#39;t see your databases at all. e.g. if you hop into mysql as user &lt;code&gt;deploy&lt;/code&gt; and show databases, nothing shows up. But if you log into mysql as &lt;code&gt;root&lt;/code&gt; then you see your databases. This really threw me for a loop.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# as user deploy&lt;/span&gt;
mysql
&lt;span class=&quot;c&quot;&gt;# then in mysql, list the databases&lt;/span&gt;
show databases;
&lt;span class=&quot;c&quot;&gt;# you shouldn&#39;t see anything&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# now log in as root (with password)&lt;/span&gt;
mysql -u root -p
show databases;
&lt;span class=&quot;c&quot;&gt;# boom! all your databases are there&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But, I digress.&lt;/p&gt;

&lt;h4 id=&quot;changing-settings-joomla&quot;&gt;Changing Settings - Joomla&lt;/h4&gt;

&lt;p&gt;Now that the files you need have been moved over, it&amp;#39;s time to adjust the settings. Again, I had about 5 Joomla sites, so I picked one to test things on.&lt;/p&gt;

&lt;p&gt;First, let&amp;#39;s focus on the Joomla settings.&lt;/p&gt;

&lt;p&gt;At this point, I realize I should have imported the data into a different mysql user account vs the root one, but, what&amp;#39;s done is done. Moving on.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /var/www/vhosts/site.com/public_html
sudo nano configuration.php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Scroll down to find the database section near the top and change the settings to reflect which user you put the data under. The dump file should have created the databases for you and should be the same.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;public &lt;span class=&quot;nv&quot;&gt;$dbtype&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;mysql&#39;&lt;/span&gt;;
public &lt;span class=&quot;nv&quot;&gt;$host&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;localhost&#39;&lt;/span&gt;;
public &lt;span class=&quot;nv&quot;&gt;$user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;root&#39;&lt;/span&gt;;                       &lt;span class=&quot;c&quot;&gt;# possibly change this&lt;/span&gt;
public &lt;span class=&quot;nv&quot;&gt;$password&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;your_awesome_password&#39;&lt;/span&gt;;  &lt;span class=&quot;c&quot;&gt;# change this&lt;/span&gt;
public &lt;span class=&quot;nv&quot;&gt;$db&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;app_database&#39;&lt;/span&gt;;                 &lt;span class=&quot;c&quot;&gt;# change this&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Save and exit. This should be all you need to do for your Joomla settings.&lt;/p&gt;

&lt;h4 id=&quot;changing-settings-apache&quot;&gt;Changing Settings - Apache&lt;/h4&gt;

&lt;p&gt;There were some small differences between the httpd and Apache2 server settings so things needed to be updated/adjusted.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /etc/apache2/sites-available/
sudo nano site.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Adjust the ServerName and Directory block options. For instance, the + - symbols are no longer used in the Options, so I removed them accordingly. I also adjusted where my logs went. (I don&amp;#39;t like them in &lt;code&gt;/var/log/apache2/&lt;/code&gt; and prefer them in &lt;code&gt;/usr/share/apache2/logs/&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;I changed the ServerName from the domain to my IP address to make sure the site worked. I recommend this as well for you.&lt;/p&gt;

&lt;p&gt;If you have SSL certs that are needed, make sure to also transfer them over and adjust the apache settings.&lt;/p&gt;

&lt;p&gt;Save and exit. Enable that site and reload Apache2.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;sudo a2ensite site.conf
sudo service apache2 restart &lt;span class=&quot;c&quot;&gt;# or reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If there are any errors and Apache won&amp;#39;t restart, correct them.&lt;/p&gt;

&lt;p&gt;Go to &lt;a href=&quot;http://123.123.123.123&quot;&gt;http://123.123.123.123&lt;/a&gt; (whatever your IP is) and confirm that your site loads properly and things are looking super fly.  Celebrate and then complete the crossover by switching the IPs for the domain.&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +5&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Wed, 29 Jul 2015 09:25:52 -0700</pubDate>
        <link>http://risaonrails.com/joomla-migrations</link>
        <guid isPermaLink="true">http://risaonrails.com/joomla-migrations</guid>
        
        <category>joomla</category>
        
        <category>migrations</category>
        
        
        <category>joomla</category>
        
        <category>devops</category>
        
      </item>
    
      <item>
        <title>Multiple Deploy Keys on a Server</title>
        <description>&lt;p&gt;Life in DevOps land has been pretty exciting for me. I&amp;#39;ve learned to create a tiered service (an app server + worker server), deal with RVM + upstart, finagle monit into submission, and most recently work with multiple SSH keys on one server.&lt;/p&gt;

&lt;h2 id=&quot;scenario&quot;&gt;Scenario&lt;/h2&gt;

&lt;p&gt;You&amp;#39;ve got a server with 2-3 apps on it, and you&amp;#39;d like to use deploy keys for each app, but how do we keep them separate and how will the system know which to use? There&amp;#39;s a thing called a config file that can be used for our SSH keys. It&amp;#39;s kind of awesome.&lt;/p&gt;

&lt;p&gt;I&amp;#39;ll step through how to set it up and the like in this post.&lt;/p&gt;

&lt;h3 id=&quot;repo-examples&quot;&gt;Repo examples&lt;/h3&gt;

&lt;p&gt;Let&amp;#39;s pretend we have 2 repos, and both are hosted on Github:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;App 1 = Surprise&lt;/li&gt;
&lt;li&gt;App 2 = Badger&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;server&quot;&gt;Server&lt;/h3&gt;

&lt;p&gt;The pretend server is going to be Ubuntu 14.04+. I&amp;#39;ll assume you&amp;#39;ve created a &lt;code&gt;~/.ssh&lt;/code&gt; directory and chmod&amp;#39;d it appropriately. On DigitalOcean, the directions for doing that &lt;a href=&quot;https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04&quot;&gt;can be found here&lt;/a&gt; about midway down. We&amp;#39;ll skip their directions about creating keys.&lt;/p&gt;

&lt;h3 id=&quot;creating-ssh-keys&quot;&gt;Creating SSH Keys&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://help.github.com/articles/generating-ssh-keys/#platform-linux&quot;&gt;Github&amp;#39;s SSH key article&lt;/a&gt; is probably the best thing that&amp;#39;s happened on the internet for people like me since sliced bread. (But not since buttered and toasted sliced bread...)&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s the abbreviated version of that article for our purposes, using our examples. Because examples are great! I&amp;#39;ll use Surprise (app 1) as the example since the steps are repeated for Badger (app 2).&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;ssh-keygen -t rsa -b 4096 -C &lt;span class=&quot;s2&quot;&gt;&quot;devops@surprise&quot;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# really, we don&#39;t need to put an email. this just lets us know&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# that it&#39;s the surprise repo we&#39;re dealing with.&lt;/span&gt;
Enter file &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;which to save the key &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;/Users/you/.ssh/id_rsa&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: surprise_rsa
&lt;span class=&quot;c&quot;&gt;# here put in something like surprise_rsa then hit enter&lt;/span&gt;
Enter passphrase &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;empty &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;no passphrase&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: &lt;span class=&quot;c&quot;&gt;# optional&lt;/span&gt;
Enter same passphrase again: &lt;span class=&quot;c&quot;&gt;# optional&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# these are optional but highly recommended&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You should get some funky design/image after this. Repeat these for badger.&lt;/p&gt;

&lt;p&gt;Once both are set up, we can proceed to the next step, adding the keys to the ssh-agent. You want to run the eval statement once. This tripped me up a couple of times.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;ssh-agent -s&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# this gets you a pid. Agent pid ####&lt;/span&gt;
ssh-add ~/.ssh/surprise_rsa
ssh-add ~/.ssh/badger_rsa
&lt;span class=&quot;c&quot;&gt;# you will probably have to add your passphrases in here. do it.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Sanity check yourself by confirming that both SSH keys have been added.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;ssh-add -l
&lt;span class=&quot;c&quot;&gt;# this should display both apps&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;add-keys-to-repo&quot;&gt;Add Keys to Repo&lt;/h3&gt;

&lt;p&gt;Deploy keys. The only way I could really understand their functionality was that they are the server&amp;#39;s ssh keys that get put into a particular repo, so that particular key is the only one that&amp;#39;s allowed to read/write to that repo only. This prevents the key from accessing other repos. Usually we create ssh keys and attach them to our account, so we can then grab ANY repo we&amp;#39;re associated with. This can be dangerous, and it can cause problems if the associated member leaves the organization. (i.e., if I leave the company, the keys on the servers that I had attached to my account will no longer work)&lt;/p&gt;

&lt;p&gt;First, we&amp;#39;ll need to &lt;code&gt;cat&lt;/code&gt; the public keys. Let&amp;#39;s start with Surprise.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;cat ~/.ssh/surprise_rsa.pub
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Copy the &lt;code&gt;surprise_rsa.pub&lt;/code&gt; key, from &lt;code&gt;ssh-rsa&lt;/code&gt; all the way to &lt;code&gt;devops@surprise&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, go to Surprise&amp;#39;s repo in Github &amp;gt; Settings (right panel) &amp;gt; Deploy keys. The path will look like:&lt;br&gt;
&lt;a href=&quot;https://github.com/your_github/Surprise/settings/keys&quot;&gt;https://github.com/your_github/Surprise/settings/keys&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on Add Deploy Key, name the key something you&amp;#39;d understand. &amp;quot;My awesome server&amp;#39;s deploy key&amp;quot; is totally OK. You&amp;#39;re the captain of this ship, after all. Paste in your key. Save.&lt;/p&gt;

&lt;p&gt;Repeat steps for Badger.&lt;/p&gt;

&lt;h3 id=&quot;create-ssh-config&quot;&gt;Create SSH Config&lt;/h3&gt;

&lt;p&gt;Now that we&amp;#39;ve got our 2 deploy keys set up, we need to make sure git knows which key to use and when, when we do our &lt;code&gt;git clones&lt;/code&gt; and &lt;code&gt;git pulls and pushes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create a config file in &lt;code&gt;~/.ssh/&lt;/code&gt; called config.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;nano ~/.ssh/config
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The file will be in this format:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# surprise github&lt;/span&gt;
Host surprise
  Hostname github.com
  User git
  IdentityFile ~/.ssh/surprise_rsa

&lt;span class=&quot;c&quot;&gt;# badger github&lt;/span&gt;
Host badger
  Hostname github.com
  User git
  IdentityFile ~/.ssh/badger_rsa
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;#39;s break this down.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Host&lt;/strong&gt; can be whatever you want. It can be &lt;code&gt;asdfjkl;&lt;/code&gt; for all we care.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hostname&lt;/strong&gt; is github.com as that&amp;#39;s where our repo is at.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;User&lt;/strong&gt; is git, because we are SSH&amp;#39;ing to github. (You know, &lt;a href=&quot;mailto:git@github.com&quot;&gt;git@github.com&lt;/a&gt;...)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IdentityFile&lt;/strong&gt; is the private key for that repo. Remember, we associated it&amp;#39;s public key to the repo, so we need the matching private key for the connection to work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Save and exit!&lt;/p&gt;

&lt;h3 id=&quot;setting-up-the-repos&quot;&gt;Setting up the repos&lt;/h3&gt;

&lt;p&gt;Assuming we haven&amp;#39;t pulled down the repos to the server, we&amp;#39;ll do that now. I know you&amp;#39;re inclined to copy paste the SSH URL from Github: &lt;code&gt;git@github.com:your_github/surprise.git&lt;/code&gt; &lt;em&gt;BUT DON&amp;#39;T DO IT&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DON&amp;#39;T YOU DARE!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, you will do this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;git clone git@surprise:your_github/surprise.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Do you see it? &lt;code&gt;github.com&lt;/code&gt; was replaced with &lt;code&gt;surprise&lt;/code&gt;. It forces git to look at the SSH config file and be like &amp;quot;What is this host called surprise? Oh, we&amp;#39;re going to use this identity file and connect to this repo on github! Ok got it!&amp;quot;&lt;/p&gt;

&lt;p&gt;At that point, your origin will be pointing to &lt;code&gt;surprise&lt;/code&gt; and you&amp;#39;ll be able to do &lt;code&gt;git pull origin master&lt;/code&gt; or &lt;code&gt;git push origin master&lt;/code&gt; and life will be deliciously fabulous.&lt;/p&gt;

&lt;p&gt;Repeat this for badger, of course.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: If you&amp;#39;ve already cloned, you can change your origin to point to the above URL instead and that&amp;#39;ll solve your issues.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Serious thanks to &lt;a href=&quot;http://stackoverflow.com/a/14703929/2464546&quot;&gt;this StackOverflow answer&lt;/a&gt; that cleared things up for me.&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +10&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Wed, 20 May 2015 05:40:53 -0700</pubDate>
        <link>http://risaonrails.com/multiple-deploy-keys-on-a-server/</link>
        <guid isPermaLink="true">http://risaonrails.com/multiple-deploy-keys-on-a-server/</guid>
        
        <category>devops</category>
        
        <category>guide</category>
        
        
        <category>devops</category>
        
      </item>
    
      <item>
        <title>Mounting excitement</title>
        <description>&lt;p&gt;I used to be risk-averse. But now? Totally not. In fact, I may be ever so slightly an adrenaline junkie. And I love every bit of it.  &lt;/p&gt;

&lt;p&gt;Maybe a few years ago I found a pair of &lt;em&gt;&amp;quot;see the world thru the eyes of a 3 yr old&amp;quot;&lt;/em&gt; glasses, but ever since then, life is pretty awesome.&lt;/p&gt;

&lt;h3 id=&quot;so-why-am-i-excited&quot;&gt;So why am I excited?!&lt;/h3&gt;

&lt;p&gt;Because we&amp;#39;re going on an &lt;em&gt;ADVENTURE&lt;/em&gt;, Charlie! Yeah, Charlie, an adventure!&lt;/p&gt;

&lt;p&gt;No really though. I love adventures. Everything is an adventure! It&amp;#39;s totally not limited to going to new places. It&amp;#39;s everything. Heck, every day is an adventure!&lt;/p&gt;

&lt;p&gt;So really, why am I this excited?&lt;/p&gt;

&lt;p&gt;I&amp;#39;m going to Vancouver, Canada for a month. Ok ok, it may not seem like such a big deal, but here&amp;#39;s where it gets more interesting.&lt;br&gt;
1. I have never been to Vancouver.
1. Women&amp;#39;s World Cup soccer games are there!
1. I&amp;#39;m going to at least 4 Cup games!
1. I can work remotely anywhere I want!
1. Did I mention Women&amp;#39;s World Cup soccer?
1. I&amp;#39;m going to get me a Finals ticket, darn it.&lt;/p&gt;

&lt;p&gt;So how did this all come to be? What made me decide to do something kinda crazy (for me)? Simple answer: The hairs on the back of my neck are standing up. I&amp;#39;m scared to go! And that means that I&amp;#39;ve got to do this. I can do this, and I&amp;#39;m going to have a blast!&lt;/p&gt;

&lt;h3 id=&quot;no-really&quot;&gt;No really.&lt;/h3&gt;

&lt;p&gt;In reality, I was talking about soccer in Slack with my team and realized that this year was Women&amp;#39;s World Cup. I love watching soccer, and women&amp;#39;s soccer is by far more entertaining than men&amp;#39;s. Why? They don&amp;#39;t flop about like wet fish on the field; they are a lot more brutal with their tackles; and Abby Wambach is the highest goal scorer (int&amp;#39;l games) on the face of the planet at currently 178 goals. The closest man is Ali Daei at 105, for comparison. Just saying. On top of it, Men&amp;#39;s World Cup is so much more expensive, and I have no desire to go to the next few countries for their Cup games.&lt;/p&gt;

&lt;p&gt;On top of it, it&amp;#39;s in Vancouver, just a short flight away! Because my job is already remote, I can even work from there. So I started calculating things out and decided that going for almost the entire month of June made sense. Financially, not really, but whatever. We&amp;#39;ve only got this life, we might as well enjoy every bit of it!&lt;/p&gt;

&lt;p&gt;It really helps that I&amp;#39;ve no ties to SF. And maybe I&amp;#39;ll end up falling in love with Vancouver and spend more time up there. Anything&amp;#39;s possible.  (I still love you, SF. But y&amp;#39;know. &lt;em&gt;You know why.&lt;/em&gt;)&lt;/p&gt;

&lt;h3 id=&quot;what-about-your-birthday&quot;&gt;What about your birthday?&lt;/h3&gt;

&lt;p&gt;Psh. GTFO. Birthdays ain&amp;#39;t got nuttin&amp;#39; on dis! Birthdays are just another day of disappointment for me -- I say this as my birthday is a few days away, and the disappointment is ever increasing.&lt;/p&gt;

&lt;h3 id=&quot;ahhhh&quot;&gt;AHHHH!&lt;/h3&gt;

&lt;p&gt;Oh my effing gawd. I&amp;#39;ve never been to a soccer match before! So many firsts! I&amp;#39;m going to so many games! AHHHH. And I hear Vancouver is so pretty, and it&amp;#39;s a walkable city! And all the nature trails! It&amp;#39;s totally gay friendly too! You can&amp;#39;t get any better than this right now for me!  Now to experience other new things outside of soccer!&lt;/p&gt;

&lt;p&gt;...Secretly-not-so-secretly, I think that if I fall in love with this city and then some, I may spend more time up there. 2-3 months at a time! Geezus, I love working remotely.&lt;/p&gt;

&lt;p&gt;How does one sleep with this excitement?!?&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +1 for getting to know me better?&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Wed, 29 Apr 2015 16:34:32 -0700</pubDate>
        <link>http://risaonrails.com/mounting-excitement/</link>
        <guid isPermaLink="true">http://risaonrails.com/mounting-excitement/</guid>
        
        <category>offtopic</category>
        
        
        <category>offtopic</category>
        
      </item>
    
      <item>
        <title>Monitoring Nginx&#39;s Passenger</title>
        <description>&lt;p&gt;In the ever learning world I&amp;#39;m in, someone had commented on &lt;a href=&quot;/integrate-monit-with-slack/&quot;&gt;my post&lt;/a&gt; about monitoring Passenger.&lt;/p&gt;

&lt;p&gt;That got me thinking and diving deep into the depths of Google to find a solution that was acceptable for those of using Nging to create the Passenger instances.&lt;/p&gt;

&lt;p&gt;The most common search result for this was to use the gem &lt;a href=&quot;https://github.com/romanbsd/passenger_monit&quot;&gt;passenger-monit&lt;/a&gt;. However it hasn&amp;#39;t been updated in at least 2 years. I am not that ambitious enough to revive this gem for my own purposes. The biggest drawback for using this gem is that the rack PIDs are hardcoded and if the first one dies, Monit will complain. It isn&amp;#39;t ideal for my purposes since I run multiple apps on one server and I&amp;#39;d like to keep track of them individually.&lt;/p&gt;

&lt;p&gt;HOWEVER~! I have found a way that seems to work.&lt;/p&gt;

&lt;h3 id=&quot;the-solution&quot;&gt;The Solution&lt;/h3&gt;

&lt;p&gt;There&amp;#39;s a method called &lt;code&gt;matching&lt;/code&gt; that monit allows. Essentially monit does a &lt;code&gt;ps aux | grep &amp;#39;whatever thing&amp;#39;&lt;/code&gt; to check for a process. &lt;a href=&quot;http://serverfault.com/questions/523226/monit-daemonize-non-daemon-process&quot;&gt;Holy balls, I love you person on ServerFault who answered this&lt;/a&gt; and I wish I had the reputation to upvote the crap out of that answer. But instead, mad props to you. &lt;/p&gt;

&lt;p&gt;Anyway, after reading that, everything became clear.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s what I ended up doing. &lt;/p&gt;

&lt;h4 id=&quot;the-example-setup&quot;&gt;The example setup&lt;/h4&gt;

&lt;p&gt;Assume the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nginx + passenger (Passenger is spawned via Nginx)

&lt;ul&gt;
&lt;li&gt;there is no passenger PID file that exists&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Rails app named &amp;#39;Surprise&amp;#39; 

&lt;ul&gt;
&lt;li&gt;nginx configured to point to &lt;code&gt;/home/rails/Surprise/public&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Deployer user named &amp;#39;rails&amp;#39;&lt;/li&gt;
&lt;li&gt;Monit version 5.6 or 5.8 is running&lt;/li&gt;
&lt;li&gt;Digital Ocean droplet for server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, if you have not hit Surprise at all, you won&amp;#39;t have any passenger instances created. That makes sense. Why spawn a process when it isn&amp;#39;t being used? &lt;/p&gt;

&lt;p&gt;The process is brutally simple, hit the app to spawn a Passenger process, then check &lt;code&gt;top&lt;/code&gt; to see what the name is and create the monit file.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;top -c
&lt;span class=&quot;c&quot;&gt;# hit shift M to sort by memory&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# you should see near the top right&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Passenger RubyApp: /home/rails/surprise/public &lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# copy paste that&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;sudo nano /etc/monit/conf.d/surprise-pass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now in this file, write in the following:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;check process surprise-pass matching &lt;span class=&quot;s1&quot;&gt;&#39;Passenger RubyApp: \/home\/rails\/surprise\/public&#39;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;totalmem &amp;gt; 35% &lt;span class=&quot;k&quot;&gt;then &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; /etc/monit/slack.rb
  &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;totalmem &amp;gt; 60% &lt;span class=&quot;k&quot;&gt;then &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; /etc/monit/touch_surprise_sh  &lt;span class=&quot;c&quot;&gt;# restart app&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;changed pid &lt;span class=&quot;k&quot;&gt;then &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; /etc/monit/slack.rb              &lt;span class=&quot;c&quot;&gt;# sanity check&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if &lt;/span&gt;totalcpu &amp;gt; 25% &lt;span class=&quot;k&quot;&gt;then &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt; /etc/monit/slack.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Save and exit. Notice the &lt;code&gt;\&lt;/code&gt; marks to escape the &lt;code&gt;/&lt;/code&gt; marks. Because monit does essentially a &lt;code&gt;ps aux | grep ____&lt;/code&gt;, copy pasting what you find in &lt;code&gt;top -c&lt;/code&gt; is a great way to get the process match.&lt;/p&gt;

&lt;p&gt;A couple of things to note. 1) There is also a new file being executed here. More on this in a bit. 2) We are talking totalmem and totalcpu. Passenger can spin up child processes if it needs it, but I want to look at the total memory consumption of that particular app. Total cpu is not per core but for the whole thing. I also based these off of monit&amp;#39;s web interface information. It&amp;#39;s very handy to pick out the things you want to monitor in particular.&lt;/p&gt;

&lt;p&gt;The new file &lt;code&gt;touch_surprise_sh&lt;/code&gt; is a super simple bash script that restarts Passenger (and not Nginx) via the &lt;code&gt;touch&lt;/code&gt; command.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;sudo nano /etc/monit/touch_surprise_sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the file type the following lines:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;

touch /home/rails/surprise/tmp/restart.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alternatively you can make it more verbose and sudo in as your user (rails) to restart Passenger.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;sudo -u rails -H sh -c &quot;touch /home/rails/surprise/tmp/restart.txt”
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Save and exit. Make it into an executable file.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;sudo chmod +x /etc/monit/touch_surprise_sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Do your standard monit sanity check, then reload monit.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;sudo monit -t       &lt;span class=&quot;c&quot;&gt;# fix any problems, obv&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;sudo monit reload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Confirm on your web interface that surprise-pass is loaded up and displaying all the goodies you like to see. When you restart nginx, Passenger should lose a PID. It&amp;#39;ll alert you on Slack when you hit the app and create a new PID. Right on. &lt;img class=&#39;emoji&#39; title=&#39;fist&#39; alt=&#39;fist&#39; src=&#39;https://github.global.ssl.fastly.net/images/icons/emoji/fist.png&#39; height=&#39;20&#39; width=&#39;20&#39; align=&#39;absmiddle&#39; &gt;&lt;/p&gt;

&lt;p&gt;Congrats! Create a monit conf file for each of your apps and behold your power!&lt;/p&gt;

&lt;p&gt;Special thanks to Grant Trevor who got me to delve into the depths of monit and Google &lt;img class=&#39;emoji&#39; title=&#39;grinning&#39; alt=&#39;grinning&#39; src=&#39;https://github.global.ssl.fastly.net/images/icons/emoji/grinning.png&#39; height=&#39;20&#39; width=&#39;20&#39; align=&#39;absmiddle&#39; &gt;&lt;/p&gt;

&lt;p&gt;&lt;i class=&quot;fa fa-angle-double-up&quot;&gt;&lt;/i&gt; Level up +5&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;Questions? Comments? Hit me up at &lt;a href=&quot;mailto:risaonrails@gmail.com&quot;&gt;risaonrails &lt;i class=&quot;fa fa-paper-plane-o&quot;&gt;&lt;/i&gt;&lt;/a&gt;!&lt;/p&gt;
</description>
        <pubDate>Wed, 15 Apr 2015 14:21:01 -0700</pubDate>
        <link>http://risaonrails.com/monitoring-nginx-and-passenger/</link>
        <guid isPermaLink="true">http://risaonrails.com/monitoring-nginx-and-passenger/</guid>
        
        <category>monit</category>
        
        <category>passenger</category>
        
        
        <category>guide</category>
        
        <category>devops</category>
        
        <category>monit</category>
        
        <category>passenger</category>
        
      </item>
    
  </channel>
</rss>
