Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
move extra functions to utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciprian Ciubotariu committed Apr 20, 2015
1 parent 4ca8cbd commit a394a8a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
58 changes: 1 addition & 57 deletions bootstrap_cfn/fab_tasks.py
Expand Up @@ -2,7 +2,6 @@

import os
from StringIO import StringIO
from time import sleep
import sys
import random
import yaml
Expand All @@ -16,7 +15,7 @@
from bootstrap_cfn.cloudformation import Cloudformation
from bootstrap_cfn.ec2 import EC2
from bootstrap_cfn.iam import IAM

from bootstrap_cfn.utils import tail


# GLOBAL VARIABLES
Expand Down Expand Up @@ -115,61 +114,6 @@ def get_connection(klass):
return klass(env.aws, env.aws_region)


def get_events(stack, stack_name):
"""Get the events in batches and return in chronological order"""
next = None
event_list = []
while 1 and not stack.stack_missing(stack_name):
try:
events = stack.conn_cfn.describe_stack_events(stack_name, next)
except:
break
event_list.append(events)
if events.next_token is None:
break
next = events.next_token
sleep(1)
return reversed(sum(event_list, []))


def tail(stack, stack_name):
"""Show and then tail the event log"""

def colorize(e):
if e.endswith("_IN_PROGRESS"):
return yellow(e)
elif e.endswith("_FAILED"):
return red(e)
elif e.endswith("_COMPLETE"):
return green(e)
else:
return e

def tail_print(e):
print("%s %s %s" % (colorize(e.resource_status).ljust(30), e.resource_type.ljust(50), e.event_id))

# First dump the full list of events in chronological order and keep
# track of the events we've seen already
seen = set()
initial_events = get_events(stack, stack_name)
for e in initial_events:
tail_print(e)
seen.add(e.event_id)

# Now keep looping through and dump the new events
while 1:
if stack.stack_missing(stack_name):
break
elif stack.stack_done(stack_name):
break
events = get_events(stack, stack_name)
for e in events:
if e.event_id not in seen:
tail_print(e)
seen.add(e.event_id)
sleep(2)


@task
def cfn_delete(force=False):
if not force:
Expand Down
58 changes: 58 additions & 0 deletions bootstrap_cfn/utils.py
Expand Up @@ -4,6 +4,7 @@
import time

import bootstrap_cfn.errors as errors
from fabric.colors import green, red, yellow


def timeout(timeout, interval):
Expand Down Expand Up @@ -33,3 +34,60 @@ def connect_to_aws(module, instance):
raise errors.NoCredentialsError()
except boto.provider.ProfileNotFoundError as e:
raise errors.ProfileNotFoundError(instance.aws_profile_name)


def tail(stack, stack_name):
"""Show and then tail the event log"""

def colorize(e):
if e.endswith("_IN_PROGRESS"):
return yellow(e)
elif e.endswith("_FAILED"):
return red(e)
elif e.endswith("_COMPLETE"):
return green(e)
else:
return e

def tail_print(e):
print("%s %s %s" % (colorize(e.resource_status).ljust(30), e.resource_type.ljust(50), e.event_id))

# First dump the full list of events in chronological order and keep
# track of the events we've seen already
seen = set()
initial_events = get_events(stack, stack_name)
for e in initial_events:
tail_print(e)
seen.add(e.event_id)

# Now keep looping through and dump the new events
while 1:
if stack.stack_missing(stack_name):
break
elif stack.stack_done(stack_name):
break
events = get_events(stack, stack_name)
for e in events:
if e.event_id not in seen:
tail_print(e)
seen.add(e.event_id)
time.sleep(2)


def get_events(stack, stack_name):
"""Get the events in batches and return in chronological order"""
next = None
event_list = []
while 1 and not stack.stack_missing(stack_name):
try:
events = stack.conn_cfn.describe_stack_events(stack_name, next)
except:
break
event_list.append(events)
if events.next_token is None:
break
next = events.next_token
time.sleep(1)
return reversed(sum(event_list, []))


0 comments on commit a394a8a

Please sign in to comment.