<!-- SC_OFF --><div class="md"><p>Working with Opscode Chef has it's own issues. As I've been automating everything lately, I'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 "name:$dest" -a ipaddress|grep ipaddress|sed -e 's/ //g'|cut -d':' -f2) case $2 in "") echo "Connecting to $dest [$ip_address] via SSH" ssh $ip_address ;; *) echo "Connecting to $dest [$ip_address] via SSH and running '$2'" 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 ""|help|--help|-h) echo "Usage:" echo " lb_name [LB NAME] [INTEGER]" echo "If second value set to an integer, it will keep running, checking every x seconds CTRL+C to stop" ;; *) case $2 in [0-9]) count=$2 while [ 1 ]; do should_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep "InstanceId"|wc -l); do_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep "InService"|wc -l); echo "Instances Assigned to the LB: $should_have" echo "Instances in service on the LB: $do_have" echo "----------------------------" sleep $2 done ;; *) should_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep "InstanceId"|wc -l); do_have=$(aws elb describe-instance-health --load-balancer-name ${lb_name}|grep "InService"|wc -l); echo "Instances Assigned to the LB: $should_have" echo "Instances in service on the LB: $do_have" ;; 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 '{print $2}'|cut -d'"' -f2) case $2 in "") echo "Connecting to $1 [$ip_addr]" ssh $ip_addr ;; *) echo "Connecting to $1 [$ip_addr] and running command '$2'" ssh $ip_addr $2 ;; esac ;; *) echo "Usage: issh [instance_id] [optional: cmd]" ;; 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 "Usage:" echo " reginstance [LB Name] [instance ID]" echo "" echo "" ;; 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 "Usage:" echo " dereg [LB Name] [instance ID]" echo "" echo "" ;; 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 "") echo "Usage:" echo "" echo " $0 [id or string of ids encapsulated in quotes]" echo "" ;; *) for i in $1; do aws ec2 reboot-instances --instance-ids $i done ;; esac } </code></pre> <h1>AWS & 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 "") echo "Usage: $0 [node name] [--hard] ------- reboots node by name -- if found via knife" ;; *) instance_id=$(knife search node "name:$1" -a ec2.instance_id|grep instance_id|sed -e 's/ //g'|cut -d':' -f2) if [ "$instance_id" != "" ]; then case $2 in --hard) reboot_instance $instance_id reboot_instance $instance_id ;; *) reboot_instance $instance_id ;; esac else echo "Unable to find chef node with the name $1" 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 '{print $2}'|cut -d'"' -f2) node_name=$(knife search node "ipaddress:${ip_addr}" -a name|grep "name:"|sed -e 's/ //g'|cut -d':' -f2) case $2 in --no-ssh) echo "$1's name is '$node_name'. The internal IP address for $node_name is $ip_addr." ;; *) echo "$1's name is '$node_name'. The internal IP address for $node_name is $ip_addr." echo " " echo -n "Would you like to SSH into $node_name now? [y/N] " read ssh if [ "$ssh" = "n" ] || [ "$ssh" = "N" ] || [ "$ssh" = "" ]; then echo "" else ssh $ip_addr fi ;; esac ;; *) echo "Usage:" echo " instance_info [instance ID]" echo "" ;; esac } </code></pre> </div><!-- SC_ON --> submitted by
↧