Merge pull request #19 from petterreinholdtsen/first-boot-set-hostname

Change first_boot module to show and update current hostname,
This commit is contained in:
Nick Daly 2013-09-15 17:43:31 -07:00
commit 32a86a54b5
2 changed files with 15 additions and 20 deletions

View File

@ -7,6 +7,7 @@ from forms import Form
import util as u import util as u
from withsqlite.withsqlite import sqlite_db from withsqlite.withsqlite import sqlite_db
import cfg import cfg
import config
class FirstBoot(PagePlugin): class FirstBoot(PagePlugin):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -18,18 +19,13 @@ class FirstBoot(PagePlugin):
return self.state0(*args, **kwargs) return self.state0(*args, **kwargs)
## TODO: flesh out these tests values ## TODO: flesh out these tests values
def valid_box_name_p(self, name):
name = name.strip()
if re.search("\W", name):
return False
return True
def valid_box_key_p(self, key): def valid_box_key_p(self, key):
return True return True
def generate_box_key(self): def generate_box_key(self):
return "fake key" return "fake key"
@cherrypy.expose @cherrypy.expose
def state0(self, message="", box_name="", box_key="", submitted=False): def state0(self, message="", hostname="", box_key="", submitted=False):
""" """
In this state, we do time config over HTTP, name the box and In this state, we do time config over HTTP, name the box and
server key selection. server key selection.
@ -49,15 +45,13 @@ class FirstBoot(PagePlugin):
## Must resist the sick temptation to write an LDAP interface to the sqlite file ## Must resist the sick temptation to write an LDAP interface to the sqlite file
with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as db: with sqlite_db(cfg.store_file, table="thisbox", autocommit=True) as db:
db['about'] = "This table is for information about this FreedomBox" db['about'] = "This table is for information about this FreedomBox"
if box_name: if hostname:
if self.valid_box_name_p(box_name): if '' == config.valid_hostname(hostname):
db['box_name'] = box_name config.set_hostname(hostname)
else: else:
message += _("Invalid box name.") message += _("Invalid box name: %s") % config.valid_hostname(hostname)
elif 'box_name' in db and db['box_name']: else:
box_name = db['box_name'] hostname = config.get_hostname()
#TODO: set /etc/hostname to box name via ex machina
if box_key: if box_key:
if self.valid_box_key_p(box_key): if self.valid_box_key_p(box_key):
db['box_key'] = box_key db['box_key'] = box_key
@ -70,7 +64,7 @@ class FirstBoot(PagePlugin):
db['box_key'] = box_key db['box_key'] = box_key
if box_name and box_key and self.valid_box_name_p(box_name) and self.valid_box_key_p(box_key): if hostname and box_key and '' == config.valid_hostname(hostname) and self.valid_box_key_p(box_key):
## Update state to 1 and head there ## Update state to 1 and head there
with sqlite_db(cfg.store_file, table="firstboot", autocommit=True) as db: with sqlite_db(cfg.store_file, table="firstboot", autocommit=True) as db:
db['state']=1 db['state']=1
@ -81,11 +75,9 @@ class FirstBoot(PagePlugin):
action="/firstboot", action="/firstboot",
name="whats_my_name", name="whats_my_name",
message=message) message=message)
if not box_name:
box_name = cfg.box_name
form.html("<p>For convenience, your FreedomBox needs a name. It should be something short that doesn't contain spaces or punctuation. 'Willard' would be a good name. 'Freestyle McFreedomBox!!!' would not.</p>") form.html("<p>For convenience, your FreedomBox needs a name. It should be something short that doesn't contain spaces or punctuation. 'Willard' would be a good name. 'Freestyle McFreedomBox!!!' would not.</p>")
form.text_input('Name your FreedomBox', id="box_name", value=box_name) form.text_input('Name your FreedomBox', id="hostname", value=hostname)
form.html("<p>%(box_name)s uses cryptographic keys so it can prove its identity when talking to you. %(box_name)s can make a key for itself, but if one already exists (from a prior FreedomBox, for example), you can paste it below. This key should not be the same as your key because you are not your FreedomBox!</p>" % {'box_name':cfg.box_name}) form.html("<p>%(hostname)s uses cryptographic keys so it can prove its identity when talking to you. %(hostname)s can make a key for itself, but if one already exists (from a prior FreedomBox, for example), you can paste it below. This key should not be the same as your key because you are not your FreedomBox!</p>" % {'hostname':cfg.box_name})
form.text_box("If you want, paste your box's key here.", id="box_key", value=box_key) form.text_box("If you want, paste your box's key here.", id="box_key", value=box_key)
form.hidden(name="submitted", value="True") form.hidden(name="submitted", value="True")
form.submit("Box it up!") form.submit("Box it up!")

View File

@ -43,6 +43,9 @@ def valid_hostname(name):
return message return message
def get_hostname():
return gethostname()
def set_hostname(hostname): def set_hostname(hostname):
"Sets machine hostname to hostname" "Sets machine hostname to hostname"
cfg.log.info("Writing '%s' to /etc/hostname with exmachina" % hostname) cfg.log.info("Writing '%s' to /etc/hostname with exmachina" % hostname)
@ -56,7 +59,7 @@ def set_hostname(hostname):
if platform.linux_distribution()[0]=="Ubuntu" : if platform.linux_distribution()[0]=="Ubuntu" :
cfg.exmachina.service.start("hostname") cfg.exmachina.service.start("hostname")
else: else:
cfg.exmachina.initd.restart("hostname.sh") # is hostname.sh debian-only? cfg.exmachina.initd.start("hostname.sh") # is hostname.sh debian-only?
except OSError, e: except OSError, e:
raise cherrypy.HTTPError(500, "Hostname restart failed: %s" % e) raise cherrypy.HTTPError(500, "Hostname restart failed: %s" % e)