Permalink
Browse files

Sets the hostname on the hypervisor based of the vm registration. Cre…

…ates a puck user on the hypervisor with the selected ssh keys.
  • Loading branch information...
1 parent f165022 commit 36d2d3d4929af3ee744d7c1b0a0f234a1a74f1da @masom committed Apr 19, 2012
Showing with 55 additions and 2 deletions.
  1. +1 −1 client/pixie.conf
  2. +54 −1 client/pixie/lib/setup_plugin.py
View
@@ -9,7 +9,7 @@ setup_plugin.jail_dir = "/usr/jails"
setup_plugin.yum_repo_dir = "/usr/local/etc/yum.repos.d"
setup_plugin.ftp_mirror = "10.0.253.23"
setup_plugin.ezjail_options = ['-m']
-setup_plugin.tasks = [pixie.lib.setup_plugin.HypervisorSetupTask, pixie.lib.setup_plugin.EZJailSetupTask, pixie.lib.setup_plugin.InterfacesSetupTask, pixie.lib.setup_plugin.JailConfigTask, pixie.lib.setup_plugin.JailStartupTask]
+setup_plugin.tasks = [pixie.lib.setup_plugin.HypervisorSetupTask, pixie.lib.setup_plugin.EZJailSetupTask, pixie.lib.setup_plugin.InterfacesSetupTask, pixie.lib.setup_plugin.SSHTask,pixie.lib.setup_plugin.JailConfigTask, pixie.lib.setup_plugin.JailStartupTask]
hypervisor.jail_sysvipc_allow = "YES"
hypervisor.syslogd_flags = "-ss"
@@ -16,7 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import threading, Queue as queue, time, subprocess, shlex, datetime
-import urllib, tarfile, os, shutil, tmpfile
+import urllib, tarfile, os, shutil, tmpfile, pwd
import cherrypy
from cherrypy.process import wspbus, plugins
from pixie.lib.jails import EzJail
@@ -86,6 +86,36 @@ def _enable_ezjail(self):
with open('/etc/rc.conf', 'a') as f:
f.write("ezjail_enable=\"YES\"\n")
+class SSHTask(SetupTask):
+ '''Create the base user `puck` and add the authorized ssh keys'''
+
+ def run(self):
+ if not self.vm.keys:
+ self.log("No keys to install.");
+ return True
+
+ #@TODO Could be moved to config values instead of hardcoded.
+ user = 'puck'
+ try:
+ pwd.getpwnam(user)
+ except KeyError as e:
+ cmd = 'pw user add %s -m -G wheel' % user
+ subprocess.Popen(shlex.split(str(command))).wait()
+
+ path = '/home/%s/.ssh' % user
+ authorized_file = "%s/authorized_keys" % path
+ if not os.path.exists(path):
+ os.mkdir(path)
+ os.chown(path, user, user)
+
+ open(authorized_file, "a").close()
+
+ os.chmod(path, 0400)
+
+ with open(authorized_file, 'a') as f:
+ for key in self.vm.keys:
+ f.write('%s\n' % self.vm.keys[key])
+
class FirewallSetupTask(SetupTask, RcReader):
def run(self):
@@ -219,8 +249,31 @@ class HypervisorSetupTask(SetupTask, RcReader):
def run(self):
self._add_rc_settings()
self._add_sysctl_settings()
+ self._set_hostname()
return True
+ def _set_hostname(self):
+
+ self.log("Replacing hostname in /etc/rc.conf")
+ (fh, abspath) = tempfile.mkstemp()
+
+ tmp = open(abspath, 'w')
+ with open('/etc/rc.conf', 'r') as f:
+ for line in f:
+ if not line.startswith('hostname'):
+ tmp.write(line)
+ continue
+ tmp.write('hostname="%s"\n' % self.vm.name)
+ tmp.close()
+ os.close(fh)
+ os.remove('/etc/rc.conf')
+ shutil.move(abspath, '/etc/rc.conf')
+
+ cmd = 'hostname %s' % self.vm.name
+ self.log('Executing: `%s`' % cmd)
+ subprocess.Popen(shlex.split(cmd)).wait()
+
+
def _add_sysctl_settings(self):
sysvipc = cherrypy.config.get('hypervisor.jail_sysvipc_allow')
ipc_setting = 'security.jail.sysvipc_allowed'

0 comments on commit 36d2d3d

Please sign in to comment.