@@ -10,9 +10,12 @@ import lombok.NonNull; | |||||
import org.glassfish.jersey.server.ContainerRequest; | import org.glassfish.jersey.server.ContainerRequest; | ||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import javax.annotation.Nullable; | |||||
import javax.ws.rs.*; | import javax.ws.rs.*; | ||||
import javax.ws.rs.core.Context; | import javax.ws.rs.core.Context; | ||||
import javax.ws.rs.core.Response; | import javax.ws.rs.core.Response; | ||||
import java.util.Optional; | |||||
import java.util.concurrent.TimeUnit; | |||||
import static bubble.ApiConstants.*; | import static bubble.ApiConstants.*; | ||||
import static org.cobbzilla.util.http.HttpContentTypes.APPLICATION_JSON; | import static org.cobbzilla.util.http.HttpContentTypes.APPLICATION_JSON; | ||||
@@ -37,13 +40,19 @@ public class LogsResource { | |||||
} | } | ||||
@POST @Path(EP_START) | @POST @Path(EP_START) | ||||
@NonNull public Response startLogging(@NonNull @Context final ContainerRequest ctx) { return setLogFlag(true); } | |||||
@NonNull public Response startLogging(@NonNull @Context final ContainerRequest ctx, | |||||
@Nullable @QueryParam("ttlDays") final Byte ttlDays) { | |||||
return setLogFlag(true, Optional.ofNullable(ttlDays)); | |||||
} | |||||
@POST @Path(EP_STOP) | @POST @Path(EP_STOP) | ||||
@NonNull public Response stopLogging(@NonNull @Context final ContainerRequest ctx) { return setLogFlag(false); } | |||||
@NonNull public Response stopLogging(@NonNull @Context final ContainerRequest ctx) { | |||||
return setLogFlag(false, Optional.empty()); | |||||
} | |||||
@NonNull private Response setLogFlag(final boolean b) { | |||||
@NonNull private Response setLogFlag(final boolean b, @NonNull final Optional<Byte> ttlInDays) { | |||||
if (!account.admin()) throw forbiddenEx(); // caller must be admin | if (!account.admin()) throw forbiddenEx(); // caller must be admin | ||||
selfNodeService.setLogFlag(b); | |||||
selfNodeService.setLogFlag(b, ttlInDays.map(days -> (int) TimeUnit.DAYS.toSeconds(days))); | |||||
return ok(); | return ok(); | ||||
} | } | ||||
} | } |
@@ -7,6 +7,9 @@ package bubble.service.boot; | |||||
import bubble.model.bill.BubblePlan; | import bubble.model.bill.BubblePlan; | ||||
import bubble.model.cloud.BubbleNetwork; | import bubble.model.cloud.BubbleNetwork; | ||||
import bubble.model.cloud.BubbleNode; | import bubble.model.cloud.BubbleNode; | ||||
import lombok.NonNull; | |||||
import java.util.Optional; | |||||
public interface SelfNodeService { | public interface SelfNodeService { | ||||
@@ -25,5 +28,5 @@ public interface SelfNodeService { | |||||
BubblePlan getThisPlan(); | BubblePlan getThisPlan(); | ||||
Boolean getLogFlag(); | Boolean getLogFlag(); | ||||
void setLogFlag(final boolean logFlag); | |||||
void setLogFlag(final boolean logFlag, @NonNull final Optional<Integer> ttlInSeconds); | |||||
} | } |
@@ -41,6 +41,7 @@ import org.springframework.stereotype.Service; | |||||
import java.io.File; | import java.io.File; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Optional; | |||||
import java.util.concurrent.atomic.AtomicBoolean; | import java.util.concurrent.atomic.AtomicBoolean; | ||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
@@ -72,8 +73,8 @@ public class StandardSelfNodeService implements SelfNodeService { | |||||
public static final long MIN_SAGE_KEY_TTL = MINUTES.toMillis(5); | public static final long MIN_SAGE_KEY_TTL = MINUTES.toMillis(5); | ||||
private static final String REDIS_LOG_FLAG_KEY = "bubble_server_logs_enabled"; | private static final String REDIS_LOG_FLAG_KEY = "bubble_server_logs_enabled"; | ||||
private static final long TTL_LOG_FLAG_NODE = DAYS.toSeconds(7); | |||||
private static final long TTL_LOG_FLAG_SAGE = DAYS.toSeconds(30); | |||||
private static final int TTL_LOG_FLAG_NODE = (int) DAYS.toSeconds(7); | |||||
private static final int TTL_LOG_FLAG_SAGE = (int) DAYS.toSeconds(30); | |||||
@Autowired private BubbleNodeDAO nodeDAO; | @Autowired private BubbleNodeDAO nodeDAO; | ||||
@Autowired private BubbleNodeKeyDAO nodeKeyDAO; | @Autowired private BubbleNodeKeyDAO nodeKeyDAO; | ||||
@@ -448,10 +449,10 @@ public class StandardSelfNodeService implements SelfNodeService { | |||||
} | } | ||||
@Override | @Override | ||||
public void setLogFlag(final boolean logFlag) { | |||||
public void setLogFlag(final boolean logFlag, @NonNull final Optional<Integer> ttlInSeconds) { | |||||
if (logFlag) { | if (logFlag) { | ||||
getNodeConfig().set_plaintext(REDIS_LOG_FLAG_KEY, "true", EX, | getNodeConfig().set_plaintext(REDIS_LOG_FLAG_KEY, "true", EX, | ||||
isSelfSage() ? TTL_LOG_FLAG_SAGE : TTL_LOG_FLAG_NODE); | |||||
ttlInSeconds.orElse(isSelfSage() ? TTL_LOG_FLAG_SAGE : TTL_LOG_FLAG_NODE)); | |||||
} else { | } else { | ||||
// just (try to) remove the flag | // just (try to) remove the flag | ||||
getNodeConfig().del(REDIS_LOG_FLAG_KEY); | getNodeConfig().del(REDIS_LOG_FLAG_KEY); | ||||
@@ -8,8 +8,11 @@ import bubble.model.bill.BubblePlan; | |||||
import bubble.model.cloud.BubbleNetwork; | import bubble.model.cloud.BubbleNetwork; | ||||
import bubble.model.cloud.BubbleNode; | import bubble.model.cloud.BubbleNode; | ||||
import bubble.service.boot.SelfNodeService; | import bubble.service.boot.SelfNodeService; | ||||
import lombok.NonNull; | |||||
import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
import java.util.Optional; | |||||
import static org.cobbzilla.util.daemon.ZillaRuntime.notSupported; | import static org.cobbzilla.util.daemon.ZillaRuntime.notSupported; | ||||
@Service | @Service | ||||
@@ -30,6 +33,8 @@ public class DbFilterSelfNodeService implements SelfNodeService { | |||||
@Override public BubblePlan getThisPlan() { return notSupported("getThisPlan"); } | @Override public BubblePlan getThisPlan() { return notSupported("getThisPlan"); } | ||||
@Override public Boolean getLogFlag() { return notSupported("getLogFlag"); } | @Override public Boolean getLogFlag() { return notSupported("getLogFlag"); } | ||||
@Override public void setLogFlag(boolean logFlag) { notSupported("setLogFlag"); } | |||||
@Override public void setLogFlag(boolean logFlag, @NonNull final Optional<Integer> ttlInSeconds) { | |||||
notSupported("setLogFlag"); | |||||
} | |||||
} | } |