#!/usr/bin/python -tt

import sys,getopt
import commands 
import logging
import json

logger = logging.getLogger('stencil')
hdlr = logging.StreamHandler(sys.stdout)
#hdlr = logging.FileHandler('stencil.log') 
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
logger.setLevel(logging.INFO) #logging.DEBUG


def help():

  print " Usage: getInstanceId.py > instance.json [--help] "



def Run(cmd):
 
  logger.debug("** Running: %s" % cmd)

  (status,output) = commands.getstatusoutput(cmd)

  if status > 0:
    logger.error(output)
    sys.exit(2)
 
  logger.debug('[OK]')
  return output



def main(argv):

  # make sure command line arguments are valid
  try:
    options, args = getopt.getopt(

       argv, 
      'hv', 
      [ 
        'help',
        'verbose' 
    
      ])
 
  except getopt.GetoptError:
    logging.fatal("Bad options!")
    help()
    sys.exit(2)


  # handle command line arugments
  for opt, arg in options:
    if opt in ('-h', '--help'):
      help()
      sys.exit(2)
    elif opt in ('-v', '--verbose'):
      logger.setLevel(logging.DEBUG) 
 

  ###################################
  # main code starts here
  ###################################

  cmd = "wget --timeout 3 --tries 1 -q -O - http://169.254.169.254/latest/meta-data/instance-id"
  instance_id = Run(cmd) 

  j = {

    "instanceId":instance_id

  }

  print json.dumps(j)


if __name__ == "__main__":
  main(sys.argv[1:])
 

 



@@ -30,23 +30,16 @@
  sys
-,getopt
 %0Aimport 
 comm
@@ -38,567 +38,254 @@
 ort 
-commands %0Aimport logging%0Aimport json%0A%0Alogger = logging.getLogger('stencil')%0Ahdlr = logging.StreamHandler(sys.stdout)%0A#hdlr = logging.FileHandler('stencil.log') %0Aformatter = logging.Formatter('%25(asctime)s %25(levelname)s %25(message)s')%0Ahdlr.setFormatter(formatter)%0Alogger.addHandler(hdlr) %0Alogger.setLevel(logging.INFO) #logging.DEBUG%0A%0A%0Adef help():%0A%0A  print %22 Usage: getInstanceId.py %3E instance.json %5B--help%5D %22%0A%0A%0A%0Adef Run(cmd):%0A %0A  logger.debug(%22** Running: %25s%22 %25 cmd)%0A%0A  (status,output) = commands.getstatusoutput(cmd)%0A%0A  if status %3E 0:%0A    logger.error(output)%0A
+boto.utils%0Aimport json%0A %0Aclass AwsInstanceDetails(object):%0A%0A  def __init__(self):%0A%0A    try:%0A      self.instance = boto.utils.get_instance_metadata() %0A    except Exception as e:%0A      print %22FATAL: Could not grab instance information from boto%22%0A  
     
@@ -302,540 +302,180 @@
 )%0A %0A
+%0A
   
-logger.debug('%5BOK%5D')%0A  return output%0A%0A%0A%0Adef main(argv):%0A%0A  # make sure command line arguments are valid%0A  try:%0A    options, args = getopt.getopt(%0A%0A       argv, %0A      'hv', %0A      %5B %0A        'help',%0A        'verbose' %0A    %0A      %5D)%0A %0A  except getopt.GetoptError:%0A    logging.fatal(%22Bad options!%22)%0A    help()%0A    sys.exit(2)%0A%0A%0A  # handle command line arugments%0A  for opt, arg in options:%0A    if opt in ('-h', '--help'):%0A      help()%0A      sys.exit(2)%0A    elif opt in ('-v', '--verbose'):%0A      logger.setLevel(logging.DEBUG) %0A %0A%0A  
+def getPublicHost(self):%0A%0A    return self.instance%5B'public-hostname'%5D%0A %0A  def getInstanceId(self):%0A%0A    return self.instance%5B'instance-id'%5D%0A%0A%0Adef main(argv):%0A%0A  ########
 ####
@@ -514,32 +514,96 @@
   # 
-main code start
+some test code, never run
 s 
+w
 he
-re%0A  
+n included%0A  # This must be run from an AWS box%0A  ########
 ####
@@ -638,141 +638,87 @@
 ###%0A
+  
 %0A  
-cmd = %22wget --timeout 3 --tries 1 -q -O - http://169.254.169.254/latest/meta-data/instance-id%22%0A  instance_id = Run(cmd) %0A%0A  j = %7B%0A
+k = AwsInstanceDetails()%0A  %0A  j = %7B%0A%0A    %22public-host%22: k.getPublicHost(),
 %0A   
@@ -731,24 +731,31 @@
 ance
-Id%22:i
+-id%22: k.getI
 nstance
-_id%0A
+Id()
 %0A  %7D
@@ -831,12 +831,4 @@
 1:%5D)
-%0A %0A%0A %0A%0A%0A

Complete rewrite