from __future__ import print_function
import os, pyrax, sys
import pyrax.exceptions as pexc
from termcolor import colored
import ConfigParser
from subprocess import check_output

path = os.path.dirname(os.path.realpath(__file__))
config_file = path + "/config.ini"

def log(level, message):
  if level == 'OK':
    print(colored('[  OK  ]', 'green'), "\t", message, file=sys.stderr)
  elif level == 'INFO':
    print(colored('[ INFO ]', 'blue'), "\t", message, file=sys.stderr)
  elif level == 'ERROR':
    print(colored('[ FAIL ]', 'red'), "\t", message, file=sys.stderr)
  else:
    print(message)

def authenticate(dc):
  pyrax.set_setting("identity_type", "rackspace")

  log("INFO", "Authenticating")

  try:
    pyrax.set_credential_file(config_file, region=dc)
  except pexc.AuthenticationFailed:
    log('ERROR', 'Authentication Failure')
  
  log('OK', 'Authentication Successful')

def get_config(group):
  config = ConfigParser.ConfigParser()
  config.read(config_file)
  if config.has_section(group):
    return config
  else:
    raise Exception('Unknown config section') 

def get_machine_uuid():
  name = check_output('xenstore-read name', shell=True)
  id = name.strip()
  return id[9:]

from __future__ import print_function
import os, pyrax, sys
import pyrax.exceptions as pexc
from termcolor import colored
import ConfigParser
import subprocess

path = os.path.dirname(os.path.realpath(__file__))
config_file = path + "/config.ini"

def log(level, message):
  if level == 'OK':
    print(colored('[  OK  ]', 'green'), "\t", message, file=sys.stderr)
  elif level == 'INFO':
    print(colored('[ INFO ]', 'blue'), "\t", message, file=sys.stderr)
  elif level == 'ERROR':
    print(colored('[ FAIL ]', 'red'), "\t", message, file=sys.stderr)
  else:
    print(message)

def authenticate(dc):
  pyrax.set_setting("identity_type", "rackspace")

  log("INFO", "Authenticating")

  try:
    pyrax.set_credential_file(config_file, region=dc)
  except pexc.AuthenticationFailed:
    log('ERROR', 'Authentication Failure')
  
  log('OK', 'Authentication Successful')

def get_config(group):
  config = ConfigParser.ConfigParser()
  config.read(config_file)
  if config.has_section(group):
    return config
  else:
    raise Exception('Unknown config section') 

def get_machine_uuid():
  name = subprocess.Popen(['xenstore-read name'], shell=True, stdout=subprocess.PIPE).communicate()[0]
  id = name.strip()
  return id[9:]

Fix for python 2.6 compatibility in subprocess
Fix for python 2.6 compatibility in subprocess

  from __future__ import print_function
  import os, pyrax, sys
  import pyrax.exceptions as pexc
  from termcolor import colored
  import ConfigParser
- from subprocess import check_output
+ import subprocess
  
  path = os.path.dirname(os.path.realpath(__file__))
  config_file = path + "/config.ini"
  
  def log(level, message):
    if level == 'OK':
      print(colored('[  OK  ]', 'green'), "\t", message, file=sys.stderr)
    elif level == 'INFO':
      print(colored('[ INFO ]', 'blue'), "\t", message, file=sys.stderr)
    elif level == 'ERROR':
      print(colored('[ FAIL ]', 'red'), "\t", message, file=sys.stderr)
    else:
      print(message)
  
  def authenticate(dc):
    pyrax.set_setting("identity_type", "rackspace")
  
    log("INFO", "Authenticating")
  
    try:
      pyrax.set_credential_file(config_file, region=dc)
    except pexc.AuthenticationFailed:
      log('ERROR', 'Authentication Failure')
    
    log('OK', 'Authentication Successful')
  
  def get_config(group):
    config = ConfigParser.ConfigParser()
    config.read(config_file)
    if config.has_section(group):
      return config
    else:
      raise Exception('Unknown config section') 
  
  def get_machine_uuid():
-   name = check_output('xenstore-read name', shell=True)
+   name = subprocess.Popen(['xenstore-read name'], shell=True, stdout=subprocess.PIPE).communicate()[0]
    id = name.strip()
    return id[9:]
  
Fix for python 2.6 compatibility in subprocess
## Code Before:
from __future__ import print_function
import os, pyrax, sys
import pyrax.exceptions as pexc
from termcolor import colored
import ConfigParser
from subprocess import check_output

path = os.path.dirname(os.path.realpath(__file__))
config_file = path + "/config.ini"

def log(level, message):
  if level == 'OK':
    print(colored('[  OK  ]', 'green'), "\t", message, file=sys.stderr)
  elif level == 'INFO':
    print(colored('[ INFO ]', 'blue'), "\t", message, file=sys.stderr)
  elif level == 'ERROR':
    print(colored('[ FAIL ]', 'red'), "\t", message, file=sys.stderr)
  else:
    print(message)

def authenticate(dc):
  pyrax.set_setting("identity_type", "rackspace")

  log("INFO", "Authenticating")

  try:
    pyrax.set_credential_file(config_file, region=dc)
  except pexc.AuthenticationFailed:
    log('ERROR', 'Authentication Failure')
  
  log('OK', 'Authentication Successful')

def get_config(group):
  config = ConfigParser.ConfigParser()
  config.read(config_file)
  if config.has_section(group):
    return config
  else:
    raise Exception('Unknown config section') 

def get_machine_uuid():
  name = check_output('xenstore-read name', shell=True)
  id = name.strip()
  return id[9:]

## Instruction:
Fix for python 2.6 compatibility in subprocess
## Code After:
from __future__ import print_function
import os, pyrax, sys
import pyrax.exceptions as pexc
from termcolor import colored
import ConfigParser
import subprocess

path = os.path.dirname(os.path.realpath(__file__))
config_file = path + "/config.ini"

def log(level, message):
  if level == 'OK':
    print(colored('[  OK  ]', 'green'), "\t", message, file=sys.stderr)
  elif level == 'INFO':
    print(colored('[ INFO ]', 'blue'), "\t", message, file=sys.stderr)
  elif level == 'ERROR':
    print(colored('[ FAIL ]', 'red'), "\t", message, file=sys.stderr)
  else:
    print(message)

def authenticate(dc):
  pyrax.set_setting("identity_type", "rackspace")

  log("INFO", "Authenticating")

  try:
    pyrax.set_credential_file(config_file, region=dc)
  except pexc.AuthenticationFailed:
    log('ERROR', 'Authentication Failure')
  
  log('OK', 'Authentication Successful')

def get_config(group):
  config = ConfigParser.ConfigParser()
  config.read(config_file)
  if config.has_section(group):
    return config
  else:
    raise Exception('Unknown config section') 

def get_machine_uuid():
  name = subprocess.Popen(['xenstore-read name'], shell=True, stdout=subprocess.PIPE).communicate()[0]
  id = name.strip()
  return id[9:]
