@task
def status():
  """Overall production status."""
  cl_timestamps = defaultdict(str)
  def _ResolveCLDate(rev):
    if rev == '??' or rev in cl_timestamps.keys():
      return
    ts = hg_revision_timestamp(rev)
    if ts is not None:
      cl_timestamps[rev] = util.TimestampUTCToISO8601(ts)

  env_rev = latest_requirements_revision()
  code_rev = hg_revision()
  _ResolveCLDate(env_rev)
  _ResolveCLDate(code_rev)
  print '=' * 80
  print 'Local environment:'
  print '  Env revision: %s (%s)' % (env_rev, cl_timestamps.get(env_rev, '??'))
  print '  Code revision: %s (%s)' % (code_rev, cl_timestamps.get(code_rev, '??'))

  for nodetype in ec2_utils.kValidNodeTypes:
    elbs = ec2_utils.GetLoadBalancers(env.region, [nodetype])
    assert len(elbs) == 1, 'Need exactly one %s load balancer in %s' % (nodetype, env.region)

    elb = elbs[0]
    instances = ec2_utils.ListInstances(env.region, node_types=[nodetype])

    elb_zones = {z:0 for z in elb.availability_zones}
    elb_health = {h.instance_id: h.state for h in elb.get_instance_health()}

    for i in instances:
      id = i.id
      if i.state != 'running':
        continue
      zone = i.placement
      if zone in elb_zones.keys():
        elb_zones[zone] += 1
      if id in elb_health:
        setattr(i, '_elb_health', elb_health[id])
      with settings(host_string=i.public_dns_name):
        setattr(i, '_env_rev', active_env() or '??')
        setattr(i, '_code_rev', active_code() or '??')
        _ResolveCLDate(i._env_rev)
        _ResolveCLDate(i._code_rev)

    print '\n%s' % ('=' * 80)
    print '%s ELB: %s' % (nodetype, elb.name)
    print '  # Running instances by ELB zone:'
    zone_str = ', '.join(['%s: %s' % (k, v) for k, v in elb_zones.iteritems()])
    print '  %s' % zone_str

    print ''
    print '%s instances: %d' % (nodetype, len(instances))
    print '  # %-8s %-12s %-13s %-10s %-12s %-13s %-10s %-13s %-10s' % \
          ('ID', 'Name', 'State', 'Zone', 'ELB state', 'Active env', 'Env date', 'Active code', 'Code date')
    for i in instances:
      env_rev = getattr(i, '_env_rev', '')
      code_rev = getattr(i, '_code_rev', '')
      print '  %-10s %-12s %-13s %-10s %-12s %-13s %-10s %-13s %-10s' % (i.id,
                                                             i.tags.get('Name', ''),
                                                             i.state,
                                                             i.placement,
                                                             getattr(i, '_elb_health', ''),
                                                             env_rev,
                                                             cl_timestamps[env_rev],
                                                             code_rev,
                                                             cl_timestamps[code_rev])

    if instances and code_rev and cl_timestamps.get(code_rev):
      # If the deployed revision exists locally, create a bookmark to it (one per nodetype).
      # This allows queries like these (some aliases are defined in viewfinder.hgrc)
      #   hg log -r ::.-::deployed_staging
      #   hg log -r ::deployed_staging-::deployed_prod
      local("hg bookmark -f -r %s deployed_%s" % (code_rev, nodetype.lower()))
dataset/ETHPy150Open viewfinderco/viewfinder/fabfile.py/status/original