Ejemplo n.º 1
0
def get_master_instance(ec2_conn, subnet_id):
    """
        Given EC2Connection object and Master Subnet id, check that there is
        just one instance running in that subnet - this is the Master. Raise
        exception if the number of instances is != 0. 
        Return the Master instance object.
    """
    instances = ec2_conn.get_only_instances(filters={"subnet-id": subnet_id})
    if 1 > len(instances):
        raise SpinupError("There are no instances in the master subnet")
    if 1 < len(instances):
        raise SpinupError("There are too many instances in the master subnet")

    return instances[0]
Ejemplo n.º 2
0
def yaml_attr(node, name, default):
    """Takes three arguments: node in yaml structure, attr name, and default
       If default is None, checks that attr exists in node.
       If default is not None, returns attr value if attr exists and is not None,
       otherwise returns default."""
    if not (name in node):
        raise SpinupError("Attribute ->{}<- missing in yaml node".format(name))
    if default == None and node[name] == None:
        raise SpinupError(
            "Required yaml attribute ->{}<- has no value".format(name))
    r = ''
    r = node[name] if name in node else default
    r = node[name] if node[name] != None else default
    #print "Using {} ->{}<-".format( name, r )
    return r
Ejemplo n.º 3
0
def write_yaml(y, fn):
    """
        Takes YaML data structure and filename. Writes/updates the file.
    """
    try:
        f = open(fn, 'w')
    except OSError as e:
        raise SpinupError(
            "Could not open yaml file ->{}<- for writing".format(fn))
    f.write(yaml.safe_dump(y, default_flow_style=False))
    return None
Ejemplo n.º 4
0
def parse_yaml(yaml_file):
    """
        Takes full path of aws.yaml file. Parses aws.yaml file. Returns a dictionary.
    """
    try:
        f = open(yaml_file)
    except OSError as e:
        raise SpinupError("Could not read yaml file ->{}<-".format(yaml_file))
    conf = yaml.safe_load(f)
    f.close()

    return conf
Ejemplo n.º 5
0
def process_user_data(fn, vars=[]):
    """
        Given filename of user-data file and a list of environment
        variable names, replaces @@...@@ tokens with the values of the
        environment variables.  Returns the user-data string on success
        raises exception on failure.
    """
    # Get user_data string.
    buf = read_user_data(fn)
    for e in vars:
        if not e in environ:
            raise SpinupError("Missing environment variable {}!".format(e))
        buf = template_token_subst(buf, '@@' + e + '@@', environ[e])
    return buf
Ejemplo n.º 6
0
def derive_ip_address(cidr_block, delegate, final8):
    """
        Given a CIDR block string, a delegate number, and an integer
        representing the final 8 bits of the IP address, construct and return
        the IP address derived from this values.  For example, if cidr_block is
        10.0.0.0/16, the delegate number is 10, and the final8 is 8, the
        derived IP address will be 10.0.10.8.
    """
    result = ''
    match = re.match(r'\d+\.\d+', cidr_block)
    if match:
        result = '{}.{}.{}'.format(match.group(0), delegate, final8)
    else:
        raise SpinupError(
            "{} passed to derive_ip_address() is not a CIDR block!".format(
                cidr_block))

    return result
Ejemplo n.º 7
0
def init_vpc(c, cidr):
    """
        Takes VPCConnection object (which is actually a connection to a
        particular region) and a CIDR block string. Looks for our VPC in that
        region.  Returns the boto.vpc.vpc.VPC object corresponding to our VPC.
        See: 
        http://boto.readthedocs.org/en/latest/ref/vpc.html#boto.vpc.vpc.VPC
    """
    # look for our VPC
    all_vpcs = c.get_all_vpcs()
    found = 0
    our_vpc = None
    for v in all_vpcs:
        if v.cidr_block == cidr:
            our_vpc = v
            found = 1
            break
    if not found:
        raise SpinupError("VPC {} not found".format(cidr))

    return our_vpc
Ejemplo n.º 8
0
n['cidr-block'] = yaml_lib.yaml_attr( n, 'cidr-block', None )
n['name'] = yaml_lib.yaml_attr( n, 'name', 'susecon' )
print "Looking for VPC {}".format(n['cidr-block'])
g['vpc_obj'] = init_lib.init_vpc( g['vpc_conn'], n['cidr-block'] )

#PC * Clobber existing VPC tag with YAML value ("vpc" -> "name").
init_lib.update_tag( g['vpc_obj'], 'Name', n['name'] )
print "Found VPC {} (Name: {})".format(n['cidr-block'], n['name'])

#PC * Look at YAML "subnets" and see how many there are.
subnets = yaml_lib.yaml_attr( y, 'subnets', None )
print "{} subnets in yaml".format(len(subnets))

#PC * Raise exception if there is not at least one subnet.
if 1 > len(n):
    raise SpinupError( "No subnets in yaml" )

#PC * Loop through the YAML subnet definitions.
g['subnet_obj'] = []
count = 0
for s in subnets:

    #PC * First subnet is assumed to be the "Master Subnet" (CIDR block
    #PC   defaults to 10.0.0.0/24).
    if count == 0:
        # master subnet
        s['name'] = yaml_lib.yaml_attr( s, 'delegate', '0' )
        s['cidr-block'] = yaml_lib.yaml_attr( s, 'cidr-block', '10.0.0.0/24' )
        print "Looking for master subnet {} ({})".format(s['cidr-block'], s['name'])
    #PC * All others are "Minion Subnets" (i.e. for delegates): CIDR block
    #PC   defaults to 10.0.<delegate>.0/24
Ejemplo n.º 9
0
parser = argparse.ArgumentParser( description='Wipe out a Ceph cluster in AWS.' )
parser.add_argument( 
    '--yaml', 
    default='./aws.yaml', 
    help="yaml file to read (defaults to ./aws.yaml)" 
)
parser.add_argument( 
    'delegate', 
    help="Delegate number to wipe out",
    nargs='?' 
)
args = parser.parse_args()

# Verify that delegate number was given on command line and that it is an integer.
if args.delegate is None:
    raise SpinupError( "Must provide delegate number to wipe out" )
delegate = int(args.delegate)

# Initialize dictionary for storage of globals (values that do not change,
# but are discarded when the script exits).
g = {}

#PC * Parse YAML file.
y = yaml_lib.parse_yaml( args.yaml )

#PC * Connect to region specified in YAML ("region").
# (i.e., get VPCConnection and EC2Connection objects).
# FIXME: validate that the region exists
y['region'] = yaml_lib.yaml_attr( y, 'region', 'eu-west-1' )
( g['vpc_conn'], g['ec2_conn'] ) = init_lib.init_region( y['region'] )
print "Connected to region {}".format( y['region'] )
Ejemplo n.º 10
0
#PC       group, etc.
#PC     * aws.yaml present in current directory or --yaml option provided;
#PC     * Salt Master exists and is alone in subnet 10.0.0.0/24
#PC * If the above assumptions are fulfilled, the script should work.
#PC * The following is a high-level description of what the script does.
#PC * Parse command-line arguments.
parser = argparse.ArgumentParser(description='Start a Ceph cluster in AWS.')
parser.add_argument('--yaml',
                    default='./aws.yaml',
                    help="yaml file to read (defaults to ./aws.yaml)")
parser.add_argument('delegate', help="Delegate number to start", nargs='?')
args = parser.parse_args()

# Verify that delegate number was given on command line and that it is an integer.
if args.delegate is None:
    raise SpinupError("Must provide delegate number to start")
delegate = int(args.delegate)

# Initialize dictionary for storage of globals (values that do not change,
# but are discarded when the script exits).
g = {}

#PC * Parse YAML file.
y = yaml_lib.parse_yaml(args.yaml)

#PC * Connect to region specified in YAML ("region").
# (i.e., get VPCConnection and EC2Connection objects).
# FIXME: validate that the region exists
y['region'] = yaml_lib.yaml_attr(y, 'region', 'eu-west-1')
(g['vpc_conn'], g['ec2_conn']) = init_lib.init_region(y['region'])
print "Connected to region {}".format(y['region'])