All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bear.plugins.db.MongoDbService Maven / Gradle / Ivy

The newest version!
package bear.plugins.db;

import bear.core.SessionContext;
import bear.main.event.NoticeEventToUI;
import bear.plugins.mongo.MongoDbPlugin;
import bear.vcs.CommandLineResult;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.joda.time.DateTime;

import java.util.List;
import java.util.concurrent.Callable;

import static bear.core.SessionContext.ui;

/**
* @author Andrey Chaschev [email protected]
*/
public class MongoDbService extends DbDumpManager.AbstractDbService {
    public MongoDbService(SessionContext $) {
        super($);
    }

    @Override
    public Class getDbDumpInfoClass() {
        return DbDumpInfo.class;
    }

    @Override
    public Class getDumpableEntryClass() {
        return DbDumpManager.MongoDumpableEntry.class;
    }

    @Override
    public DbDumpManager.MongoDumpableEntry list(String dbName) {
        $.plugin(MongoDbPlugin.class).runScript($, null);
        throw new UnsupportedOperationException("todo");
    }

    @Override
    public void restoreDump(final DbDumpInfo dbDumpInfo) {
        $.withMap(
            ImmutableMap.builder()
                .put(plugin.dumpName, dbDumpInfo.name)
                .build(),
            new Callable() {
                @Override
                public Object call() throws Exception {
                    $.sys.mkdirs($(plugin.dumpFolderPath)).run();

                    ui.info(new NoticeEventToUI("Mongo Restore", "Started mongo restore at " + $($.bear.sessionHostname)
                        + " for dump " + dbDumpInfo.name));

                    $.sys.script().line()
                        .cd($(plugin.sharedDbDumpsPath))
                        .addRaw("tar xvfz %s", $(plugin.dumpArchivePath))
                        .timeoutForInstallation()
                        .build()
                        .run();

                    CommandLineResult result = $.sys.script().line()
                        .timeoutMin(24 * 60) //24h
                        .addRaw("mongorestore %s", $(plugin.dumpFolderPath))
                        .build()
                        .run();

                    ui.info(new NoticeEventToUI("Mongo Restore", "Finished mongo restore at " + $($.bear.sessionHostname)
                        + ", dump " + dbDumpInfo.name));

                    return null;
                }
            }
        );

    }

    @Override
    public DbDumpInfo createDump(DbDumpManager.MongoDumpableEntry entry) {
//            ReleasesPlugin.RELEASE_FORMATTER.
        String dbName = entry.database;

        $.sys.mkdirs($(plugin.dumpFolderPath));

        ui.info(new NoticeEventToUI("Mongo Dump", "Started mongo dump at " + $($.bear.sessionHostname)));

        DateTime startedAt = new DateTime();

        CommandLineResult result = $.sys.script().line().timeoutMin(24 * 60) //24h
            .addRaw("mongodump -d %s -o %s", entry.database, $(plugin.dumpFolderPath)).build().line().timeoutMin(24 * 60)
            .addRaw("tar cvfz %s %s", $(plugin.dumpArchivePath), $(plugin.dumpFolderPath)).build()
            .run();

        $.sys.rm($(plugin.dumpFolderPath)).run();

        DateTime finishedAt = new DateTime();

        ui.info(new NoticeEventToUI("Mongo Dump", "Mongo dump finished in " + $($.bear.sessionHostname)));

        List dumpInfos = Lists.newArrayList(listDumps());

        DbDumpInfo dbDumpInfo = new DbDumpInfo(
            $(plugin.dumpName),
            $(plugin.dbType),
            dbName,
            "",
            startedAt,
            finishedAt,
            -1);

        dumpInfos.add(dbDumpInfo);

        saveDumpList(dumpInfos);

        return dbDumpInfo;
    }

    @Override
    public DbDumpManager.MongoDumpableEntry fromString(String entry) {
        return new DbDumpManager.MongoDumpableEntry(entry);
    }
}