summaryrefslogtreecommitdiff
path: root/tickets/20bdae3913de49d4afd544d36996ce4b/Maildir/new/1455998997.M825284P17339Q73.exolobe1
blob: a5834c3fdd21869b0e8f93e8088e34913dbd695c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Return-Path: <obnam-dev-bounces@obnam.org>
X-Original-To: distix@pieni.net
Delivered-To: distix@pieni.net
Received: from bagpuss.pepperfish.net (bagpuss.pepperfish.net [148.251.8.16])
	(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by pieni.net (Postfix) with ESMTPS id 84E152B926
	for <distix@pieni.net>; Thu, 29 Oct 2015 05:50:24 +0100 (CET)
Received: from platypus.pepperfish.net (unknown [10.112.100.20])
	by bagpuss.pepperfish.net (Postfix) with ESMTP id CFD49CB;
	Thu, 29 Oct 2015 04:50:23 +0000 (GMT)
Received: from ip6-localhost ([::1] helo=platypus.pepperfish.net)
	by platypus.pepperfish.net with esmtp (Exim 4.80 #2 (Debian))
	id 1ZrfAN-00044G-JC; Thu, 29 Oct 2015 04:50:23 +0000
Received: from inmail0 ([10.112.100.10] helo=mx0.pepperfish.net)
 by platypus.pepperfish.net with esmtp (Exim 4.80 #2 (Debian))
 id 1ZrfAL-00044A-Vz
 for <obnam-dev@obnam.org>; Thu, 29 Oct 2015 04:50:22 +0000
Received: from mail-yk0-f171.google.com ([209.85.160.171])
 by mx0.pepperfish.net with esmtps (TLS1.2:RSA_ARCFOUR_SHA1:128)
 (Exim 4.80) (envelope-from <mathstuf@gmail.com>) id 1ZrfAJ-0002gX-Qb
 for obnam-dev@obnam.org; Thu, 29 Oct 2015 04:50:21 +0000
Received: by ykft191 with SMTP id t191so31141295ykf.0
 for <obnam-dev@obnam.org>; Wed, 28 Oct 2015 21:50:06 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
 h=from:to:cc:subject:date:message-id:in-reply-to:references;
 bh=gYI6W/BaZtNFrMk/TFeDM3T8/PD2IwVRG0tUVOR2/pQ=;
 b=YSdfYdl3JjIPyERxdykEUa3pQt/tuQvdjLR6chuX9K5IW/X/OYxQpGLef+DeAwLlhD
 1feXCTJ/WAInNXDp8vjkuMdmG+p+hItToYS558UhvRYJFMO5l1QkLo6iq8ufltY78T1Y
 FqF4vQR8uZ+Ege7mJa1+Z4kaFhnajenVpWYbTNTWJd2FjDOFG1xgn37AW0FGPjHIDQH1
 43x2kpgDfE7fmACa1qc711btewA81tx7pVyGDTe2QMlbK7Ut+qH8Ly0RDcSs62fNmIVp
 n8q4+oH5oERhPDOF81iMJIGRZX6Ev4+Hehej+Hz0TaK8wjm8xrn4/FReLBiXpaKpTPI7
 IOrQ==
X-Received: by 10.129.157.211 with SMTP id u202mr36753486ywg.11.1446094205976; 
 Wed, 28 Oct 2015 21:50:05 -0700 (PDT)
Received: from localhost (10.sub-70-209-141.myvzw.com. [70.209.141.10])
 by smtp.gmail.com with ESMTPSA id i66sm33284774ywc.2.2015.10.28.21.50.04
 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
 Wed, 28 Oct 2015 21:50:05 -0700 (PDT)
From: Ben Boeckel <mathstuf@gmail.com>
To: obnam-dev@obnam.org
Date: Thu, 29 Oct 2015 00:49:56 -0400
Message-Id: <1446094196-14166-1-git-send-email-mathstuf@gmail.com>
X-Mailer: git-send-email 2.6.2
In-Reply-To: <20150918050247.GA8128@bronto-burt.dev.benboeckel.net>
References: <20150918050247.GA8128@bronto-burt.dev.benboeckel.net>
X-Spam-Score: -0.5
X-Spam-Score-int: -4
X-Spam-Bar: /
X-Scanned-By: pepperfish.net, Thu, 29 Oct 2015 04:50:21 +0000
X-Spam-Report: Content analysis details: (-0.5 points)
 pts rule name              description
 ---- ---------------------- --------------------------------------------------
 1.0 PPF_FROM_CONTAINS_MAIL The From header contains 'mail'
 1.2 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
 (mathstuf[at]gmail.com)
 -0.7 RCVD_IN_DNSWL_LOW      RBL: Sender listed at http://www.dnswl.org/, low
 trust [209.85.160.171 listed in list.dnswl.org]
 -0.0 SPF_PASS               SPF: sender matches SPF record
 -1.9 BAYES_00               BODY: Bayes spam probability is 0 to 1%
 [score: 0.0000]
 -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's
 domain
 0.1 DKIM_SIGNED            Message has a DKIM or DK signature,
 not necessarily valid
 -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature
X-ACL-Warn: message may be spam
X-Scan-Signature: fc70ac7cbc1a3ab5eb75965631bdeb45
Cc: Ben Boeckel <mathstuf@gmail.com>
Subject: [PATCH] backup: take an inhibitor lock if possible
X-BeenThere: obnam-dev@obnam.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: Obnam development discussions <obnam-dev-obnam.org>
List-Unsubscribe: <http://listmaster.pepperfish.net/cgi-bin/mailman/listinfo/obnam-dev-obnam.org>,
 <mailto:obnam-dev-request@obnam.org?subject=unsubscribe>
List-Archive: <http://listmaster.pepperfish.net/pipermail/obnam-dev-obnam.org>
List-Post: <mailto:obnam-dev@obnam.org>
List-Help: <mailto:obnam-dev-request@obnam.org?subject=help>
List-Subscribe: <http://listmaster.pepperfish.net/cgi-bin/mailman/listinfo/obnam-dev-obnam.org>,
 <mailto:obnam-dev-request@obnam.org?subject=subscribe>
Sender: obnam-dev-bounces@obnam.org
Errors-To: obnam-dev-bounces@obnam.org

When a backup is happening, the machine should not suspend or otherwise
go down. Add a setting for taking a systemd inhibitor lock.

The code fails safely which allows both the dbus module and the
systemd-logind DBus interface to be optional. In both cases, warnings
are generated.

Signed-off-by: Ben Boeckel <mathstuf@gmail.com>
---
 obnamlib/plugins/backup_plugin.py | 73 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/obnamlib/plugins/backup_plugin.py b/obnamlib/plugins/backup_plugin.py
index 978db2e..e15d614 100644
--- a/obnamlib/plugins/backup_plugin.py
+++ b/obnamlib/plugins/backup_plugin.py
@@ -125,6 +125,13 @@ class BackupPlugin(obnamlib.ObnamPlugin):
             'as it is quite bad for performance',
             group=backup_group)
 
+        self.app.settings.boolean(
+            ['block-suspend'],
+            'block shutdown and suspending of the '
+            'system while the backup is in progress',
+            default=True,
+            group=backup_group)
+
         # Performance related settings.
 
         perf_group = obnamlib.option_group['perf']
@@ -155,6 +162,22 @@ class BackupPlugin(obnamlib.ObnamPlugin):
 
         '''
 
+        if self.app.settings['block-suspend']:
+            inhibitor_args = {
+                'what': (IDLE, SLEEP, SHUTDOWN),
+                'why': 'backup in progress',
+                'who': 'obnam',
+                'mode': BLOCK,
+            }
+            with Inhibitor(**inhibitor_args):
+                self._backup(args)
+        else:
+            self._backup(args)
+
+    def _backup(self, args):
+        '''Actual backup logic.
+        '''
+
         logging.info('Backup starts')
 
         root_urls = self.app.settings['root'] + args
@@ -900,3 +923,53 @@ class BackupPlugin(obnamlib.ObnamPlugin):
         tracing.trace('new_roots: %s' % repr(new_roots))
         gen_id = self.new_generation
         helper('/')
+
+# Inhibitor logic.
+
+# Things that can be inhibited.
+SHUTDOWN = 'shutdown'
+SLEEP = 'sleep'
+IDLE = 'idle'
+HANDLE_POWER_KEY = 'handle-power-key'
+HANDLE_SUSPEND_KEY = 'handle-suspend-key'
+HANDLE_HIBERNATE_KEY = 'handle-hibernate-key'
+HANDLE_LID_SWITCH = 'handle-lid-switch'
+
+# What kind of block can be taken.
+BLOCK = 'block'
+DELAY = 'delay'
+
+
+class Inhibitor(object):
+    def __init__(self, what=(IDLE, SLEEP, SHUTDOWN),
+                 who='obnam', why='Unknown reason', mode=BLOCK):
+        self.what = ':'.join(what)
+        self.who = who
+        self.why = why
+        self.mode = mode
+
+        self._fd = None
+
+    def __enter__(self):
+        try:
+            import dbus
+        except ImportError:
+            logging.warn(
+                'Failed to import the dbus module. '
+                'Skipping inhibitor locks.')
+            return
+
+        try:
+            bus = dbus.SystemBus()
+            manager = bus.get_object('org.freedesktop.login1',
+                                     '/org/freedesktop/login1')
+            iface = 'org.freedesktop.login1.Manager'
+            self._fd = manager.Inhibit(self.what, self.who,
+                                       self.why, self.mode,
+                                       dbus_interface=iface)
+        except BaseException, e:
+            logging.warning('failed to take an inhibitor lock')
+            logging.debug('Inhibitor exception %s', str(e))
+
+    def __exit__(self, type_, value, traceback):
+        self._fd = None
-- 
2.6.2


_______________________________________________
obnam-dev mailing list
obnam-dev@obnam.org
http://listmaster.pepperfish.net/cgi-bin/mailman/listinfo/obnam-dev-obnam.org