/blog/public/blog/2011/06/24/59995517/index.html
HTML | 294 lines | 182 code | 108 blank | 4 comment | 0 complexity | f715218f07dfb7c429a687ef7a7e2a16 MD5 | raw file
- <!DOCTYPE html>
- <!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
- <!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
- <!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
- <head>
- <meta charset="utf-8">
- <title>My ohai plugin - WYSIWYG</title>
- <meta name="author" content="Sascha Bates">
-
- <meta name="description" content="The plugin at github When we started the Chef infrastructure automation project last year, a dev team started up at nearly the same time. I …">
-
- <!-- http://t.co/dKP3o1e -->
- <meta name="HandheldFriendly" content="True">
- <meta name="MobileOptimized" content="320">
- <meta name="viewport" content="width=device-width, initial-scale=1">
-
- <link rel="canonical" href="http://brattyredhead.com/blog/2011/06/24/59995517">
- <link href="/favicon.png" rel="icon">
- <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
- <script src="/javascripts/modernizr-2.0.js"></script>
- <script src="/javascripts/ender.js"></script>
- <script src="/javascripts/octopress.js" type="text/javascript"></script>
- <link href="/atom.xml" rel="alternate" title="WYSIWYG" type="application/atom+xml">
- <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
- <link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
- <link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
-
- </head>
- <body >
- <header role="banner"><hgroup>
- <h1><a href="/">WYSIWYG</a></h1>
-
- <h2>no subtext here</h2>
-
- </hgroup>
- </header>
- <nav role="navigation"><ul class="subscription" data-subscription="rss">
- <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
-
- </ul>
-
- <form action="http://www.duckduckgo.com/" method="get">
- <fieldset role="search">
- <input type="hidden" name="q" value="site:brattyredhead.com" />
- <input class="search" type="text" name="q" results="0" placeholder="Search"/>
- </fieldset>
- </form>
-
- <ul class="main-navigation">
- <li><a href="/">Blog</a></li>
- <li><a href="/blog/archives">Archives</a></li>
- </ul>
- </nav>
- <div id="main">
- <div id="content">
- <div>
- <article class="hentry" role="article">
-
- <header>
-
- <h1 class="entry-title">My Ohai Plugin</h1>
-
-
- <p class="meta">
-
-
- <time datetime="2011-06-24T00:00:00-05:00" pubdate data-updated="true">Jun 24<span>th</span>, 2011</time>
-
- </p>
-
- </header>
- <div class="entry-content"><p><a href="https://github.com/sbates/Chef-odds-n-ends/blob/master/ohai/parse_host_plugin.rb" title="The plugin at github" target="_blank">The plugin at github</a></p>
- <p>When we started the Chef infrastructure automation project last year, a dev team started up at nearly the same time. I didn’t know enough about Chef to have (much of) an opinion on how others were addressing cookbook issues they faced. So for the first few months, I worked on my problems to solve while the devs worked on theirs. I did eventually started paying attention to what was going on with the dev team and discovered that they had started to create role names that looked like app_name_env(e.g. riak_prod), causing a role for each app to be created in each environment, but with nearly identical run lists. They were labeling their attributes with environment and used the roles to sort them out.</p>
- <p>So we came up with a quick solution. We created an attribute called node[:env] and put it in a role called env_dev, env_qa, etc. And the same goes for other things we needed to set like node[:swimlane] and node[:zone]. Every time a node was created, relevant env attribute roles were added along with the platform_base and any application roles. We all refactored our code and started using the env variable to sort out environment-specific attributes. This worked well in the short term, for about 6 months.</p>
- <p>About 2 months ago, we started planning for provisioning a second data center. With another data center containing two environments (prod and stage) came a location issue. Now we have two sets of ntp servers, different DNS servers and a whole host of location-specific coding issues. I went to go add some location-specific roles and started thinking about the refactoring we’d need to do to account for two locations.</p>
- <p>As I started to make the first role, I realized that this was quickly becoming an unwieldy solution. We were configuring the nodes managed by our automation system <strong>by hand</strong>. This boggled me. And the number of managed nodes continues to grow.</p>
- <p> </p>
- <p>One of the problems we struggle with is that we have no system of record. We get most of our new server info on spreadsheets from various project teams or ops/mgmt teams. We can’t call any central system API to get info. All the servers have static IPs and minimum of two networks. The ops team doesn’t believe in DNS. So we have to keep all of this information somewhere or figure out clever ways to deduce the information we want (or set roles by hand, ugh!).</p>
- <p>A secondary problem I had with the roles is that their attributes weren’t set early enough for me to use logic in the attributes file. Node[:env] didn’t get set until after attributes files were compile which was causing me other problems.</p>
- <p>So as I thought about the location role, I rebelled. I was <strong>done</strong> setting roles by hand. UGH!</p>
- <p>All of our host names are the same length and contain a rich amount of data encoded in their names. I thought that there must be a way to extract and label that information for use as node attibutes. I’m no Ruby genius and I still struggle with anything beyond fancy scripting, but it’s extra fun for me to run at a problem head on and obsess over it. And that’s what I did. </p>
- <p>When I came out of the weekend, I had an oHai plugin that parsed a host name, set 3 top level attributes and 5 nested attributes thatI thought might be useful for logic in cookbooks. It looks like this:</p>
- <p>[root@pxqpkyapp05 tmp]# ohai -f /tmp/plugins/parse_host_plugin.rb -l debug<br /> [Fri, 24 Jun 2011 19:09:09 -0500] DEBUG: ohai plugin: parse_host: parsing new host pxqpkyapp05<br /> {</p>
- <p><strong> “environment”: “qa”</strong><br /> <strong> “location”: “pismo”,</strong><br /> “hostdata”: {<br /> “server_type”: “application”,<br /> “loc”: “p”,<br /> “env”: “q”,<br /> “application”: “Pooky”,<br /> “platform”: “solaris”<br /> },<br /> }<br /> </p>
- <p>I didn’t think this would be useful to anyone but us. Surely everyone else was using Chef in the cloud, right? Heck no. It turns out, as I was chatting with folks at Velocity last week, that several people are data center-bound with encoded host names. Discussing it, I thought it might be useful to share, even if it would require some tweaking to make it work with any else’s environment.</p>
- <p>Basically, I set a hash with environment data and then set variables using a regex on the host name. I set the environment and location vars separately and then put indexes in the hash for any values I want to add into the hostdata hash. I took most of our custom logic out, like the node[:zone] logic that no one needs, although I left in a custom piece I had to add for an oddly named environment at a third data center location. All app names and locations have been changed to protect the innocent (that’s me).</p>
- <p>I hope someone else can use it. It was my first step beyond recipes and I’m rather proud of it. I’ve already had a request from a team mate to move the logic to a library so that we can use it in our host management activities and that’s what I’m working on now. I’ve created a class and a function that uses the class, but need to translate it to Chef-specific DSL.</p>
- <p>Here are some examples of how we’re using them:</p>
- <p> </p>
- <p>in the recipe: dnsinfo = search(:dns, “id:#{node.location}”)</p>
- <p>in the template:</p>
- <p>domain <%= @dnsinfo[“domain”] %><br />search <%= @dnsinfo[“search”] %><br /><% @dnsinfo[“nameservers”].each do |ns| %><br />nameserver <%= ns %><br /><% end %></p>
- <p># Unload unwanted packages<br />unless node.env == “dev” || node.attribute?(“keep_gcc”)<br /> package “gcc” do<br /> action :remove<br /> end<br />end<p /></p>
- <p>chefsearch = search(“chef_servers”,”env:#{node.env} AND id:#{node.hostdata.loc}*”).first<br />node.set.chef.my_chef_server = chefsearch[“id”]</p>
- <p>node.set.chef.server_url = “http://#{node.chef.my_chef_server}”<br />node.set.chef.mrepo_url = “http://#{node.chef.my_chef_server}/mrepo”<br />node.set.chef.proxy_url = “http://#{node.chef.my_chef_server}:8000”</p>
- <p> </p>
- <p>This will also come in handy once we’ve migrated to .10 where we can set so many more things within Chef based on environment. The plugin is phrased in functions as my original plugin file was rather messy with comments and things. I felt like breaking it down this way made it easier to follow wth was going on.</p>
- <p>Please let me know if you find this useful or if you have questions. This is my first community contribution and so I’m kind of like the kid with the cool shiny thing they want to show everyone. Look at my shiny!</p>
- </div>
- <footer>
- <p class="meta">
-
-
- <span class="byline author vcard">Posted by <span class="fn">Sascha Bates</span></span>
-
-
- <time datetime="2011-06-24T00:00:00-05:00" pubdate data-updated="true">Jun 24<span>th</span>, 2011</time>
-
- </p>
-
- <div class="sharing">
-
- <a href="http://twitter.com/share" class="twitter-share-button" data-url="http://brattyredhead.com/blog/2011/06/24/59995517/" data-via="sascha_d" data-counturl="http://brattyredhead.com/blog/2011/06/24/59995517/" >Tweet</a>
-
-
-
- </div>
-
- <p class="meta">
-
- <a class="basic-alignment left" href="/blog/2011/06/23/another-chapter-done/" title="Previous Post: Another Chapter Done">« Another Chapter Done</a>
-
-
- <a class="basic-alignment right" href="/blog/2011/06/24/reason-eleventyseven-to-automate/" title="Next Post: Reason #eleventyseven to automate">Reason #eleventyseven to automate »</a>
-
- </p>
- </footer>
- </article>
- </div>
- <aside class="sidebar">
-
- <section>
- <h1>About Me</h1>
- <p>A little something about me.</p>
- </section>
- <section>
- <h1>Recent Posts</h1>
- <ul id="recent_posts">
-
- <li class="post">
- <a href="/blog/2012/05/27/finding-sanity-with-chef/">Finding Sanity With Chef</a>
- </li>
-
- <li class="post">
- <a href="/blog/2012/05/08/test/">test</a>
- </li>
-
- <li class="post">
- <a href="/blog/2012/04/27/my-favorite-jvm-arg-ever/">My Favorite JVM Arg EVER</a>
- </li>
-
- <li class="post">
- <a href="/blog/2012/04/26/the-alien-technology-tuning-challenge/">The Alien Technology Tuning Challenge</a>
- </li>
-
- <li class="post">
- <a href="/blog/2012/04/15/yak-hacking/">Yak Hacking - A Story</a>
- </li>
-
- <li class="post">
- <a href="/blog/2012/04/01/weekend-chef-and-puppet-projects/">Weekend Chef and Puppet Projects</a>
- </li>
-
- <li class="post">
- <a href="/blog/2012/03/26/annoying-recruiter-call-45/">Annoying Recruiter Call #45</a>
- </li>
-
- <li class="post">
- <a href="/blog/2012/03/23/i-am-not-a-fangirl/">I am not a fangirl</a>
- </li>
-
- <li class="post">
- <a href="/blog/2011/11/19/puppet-for-chef-users-part-2-when-is-a-node-a/">Puppet for Chef Users - Part 2 - When is a node a node? </a>
- </li>
-
- <li class="post">
- <a href="/blog/2011/11/07/puppet-for-chef-users/">Puppet for Chef Users</a>
- </li>
-
- </ul>
- </section>
- <section>
- <h1>Latest Tweets</h1>
- <ul id="tweets">
- <li class="loading">Status updating...</li>
- </ul>
- <script type="text/javascript">
- $.domReady(function(){
- getTwitterFeed("sascha_d", 4, false);
- });
- </script>
- <script src="/javascripts/twitter.js" type="text/javascript"> </script>
-
- <a href="http://twitter.com/sascha_d" class="twitter-follow-button" data-show-count="false">Follow @sascha_d</a>
-
- </section>
- <section>
- <h1>My Pinboard</h1>
- <ul id="pinboard_linkroll">Fetching linkroll...</ul>
- <p><a href="http://pinboard.in/u:sascha_d">My Pinboard Bookmarks »</a></p>
- </section>
- <script type="text/javascript">
- var linkroll = 'pinboard_linkroll'; //id target for pinboard list
- var pinboard_user = "sascha_d"; //id target for pinboard list
- var pinboard_count = 3; //id target for pinboard list
- (function(){
- var pinboardInit = document.createElement('script');
- pinboardInit.type = 'text/javascript';
- pinboardInit.async = true;
- pinboardInit.src = '/javascripts/pinboard.js';
- document.getElementsByTagName('head')[0].appendChild(pinboardInit);
- })();
- </script>
-
- </aside>
- </div>
- </div>
- <footer role="contentinfo"><p>
- Copyright © 2012 - Sascha Bates -
- <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
- </p>
- </footer>
-
- <script type="text/javascript">
- (function(){
- var twitterWidgets = document.createElement('script');
- twitterWidgets.type = 'text/javascript';
- twitterWidgets.async = true;
- twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
- document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
- })();
- </script>
- </body>
- </html>