Skip to content

Commit

Permalink
Merge pull request #35 from antonplagemann/development
Browse files Browse the repository at this point in the history
v4.1.1 Added syslog support
  • Loading branch information
antonplagemann committed Dec 27, 2021
2 parents bbadde9 + a34c324 commit 3eac584
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ MONICA_LABELS_EXCLUDE=
DATABASE_FILE=data/syncState.db
GOOGLE_TOKEN_FILE=data/token.pickle
GOOGLE_CREDENTIALS_FILE=data/credentials.json

# Send messages to a syslog server
# An alternative to providing target host and port is providing only a target address, for example "/dev/log".
# In this case, a Unix domain socket is used to send the message to the syslog.
SYSLOG_TARGET=
SYSLOG_PORT=
1 change: 1 addition & 0 deletions .github/actions/cleanup-environment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ runs:

- name: Upload Google token to repo secrets
run: python test/UpdateToken.py
if: always()
env:
REPO_TOKEN: ${{ inputs.REPO_TOKEN }}
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/docker-cd-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
-
name: "Create tags for Docker image"
id: docker_meta
uses: docker/metadata-action@v3.6.1
uses: docker/metadata-action@v3.6.2
with:
tag-latest: true
images: antonplagemann/google-monica-sync
Expand Down
19 changes: 16 additions & 3 deletions GMSync.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import argparse
import logging
import logging.handlers
import os
import sys
from os.path import abspath, join
from typing import Tuple, Union

from dotenv import dotenv_values, find_dotenv # type: ignore
from dotenv.main import set_key # type: ignore
Expand All @@ -14,7 +16,7 @@
from helpers.MonicaHelper import Monica
from helpers.SyncHelper import Sync

VERSION = "v4.0.0"
VERSION = "v4.1.1"
LOG_FOLDER = "logs"
LOG_FILENAME = "sync.log"
DEFAULT_CONFIG_FILEPATH = join("helpers", ".env.default")
Expand All @@ -39,6 +41,17 @@ def main(self) -> None:
# Load config
self.load_config()

# Create syslog handler
if self.conf.SYSLOG_TARGET:
address: Union[Tuple[str, int], str] = (
(self.conf.SYSLOG_TARGET, int(self.conf.SYSLOG_PORT))
if self.conf.SYSLOG_PORT
else self.conf.SYSLOG_TARGET
)
syslog_handler = logging.handlers.SysLogHandler(address=address)
syslog_handler.setFormatter(self.logging_format)
self.log.addHandler(syslog_handler)

# Create sync object
self.create_sync_helper()

Expand Down Expand Up @@ -94,11 +107,11 @@ def create_logger(self) -> None:
log = logging.getLogger("GMSync")
dotenv_log = logging.getLogger("dotenv.main")
log.setLevel(logging.INFO)
logging_format = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
self.logging_format = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
log_filepath = join(LOG_FOLDER, LOG_FILENAME)
handler = logging.FileHandler(filename=log_filepath, mode="a", encoding="utf8")
handler.setLevel(logging.INFO)
handler.setFormatter(logging_format)
handler.setFormatter(self.logging_format)
log.addHandler(handler)
dotenv_log.addHandler(handler)
self.log = log
Expand Down
2 changes: 2 additions & 0 deletions Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ To configure the OAuth consent screen:
> Hint: If you get a `403 forbidden` during consent later, you may have entered the wrong email here.
12. Click `Save and Continue`. The "OAuth consent screen" appears.
13. Click `Back to Dashboard`.
14. Back at the "OAuth consent screen" screen, click `PUBLISH APP` and then `CONFIRM`.
> This step is necessary because when in "Testing"-status, Google limits the token lifetime to seven days. You don't need to complete the app verification process.
### Create an OAuth client ID credential

Expand Down
6 changes: 6 additions & 0 deletions helpers/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ MONICA_LABELS_EXCLUDE=
DATABASE_FILE=data/syncState.db
GOOGLE_TOKEN_FILE=data/token.pickle
GOOGLE_CREDENTIALS_FILE=data/credentials.json

# Send messages to a syslog server
# An alternative to providing target host and port is providing only a target address, for example "/dev/log".
# In this case, a Unix domain socket is used to send the message to the syslog.
SYSLOG_TARGET=
SYSLOG_PORT=
2 changes: 2 additions & 0 deletions helpers/ConfigHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def __init__(self, log: Logger, raw_config: Dict[str, Union[str, None]]) -> None
self.DATABASE_FILE = abspath(self._values["DATABASE_FILE"])
self.GOOGLE_TOKEN_FILE = abspath(self._values["GOOGLE_TOKEN_FILE"])
self.GOOGLE_CREDENTIALS_FILE = abspath(self._values["GOOGLE_CREDENTIALS_FILE"])
self.SYSLOG_TARGET = self._values["SYSLOG_TARGET"]
self.SYSLOG_PORT = self._values["SYSLOG_PORT"]
except Exception as e:
raise ConfigError("Error parsing config, check syntax and required args!") from e

Expand Down
2 changes: 1 addition & 1 deletion helpers/GoogleHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __build_service(self) -> Resource:
creds_pickled = base64_token.read()
creds = pickle.loads(codecs.decode(creds_pickled.encode(), "base64"))
else:
raise ConfigError("Google token file not found!")
self.log.warning("Google token file not found!")
except UnicodeDecodeError:
# Maybe old pickling file, try to update
with open(self.token_file, "rb") as binary_token:
Expand Down
7 changes: 4 additions & 3 deletions test/SetupToken.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

import requests
from bs4 import BeautifulSoup # type: ignore
from dotenv.main import load_dotenv, set_key # type: ignore
from dotenv.main import dotenv_values, set_key # type: ignore
from requests import ConnectionError, ConnectTimeout, ReadTimeout

LOG_FOLDER = "logs"
LOG_FILENAME = "setup.log"
try:
load_dotenv()
PROTOCOL, HOST, PORT = re.findall(r"(https?)://(.+?):(\d+)/api/?", os.getenv("BASE_URL", ""))[0]
config = dotenv_values(".env")
result = re.findall(r"(https?)://(.+?):(\d+)/api/?", str(config.get("BASE_URL", "")))
PROTOCOL, HOST, PORT = result[0]
except IndexError:
PROTOCOL, HOST, PORT = "http", "localhost", 8080
ENV_FILE = ".env"
Expand Down

0 comments on commit 3eac584

Please sign in to comment.