Commit 4a7af070 authored by Marco Mariani's avatar Marco Mariani

prepare: further cleanup

parent f1b623c2
......@@ -106,8 +106,10 @@ def _call(cmd_args, stdout=None, stderr=None, dry_run=False):
(e, ' '.join(cmd_args)))
# Utility function to get yes/no answers
def get_yes_no(prompt, default=None):
"""
Utility function to get yes/no answers
"""
if default:
def_value = '/ Default yes'
elif default is False:
......@@ -136,19 +138,22 @@ def getSlaposConfiguration(slapos_configuration_file_path=None):
return {}
# Return OpenSUSE version if it is SuSE
def suse_version():
"""
Return OpenSUSE version if it is SuSE
"""
if not os.path.exists('/etc/SuSE-release'):
return 0
with open('/etc/SuSE-release') as f:
for line in f:
if "VERSION" in line:
dist = line.split()
return float(dist[2])
for line in open('/etc/SuSE-release'):
if 'VERSION' in line:
dist = line.split()
return float(dist[2])
# Parse certificate to get computer name and return it
def get_computer_name(slapos_configuration):
"""
Parse certificate to get computer name and return it
"""
try:
return getSlaposConfiguration(slapos_configuration).get('slapos',
'computer_id')
......@@ -169,9 +174,7 @@ def setup_bridge(slapos_configuration, create_tap):
slapos_cfg.write(fout)
# Function to get ssh key
def get_ssh(temp_dir):
# Downloading ssh_key
def get_ssh_keys(temp_dir):
count = 10
gotten = True
while count:
......@@ -196,9 +199,8 @@ def get_ssh(temp_dir):
break
else:
count -= 1
ssh_file = open(os.path.join(temp_dir, "authorized_keys"), "w")
ssh_file.write(''.join(ssh_pub_key))
ssh_file.close()
with open(os.path.join(temp_dir, 'authorized_keys'), 'w') as ssh_file:
ssh_file.write(ssh_pub_key)
return 0
......@@ -225,15 +227,13 @@ def slapserver(config):
mount_dir_path = config.mount_dir_path
try:
# Setting hostname
hostname_path = os.path.normpath('/'.join([mount_dir_path,
config.hostname_path]))
hostname_path = os.path.normpath(os.path.join(mount_dir_path, config.hostname_path))
print "Setting hostname in : %s" % hostname_path
if not dry_run:
open(hostname_path, 'w').write("%s\n" % config.computer_id)
# Adding the hostname as a valid address
host_path = os.path.normpath('/'.join([mount_dir_path,
config.host_path]))
host_path = os.path.normpath(os.path.join(mount_dir_path, config.host_path))
print "Creating %r" % host_path
if not dry_run:
open(host_path, 'w').write(
......@@ -243,8 +243,7 @@ def slapserver(config):
})
# Creating safe sshd_config
sshd_path = os.path.normpath('/'.join([mount_dir_path, 'etc', 'ssh',
'sshd_config']))
sshd_path = os.path.normpath(os.path.join(mount_dir_path, 'etc', 'ssh', 'sshd_config'))
print "Creating %r" % sshd_path
if not dry_run:
open(sshd_path, 'w').write(
......@@ -253,8 +252,7 @@ def slapserver(config):
os.chmod(sshd_path, 0o600)
# Creating default bridge config
br0_path = os.path.normpath('/'.join([mount_dir_path, 'etc',
'sysconfig', 'network', 'ifcfg-br0']))
br0_path = os.path.normpath(os.path.join(mount_dir_path, 'etc', 'sysconfig', 'network', 'ifcfg-br0'))
print "Creating %r" % br0_path
if not dry_run:
open(br0_path, 'w').write(
......@@ -272,10 +270,9 @@ def slapserver(config):
# Writing ssh key
if config.need_ssh:
user_path = os.path.normpath('/'.join([mount_dir_path, 'root']))
ssh_key_directory = os.path.normpath('/'.join([user_path, '.ssh']))
ssh_key_path = os.path.normpath('/'.join([ssh_key_directory,
'authorized_keys']))
user_path = os.path.normpath(os.path.join(mount_dir_path, 'root'))
ssh_key_directory = os.path.normpath(os.path.join(user_path, '.ssh'))
ssh_key_path = os.path.normpath(os.path.join(ssh_key_directory, 'authorized_keys'))
stat_info = os.stat(user_path)
uid, gid = stat_info.st_uid, stat_info.st_gid
ssh_key_directory = os.path.dirname(ssh_key_path)
......@@ -290,7 +287,7 @@ def slapserver(config):
print "Creating file: %s" % ssh_key_path
if not dry_run:
open(ssh_key_path, 'a').write(''.join(open(config.key_path).read()))
open(ssh_key_path, 'a').write(open(config.key_path).read())
if not dry_run:
print "Setting uid:gid of %r to %s:%s" % (ssh_key_path, uid, gid)
......@@ -349,7 +346,7 @@ def prepare_scripts(config):
if not dry_run:
open(path, 'w').write(
pkg_resources.resource_stream(__name__,
'script/%s' % 'slapos').read() % {
'script/slapos').read() % {
'slapos_configuration': slapos_configuration
})
os.chmod(path, 0o755)
......@@ -359,7 +356,7 @@ def prepare_scripts(config):
if not dry_run:
open(path, 'w').write(
pkg_resources.resource_stream(__name__,
'script/%s' % 'slapos.service').read() % {
'script/slapos.service').read() % {
'slapos_configuration': slapos_configuration
})
os.chmod(path, 0o755)
......@@ -370,7 +367,7 @@ def prepare_scripts(config):
if not dry_run:
open(path, 'w').write(
pkg_resources.resource_stream(__name__,
'template/%s' % 'clientipv4.conf.in').read())
'template/clientipv4.conf.in').read())
os.chmod(path, 0o755)
# Remove old-timers scripts
......@@ -470,8 +467,8 @@ class Config:
print "Will register a computer on master"
if self.master_url:
print "URL of master: %s" % self.master_url
print "URL \"web\" of master: %s" % self.master_url_web
print "Number of partition: %s" % (self.partition_amount)
print 'URL "web" of master: %s' % self.master_url_web
print "Number of partition: %s" % self.partition_amount
print "Computer name: %s" % self.computer_name
print "Network bridge for hosted VMs: %s" % self.need_bridge
print "Ipv6 over VPN: %s" % self.force_vpn
......@@ -529,7 +526,7 @@ def prepare_from_scratch(config):
else:
setup_bridge(slapos_configuration, False)
else:
print 'Warning: impossible to set up bridge because slapos configuration doesn\'t exist.'
print "Warning: impossible to set up bridge because slapos configuration doesn't exist."
computer_id = get_computer_name(
os.path.join(slapos_configuration, 'slapos.cfg'))
......@@ -547,7 +544,7 @@ def prepare_from_scratch(config):
# Prepare SlapOS Suse Server confuguration
if config.need_ssh:
get_ssh(temp_directory)
get_ssh_keys(temp_directory)
slapserver(config)
......@@ -593,21 +590,23 @@ def chownSlaposDirectory():
path = slapos_slapgrid_instance
print "Changing owners of software directory and partitions directories…"
for i in range(int(slapformat_partition)):
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)
user = '%s%s' % (slapformat_user_base_name, i)
uid = pwd.getpwnam(user).pw_uid
gid = pwd.getpwnam(user).pw_gid
partition = '%s%s' % (slapformat_partition_base_name, i)
if not os.path.islink(os.path.join(path, partition)):
os.chown(os.path.join(path, partition), uid, gid)
for i in range(int(slapformat_partition)):
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),
pwd.getpwnam('%s%s' % (slapformat_user_base_name, i))[2],
pwd.getpwnam('%s%s' % (slapformat_user_base_name, i))[3])
user = '%s%s' % (slapformat_user_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),
pwd.getpwnam(user).pw_uid,
pwd.getpwnam(user).pw_gid)
# chown of software root (/opt/slapgrid)
for root, dirs, files in os.walk(slapos_slapgrid_software):
......@@ -615,8 +614,8 @@ def chownSlaposDirectory():
for item in items:
if not os.path.islink(os.path.join(root, item)):
os.chown(os.path.join(root, item),
pwd.getpwnam('slapsoft')[2],
pwd.getpwnam('slapsoft')[3])
pwd.getpwnam('slapsoft').pw_uid,
pwd.getpwnam('slapsoft').pw_gid)
def slapprepare():
......
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