diff --git a/Pipfile.lock b/Pipfile.lock index 9f3ec69..6097534 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -64,10 +64,10 @@ }, "idna": { "hashes": [ - "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", - "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c" + "sha256:7588d1c14ae4c77d74036e8c22ff447b26d0fde8f007354fd48a7814db15b7cb", + "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa" ], - "version": "==2.8" + "version": "==2.9" }, "pyyaml": { "hashes": [ @@ -88,11 +88,11 @@ }, "requests": { "hashes": [ - "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4", - "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31" + "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee", + "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6" ], "index": "pypi", - "version": "==2.22.0" + "version": "==2.23.0" }, "six": { "hashes": [ @@ -198,6 +198,12 @@ "index": "pypi", "version": "==3.7.9" }, + "future": { + "hashes": [ + "sha256:b1bead90b70cf6ec3f0710ae53a525360fa360d306a86583adc6bf83a4db537d" + ], + "version": "==0.18.2" + }, "importlib-metadata": { "hashes": [ "sha256:06f5b3a99029c7134207dd882428a66992a9de2bef7c2b699b5641f9886c3302", @@ -228,6 +234,16 @@ ], "version": "==2.6.1" }, + "lunr": { + "extras": [ + "languages" + ], + "hashes": [ + "sha256:1208622930c915a07e6f8e8640474357826bad48534c0f57969b6fca9bffc88e", + "sha256:7be69d7186f65784a4f2adf81e5c58efd6a9921aa95966babcb1f2f2ada75c20" + ], + "version": "==0.5.6" + }, "markdown": { "hashes": [ "sha256:90fee683eeabe1a92e149f7ba74e5ccdc81cd397bd6c516d93a8da0ef90b6902", @@ -290,11 +306,17 @@ }, "mkdocs": { "hashes": [ - "sha256:17d34329aad75d5de604b9ed4e31df3a4d235afefdc46ce7b1964fddb2e1e939", - "sha256:8cc8b38325456b9e942c981a209eaeb1e9f3f77b493ad755bfef889b9c8d356a" + "sha256:1e385a70aea8a9dedb731aea4fd5f3704b2074801c4f96f06b2920999babda8a", + "sha256:9243291392f59e20b655e4e46210233453faf97787c2cf72176510e868143174" ], "index": "pypi", - "version": "==1.0.4" + "version": "==1.1" + }, + "nltk": { + "hashes": [ + "sha256:bed45551259aa2101381bbdd5df37d44ca2669c5c3dad72439fa459b29137d94" + ], + "version": "==3.4.5" }, "packaging": { "hashes": [ @@ -364,29 +386,29 @@ }, "regex": { "hashes": [ - "sha256:07b39bf943d3d2fe63d46281d8504f8df0ff3fe4c57e13d1656737950e53e525", - "sha256:0932941cdfb3afcbc26cc3bcf7c3f3d73d5a9b9c56955d432dbf8bbc147d4c5b", - "sha256:0e182d2f097ea8549a249040922fa2b92ae28be4be4895933e369a525ba36576", - "sha256:10671601ee06cf4dc1bc0b4805309040bb34c9af423c12c379c83d7895622bb5", - "sha256:23e2c2c0ff50f44877f64780b815b8fd2e003cda9ce817a7fd00dea5600c84a0", - "sha256:26ff99c980f53b3191d8931b199b29d6787c059f2e029b2b0c694343b1708c35", - "sha256:27429b8d74ba683484a06b260b7bb00f312e7c757792628ea251afdbf1434003", - "sha256:3e77409b678b21a056415da3a56abfd7c3ad03da71f3051bbcdb68cf44d3c34d", - "sha256:4e8f02d3d72ca94efc8396f8036c0d3bcc812aefc28ec70f35bb888c74a25161", - "sha256:4eae742636aec40cf7ab98171ab9400393360b97e8f9da67b1867a9ee0889b26", - "sha256:6a6ae17bf8f2d82d1e8858a47757ce389b880083c4ff2498dba17c56e6c103b9", - "sha256:6a6ba91b94427cd49cd27764679024b14a96874e0dc638ae6bdd4b1a3ce97be1", - "sha256:7bcd322935377abcc79bfe5b63c44abd0b29387f267791d566bbb566edfdd146", - "sha256:98b8ed7bb2155e2cbb8b76f627b2fd12cf4b22ab6e14873e8641f266e0fb6d8f", - "sha256:bd25bb7980917e4e70ccccd7e3b5740614f1c408a642c245019cff9d7d1b6149", - "sha256:d0f424328f9822b0323b3b6f2e4b9c90960b24743d220763c7f07071e0778351", - "sha256:d58e4606da2a41659c84baeb3cfa2e4c87a74cec89a1e7c56bee4b956f9d7461", - "sha256:e3cd21cc2840ca67de0bbe4071f79f031c81418deb544ceda93ad75ca1ee9f7b", - "sha256:e6c02171d62ed6972ca8631f6f34fa3281d51db8b326ee397b9c83093a6b7242", - "sha256:e7c7661f7276507bce416eaae22040fd91ca471b5b33c13f8ff21137ed6f248c", - "sha256:ecc6de77df3ef68fee966bb8cb4e067e84d4d1f397d0ef6fce46913663540d77" - ], - "version": "==2020.1.8" + "sha256:01b2d70cbaed11f72e57c1cfbaca71b02e3b98f739ce33f5f26f71859ad90431", + "sha256:046e83a8b160aff37e7034139a336b660b01dbfe58706f9d73f5cdc6b3460242", + "sha256:113309e819634f499d0006f6200700c8209a2a8bf6bd1bdc863a4d9d6776a5d1", + "sha256:200539b5124bc4721247a823a47d116a7a23e62cc6695744e3eb5454a8888e6d", + "sha256:25f4ce26b68425b80a233ce7b6218743c71cf7297dbe02feab1d711a2bf90045", + "sha256:269f0c5ff23639316b29f31df199f401e4cb87529eafff0c76828071635d417b", + "sha256:5de40649d4f88a15c9489ed37f88f053c15400257eeb18425ac7ed0a4e119400", + "sha256:7f78f963e62a61e294adb6ff5db901b629ef78cb2a1cfce3cf4eeba80c1c67aa", + "sha256:82469a0c1330a4beb3d42568f82dffa32226ced006e0b063719468dcd40ffdf0", + "sha256:8c2b7fa4d72781577ac45ab658da44c7518e6d96e2a50d04ecb0fd8f28b21d69", + "sha256:974535648f31c2b712a6b2595969f8ab370834080e00ab24e5dbb9d19b8bfb74", + "sha256:99272d6b6a68c7ae4391908fc15f6b8c9a6c345a46b632d7fdb7ef6c883a2bbb", + "sha256:9b64a4cc825ec4df262050c17e18f60252cdd94742b4ba1286bcfe481f1c0f26", + "sha256:9e9624440d754733eddbcd4614378c18713d2d9d0dc647cf9c72f64e39671be5", + "sha256:9ff16d994309b26a1cdf666a6309c1ef51ad4f72f99d3392bcd7b7139577a1f2", + "sha256:b33ebcd0222c1d77e61dbcd04a9fd139359bded86803063d3d2d197b796c63ce", + "sha256:bba52d72e16a554d1894a0cc74041da50eea99a8483e591a9edf1025a66843ab", + "sha256:bed7986547ce54d230fd8721aba6fd19459cdc6d315497b98686d0416efaff4e", + "sha256:c7f58a0e0e13fb44623b65b01052dae8e820ed9b8b654bb6296bc9c41f571b70", + "sha256:d58a4fa7910102500722defbde6e2816b0372a4fcc85c7e239323767c74f5cbc", + "sha256:f1ac2dc65105a53c1c2d72b1d3e98c2464a133b4067a51a3d2477b28449709a0" + ], + "version": "==2020.2.20" }, "six": { "hashes": [ @@ -416,11 +438,11 @@ }, "tox": { "hashes": [ - "sha256:5c45d08f1dcc9bc97cdea3e5a69c8f4ad042cc37cbe6cf53e126f4a9005b7d3b", - "sha256:73e2ade68cd71a2765ee739ecc27c8c92e9b9c09acbad0f2c5307b20214e0d13" + "sha256:0cbe98369081fa16bd6f1163d3d0b2a62afa29d402ccfad2bd09fb2668be0956", + "sha256:676f1e3e7de245ad870f956436b84ea226210587d1f72c8dfb8cd5ac7b6f0e70" ], "index": "pypi", - "version": "==3.14.4" + "version": "==3.14.5" }, "typed-ast": { "hashes": [ @@ -450,17 +472,17 @@ }, "virtualenv": { "hashes": [ - "sha256:08f3623597ce73b85d6854fb26608a6f39ee9d055c81178dc6583803797f8994", - "sha256:de2cbdd5926c48d7b84e0300dea9e8f276f61d186e8e49223d71d91250fbaebd" + "sha256:531b142e300d405bb9faedad4adbeb82b4098b918e35209af2adef3129274aae", + "sha256:5dd42a9f56307542bddc446cfd10ef6576f11910366a07609fe8d0d88fa8fb7e" ], - "version": "==20.0.4" + "version": "==20.0.5" }, "zipp": { "hashes": [ - "sha256:5c56e330306215cd3553342cfafc73dda2c60792384117893f3a83f8a1209f50", - "sha256:d65287feb793213ffe11c0f31b81602be31448f38aeb8ffc2eb286c4f6f6657e" + "sha256:12248a63bbdf7548f89cb4c7cda4681e537031eda29c02ea29674bc6854460c2", + "sha256:7c0f8e91abc0dc07a5068f315c52cb30c66bfbc581e5b50704c8a2f6ebae794a" ], - "version": "==2.2.0" + "version": "==3.0.0" } } } diff --git a/wilfred/database.py b/wilfred/database.py index a11ce75..14468fc 100644 --- a/wilfred/database.py +++ b/wilfred/database.py @@ -37,6 +37,7 @@ class Server(Base): status = Column(String) environment_variables = relationship("EnvironmentVariable") + scheduled_tasks = relationship("ScheduledTask") def __repr__(self): return f"" @@ -51,6 +52,22 @@ class EnvironmentVariable(Base): value = Column(String) +class ScheduledTask(Base): + __tablename__ = "scheduled_task" + + id = Column(Integer, primary_key=True) + server_id = Column(String, ForeignKey("servers.id"), unique=False) + + minute = Column(Integer, nullable=True) + hour = Column(Integer, nullable=True) + day_of_the_month = Column(Integer, nullable=True) + month = Column(Integer, nullable=True) + day_of_the_week = Column(Integer, nullable=True) + + task_type = Column(String) + value = Column(String) + + Base.metadata.create_all(engine) Session = sessionmaker() diff --git a/wilfred/scheduler.py b/wilfred/scheduler.py new file mode 100644 index 0000000..51fa121 --- /dev/null +++ b/wilfred/scheduler.py @@ -0,0 +1,56 @@ +################################################################# +# # +# Wilfred # +# Copyright (C) 2020, Vilhelm Prytz, # +# # +# Licensed under the terms of the MIT license, see LICENSE. # +# https://github.com/wilfred-dev/wilfred # +# # +################################################################# + +import click + +from tabulate import tabulate + +from wilfred.database import session, ScheduledTask + + +class TaskScheduler(object): + def __init__(self, server): + self._server = server + + def pretty(self): + data = [u.__dict__ for u in self._server.scheduled_tasks] + + headers = { + "minute": click.style("Minute", bold=True), + "hour": click.style("Hour", bold=True), + "day_of_the_month": click.style("Day of the Month", bold=True), + "month": click.style("Month", bold=True), + "day_of_the_week": click.style("Hour", bold=True), + "task_type": click.style("Task Type", bold=True), + "value": click.style("Value", bold=True), + } + + return tabulate(data, headers=headers, tablefmt="fancy_grid",) + + def create( + self, minute, hour, day_of_the_month, month, day_of_the_week, task_type, value + ): + # create + new_task = ScheduledTask( + server_id=self._server.id, + minute=minute, + hour=hour, + day_of_the_month=day_of_the_month, + month=month, + day_of_the_week=day_of_the_week, + task_type=task_type, + value=value, + ) + session.add(new_task) + + try: + session.commit() + except Exception as e: + raise Exception(f"could not create task, {str(e)}") diff --git a/wilfred/wilfred.py b/wilfred/wilfred.py index 4e39c5e..cf31e13 100755 --- a/wilfred/wilfred.py +++ b/wilfred/wilfred.py @@ -32,6 +32,7 @@ from wilfred.core import is_integer, random_string, check_for_new_releases from wilfred.migrate import Migrate from wilfred.server_config import ServerConfig +from wilfred.scheduler import TaskScheduler if sys.platform.startswith("win"): click.echo("Wilfred does not support Windows") @@ -717,5 +718,24 @@ def _get_variable_occurrences(variable, raw): click.echo(server_conf.pretty()) +@cli.command(short_help="Schedule tasks/scripts for a specific server.") +@click.argument("name") +@click.argument("action", required=False) +def tasks(name, action): + server = session.query(Server).filter_by(name=name.lower()).first() + + if not server: + error("Server does not exist", exit_code=1) + + _tasks = TaskScheduler(server) + + if not action: + click.echo(_tasks.pretty()) + exit(0) + + if action.lower() == "create": + click.echo("todo") + + if __name__ == "__main__": main()