From 346134dd3eddaff0264d99813ba42e873fc610d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu>
Date: Fri, 23 Jun 2017 11:24:34 +0200
Subject: [PATCH] Added `--reregister` CLI option to re-register this worker at
 its Manager.

WARNING: this can cause duplicate worker information in the Manager's
database.

This makes it easier to re-register workers; now this can be used, rather
than editing ~/.flamenco-worker.cfg. Since that file is likely to have been
created by the Worker itself, the user may not even know it exists.
---
 CHANGELOG.md              | 2 ++
 flamenco_worker/cli.py    | 9 +++++++++
 flamenco_worker/config.py | 3 +++
 3 files changed, 14 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 28d9f09b..121201b6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@ changed functionality, fixed bugs).
 ## Version 2.0.6 (in development)
 
 - Fixed incompatibility with attrs version 17.1+.
+- Added `--reregister` CLI option to re-register this worker at its Manager.
+  WARNING: this can cause duplicate worker information in the Manager's database.
 
 
 ## Version 2.0.5 (released 2017-05-09)
diff --git a/flamenco_worker/cli.py b/flamenco_worker/cli.py
index c9ec21f9..52bca805 100644
--- a/flamenco_worker/cli.py
+++ b/flamenco_worker/cli.py
@@ -16,6 +16,10 @@ def main():
     parser.add_argument('-v', '--verbose', action='store_true',
                         help='Show configuration before starting, '
                              'and asyncio task status at shutdown.')
+    parser.add_argument('-r', '--reregister', action='store_true',
+                        help="Erases authentication information and re-registers this worker "
+                             "at the Manager. WARNING: this can cause duplicate worker information "
+                             "in the Manager's database.")
     args = parser.parse_args()
 
     # Load configuration
@@ -26,6 +30,11 @@ def main():
     log = logging.getLogger(__name__)
     log.debug('Starting')
 
+    if args.reregister:
+        log.warning('Erasing worker_id and worker_secret so we can attempt re-registration.')
+        confparser.erase('worker_id')
+        confparser.erase('worker_secret')
+
     # Patch AsyncIO
     from . import patch_asyncio
     patch_asyncio.patch_asyncio()
diff --git a/flamenco_worker/config.py b/flamenco_worker/config.py
index 2e9ac2b3..411db829 100644
--- a/flamenco_worker/config.py
+++ b/flamenco_worker/config.py
@@ -45,6 +45,9 @@ class ConfigParser(configparser.ConfigParser):
         secs = self.value(key, float)
         return datetime.timedelta(seconds=secs)
 
+    def erase(self, key: str) -> bool:
+        return self.set(CONFIG_SECTION, key, '')
+
 
 def merge_with_home_config(new_conf: dict):
     """Updates the home configuration file with the given config dict."""
-- 
GitLab