Esempio n. 1
0
def create_base_template(name,
        append_date,append_version,
        tags,description,
        provisioner,cli_args):


    version = 0
    parent = 0
    name = unicode(name)
    description = unicode(description)
    provisioner = unicode(provisioner)

    _args = []
    for arg in cli_args:
        _args.append({
            'name':unicode(arg['name']),
            'value':unicode(arg['value']),
        })
    _args = json.dumps(_args)

    
    try:
        sqlite3_conn.execute("BEGIN TRANSACTION;")
        sqlite3_conn.execute("""INSERT INTO AminationTemplates (
                `version`,
                `parent`,
                `name`,
                `description`,
                `provisioner`,
                `cli`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:provisioner,:cli,:append_date,:append_version)""",
            {
                "version" : 0,
                "parent" : 0,
                "name" : name,
                "description" : description,
                "provisioner" : provisioner,
                "cli" : _args,
                "append_date" : int(append_date),
                "append_version" : int(append_version)
            }
        )
        _last_id = sqlite3_conn.execute("SELECT last_insert_rowid()").fetchone()

        add_tags(tags,conn=sqlite3_conn)
        _tags = retrieve_tags(tags,require_all=True)
        _insert_tags = []
        for _tag in _tags:
            _insert_tags.append(
                (_tag['id'],_last_id[0],)
            )

        sqlite3_conn.executemany("INSERT INTO AminationTemplatesTags(`tag`,`template`) VALUES (?,?)",_insert_tags)

        sqlite3_conn.execute("COMMIT TRANSACTION;")
    except Exception as e:
        sqlite3_conn.execute("ROLLBACK TRANSACTION;")
        raise e
Esempio n. 2
0
def create_base_template(name, append_date, append_version, tags, description,
                         provisioner, cli_args):

    version = 0
    parent = 0
    name = unicode(name)
    description = unicode(description)
    provisioner = unicode(provisioner)

    _args = []
    for arg in cli_args:
        _args.append({
            'name': unicode(arg['name']),
            'value': unicode(arg['value']),
        })
    _args = json.dumps(_args)

    try:
        sqlite3_conn.execute("BEGIN TRANSACTION;")
        sqlite3_conn.execute(
            """INSERT INTO AminationTemplates (
                `version`,
                `parent`,
                `name`,
                `description`,
                `provisioner`,
                `cli`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:provisioner,:cli,:append_date,:append_version)""",
            {
                "version": 0,
                "parent": 0,
                "name": name,
                "description": description,
                "provisioner": provisioner,
                "cli": _args,
                "append_date": int(append_date),
                "append_version": int(append_version)
            })
        _last_id = sqlite3_conn.execute(
            "SELECT last_insert_rowid()").fetchone()

        add_tags(tags, conn=sqlite3_conn)
        _tags = retrieve_tags(tags, require_all=True)
        _insert_tags = []
        for _tag in _tags:
            _insert_tags.append((
                _tag['id'],
                _last_id[0],
            ))

        sqlite3_conn.executemany(
            "INSERT INTO AminationTemplatesTags(`tag`,`template`) VALUES (?,?)",
            _insert_tags)

        sqlite3_conn.execute("COMMIT TRANSACTION;")
    except Exception as e:
        sqlite3_conn.execute("ROLLBACK TRANSACTION;")
        raise e
Esempio n. 3
0
def start_amination(amination):
    amination = load_amination(amination)

    template = load_template(amination['template'])
    ami = load_ami(amination['amiversion'])

    _cli = list(template['cli'])
    _has_name = False
    for arg in _cli:
        if arg['name'].strip() == '-n':
            _has_name = True

    if not _cli:
        raise RuntimeError(
            "Could not determine CLI args to use! Should be stored in amination template"
        )

    if not _has_name:
        _cli.append({'name': '-n', 'value': amination['name']})

    if not ami['base_region']:
        raise RuntimeError(
            "Could not determine base region to use! Should be stored in ami version"
        )

    from_region = ami['base_region']
    _regions = []
    for region in ami['regions']:
        if region['region'] != from_region:
            _regions.append(region['region'])

    amination_dir = '/etc/drawingboard/aminations/' + amination['cache_key']
    cli_status_file = "%s/exit_code" % amination_dir
    mkdir_p(amination_dir)

    aminator_command = 'aminate ' + cli_to_str(
        _cli, escape=True) + ' ' + template['provisioner']

    to_regions = ";".join(_regions)
    command = '/etc/drawingboard/bin/drawingboard_amination \'%s\' \'%s\' \'%s\' \'%s\' -- ; echo $? > %s' % (
        amination_dir, aminator_command, from_region, to_regions,
        cli_status_file)
    cli = ["/bin/bash", "-c", command]

    _process = subprocess.Popen(cli,
                                stdin=None,
                                stdout=None,
                                stderr=None,
                                close_fds=True)

    sqlite3_conn.execute(
        "UPDATE Aminations SET started = 1 WHERE id = :id LIMIT 1;",
        {"id": amination['id']})
Esempio n. 4
0
def create_base_version(template, name, append_date, append_version, tags,
                        description):

    template = load_base_template(template)

    if not template:
        raise RuntimeError("Unable to find base template with id %s" %
                           template)

    version = 0
    parent = 0
    name = unicode(name)
    description = unicode(description)

    try:
        sqlite3_conn.execute("BEGIN TRANSACTION;")
        sqlite3_conn.execute(
            """INSERT INTO AMIVersions (
                `version`,
                `parent`,
                `template`,
                `name`,
                `description`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:template,:name,:description,:append_date,:append_version)""",
            {
                "version": 0,
                "parent": 0,
                "template": template['id'],
                "name": name,
                "description": description,
                "append_date": int(append_date),
                "append_version": int(append_version)
            })
        _last_id = sqlite3_conn.execute(
            "SELECT last_insert_rowid()").fetchone()

        add_tags(tags, conn=sqlite3_conn)
        _tags = retrieve_tags(tags, require_all=True)
        _insert_tags = []
        for _tag in _tags:
            _insert_tags.append((
                _tag['id'],
                _last_id[0],
            ))

        sqlite3_conn.executemany(
            "INSERT INTO AMIVersionsTags(`tag`,`amiversion`) VALUES (?,?)",
            _insert_tags)

        sqlite3_conn.execute("COMMIT TRANSACTION;")
    except Exception as e:
        sqlite3_conn.execute("ROLLBACK TRANSACTION;")
        raise e
Esempio n. 5
0
def all_regions():
    with sqlite3_conn:
        regions = sqlite3_conn.execute("""SELECT AWSRegions.*, AMIVersionsRegions.amiversion,AMIVersionsRegions.base AS base FROM AMIVersionsRegions 
            INNER JOIN AWSRegions ON AMIVersionsRegions.region = AWSRegions.id"""
        ).fetchall()

    return regions
Esempio n. 6
0
def all_tags():
    with sqlite3_conn:
        tags = sqlite3_conn.execute(
            """SELECT Tags.*, AMIVersionsTags.amiversion FROM AMIVersionsTags 
            INNER JOIN Tags ON AMIVersionsTags.tag = Tags.id""").fetchall()

    return tags
Esempio n. 7
0
def all_tags():
    with sqlite3_conn:
        tags = sqlite3_conn.execute("""SELECT Tags.*, AminationTemplatesTags.template FROM AminationTemplatesTags 
            INNER JOIN Tags ON AminationTemplatesTags.tag = Tags.id"""
        ).fetchall()

    return tags
Esempio n. 8
0
def load_base(base):
    with sqlite3_conn:
        base = sqlite3_conn.execute("SELECT * FROM AminationTemplates WHERE parent = 0 AND id = :id LIMIT 1;",{"id":base}).fetchone()

    tags = tags_for_template(base['id'])

    return formatted_row(base,tags)
Esempio n. 9
0
def all_versions():
    with sqlite3_conn:
        bases = sqlite3_conn.execute("SELECT * FROM AminationTemplates WHERE parent != 0 ORDER BY id,created ASC").fetchall()

    tags = all_tags()
    _tags = {}
    for tag in tags:
        _id = int(tag['template'])
        if _id not in _tags:
            _tags[_id] = []   
        
        _tags[_id].append(tag)

    _bases = []
    for base in bases:

        _t = []
        try:
            _t = _tags[int(base['id'])]
        except:
            pass

        _bases.append(formatted_row(
            base,
            _t
        ))

    return _bases
Esempio n. 10
0
def load(amination):
    with sqlite3_conn:
        base = sqlite3_conn.execute(
            "SELECT * FROM Aminations WHERE id = :amination LIMIT 1;", {
                "amination": amination
            }).fetchone()
    return _load(base)
Esempio n. 11
0
def all_tags():
    with sqlite3_conn:
        tags = sqlite3_conn.execute("""SELECT Tags.*, AMIVersionsTags.amiversion FROM AMIVersionsTags 
            INNER JOIN Tags ON AMIVersionsTags.tag = Tags.id"""
        ).fetchall()

    return tags
Esempio n. 12
0
def versions_for_base(parent):
    with sqlite3_conn:
        versions = sqlite3_conn.execute(
            "SELECT * FROM AminationTemplates WHERE parent = :id ORDER BY version,created ASC",
            {"id":parent}
        ).fetchall()

    tags = all_tags()
    _tags = {}
    for tag in tags:
        _id = int(tag['template'])
        if _id not in _tags:
            _tags[_id] = []   
        
        _tags[_id].append(tag)

    _versions = []
    for version in versions:
        _t = []
        try:
            _t = _tags[int(base['id'])]
        except:
            pass

        _versions.append(formatted_row(
            version,
            _t
        ))

    return _versions
Esempio n. 13
0
def create_base_version(template,
        name,append_date,append_version,
        tags,description):

    
    template = load_base_template(template)

    if not template:
        raise RuntimeError("Unable to find base template with id %s" % template)

    version = 0
    parent = 0
    name = unicode(name)
    description = unicode(description)
    
    try:
        sqlite3_conn.execute("BEGIN TRANSACTION;")
        sqlite3_conn.execute("""INSERT INTO AMIVersions (
                `version`,
                `parent`,
                `template`,
                `name`,
                `description`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:template,:name,:description,:append_date,:append_version)""",
            {
                "version" : 0,
                "parent" : 0,
                "template" : template['id'],
                "name" : name,
                "description" : description,
                "append_date" : int(append_date),
                "append_version" : int(append_version)
            }
        )
        _last_id = sqlite3_conn.execute("SELECT last_insert_rowid()").fetchone()

        add_tags(tags,conn=sqlite3_conn)
        _tags = retrieve_tags(tags,require_all=True)
        _insert_tags = []
        for _tag in _tags:
            _insert_tags.append(
                (_tag['id'],_last_id[0],)
            )

        sqlite3_conn.executemany("INSERT INTO AMIVersionsTags(`tag`,`amiversion`) VALUES (?,?)",_insert_tags)

        sqlite3_conn.execute("COMMIT TRANSACTION;")
    except Exception as e:
        sqlite3_conn.execute("ROLLBACK TRANSACTION;")
        raise e
Esempio n. 14
0
def tags_for_version(version):
    with sqlite3_conn:
        tags = sqlite3_conn.execute("""SELECT Tags.* FROM AMIVersionsTags 
            INNER JOIN Tags ON AMIVersionsTags.tag = Tags.id WHERE AMIVersionsTags.amiversion = :id""",
            {"id":version}
        ).fetchall()

    return tags
Esempio n. 15
0
def tags_for_template(template):
    with sqlite3_conn:
        tags = sqlite3_conn.execute("""SELECT Tags.* FROM AminationTemplatesTags 
            INNER JOIN Tags ON AminationTemplatesTags.tag = Tags.id WHERE AminationTemplatesTags.template = :id""",
            {"id":template}
        ).fetchall()

    return tags
Esempio n. 16
0
def all_tags():
    tags = sqlite3_conn.execute("SELECT * FROM Tags").fetchall()

    _tags = []
    for tag in tags:
        _tags.append(formatted_row(tag))

    return _tags
Esempio n. 17
0
def regions_for_version(version):
    with sqlite3_conn:
        regions = sqlite3_conn.execute("""SELECT AWSRegions.*,AMIVersionsRegions.base AS base FROM AMIVersionsRegions 
            INNER JOIN AWSRegions ON AMIVersionsRegions.region = AWSRegions.id WHERE AMIVersionsRegions.amiversion = :id""",
            {"id":version}
        ).fetchall()

    return regions
Esempio n. 18
0
def all_regions():
    with sqlite3_conn:
        regions = sqlite3_conn.execute(
            """SELECT AWSRegions.*, AMIVersionsRegions.amiversion,AMIVersionsRegions.base AS base FROM AMIVersionsRegions 
            INNER JOIN AWSRegions ON AMIVersionsRegions.region = AWSRegions.id"""
        ).fetchall()

    return regions
Esempio n. 19
0
def all_bases():
    with sqlite3_conn:
        bases = sqlite3_conn.execute(
            "SELECT * FROM AMIVersions WHERE parent = 0 ORDER BY id,created ASC"
        ).fetchall()

    tags = all_tags()
    _tags = {}
    for tag in tags:
        _id = int(tag['amiversion'])
        if _id not in _tags:
            _tags[_id] = []

        _tags[_id].append(tag)

    regions = all_regions()
    _regions = {}
    for region in regions:

        _id = int(region['amiversion'])
        if _id not in _regions:
            _regions[_id] = []

        _regions[_id].append(region)

    _all_versions = all_versions()
    _versions = {}
    for _v in _all_versions:
        _id = int(_v['parent'])
        if _id not in _versions:
            _versions[_id] = []

        _versions[_id].append(_v)

    _bases = []
    for base in bases:

        _t = []
        try:
            _t = _tags[int(base['id'])]
        except:
            pass

        _r = []
        try:
            _r = _regions[int(base['id'])]
        except:
            pass

        _v = []
        try:
            _v = _versions[int(base['id'])]
        except:
            pass

        _bases.append(formatted_row(base, _t, _r, _v))

    return _bases
Esempio n. 20
0
def load_version(base, version):
    with sqlite3_conn:
        base = sqlite3_conn.execute(
            "SELECT * FROM Aminations WHERE id = :version AND parent = :base LIMIT 1;",
            {
                "base": base,
                "version": version
            }).fetchone()
    return _load(base)
Esempio n. 21
0
def load_version(base,version):
    with sqlite3_conn:
        base = sqlite3_conn.execute("SELECT * FROM AminationTemplates WHERE parent = :parent AND id = :version LIMIT 1;",
            {"parent":base,"version":version}
        ).fetchone()

    tags = tags_for_template(base['id'])

    return formatted_row(base,tags)
Esempio n. 22
0
def load(template):
    with sqlite3_conn:
        base = sqlite3_conn.execute("SELECT * FROM AminationTemplates WHERE id = :template LIMIT 1;",
            {"template":template}
        ).fetchone()

    tags = tags_for_template(base['id'])

    return formatted_row(base,tags)
Esempio n. 23
0
def load_base(base):
    with sqlite3_conn:
        base = sqlite3_conn.execute("SELECT * FROM AMIVersions WHERE parent = 0 AND id = :id LIMIT 1;",{"id":base}).fetchone()

    tags = tags_for_version(base['id'])
    regions = regions_for_version(base['id'])
    versions = versions_for_base(base['id'])

    return formatted_row(base,tags,regions,versions)
Esempio n. 24
0
def load_version(base,version):
    with sqlite3_conn:
        base = sqlite3_conn.execute("SELECT * FROM AMIVersions WHERE parent = :base AND id = :version LIMIT 1;",
            {"base":base,"version":version}
        ).fetchone()

    tags = tags_for_version(base['id'])
    regions = regions_for_version(base['id'])

    return formatted_row(base,tags,regions,[])
Esempio n. 25
0
def tags_for_version(version):
    with sqlite3_conn:
        tags = sqlite3_conn.execute(
            """SELECT Tags.* FROM AMIVersionsTags 
            INNER JOIN Tags ON AMIVersionsTags.tag = Tags.id WHERE AMIVersionsTags.amiversion = :id""",
            {
                "id": version
            }).fetchall()

    return tags
Esempio n. 26
0
def all_base_aminations():
    with sqlite3_conn:
        bases = sqlite3_conn.execute("SELECT * FROM Aminations WHERE parent = 0 ORDER BY id,created ASC").fetchall()

    _bases = []
    for base in bases:

        _bases.append(_load(base))

    return _bases
Esempio n. 27
0
def regions_for_version(version):
    with sqlite3_conn:
        regions = sqlite3_conn.execute(
            """SELECT AWSRegions.*,AMIVersionsRegions.base AS base FROM AMIVersionsRegions 
            INNER JOIN AWSRegions ON AMIVersionsRegions.region = AWSRegions.id WHERE AMIVersionsRegions.amiversion = :id""",
            {
                "id": version
            }).fetchall()

    return regions
Esempio n. 28
0
def retrieve_tags(tags,require_all=True):
    _tags = []
    for tag in tags:
        _tag = sqlite3_conn.execute("SELECT * FROM Tags WHERE tag = :tag LIMIT 1;",{"tag":tag}).fetchone()
        if not _tag and require_all:
            raise RuntimeError("Missing tag %s" % tag)
                
        if _tag:
            _tags.append(_tag)

    return _tags
Esempio n. 29
0
def aminations_for_base(base):
    with sqlite3_conn:
        bases = sqlite3_conn.execute("SELECT * FROM Aminations WHERE parent = :base ORDER BY id,created ASC",
            {"base":base}
        ).fetchall()

    _bases = []
    for base in bases:

        _bases.append(_load(base))

    return _bases
Esempio n. 30
0
def load(base):
    with sqlite3_conn:
        base = sqlite3_conn.execute(
            "SELECT * FROM AMIVersions WHERE id = :id LIMIT 1;", {
                "id": base
            }).fetchone()

    tags = tags_for_version(base['id'])
    regions = regions_for_version(base['id'])
    versions = versions_for_base(base['id'])

    return formatted_row(base, tags, regions, versions)
Esempio n. 31
0
def all_base_aminations():
    with sqlite3_conn:
        bases = sqlite3_conn.execute(
            "SELECT * FROM Aminations WHERE parent = 0 ORDER BY id,created ASC"
        ).fetchall()

    _bases = []
    for base in bases:

        _bases.append(_load(base))

    return _bases
Esempio n. 32
0
def load_version(base, version):
    with sqlite3_conn:
        base = sqlite3_conn.execute(
            "SELECT * FROM AMIVersions WHERE parent = :base AND id = :version LIMIT 1;",
            {
                "base": base,
                "version": version
            }).fetchone()

    tags = tags_for_version(base['id'])
    regions = regions_for_version(base['id'])

    return formatted_row(base, tags, regions, [])
Esempio n. 33
0
def aminations_for_base(base):
    with sqlite3_conn:
        bases = sqlite3_conn.execute(
            "SELECT * FROM Aminations WHERE parent = :base ORDER BY id,created ASC",
            {
                "base": base
            }).fetchall()

    _bases = []
    for base in bases:

        _bases.append(_load(base))

    return _bases
Esempio n. 34
0
def retrieve_regions(regions, require_all=True):
    _regions = []
    for region in set(regions):
        _region = sqlite3_conn.execute(
            "SELECT * FROM AWSRegions WHERE region = :region LIMIT 1;", {
                "region": region
            }).fetchone()
        if not _region and require_all:
            raise RuntimeError("Missing region %s" % region)

        if _region:
            _regions.append(_region)

    return _regions
Esempio n. 35
0
def versions_for_base(parent):
    with sqlite3_conn:
        versions = sqlite3_conn.execute(
            "SELECT * FROM AMIVersions WHERE parent = :id ORDER BY version,created ASC",
            {"id":parent}
        ).fetchall()

    tags = all_tags()
    _tags = {}
    for tag in tags:
        _id = int(tag['amiversion'])
        if _id not in _tags:
            _tags[_id] = []   
        
        _tags[_id].append(tag)


    regions = all_regions()
    _regions = {}
    for region in regions:
        
        _id = int(region['amiversion'])
        if _id not in _regions:
            _regions[_id] = []   
        
        _regions[_id].append(region)

    _versions = []
    for version in versions:
        _t = []
        try:
            _t = _tags[int(version['id'])]
        except:
            pass

        _r = []
        try:
            _r = _regions[int(version['id'])]
        except:
            pass

        _versions.append(formatted_row(
            version,
            _t,
            _r,
            -1
        ))

    return _versions
Esempio n. 36
0
def versions_for_base(parent):
    with sqlite3_conn:
        versions = sqlite3_conn.execute(
            "SELECT * FROM AMIVersions WHERE parent = :id ORDER BY version,created ASC",
            {
                "id": parent
            }).fetchall()

    tags = all_tags()
    _tags = {}
    for tag in tags:
        _id = int(tag['amiversion'])
        if _id not in _tags:
            _tags[_id] = []

        _tags[_id].append(tag)

    regions = all_regions()
    _regions = {}
    for region in regions:

        _id = int(region['amiversion'])
        if _id not in _regions:
            _regions[_id] = []

        _regions[_id].append(region)

    _versions = []
    for version in versions:
        _t = []
        try:
            _t = _tags[int(version['id'])]
        except:
            pass

        _r = []
        try:
            _r = _regions[int(version['id'])]
        except:
            pass

        _versions.append(formatted_row(version, _t, _r, -1))

    return _versions
Esempio n. 37
0
def add_tags(tags,conn=None):
    if not isinstance(tags,list):
        tags = [tags]

    _args = []
    for tag in tags:
        _args.append(
            (tag,)
        )
    if not conn:
        try:
            sqlite3_conn.execute("BEGIN TRANSACTION;")
            sqlite3_conn.executemany(
                "INSERT INTO Tags(`tag`) VALUES (?)",_args)
            sqlite3_conn.execute("COMMIT;")
        except Exception as e:
            sqlite3_conn.execute("ROLLBACK;")
            raise e

    else:
        conn.executemany("INSERT INTO Tags(`tag`) VALUES (?)",_args)
Esempio n. 38
0
def all_bases():
    with sqlite3_conn:
        bases = sqlite3_conn.execute("SELECT * FROM AMIVersions WHERE parent = 0 ORDER BY id,created ASC").fetchall()

    tags = all_tags()
    _tags = {}
    for tag in tags:
        _id = int(tag['amiversion'])
        if _id not in _tags:
            _tags[_id] = []   
        
        _tags[_id].append(tag)


    regions = all_regions()
    _regions = {}
    for region in regions:

        _id = int(region['amiversion'])
        if _id not in _regions:
            _regions[_id] = []   
        
        _regions[_id].append(region)

    _all_versions = all_versions()
    _versions = {}
    for _v in _all_versions:
        _id = int(_v['parent'])
        if _id not in _versions:
            _versions[_id] = []
        
        _versions[_id].append(_v)


    _bases = []
    for base in bases:

        _t = []
        try:
            _t = _tags[int(base['id'])]
        except:
            pass

        _r = []
        try:
            _r = _regions[int(base['id'])]
        except:
            pass

        _v = []
        try:
            _v = _versions[int(base['id'])]
        except:
            pass

        _bases.append(formatted_row(
            base,
            _t,
            _r,
            _v
        ))

    return _bases
Esempio n. 39
0
def create_template_version(parent,provisioner=None,cli_args=None):

    parent_template = load_base(parent)
    tags = tags_for_template(parent)

    try:
        sqlite3_conn.execute('BEGIN EXCLUSIVE TRANSACTION;')

        versions = sqlite3_conn.execute("SELECT * FROM AminationTemplates WHERE parent = :id ORDER BY id DESC LIMIT 1;",{"id":parent}).fetchone()

        if not versions:
            version = 1
        else:
            version = _increment_version(versions['version'])

        parent = parent_template['id']
        name = parent_template['name']
        description = parent_template['description']
        if provisioner is None:
            provisioner = parent_template['provisioner']
        else:
            provisioner = unicode(provisioner)

        if cli_args is None:
            cli_args = parent_template['cli']
        else:
            _args = []
            for arg in cli_args:
                _args.append({
                    'name':unicode(arg['name']),
                    'value':unicode(arg['value']),
                })
            cli_args = json.dumps(_args)

        append_date = parent_template['append_date']
        append_version = parent_template['append_version']

        sqlite3_conn.execute("""INSERT INTO AminationTemplates (
                `version`,
                `parent`,
                `name`,
                `description`,
                `provisioner`,
                `cli`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:provisioner,:cli,:append_date,:append_version)""",
            {
                "version" : version,
                "parent" : parent,
                "name" : name,
                "description" : description,
                "provisioner" : provisioner,
                "cli" : cli_args,
                "append_date" : int(append_date),
                "append_version" : int(append_version)
            }
        )

        _last_id = sqlite3_conn.execute("SELECT last_insert_rowid()").fetchone()

        _insert_tags = []
        for _tag in tags:
            _insert_tags.append(
                (_tag['id'],_last_id[0],)
            )

        sqlite3_conn.executemany("INSERT INTO AminationTemplatesTags(`tag`,`template`) VALUES (?,?)",_insert_tags)

        sqlite3_conn.execute('COMMIT TRANSACTION;')
    except Exception as e:
        sqlite3_conn.execute('ROLLBACK TRANSACTION;')
        raise e
Esempio n. 40
0
def create_template_version(parent, provisioner=None, cli_args=None):

    parent_template = load_base(parent)
    tags = tags_for_template(parent)

    try:
        sqlite3_conn.execute('BEGIN EXCLUSIVE TRANSACTION;')

        versions = sqlite3_conn.execute(
            "SELECT * FROM AminationTemplates WHERE parent = :id ORDER BY id DESC LIMIT 1;",
            {
                "id": parent
            }).fetchone()

        if not versions:
            version = 1
        else:
            version = _increment_version(versions['version'])

        parent = parent_template['id']
        name = parent_template['name']
        description = parent_template['description']
        if provisioner is None:
            provisioner = parent_template['provisioner']
        else:
            provisioner = unicode(provisioner)

        if cli_args is None:
            cli_args = parent_template['cli']
        else:
            _args = []
            for arg in cli_args:
                _args.append({
                    'name': unicode(arg['name']),
                    'value': unicode(arg['value']),
                })
            cli_args = json.dumps(_args)

        append_date = parent_template['append_date']
        append_version = parent_template['append_version']

        sqlite3_conn.execute(
            """INSERT INTO AminationTemplates (
                `version`,
                `parent`,
                `name`,
                `description`,
                `provisioner`,
                `cli`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:provisioner,:cli,:append_date,:append_version)""",
            {
                "version": version,
                "parent": parent,
                "name": name,
                "description": description,
                "provisioner": provisioner,
                "cli": cli_args,
                "append_date": int(append_date),
                "append_version": int(append_version)
            })

        _last_id = sqlite3_conn.execute(
            "SELECT last_insert_rowid()").fetchone()

        _insert_tags = []
        for _tag in tags:
            _insert_tags.append((
                _tag['id'],
                _last_id[0],
            ))

        sqlite3_conn.executemany(
            "INSERT INTO AminationTemplatesTags(`tag`,`template`) VALUES (?,?)",
            _insert_tags)

        sqlite3_conn.execute('COMMIT TRANSACTION;')
    except Exception as e:
        sqlite3_conn.execute('ROLLBACK TRANSACTION;')
        raise e
Esempio n. 41
0
def create_amination_version(amination_base, ami_version, start=False):
    parent_amination = load_base_amination(amination_base)

    ami_version = load_ami_version(parent_amination['amiversion'], ami_version)
    template = load_template_version(parent_amination['template'],
                                     ami_version['template'])
    _id = None

    try:
        sqlite3_conn.execute('BEGIN EXCLUSIVE TRANSACTION;')
        parent = parent_amination['id']

        versions = sqlite3_conn.execute(
            "SELECT * FROM Aminations WHERE parent = :id ORDER BY id DESC LIMIT 1;",
            {
                "id": parent
            }).fetchone()

        if not versions:
            version = 1
        else:
            version = _increment_version(versions['version'])

        name = parent_amination['name']
        description = parent_amination['description']
        append_date = parent_amination['append_date']
        append_version = parent_amination['append_version']

        sqlite3_conn.execute(
            """INSERT INTO Aminations (
                `version`,
                `parent`,
                `name`,
                `description`,
                `started`,
                `template`,
                `amiversion`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:started,:template,:amiversion,:append_date,:append_version)""",
            {
                "version": version,
                "parent": parent,
                "name": name,
                "description": description,
                "started": False,
                "template": template['id'],
                "amiversion": ami_version['id'],
                "append_date": append_date,
                "append_version": append_version
            })

        _last_id = sqlite3_conn.execute(
            "SELECT last_insert_rowid()").fetchone()
        cache_key = parent_amination['cache_key'] + "_" + str(_last_id[0])

        sqlite3_conn.execute(
            "UPDATE Aminations SET cache_key = :cache_key WHERE id = :id", {
                "cache_key": cache_key,
                "id": _last_id[0]
            })

        _id = _last_id[0]
        sqlite3_conn.execute("COMMIT;")

    except Exception as e:
        import traceback
        traceback.print_exc()
        sqlite3_conn.execute("ROLLBACK;")
        raise e

    if start:
        if _id is None:
            raise RuntimeError(
                "Failed to start amination, unable to determine ID!")

        start_amination(_id)
Esempio n. 42
0
def create_base_amination(name,description,ami_version_base):

    name = unicode(name)
    description = unicode(description)

    ami_base = load_ami_base(ami_version_base)

    template = load_template_base(ami_base['template'])

    try:
        sqlite3_conn.execute("BEGIN TRANSACTION;")
        sqlite3_conn.execute("""INSERT INTO Aminations (
                `version`,
                `parent`,
                `name`,
                `description`,
                `started`,
                `template`,
                `amiversion`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:started,:template,:amiversion,:append_date,:append_version)""",
            {
                "version" : 0,
                "parent" : 0,
                "name" : name,
                "description" : description,
                "started" : False,
                "template" : template['id'],
                "amiversion" : ami_base['id'],
                "append_date" : True,
                "append_version" : True
            }
        )

        _last_id = sqlite3_conn.execute("SELECT last_insert_rowid()").fetchone()
        cache_key = "amination_"+str(_last_id[0])

        sqlite3_conn.execute("UPDATE Aminations SET cache_key = :cache_key WHERE id = :id",
            {
                "cache_key" : cache_key,
                "id" : _last_id[0]
            }
        )

        sqlite3_conn.execute("COMMIT;")
        return _last_id[0]
    except Exception as e:
        sqlite3_conn.execute("ROLLBACK;")
        raise e

    return None
Esempio n. 43
0
def create_amination_version(amination_base,ami_version,start=False):
    parent_amination = load_base_amination(amination_base)

    ami_version = load_ami_version(parent_amination['amiversion'],ami_version)
    template = load_template_version(parent_amination['template'],ami_version['template'])
    _id = None

    try:
        sqlite3_conn.execute('BEGIN EXCLUSIVE TRANSACTION;')
        parent = parent_amination['id']

        versions = sqlite3_conn.execute("SELECT * FROM Aminations WHERE parent = :id ORDER BY id DESC LIMIT 1;",{"id":parent}).fetchone()

        if not versions:
            version = 1
        else:
            version = _increment_version(versions['version'])

        name = parent_amination['name']
        description = parent_amination['description']
        append_date = parent_amination['append_date']
        append_version = parent_amination['append_version']
        

        sqlite3_conn.execute("""INSERT INTO Aminations (
                `version`,
                `parent`,
                `name`,
                `description`,
                `started`,
                `template`,
                `amiversion`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:started,:template,:amiversion,:append_date,:append_version)""",
            {
                "version" : version,
                "parent" : parent,
                "name" : name,
                "description" : description,
                "started" : False,
                "template" : template['id'],
                "amiversion" : ami_version['id'],
                "append_date" : append_date,
                "append_version" : append_version
            }
        )

        _last_id = sqlite3_conn.execute("SELECT last_insert_rowid()").fetchone()
        cache_key = parent_amination['cache_key']+"_"+str(_last_id[0])

        sqlite3_conn.execute("UPDATE Aminations SET cache_key = :cache_key WHERE id = :id",
            {
                "cache_key" : cache_key,
                "id" : _last_id[0]
            }
        )

        _id = _last_id[0] 
        sqlite3_conn.execute("COMMIT;")

    except Exception as e:
        import traceback
        traceback.print_exc()
        sqlite3_conn.execute("ROLLBACK;")
        raise e

    if start:
        if _id is None:
            raise RuntimeError("Failed to start amination, unable to determine ID!")

        start_amination(_id)
Esempio n. 44
0
def create_base_amination(name, description, ami_version_base):

    name = unicode(name)
    description = unicode(description)

    ami_base = load_ami_base(ami_version_base)

    template = load_template_base(ami_base['template'])

    try:
        sqlite3_conn.execute("BEGIN TRANSACTION;")
        sqlite3_conn.execute(
            """INSERT INTO Aminations (
                `version`,
                `parent`,
                `name`,
                `description`,
                `started`,
                `template`,
                `amiversion`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:name,:description,:started,:template,:amiversion,:append_date,:append_version)""",
            {
                "version": 0,
                "parent": 0,
                "name": name,
                "description": description,
                "started": False,
                "template": template['id'],
                "amiversion": ami_base['id'],
                "append_date": True,
                "append_version": True
            })

        _last_id = sqlite3_conn.execute(
            "SELECT last_insert_rowid()").fetchone()
        cache_key = "amination_" + str(_last_id[0])

        sqlite3_conn.execute(
            "UPDATE Aminations SET cache_key = :cache_key WHERE id = :id", {
                "cache_key": cache_key,
                "id": _last_id[0]
            })

        sqlite3_conn.execute("COMMIT;")
        return _last_id[0]
    except Exception as e:
        sqlite3_conn.execute("ROLLBACK;")
        raise e

    return None
Esempio n. 45
0
def load_base(amination):
    with sqlite3_conn:
        base = sqlite3_conn.execute("SELECT * FROM Aminations WHERE id = :amination AND parent = 0 LIMIT 1;",
            {"amination":amination}
        ).fetchone()
    return _load(base)
Esempio n. 46
0
def create_version_version(parent, template, base_region, regions):

    parent_version = load_base(parent)

    template = load_template_version(parent_version['template'], template)

    if not template:
        raise RuntimeError("Unable to find base template with id %s" %
                           template)

    tags = tags_for_version(parent)

    regions.append(base_region)
    regions = retrieve_regions(regions, require_all=True)

    try:
        sqlite3_conn.execute('BEGIN EXCLUSIVE TRANSACTION;')

        versions = sqlite3_conn.execute(
            "SELECT * FROM AMIVersions WHERE parent = :id ORDER BY id DESC LIMIT 1;",
            {
                "id": parent
            }).fetchone()

        if not versions:
            version = 1
        else:
            version = _increment_version(versions['version'])

        parent = parent_version['id']
        name = parent_version['name']
        description = parent_version['description']

        append_date = parent_version['append_date']
        append_version = parent_version['append_version']

        sqlite3_conn.execute(
            """INSERT INTO AMIVersions (
                `version`,
                `parent`,
                `template`,
                `name`,
                `description`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:template,:name,:description,:append_date,:append_version)""",
            {
                "version": version,
                "parent": parent,
                "template": template['id'],
                "name": name,
                "description": description,
                "append_date": int(append_date),
                "append_version": int(append_version)
            })
        _last_id = sqlite3_conn.execute(
            "SELECT last_insert_rowid()").fetchone()

        _insert_tags = []
        for _tag in tags:
            _insert_tags.append((
                _tag['id'],
                _last_id[0],
            ))

        sqlite3_conn.executemany(
            "INSERT INTO AMIVersionsTags(`tag`,`amiversion`) VALUES (?,?)",
            _insert_tags)

        _insert_regions = []
        for _region in regions:
            base = 0
            if _region['region'] == base_region:
                base = 1

            _insert_regions.append((
                _region['id'],
                _last_id[0],
                base,
            ))

        sqlite3_conn.executemany(
            "INSERT INTO AMIVersionsRegions(`region`,`amiversion`,`base`) VALUES (?,?,?)",
            _insert_regions)

        sqlite3_conn.execute('COMMIT TRANSACTION;')
    except Exception as e:
        sqlite3_conn.execute('ROLLBACK TRANSACTION;')
        raise e
Esempio n. 47
0
def start_amination(amination):
    amination = load_amination(amination)

    template = load_template(amination['template'])
    ami = load_ami(amination['amiversion'])

    _cli = list(template['cli'])
    _has_name = False
    for arg in _cli:
        if arg['name'].strip() == '-n':
            _has_name = True

    if not _cli:
        raise RuntimeError("Could not determine CLI args to use! Should be stored in amination template")

    if not _has_name:
        _cli.append({
            'name' : '-n',
            'value' : amination['name']
        })

    if not ami['base_region']:
        raise RuntimeError("Could not determine base region to use! Should be stored in ami version")

    from_region=ami['base_region']
    _regions = []
    for region in ami['regions']:
        if region['region'] != from_region:
            _regions.append(region['region'])


    amination_dir = '/etc/drawingboard/aminations/'+amination['cache_key']
    cli_status_file = "%s/exit_code" % amination_dir
    mkdir_p(amination_dir)

    aminator_command = 'aminate '+cli_to_str(_cli,escape=True)+' '+template['provisioner']
    
    to_regions=";".join(_regions)
    command = '/etc/drawingboard/bin/drawingboard_amination \'%s\' \'%s\' \'%s\' \'%s\' -- ; echo $? > %s' % (
        amination_dir,
        aminator_command,
        from_region,
        to_regions,
        cli_status_file
    )
    cli = [
        "/bin/bash",
        "-c",
        command
    ]

    _process = subprocess.Popen(
        cli,
        stdin=None,
        stdout=None,
        stderr=None,
        close_fds=True
    )

    sqlite3_conn.execute("UPDATE Aminations SET started = 1 WHERE id = :id LIMIT 1;",
        {
            "id" : amination['id']
        }
    )
Esempio n. 48
0
def create_version_version(parent,template,base_region,regions):
    
    parent_version = load_base(parent)

    template = load_template_version(parent_version['template'],template)

    if not template:
        raise RuntimeError("Unable to find base template with id %s" % template)

    tags = tags_for_version(parent)

    regions.append(base_region)
    regions = retrieve_regions(regions,require_all=True)

    try:
        sqlite3_conn.execute('BEGIN EXCLUSIVE TRANSACTION;')

        versions = sqlite3_conn.execute("SELECT * FROM AMIVersions WHERE parent = :id ORDER BY id DESC LIMIT 1;",{"id":parent}).fetchone()

        if not versions:
            version = 1
        else:
            version = _increment_version(versions['version'])

        parent = parent_version['id']
        name = parent_version['name']
        description = parent_version['description']

        append_date = parent_version['append_date']
        append_version = parent_version['append_version']

        sqlite3_conn.execute("""INSERT INTO AMIVersions (
                `version`,
                `parent`,
                `template`,
                `name`,
                `description`,
                `append_date`,
                `append_version`
            ) VALUES (:version,:parent,:template,:name,:description,:append_date,:append_version)""",
            {
                "version" : version,
                "parent" : parent,
                "template" : template['id'],
                "name" : name,
                "description" : description,
                "append_date" : int(append_date),
                "append_version" : int(append_version)
            }
        )
        _last_id = sqlite3_conn.execute("SELECT last_insert_rowid()").fetchone()

        _insert_tags = []
        for _tag in tags:
            _insert_tags.append(
                (_tag['id'],_last_id[0],)
            )

        sqlite3_conn.executemany("INSERT INTO AMIVersionsTags(`tag`,`amiversion`) VALUES (?,?)",_insert_tags)

        _insert_regions = []
        for _region in regions:
            base = 0
            if _region['region'] == base_region:
                base = 1
            
            _insert_regions.append(
                (_region['id'],_last_id[0],base,)
            )

        sqlite3_conn.executemany("INSERT INTO AMIVersionsRegions(`region`,`amiversion`,`base`) VALUES (?,?,?)",_insert_regions)


        sqlite3_conn.execute('COMMIT TRANSACTION;')
    except Exception as e:
        sqlite3_conn.execute('ROLLBACK TRANSACTION;')
        raise e
Esempio n. 49
0
def load_version(base,version):
    with sqlite3_conn:
        base = sqlite3_conn.execute("SELECT * FROM Aminations WHERE id = :version AND parent = :base LIMIT 1;",
            {"base":base,"version":version}
        ).fetchone()
    return _load(base)