Quantcast
Channel: linuxadmin: Expanding Linux SysAdmin knowledge
Viewing all articles
Browse latest Browse all 17848

Chef/AWS Simplifiers (X-Post from /r/sysadmin)

$
0
0
<!-- SC_OFF --><div class="md"><p>Working with Opscode Chef has it&#39;s own issues. As I&#39;ve been automating everything lately, I&#39;ve decided to share some of my simplifiers. Some require that you have AWS-CLI installed, others merely require Chef. Some require both:</p> <h1>Chef Tools</h1> <p><code>kssh</code>: take hostname (<code>host1</code> for example) and log in without knowing the IP, and keeping a persistent connection</p> <pre><code>function kssh(){ dest=$1 if valid_ip $dest; then instance_info $dest --no-ssh ssh $dest else ip_address=$(knife search node &quot;name:$dest&quot; -a ipaddress|grep ipaddress|sed -e &#39;s/ //g&#39;|cut -d&#39;:&#39; -f2) case $2 in &quot;&quot;) echo &quot;Connecting to $dest [$ip_address] via SSH&quot; ssh $ip_address ;; *) echo &quot;Connecting to $dest [$ip_address] via SSH and running &#39;$2&#39;&quot; ssh $ip_address $2 esac fi } </code></pre> <h1>AWS tools:</h1> <p><code>lb_check</code>: takes the load balancer, and gives you easily-readable info about it.</p> <pre><code>function lb_check(){ lb_name=$1 case $lb_name in &quot;&quot;|help|--help|-h) echo &quot;Usage:&quot; echo &quot; lb_name [LB NAME] [INTEGER]&quot; echo &quot;If second value set to an integer, it will keep running, checking every x seconds CTRL+C to stop&quot; ;; *) case $2 in [0-9]) count=$2 while [ 1 ]; do should_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep &quot;InstanceId&quot;|wc -l); do_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep &quot;InService&quot;|wc -l); echo &quot;Instances Assigned to the LB: $should_have&quot; echo &quot;Instances in service on the LB: $do_have&quot; echo &quot;----------------------------&quot; sleep $2 done ;; *) should_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep &quot;InstanceId&quot;|wc -l); do_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep &quot;InService&quot;|wc -l); echo &quot;Instances Assigned to the LB: $should_have&quot; echo &quot;Instances in service on the LB: $do_have&quot; ;; esac ;; esac } </code></pre> <p><code>issh</code>: log into AWS instance (via the private IP) using the instance ID</p> <pre><code>function issh(){ case $1 in i-*) ip_addr=$(aws ec2 describe-instances --instance $1|grep PrivateIpAddress|head -1|awk &#39;{print $2}&#39;|cut -d&#39;&quot;&#39; -f2) case $2 in &quot;&quot;) echo &quot;Connecting to $1 [$ip_addr]&quot; ssh $ip_addr ;; *) echo &quot;Connecting to $1 [$ip_addr] and running command &#39;$2&#39;&quot; ssh $ip_addr $2 ;; esac ;; *) echo &quot;Usage: issh [instance_id] [optional: cmd]&quot; ;; esac } </code></pre> <p><code>reginstance</code>: register an instance with an AWS LB:</p> <pre><code>function reginstance(){ case $2 in i-*) aws elb register-instances-with-load-balancer --load-balancer-name $1 --instances $2 ;; *) echo &quot;Usage:&quot; echo &quot; reginstance [LB Name] [instance ID]&quot; echo &quot;&quot; echo &quot;&quot; ;; esac } </code></pre> <p><code>dereg</code>: deregister an instance from an AWS LB</p> <pre><code>function dereg(){ case $2 in i-*) aws elb deregister-instances-from-load-balancer --load-balancer-name $1 --instances $2 ;; *) echo &quot;Usage:&quot; echo &quot; dereg [LB Name] [instance ID]&quot; echo &quot;&quot; echo &quot;&quot; ;; esac } </code></pre> <p><code>reboot_instance</code> (needed by <code>kreboot</code> below) -- reboots instance by AWS Instance ID</p> <pre><code>function reboot_instance(){ case $1 in &quot;&quot;) echo &quot;Usage:&quot; echo &quot;&quot; echo &quot; $0 [id or string of ids encapsulated in quotes]&quot; echo &quot;&quot; ;; *) for i in $1; do aws ec2 reboot-instances --instance-ids $i done ;; esac } </code></pre> <h1>AWS &amp; Chef scripts:</h1> <p><code>kreboot</code>: Takes the node name, and sends an AWS command to reboot the box.</p> <pre><code>function kreboot(){ case $1 in &quot;&quot;) echo &quot;Usage: $0 [node name] [--hard] ------- reboots node by name -- if found via knife&quot; ;; *) instance_id=$(knife search node &quot;name:$1&quot; -a ec2.instance_id|grep instance_id|sed -e &#39;s/ //g&#39;|cut -d&#39;:&#39; -f2) if [ &quot;$instance_id&quot; != &quot;&quot; ]; then case $2 in --hard) reboot_instance $instance_id reboot_instance $instance_id ;; *) reboot_instance $instance_id ;; esac else echo &quot;Unable to find chef node with the name $1&quot; fi ;; esac } </code></pre> <p><code>instance_info</code>: Takes an AWS instance info and returns node name and internal IP address</p> <pre><code>function instance_info(){ case $1 in i-*) ip_addr=$(aws ec2 describe-instances --instance $1|grep PrivateIpAddress|head -1|awk &#39;{print $2}&#39;|cut -d&#39;&quot;&#39; -f2) node_name=$(knife search node &quot;ipaddress:${ip_addr}&quot; -a name|grep &quot;name:&quot;|sed -e &#39;s/ //g&#39;|cut -d&#39;:&#39; -f2) case $2 in --no-ssh) echo &quot;$1&#39;s name is &#39;$node_name&#39;. The internal IP address for $node_name is $ip_addr.&quot; ;; *) echo &quot;$1&#39;s name is &#39;$node_name&#39;. The internal IP address for $node_name is $ip_addr.&quot; echo &quot; &quot; echo -n &quot;Would you like to SSH into $node_name now? [y/N] &quot; read ssh if [ &quot;$ssh&quot; = &quot;n&quot; ] || [ &quot;$ssh&quot; = &quot;N&quot; ] || [ &quot;$ssh&quot; = &quot;&quot; ]; then echo &quot;&quot; else ssh $ip_addr fi ;; esac ;; *) echo &quot;Usage:&quot; echo &quot; instance_info [instance ID]&quot; echo &quot;&quot; ;; esac } </code></pre> </div><!-- SC_ON --> submitted by

Viewing all articles
Browse latest Browse all 17848

Trending Articles