Commit 66c9dd1f authored by Marco Mariani's avatar Marco Mariani

whitespace, except..as syntax, typos

parent 54ea0de7
......@@ -30,12 +30,12 @@ from optparse import OptionParser, Option
import ConfigParser
import os
import pkg_resources
import pwd
import socket
import subprocess
import sys
import time
import urllib2
from pwd import getpwnam
import iniparse
......@@ -73,7 +73,6 @@ class Parser(OptionParser):
return options
class SlapError(Exception):
"""
Slap error
......@@ -81,14 +80,15 @@ class SlapError(Exception):
def __init__(self, message):
self.msg = message
class UsageError(SlapError):
pass
class ExecError(SlapError):
pass
def _call(cmd_args, stdout=None, stderr=None, dry_run=False):
"""
Wrapper for subprocess.call() which'll secure the usage of external program's.
......@@ -103,15 +103,15 @@ def _call(cmd_args, stdout=None, stderr=None, dry_run=False):
if subprocess.call(cmd_args, stdout=stdout, stderr=stderr) != 0:
raise ValueError('Issues during running %r' % cmd_args)
except OSError as e:
raise ExecError('Process respond:"%s" when calling "%s"' % \
(str(e), ' '.join(cmd_args)))
raise ExecError('Process responded: "%s" when calling "%s"' %
(e, ' '.join(cmd_args)))
# Utility function to get yes/no answers
def get_yes_no(prompt, default=None):
if default:
def_value = '/ Default yes'
elif default == False:
elif default is False:
def_value = '/ Default no'
else:
def_value = ''
......@@ -190,7 +190,7 @@ def get_ssh(temp_dir):
gotten = True
except ValueError:
# add http:// if it is missing (needed by urllib2)
ssh_web = """http://"""+ssh_web
ssh_web = """http://""" + ssh_web
ssh_key_all = urllib2.urlopen(''.join(ssh_web))
gotten = True
except urllib2.URLError:
......@@ -268,7 +268,6 @@ def slapserver(config):
pkg_resources.resource_stream(__name__,
'template/ifcfg-br0.in').read())
# Creating default limits config
limits_conf_path = os.path.join(mount_dir_path,
'etc', 'security', 'limits.conf')
......@@ -335,7 +334,6 @@ def slapserver(config):
return 0
def prepare_scripts(config):
"""
Will prepare script for slapos dedicated computer
......@@ -348,9 +346,9 @@ def prepare_scripts(config):
else:
# Check for config file in /etc/slapos/
if os.path.exists('/etc/slapos/slapos.cfg'):
slapos_configuration='/etc/slapos/'
slapos_configuration = '/etc/slapos/'
else:
slapos_configuration='/etc/opt/slapos/'
slapos_configuration = '/etc/opt/slapos/'
# Creating boot script
path = os.path.join('/', 'usr', 'sbin', 'slapos-boot-dedicated')
......@@ -359,7 +357,7 @@ def prepare_scripts(config):
open(path, 'w').write(
pkg_resources.resource_stream(__name__,
'script/%s' % 'slapos').read()
% dict(slapos_configuration=slapos_configuration) )
% {'slapos_configuration': slapos_configuration})
os.chmod(path, 0755)
path = os.path.join('/', 'etc',
......@@ -396,7 +394,7 @@ def configureNtp():
continue
new_ntp.write(line)
new_ntp.write(SLAPOS_MARK)
new_ntp.write(server+'\n')
new_ntp.write(server + '\n')
new_ntp.close()
_call(['chkconfig', '--add', 'ntp'])
_call(['chkconfig', 'ntp', 'on'])
......@@ -404,8 +402,6 @@ def configureNtp():
_call(['systemctl', 'restart', 'ntp.service'])
class Config:
def setConfig(self, option_dict):
"""
......@@ -415,7 +411,6 @@ class Config:
for option, value in option_dict.__dict__.items():
setattr(self, option, value)
def slaposConfig(self,
mount_dir_path,
slapos_configuration,
......@@ -436,7 +431,6 @@ class Config:
self.temp_dir = temp_dir
self.computer_id = computer_id
def userConfig(self):
# XXX-Testme: test each possible scenario
# XXX don't use self.xx but return a dict so that it is stateless
......@@ -477,7 +471,6 @@ class Config:
return True
def displayUserConfig(self):
# XXX print everything from configuration, not arbitrary members
if self.certificates:
......@@ -550,7 +543,7 @@ def prepare_from_scratch(config):
print "Your Computer is : %s" % computer_id
config.slaposConfig(mount_dir_path = '/',
config.slaposConfig(mount_dir_path='/',
slapos_configuration=slapos_configuration,
hostname_path='/etc/HOSTNAME',
host_path='/etc/hosts',
......@@ -570,19 +563,17 @@ def prepare_from_scratch(config):
_call(['zypper', '--gpg-auto-import-keys', 'install', '-fy', 'slapos.node'])
_call(['systemctl', 'stop', 'slapos-node.service'])
return_code = 0
except ExecError, err:
except ExecError as err:
print >>sys.stderr, err.msg
return_code = 16
except SystemExit, err:
except SystemExit as err:
# Catch exception raise by optparse
return_code = err
if os.path.exists(temp_directory):
print "Deleting directory: %s" % temp_directory
_call(['rm', '-rf', temp_directory])
# Add/remove VPN file forcing/forbidding start of VPN.
if not config.dry_run:
openvpn_needed_file_path = os.path.join(slapos_configuration,
......@@ -609,26 +600,30 @@ def chownSlaposDirectory():
path = slapos_slapgrid_instance
print "Changing owners of software directory and partitions directories…"
for i in range(int(slapformat_partition)):
uid = getpwnam('%s%s' % (slapformat_user_base_name, i) )[2]
gid = getpwnam('%s%s' % (slapformat_user_base_name, i) )[3]
uid = pwd.getpwnam('%s%s' % (slapformat_user_base_name, i))[2]
gid = pwd.getpwnam('%s%s' % (slapformat_user_base_name, i))[3]
item = '%s%s' % (slapformat_partition_base_name, i)
if not os.path.islink(os.path.join(path, item)):
os.chown(os.path.join(path, item), uid, gid)
for i in range(int(slapformat_partition)):
path = "%s/%s%s" % ( slapos_slapgrid_instance, slapformat_partition_base_name, i)
path = "%s/%s%s" % (slapos_slapgrid_instance, slapformat_partition_base_name, i)
for root, dirs, files in os.walk(path):
for items in dirs, files:
for item in items:
if not os.path.islink(os.path.join(root, item)):
os.chown(os.path.join(root, item), getpwnam('%s%s' % (slapformat_user_base_name, i) )[2], getpwnam('%s%s' % (slapformat_user_base_name, i) )[3])
os.chown(os.path.join(root, item),
pwd.getpwnam('%s%s' % (slapformat_user_base_name, i))[2],
pwd.getpwnam('%s%s' % (slapformat_user_base_name, i))[3])
# chown of software root (/opt/slapgrid)
for root, dirs, files in os.walk(slapos_slapgrid_software):
for items in dirs, files:
for item in items:
if not os.path.islink(os.path.join(root, item)):
os.chown(os.path.join(root, item), getpwnam('slapsoft')[2], getpwnam('slapsoft')[3])
os.chown(os.path.join(root, item),
pwd.getpwnam('slapsoft')[2],
pwd.getpwnam('slapsoft')[3])
def slapprepare():
......@@ -642,8 +637,8 @@ def slapprepare():
# Install/update slapos
try:
_call(['zypper', 'addrepo', '-fc', '-n', '"SlapOS Official repo"'
,'http://download.opensuse.org/repositories/home:/VIFIBnexedi/openSUSE_%s/' % suse_version(), 'slapos'])
_call(['zypper', 'addrepo', '-fc', '-n', '"SlapOS Official repo"',
'http://download.opensuse.org/repositories/home:/VIFIBnexedi/openSUSE_%s/' % suse_version(), 'slapos'])
except:
pass
_call(['zypper', '--gpg-auto-import-keys', 'install', '-y', 'slapos.node'])
......@@ -671,14 +666,14 @@ def slapprepare():
_call(['systemctl', 'start', 'slapos-boot-dedicated.service'])
return_code = 0
except UsageError, err:
except UsageError as err:
print >>sys.stderr, err.msg
print >>sys.stderr, "For help use --help"
return_code = 16
except ExecError, err:
except ExecError as err:
print >>sys.stderr, err.msg
return_code = 16
except SystemExit, err:
except SystemExit as err:
# Catch exception raise by optparse
return_code = err
sys.exit(return_code)
......@@ -686,4 +681,3 @@ def slapprepare():
def main():
slapprepare()
......@@ -31,7 +31,6 @@
import ConfigParser
import datetime
import logging
from slapos.networkcachehelper import helper_download_network_cached_to_file
from optparse import OptionParser, Option
import os
import subprocess as sub
......@@ -39,6 +38,9 @@ import sys
import tempfile
import autoupdate
from slapos.networkcachehelper import helper_download_network_cached_to_file
# create console handler and set level to warning
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)
......@@ -64,7 +66,7 @@ class Parser(OptionParser):
Option("--srv-file",
default='/srv/slapupdate',
help="Server status file."),
Option("-v","--verbose",
Option("-v", "--verbose",
default=False,
action="store_true",
help="Verbose output."),
......@@ -74,7 +76,6 @@ class Parser(OptionParser):
action="store_true"),
])
def check_args(self):
"""
Check arguments
......@@ -84,19 +85,19 @@ class Parser(OptionParser):
class NetworkCache ():
def __init__(self,slapos_conf):
def __init__(self, slapos_conf):
if os.path.exists(slapos_conf):
network_cache_info = ConfigParser.RawConfigParser()
network_cache_info.read(slapos_conf)
self.download_binary_cache_url = network_cache_info.get('networkcache','download-binary-cache-url')
self.download_cache_url = network_cache_info.get('networkcache','download-cache-url')
self.download_binary_dir_url = network_cache_info.get('networkcache','download-binary-dir-url')
self.signature_certificate_list = network_cache_info.get('networkcache','signature-certificate-list')
self.download_binary_cache_url = network_cache_info.get('networkcache', 'download-binary-cache-url')
self.download_cache_url = network_cache_info.get('networkcache', 'download-cache-url')
self.download_binary_dir_url = network_cache_info.get('networkcache', 'download-binary-dir-url')
self.signature_certificate_list = network_cache_info.get('networkcache', 'signature-certificate-list')
else:
self.download_binary_cache_url = "http://www.shacache.org/shacache"
self.download_cache_url = "https://www.shacache.org/shacache"
self.download_binary_dir_url = "http://www.shacache.org/shadir"
self.signature_certificate_list =""
self.signature_certificate_list = ""
self.signature_certificate_list = """
-----BEGIN CERTIFICATE-----
......@@ -115,8 +116,6 @@ jlD7tZ2DbRKHu5FadsKWNZpqC9H0BRLjBwY=
""" + self.signature_certificate_list
def _call(cmd_args, stdout=sub.PIPE, stderr=sub.PIPE, dry_run=False):
"""
Wrapper for subprocess.call() which'll secure the usage of external program's.
......@@ -128,56 +127,57 @@ def _call(cmd_args, stdout=sub.PIPE, stderr=sub.PIPE, dry_run=False):
"""
print ("Calling: %s" % ' '.join(cmd_args))
if not dry_run :
p = sub.Popen(cmd_args,stdout=stdout,stderr=stderr)
output,err = p.communicate()
return output,err
if not dry_run:
p = sub.Popen(cmd_args, stdout=stdout, stderr=stderr)
output, err = p.communicate()
return output, err
def suse_version():
"""
Return OpenSUSE version if it is SuSE
"""
if os.path.exists('/etc/SuSE-release') :
with open('/etc/SuSE-release') as f :
if os.path.exists('/etc/SuSE-release'):
with open('/etc/SuSE-release') as f:
for line in f:
if "VERSION" in line:
dist = line.split()
return float(dist[2])
else :
else:
return 0
def repositories_purge ():
def repositories_purge():
"""
Remove all repositories
"""
listing,err = _call(['zypper','lr'])
while listing.count('\n') > 2 :
output,err = _call(['zypper','rr','1'],stdout=None)
listing,err = _call(['zypper','lr'])
listing, err = _call(['zypper', 'lr'])
while listing.count('\n') > 2:
output, err = _call(['zypper', 'rr', '1'], stdout=None)
listing, err = _call(['zypper', 'lr'])
def repositories_add (url,alias):
def repositories_add(url, alias):
""" Add a repository """
output,err = _call(['zypper','ar','-fc',url,alias],stdout=None)
output, err = _call(['zypper', 'ar', '-fc', url, alias], stdout=None)
def update_software ():
def update_software():
""" Upgrade softwares """
_call(['zypper','--gpg-auto-import-keys','up','-ly']
, stdout=None)
_call(['zypper', '--gpg-auto-import-keys', 'up', '-ly'], stdout=None)
def update_system ():
def update_system():
""" Dist-Upgrade of system """
_call(['zypper','--gpg-auto-import-keys','dup','-ly'], stdout = None)
_call(['zypper', '--gpg-auto-import-keys', 'dup', '-ly'], stdout=None)
def update_slapprepare_scripts ():
def update_slapprepare_scripts():
""" Run slapprepare -u (script that upgrade boot scripts and so on) """
_call(['slapprepare','-u'], stdout = None)
_call(['slapprepare', '-u'], stdout=None)
def download_info_from_networkcache(path,slapos_conf):
def download_info_from_networkcache(path, slapos_conf):
"""
Download a tar of the repository from cache, and untar it.
"""
......@@ -213,28 +213,30 @@ def get_info_from_master(config):
info, path = tempfile.mkstemp()
if not download_info_from_networkcache(
path, config.slapos_configuration) == False:
print open(path,'r').read()
print open(path).read()
return path
else :
else:
raise ValueError("No result from shacache")
def repositories_process(repositories):
"""
Remove and then add needed repositories
"""
repositories_purge()
for key in repositories :
repositories_add(repositories[key],key)
for key in repositories:
repositories_add(repositories[key], key)
def save_current_state(current_state,config):
def save_current_state(current_state, config):
"""
Will save ConfigParser to config file
"""
file = open(config.srv_file,"w")
file = open(config.srv_file, "w")
current_state.write(file)
file.close()
def update_machine(config):
"""
Will fetch information from web and update and/or reboot
......@@ -253,15 +255,15 @@ def update_machine(config):
next_state_file = get_info_from_master(config)
next_state.read(next_state_file)
os.remove(next_state_file)
config.getSystemInfo(current_state,next_state)
config.getSystemInfo(current_state, next_state)
config.displayConfig()
# Check if run for first time
if config.first_time:
current_state.add_section('system')
current_state.set('system','reboot',config.today.isoformat())
current_state.set('system','upgrade',config.today.isoformat())
save_current_state(current_state,config)
current_state.set('system', 'reboot', config.today.isoformat())
current_state.set('system', 'upgrade', config.today.isoformat())
save_current_state(current_state, config)
# Purge repositories list and add new ones
repositories_process(dict(next_state.items('repositories')))
# Check if dist-upgrade is needed
......@@ -270,17 +272,17 @@ def update_machine(config):
update_slapprepare_scripts()
update_system()
os.system('reboot')
else :
else:
logger.info("We will now upgrade your packages")
update_software()
autoupdate.do_update()
else:
if config.last_upgrade < config.upgrade :
if config.last_upgrade < config.upgrade:
# Purge repositories list and add new ones
repositories_process(dict(next_state.items('repositories')))
current_state.set('system','upgrade',config.today.isoformat())
save_current_state(current_state,config)
current_state.set('system', 'upgrade', config.today.isoformat())
save_current_state(current_state, config)
if suse_version() < config.opensuse_version:
logger.info("We will now upgrade your system")
update_system()
......@@ -288,12 +290,12 @@ def update_machine(config):
logger.info("We will now upgrade your packages")
update_software()
autoupdate.do_update()
else :
else:
logger.info("Your system is up to date")
if config.last_reboot < config.reboot :
current_state.set('system','reboot',config.today.isoformat())
save_current_state(current_state,config)
if config.last_reboot < config.reboot:
current_state.set('system', 'reboot', config.today.isoformat())
save_current_state(current_state, config)
os.system('reboot')
......@@ -315,39 +317,39 @@ class Config:
# add ch to logger
self.logger.addHandler(ch)
if self.verbose :
if self.verbose:
ch.setLevel(logging.DEBUG)
def getSystemInfo(self,current_state,next_state):
def getSystemInfo(self, current_state, next_state):
"""
Extract information from config file and server file
"""
self.reboot = datetime.datetime.strptime(next_state.get('system','reboot'),
self.reboot = datetime.datetime.strptime(next_state.get('system', 'reboot'),
"%Y-%m-%d").date()
self.upgrade = datetime.datetime.strptime(next_state.get('system','upgrade'),
self.upgrade = datetime.datetime.strptime(next_state.get('system', 'upgrade'),
"%Y-%m-%d").date()
self.opensuse_version = next_state.getfloat('system','opensuse_version')
self.opensuse_version = next_state.getfloat('system', 'opensuse_version')
if not current_state.has_section('system'):
self.first_time = True
else:
self.first_time = False
self.last_reboot = datetime.datetime.strptime(
current_state.get('system','reboot'),
current_state.get('system', 'reboot'),
"%Y-%m-%d").date()
self.last_upgrade = datetime.datetime.strptime(
current_state.get('system','upgrade'),
current_state.get('system', 'upgrade'),
"%Y-%m-%d").date()
def displayConfig(self):
"""
Display Config
"""
self.logger.debug( "reboot %s" % self.reboot)
self.logger.debug( "upgrade %s" % self.upgrade)
self.logger.debug( "suse version %s" % self.opensuse_version)
if not self.first_time :
self.logger.debug( "Last reboot : %s" % self.last_reboot)
self.logger.debug( "Last upgrade : %s" % self.last_upgrade)
self.logger.debug("reboot %s" % self.reboot)
self.logger.debug("upgrade %s" % self.upgrade)
self.logger.debug("suse version %s" % self.opensuse_version)
if not self.first_time:
self.logger.debug("Last reboot : %s" % self.last_reboot)
self.logger.debug("Last upgrade : %s" % self.last_upgrade)
def main():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment