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

bear.plugins.misc.ReleasesPlugin Maven / Gradle / Ivy

package bear.plugins.misc;

import bear.context.AbstractContext;
import bear.context.Fun;
import bear.core.GlobalContext;
import bear.core.SessionContext;
import bear.plugins.Plugin;
import bear.session.DynamicVariable;
import bear.task.*;
import com.google.common.base.Optional;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.TimeZone;

import static bear.session.BearVariables.joinPath;
import static bear.session.Variables.*;

/**
 * @author Andrey Chaschev [email protected]
 */
public class ReleasesPlugin extends Plugin {
    public static final DateTimeZone GMT = DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT"));
    public static final DateTimeFormatter RELEASE_FORMATTER = DateTimeFormat.forPattern("yyyyMMdd.HHmmss");

    public final DynamicVariable pendingRelease = undefined();

    // not present when: there is no activation for pending release OR when there is no release
    public final DynamicVariable> activatedRelease = dynamic(new Fun>() {
        @Override
        public Optional apply(AbstractContext $) {
            return $.var(session).getCurrentRelease();
        }
    });

    public final DynamicVariable> rollbackToRelease = undefined();

    public final DynamicVariable
        dirName = newVar("releases"),
        currentDirName = newVar("current"),
        releaseName = dynamic(new Fun() {
            @Override
            public String apply(AbstractContext $) {
                return RELEASE_FORMATTER.print(new DateTime()) + ".GMT";
            }
        }).memoizeIn(GlobalContext.class),
        pendingName = newVar("pending_"),
        pendingReleaseName = concat("pending_", releaseName),
        path = joinPath(bear.applicationPath, dirName),
        releasesJsonPath = joinPath(path, "releases.json"),
        currentReleaseLinkPath = joinPath(path, currentDirName),
        releasePath = joinPath(path, releaseName),
        pendingReleasePath = concat(path, "/pending_", releaseName);

    public final DynamicVariable
        keepXReleases = newVar(5);

    public final DynamicVariable
        cleanPending = newVar(true),
        manualRollback = newVar(false);

    public final DynamicVariable session = dynamic(new Fun() {
        @Override
        public Releases apply(SessionContext $) {
            return $.wire(new Releases($, ReleasesPlugin.this)).load();
        }
    }).memoizeIn(SessionContext.class);

    public ReleasesPlugin(GlobalContext global) {
        super(global);
    }

    @Override
    public InstallationTaskDef getInstall() {

        return new InstallationTaskDef(new NamedSupplier>("releases.install", new SingleTaskSupplier>() {
            @Override
            public Task createNewSession(SessionContext $, Task parent, TaskDef def) {
                return new InstallationTask(parent, (InstallationTaskDef) def, $) {
                    @Override
                    protected TaskResult exec(SessionRunner runner) {
                        $.sys.mkdirs($.var(path)).run();

                        return TaskResult.OK;
                    }

                    @Override
                    public Dependency asInstalledDependency() {
                        Dependency dep = new Dependency("releases.dep", $);

                        return dep.add(dep.new Directory($.var(path)));
                    }
                };
            }
        }));
    }

    public TaskDef> findReleaseToRollbackTo(final String labelOrPath) {
        return new TaskDef>(new NamedCallable>("releases.findReleaseToRollbackTo", new TaskCallable>() {
            @Override
            public TaskResult call(SessionContext $, Task> task) throws Exception {
                Releases session = $.var(ReleasesPlugin.this.session);
                Optional release = session.findAny(labelOrPath);

                if (!release.isPresent()) {
                    return TaskResult.error("release not found: " + labelOrPath + " available releases:\n " + session.show());
                }

                $.putConst(rollbackToRelease, release);

                return TaskResult.OK;
            }
        }));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy