示例#1
0
def main():
    parser = argparse.ArgumentParser(
        description="Migrate sysadmin keys",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("migration_file",
                        type=str,
                        help="Absolute path to migration file or directory")
    parser.add_argument("--migration_logs",
                        type=str,
                        default="%s/migration_logs" % os.getcwd(),
                        help="log file containing a record of migrations")
    parser.add_argument("--port", type=int, default=4000, help="sysadmin port")
    args = parser.parse_args()

    migrations = []
    try:
        migrations = load_migrations(args.migration_file)
    except ValueError as e:
        print("INFO: %s" % e)

    if len(migrations) > 0:
        migration_log = MigrationLog()
        if os.path.exists(args.migration_logs):
            migration_log.load(args.migration_logs)

        client = SysAdminClient('localhost', args.port)
        migrator = SysAdminMigrator(client)
        migration_history = []

        for i, m in enumerate(migrations):
            if migration_log.check_history(m[1]):
                print("Migration %s has already run, skipping..." % m[0])
                continue
            print("Running migration %s" % (m[0]))
            resp = migrator.migrate(dict(m[1]))
            migration_history.append((m[1], resp.commit.commit_id))
            if resp.status != sysadminctl_pb2.SUCCESS and i == len(m) - 1:
                print("Migration failed, quitting and rolling back")
                migration_history.reverse()
                for migration, cid in migration_history:
                    client.rollback(cid)
                    migration_log.redo_migration(migration)
                return 1
            migration_log.log_migration(m[1])

        migration_log.save(args.migration_logs)

        print("Migrations complete, running hooks")
        if len(migration_history) > 0:
            print(client.firehooks())

        print("Hooks complete, finished migrating")
    else:
        print("No migrations to run")

    print("Syncing filesystem")
    subprocess.check_call(['sync'])

    return 0
def main():
    parser = argparse.ArgumentParser(
        description="Generate base configs for sysadmin")
    parser.add_argument("factory_settings",
                        type=str,
                        help="File containing base factory settings in "
                        "sysadmin-agnostic format")
    parser.add_argument("--config_file",
                        type=str,
                        help="sysadmin config file for factory settings")
    parser.add_argument("--migration_logs",
                        type=str,
                        default="/etc/sysadmin/migration_log",
                        help="log file containing a record of migrations")
    parser.add_argument("migration_file",
                        type=str,
                        default="/etc/sysadmin/migrations",
                        help="Absolute path to migration file or directory")
    args = parser.parse_args()

    sysadmin = None
    if args.config_file:
        with open(args.config_file) as f:
            config = yaml.load(f)
            dest = config["storage-path"]
            shutil.rmtree(dest)
        sysadmin = SysAdminRunner()
        sysadmin.Start(args.config_file)

    try:
        with open(args.factory_settings) as f:
            config = yaml.load(f)
        client = SysAdminClient('localhost', 4000)
        migrator = SysAdminMigrator(client)
        migrator.migrate(config, sysadminctl_pb2.TEMPLATE_ONLY)
        client.commit(sysadminctl_pb2.TEMPLATE_ONLY)

        # Here we simply log all migrations that existed when this was run
        # to prevent them from being run again
        migration_log = MigrationLog()
        if os.path.exists(args.migration_logs):
            migration_log.load(args.migration_logs)
        try:
            migrations = load_migrations(args.migration_file)
            for m in migrations:
                migration_log.log_migration(m[1])
        except ValueError as e:
            print("ERROR: %s" % e)
        migration_log.save(args.migration_logs)
    finally:
        if sysadmin:
            sysadmin.Stop()
    print("INFO: Configs dumped")
    return 0