Merge "Create systemwide lock for glean"

This commit is contained in:
Jenkins 2015-09-01 14:30:12 +00:00 committed by Gerrit Code Review
commit bea0c3733a
2 changed files with 39 additions and 6 deletions

View File

@ -23,6 +23,8 @@ import subprocess
import sys
import time
from glean import systemlock
post_up = " post-up route add -net {net} netmask {mask} gw {gw} || true\n"
pre_down = " pre-down route del -net {net} netmask {mask} gw {gw} || true\n"
@ -412,12 +414,13 @@ def main():
'--skip-network', dest='skip', action='store_true',
help="Do not write network info")
args = parser.parse_args()
if args.ssh:
write_ssh_keys(args)
if args.hostname:
set_hostname_from_config_drive(args)
if args.interface != 'lo' and not args.skip:
write_network_info_from_config_drive(args)
with systemlock.Lock(os.path.join(args.root, 'tmp/glean.lock')):
if args.ssh:
write_ssh_keys(args)
if args.hostname:
set_hostname_from_config_drive(args)
if args.interface != 'lo' and not args.skip:
write_network_info_from_config_drive(args)
return 0

30
glean/systemlock.py Normal file
View File

@ -0,0 +1,30 @@
#!/usr/bin/python
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import fcntl
class Lock(object):
def __init__(self, filename):
self.filename = filename
def __enter__(self):
fh = open(self.filename, 'w')
fcntl.flock(fh, fcntl.LOCK_EX)
def __exit__(self, exc_type, exc_value, traceback):
fh = open(self.filename, 'w')
fcntl.flock(fh, fcntl.LOCK_UN)