Commit 4884a05a authored by Marco Mariani's avatar Marco Mariani

prepare: general, light cleanup

parent 74ef042c
...@@ -62,8 +62,7 @@ class Parser(OptionParser): ...@@ -62,8 +62,7 @@ class Parser(OptionParser):
help="Simulate the execution steps", help="Simulate the execution steps",
default=False, default=False,
action="store_true"), action="store_true"),
] ])
)
def check_args(self): def check_args(self):
""" """
...@@ -122,15 +121,14 @@ def get_yes_no(prompt, default=None): ...@@ -122,15 +121,14 @@ def get_yes_no(prompt, default=None):
if answer.upper() in ['N', 'NO']: if answer.upper() in ['N', 'NO']:
return False return False
if not answer and default is not None: if not answer and default is not None:
return default return default
def getSlaposConfiguration(slapos_configuration_file_path=None): def getSlaposConfiguration(slapos_configuration_file_path=None):
for path in [ for path in [
slapos_configuration_file_path, slapos_configuration_file_path,
'/etc/opt/slapos/slapos.cfg', '/etc/opt/slapos/slapos.cfg',
'/etc/slapos/slapos.cfg' '/etc/slapos/slapos.cfg']:
]:
if path and os.path.isfile(path): if path and os.path.isfile(path):
config = ConfigParser.RawConfigParser() config = ConfigParser.RawConfigParser()
config.read(path) config.read(path)
...@@ -176,17 +174,16 @@ def get_ssh(temp_dir): ...@@ -176,17 +174,16 @@ def get_ssh(temp_dir):
# Downloading ssh_key # Downloading ssh_key
count = 10 count = 10
gotten = True gotten = True
while count > 0: while count:
try: try:
print "Enter the url of your public ssh key" print "Enter the url of your public ssh key"
ssh_web = raw_input('--> ') ssh_web = raw_input('--> ')
try: try:
ssh_key_all = urllib2.urlopen(''.join(ssh_web)) ssh_key_all = urllib2.urlopen(ssh_web)
gotten = True gotten = True
except ValueError: except ValueError:
# add http:// if it is missing (needed by urllib2) # add http:// if it is missing (needed by urllib2)
ssh_web = """http://""" + ssh_web ssh_key_all = urllib2.urlopen('http://%s' % ssh_web)
ssh_key_all = urllib2.urlopen(''.join(ssh_web))
gotten = True gotten = True
except urllib2.URLError: except urllib2.URLError:
print " URL ERROR" print " URL ERROR"
...@@ -241,8 +238,9 @@ def slapserver(config): ...@@ -241,8 +238,9 @@ def slapserver(config):
if not dry_run: if not dry_run:
open(host_path, 'w').write( open(host_path, 'w').write(
pkg_resources.resource_stream(__name__, pkg_resources.resource_stream(__name__,
'template/hosts.in').read() % dict( 'template/hosts.in').read() % {
computer_id=config.computer_id)) 'computer_id': config.computer_id
})
# Creating safe sshd_config # Creating safe sshd_config
sshd_path = os.path.normpath('/'.join([mount_dir_path, 'etc', 'ssh', sshd_path = os.path.normpath('/'.join([mount_dir_path, 'etc', 'ssh',
...@@ -252,7 +250,7 @@ def slapserver(config): ...@@ -252,7 +250,7 @@ def slapserver(config):
open(sshd_path, 'w').write( open(sshd_path, 'w').write(
pkg_resources.resource_stream(__name__, pkg_resources.resource_stream(__name__,
'template/sshd_config.in').read()) 'template/sshd_config.in').read())
os.chmod(sshd_path, 0600) os.chmod(sshd_path, 0o600)
# Creating default bridge config # Creating default bridge config
br0_path = os.path.normpath('/'.join([mount_dir_path, 'etc', br0_path = os.path.normpath('/'.join([mount_dir_path, 'etc',
...@@ -288,16 +286,16 @@ def slapserver(config): ...@@ -288,16 +286,16 @@ def slapserver(config):
if not dry_run: if not dry_run:
print "Setting uid:gid of %r to %s:%s" % (ssh_key_directory, uid, gid) print "Setting uid:gid of %r to %s:%s" % (ssh_key_directory, uid, gid)
os.chown(ssh_key_directory, uid, gid) os.chown(ssh_key_directory, uid, gid)
os.chmod(ssh_key_directory, 0700) os.chmod(ssh_key_directory, 0o700)
print "Creating file: %s" % ssh_key_path print "Creating file: %s" % ssh_key_path
if not dry_run: if not dry_run:
open(ssh_key_path, 'a').write(''.join(open(config.key_path, 'r').read())) open(ssh_key_path, 'a').write(''.join(open(config.key_path).read()))
if not dry_run: if not dry_run:
print "Setting uid:gid of %r to %s:%s" % (ssh_key_path, uid, gid) print "Setting uid:gid of %r to %s:%s" % (ssh_key_path, uid, gid)
os.chown(ssh_key_path, uid, gid) os.chown(ssh_key_path, uid, gid)
os.chmod(ssh_key_path, 0600) os.chmod(ssh_key_path, 0o600)
# Put file to force VPN if user asked # Put file to force VPN if user asked
if config.force_slapcontainer: if config.force_slapcontainer:
...@@ -312,7 +310,7 @@ def slapserver(config): ...@@ -312,7 +310,7 @@ def slapserver(config):
if not dry_run: if not dry_run:
open(path, 'w').write(pkg_resources.resource_stream(__name__, open(path, 'w').write(pkg_resources.resource_stream(__name__,
'script/%s' % script).read()) 'script/%s' % script).read())
os.chmod(path, 0755) os.chmod(path, 0o755)
else: else:
for script in ['slapos_firstboot']: for script in ['slapos_firstboot']:
path = os.path.join(mount_dir_path, 'etc', 'init.d', script) path = os.path.join(mount_dir_path, 'etc', 'init.d', script)
...@@ -346,34 +344,34 @@ def prepare_scripts(config): ...@@ -346,34 +344,34 @@ def prepare_scripts(config):
slapos_configuration = '/etc/opt/slapos/' slapos_configuration = '/etc/opt/slapos/'
# Creating boot script # Creating boot script
path = os.path.join('/', 'usr', 'sbin', 'slapos-boot-dedicated') path = '/usr/sbin/slapos-boot-dedicated'
print "Creating %r" % path print "Creating %r" % path
if not dry_run: if not dry_run:
open(path, 'w').write( open(path, 'w').write(
pkg_resources.resource_stream(__name__, pkg_resources.resource_stream(__name__,
'script/%s' % 'slapos').read() 'script/%s' % 'slapos').read() % {
% {'slapos_configuration': slapos_configuration}) 'slapos_configuration': slapos_configuration
os.chmod(path, 0755) })
os.chmod(path, 0o755)
path = os.path.join('/', 'etc', path = '/etc/systemd/system/slapos-boot-dedicated.service'
'systemd', 'system',
'slapos-boot-dedicated.service')
print "Creating %r" % path print "Creating %r" % path
if not dry_run: if not dry_run:
open(path, 'w').write( open(path, 'w').write(
pkg_resources.resource_stream(__name__, pkg_resources.resource_stream(__name__,
'script/%s' % 'slapos.service').read() 'script/%s' % 'slapos.service').read() % {
% dict(slapos_configuration=slapos_configuration)) 'slapos_configuration': slapos_configuration
os.chmod(path, 0755) })
os.chmod(path, 0o755)
# add clientipv4 # add clientipv4
path = os.path.join('/', 'etc', 'openvpn', 'clientipv4.conf') path = '/etc/openvpn/clientipv4.conf'
print "Creating %r" % path print "Creating %r" % path
if not dry_run: if not dry_run:
open(path, 'w').write( open(path, 'w').write(
pkg_resources.resource_stream(__name__, pkg_resources.resource_stream(__name__,
'template/%s' % 'clientipv4.conf.in').read()) 'template/%s' % 'clientipv4.conf.in').read())
os.chmod(path, 0755) os.chmod(path, 0o755)
# Remove old-timers scripts # Remove old-timers scripts
remove_former_scripts(slapos_configuration) remove_former_scripts(slapos_configuration)
...@@ -382,7 +380,7 @@ def prepare_scripts(config): ...@@ -382,7 +380,7 @@ def prepare_scripts(config):
def configureNtp(): def configureNtp():
"""Configures NTP daemon""" """Configures NTP daemon"""
server = "server pool.ntp.org" server = "server pool.ntp.org"
old_ntp = open('/etc/ntp.conf', 'r').readlines() old_ntp = open('/etc/ntp.conf').readlines()
new_ntp = open('/etc/ntp.conf', 'w') new_ntp = open('/etc/ntp.conf', 'w')
for line in old_ntp: for line in old_ntp:
if line.startswith('server'): if line.startswith('server'):
...@@ -439,13 +437,13 @@ class Config: ...@@ -439,13 +437,13 @@ class Config:
if not get_yes_no("A SlapOS Node configuration has been found. Do you want to overwrite it?", False): if not get_yes_no("A SlapOS Node configuration has been found. Do you want to overwrite it?", False):
print "Okay, let's start from scratch." print "Okay, let's start from scratch."
return False return False
self.master_url = raw_input("""SlapOS Master URL? Empty answer will make it use slapos.org Master: """) self.master_url = raw_input("SlapOS Master URL? Empty answer will make it use slapos.org Master: ")
if self.master_url: if self.master_url:
self.master_url_web = raw_input("""SlapOS Master "web" URL?: """) self.master_url_web = raw_input('SlapOS Master "web" URL?: ')
else: else:
self.master_url_web = '' self.master_url_web = ''
self.computer_name = raw_input("Define a unique name for this computer: ") self.computer_name = raw_input("Define a unique name for this computer: ")
self.partition_amount = raw_input("""Number of SlapOS partitions for this computer? Default is 20: """) self.partition_amount = raw_input("Number of SlapOS partitions for this computer? Default is 20: ")
if self.partition_amount == '': if self.partition_amount == '':
self.partition_amount = '20' self.partition_amount = '20'
elif not already_configured: elif not already_configured:
...@@ -484,10 +482,10 @@ class Config: ...@@ -484,10 +482,10 @@ class Config:
def prepare_from_scratch(config): def prepare_from_scratch(config):
try: try:
temp_directory = os.path.join('/tmp/slaptemp/') temp_directory = '/tmp/slaptemp/'
if not os.path.exists(temp_directory): if not os.path.exists(temp_directory):
print "Creating directory: %s" % temp_directory print "Creating directory: %s" % temp_directory
os.mkdir(temp_directory, 0711) os.mkdir(temp_directory, 0o711)
while True: while True:
if config.userConfig(): if config.userConfig():
...@@ -534,7 +532,7 @@ def prepare_from_scratch(config): ...@@ -534,7 +532,7 @@ def prepare_from_scratch(config):
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( computer_id = get_computer_name(
os.path.join('/', slapos_configuration, 'slapos.cfg')) os.path.join(slapos_configuration, 'slapos.cfg'))
print "Your Computer is : %s" % computer_id print "Your Computer is : %s" % computer_id
...@@ -543,7 +541,7 @@ def prepare_from_scratch(config): ...@@ -543,7 +541,7 @@ def prepare_from_scratch(config):
hostname_path='/etc/HOSTNAME', hostname_path='/etc/HOSTNAME',
host_path='/etc/hosts', host_path='/etc/hosts',
key_path=os.path.join(temp_directory, 'authorized_keys'), key_path=os.path.join(temp_directory, 'authorized_keys'),
master_url="""https://slap.vifib.com""", master_url='https://slap.vifib.com',
temp_dir=temp_directory, temp_dir=temp_directory,
computer_id=computer_id) computer_id=computer_id)
......
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