コード例 #1
0
def create_command(args):

    tier_name = get_tier_name()

    with TSTransaction() as ts:
        prep = prepare_tenant_name(ts=ts,
                                   tenant_name=vars(args)['tenant-name'],
                                   product_name=vars(args)['product-name'])
        tenant_name = prep['tenant_name']

        if ts.get_table('tenant-names').get({'tenant_name': tenant_name}):
            print "Tenant '{}' already exists. Use 'tenant refresh' command to refresh it.".format(
                tenant_name)
            sys.exit(1)

        result = define_tenant(ts=ts,
                               tenant_name=tenant_name,
                               product_name=prep['product']['product_name'],
                               tier_name=tier_name)

    print "Tenant '{}' created. Note, config is not committed!".format(
        tenant_name)
    print "State of tenant for deployables:"
    for deployable_name, state in result['report']:
        print "\t{}: {}".format(deployable_name, state)
    else:
        print "\tNo deployable associated with this tenant!"
コード例 #2
0
ファイル: cli.py プロジェクト: nonnib/drift-config
def tier_command(args):
    if args.name is None and args.action != 'info':
        print "Tier name is missing!"
        sys.exit(1)

    if args.action == 'info':
        conf = get_default_drift_config()
        if args.name is None:
            print "Tiers:"
            for tier in conf.get_table('tiers').find():
                print "\t{} state={}, is_live={}".format(
                    tier['tier_name'].ljust(21), tier['state'],
                    tier['is_live'])
        else:
            tier = conf.get_table('tiers').find({'tier_name': args.name})
            if not tier:
                print "No tier named {} found.".format(args.name)
                sys.exit(1)
            tier = tier[0]
            print "Tier {}:".format(tier['tier_name'])
            print json.dumps(tier, indent=4)

    elif args.action == 'add':
        with TSTransaction() as ts:
            tiers = ts.get_table('tiers')
            if tiers.find({'tier_name': args.name}):
                print "Tier {} already exists!".format(args.name)
                sys.exit(1)
            tiers.add({'tier_name': args.name})
    elif args.action == 'update':
        pass

    print "Done!"
コード例 #3
0
def xxxxcreate_command(args):
    tenant_name = args.tenant
    if not tenant_name:
        tenants_report()
        return

    os.environ['DRIFT_DEFAULT_TENANT'] = tenant_name

    # Minor hack:
    from drift.flaskfactory import load_flask_config

    try:
        conf = get_drift_config(
            tier_name=get_tier_name(),
            tenant_name=tenant_name,
            drift_app=load_flask_config(),
        )
    except TenantNotConfigured as e:
        raise
    except Exception as e:
        print Fore.RED + "'tenant {}' command failed: {}".format(
            args.action, e)
        return

    if not args.action:
        tenant_report(conf)
        return

    if args.action in ['create', 'recreate']:
        # Provision resources
        with TSTransaction() as ts:
            conf = get_config(ts=ts)
            resources = conf.drift_app.get("resources")
            for module_name in resources:
                m = importlib.import_module(module_name)
                if hasattr(m, "provision"):
                    provisioner_name = m.__name__.split('.')[-1]
                    print "Provisioning '%s' for tenant '%s' on tier '%s'" % (
                        provisioner_name, tenant_name, conf.tier['tier_name'])
                    if 0:  # THIS IS BONKERS LOGIC! FIIIIX!
                        conf.tier['resource_defaults'].append({
                            'resource_name':
                            provisioner_name,
                            'parameters':
                            getattr(m, 'NEW_TIER_DEFAULTS', {}),
                        })
                    recreate = 'recreate' if args.action == 'recreate' else 'skip'
                    m.provision(conf, {}, recreate=recreate)

            row = ts.get_table('tenants').get(conf.tenant)
            row['state'] = 'active'

        tenant_report(conf)
コード例 #4
0
def enable_command(args):

    tier_name = get_tier_name()
    tenant_name = vars(args)['tenant-name']

    with TSTransaction() as ts:

        results = refresh_tenants(ts=ts,
                                  tenant_name=tenant_name,
                                  tier_name=tier_name)

    results = list(results)
    print "Result:", results
コード例 #5
0
def refresh_command(args):
    tenant_name = vars(args)['tenant-name']
    tier_name = get_tier_name(fail_hard=False)

    with TSTransaction(commit_to_origin=False, write_to_scratch=False) as ts:
        print "Refreshing configuration for tenants and deployables..."
        for report in refresh_tenants(ts=ts,
                                      tenant_name=tenant_name,
                                      tier_name=tier_name):
            print report
        else:
            print "No configuration found."

    print "Hints for associating tenants and deployables:"
    print "  Make sure deployables are registered and available on a tier."
    print "  Take a look at 'drift-admin register' to register deployables."
    print "  Then run 'dconf deployable register all' for good measure."
    print "  Run 'drift-admin deployable list' to see registration."
    print(
        "  Run 'dconf product edit <product name>' and make sure your product includes all the "
        "necessary deployables.")
コード例 #6
0
    def test_ts_transaction(self):

        with TSTransaction() as ts:
            row = ts.get_table('domain').get()
            row['display_name'] += " moar! "
コード例 #7
0
def _register_command(args):

    info = get_package_info()
    conf = get_drift_config()
    name = info['name']
    is_active = not args.inactive

    print "Registering/updating deployable: ", name
    _display_package_info()

    if not is_active:
        print "Marking the deployable as inactive!"

    with TSTransaction(commit_to_origin=not args.preview) as ts:
        # Insert or update name
        row = {'deployable_name': name, 'display_name': info['description']}
        if 'long-description' in info and info['long-description'] != "UNKNOWN":
            row['description'] = info['long-cdescription']
        ts.get_table('deployable-names').update(row)

        # Make deployable (in)active on all tiers
        deployables = ts.get_table('deployables')
        for tier in ts.get_table('tiers').find():
            row = {
                'tier_name': tier['tier_name'],
                'deployable_name': name,
                'is_active': is_active
            }
            deployables.update(row)

            # Now let's do some api-router specific stuff which is by no means my concern!
            if name == 'drift-base':
                api = 'drift'
            elif name == 'themachines-backend':
                api = 'themachines'
            elif name == 'themachines-admin':
                api = 'admin'
            elif name == 'kaleo-web':
                api = 'kaleo'
            elif name == 'kards-backend':
                api = 'kards'
            else:
                api = name

            row = {
                'tier_name': tier['tier_name'],
                'deployable_name': name,
                'api': api
            }
            ts.get_table('routing').update(row)

            # Now let's do some drift-base specific stuff which is by no means my concern!
            # Generate RSA key pairs
            from cryptography.hazmat.primitives.asymmetric import rsa
            from cryptography.hazmat.primitives import serialization
            from cryptography.hazmat.backends import default_backend

            private_key = rsa.generate_private_key(public_exponent=65537,
                                                   key_size=1024,
                                                   backend=default_backend())

            public_key = private_key.public_key()

            private_pem = private_key.private_bytes(
                encoding=serialization.Encoding.PEM,
                format=serialization.PrivateFormat.TraditionalOpenSSL,
                encryption_algorithm=serialization.NoEncryption())

            public_pem = public_key.public_bytes(
                encoding=serialization.Encoding.PEM,
                format=serialization.PublicFormat.SubjectPublicKeyInfo)

            now = datetime.datetime.utcnow()
            row = {
                'tier_name':
                tier['tier_name'],
                'deployable_name':
                name,
                'keys': [{
                    'issued':
                    now.isoformat() + "Z",
                    'expires':
                    (now + datetime.timedelta(days=365)).isoformat() + "Z",
                    'public_key':
                    public_pem,
                    'private_key':
                    private_pem,
                }]
            }
            ts.get_table('public-keys').update(row)

    if args.preview:
        print "Preview changes only, not committing to origin."

    # Display the diff
    _diff_ts(ts, get_default_drift_config())