from subprocess import call

def chef_dereg_fun(instancename):
 if call("knife node show " + instancename, shell=True) == 0:
  out1 = call("knife node delete " + instancename + " -y" , shell=True)
  if out1 == 0:
    out2 = call("knife client delete " + instancename + " -y", shell=True)
    if out2 == 0:
      out3 = call("knife node show " + instancename, shell=True)
      if out3 == 0:
        print "Node still exists in Chef Server"
        return False
      else:
        print "Node removed from Chef Server."
        return True
    else:
      print "Knife client delete failed"
      return False
  else:
    print "Knife node delete failed"
    return False
 else:
  print "Node not registered with Chef"
  return True
