summaryrefslogtreecommitdiffstats
path: root/cobbler/action_enchant.py
blob: 03ad9879cc2ae217e9e097ec31ecbcff2f1a1db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Enables the "cobbler enchant" command to apply profiles
to remote systems, whether or not they are running koan.

Copyright 2006, Red Hat, Inc
Michael DeHaan <mdehaan@redhat.com>

This software may be freely redistributed under the terms of the GNU
general public license.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""

import cexceptions
import os
import os.path
import pexpect
import pxssh
import traceback

class Enchant:

   def __init__(self,config,sysname,password=''):
       """
       Constructor.  If password is None it should rely on SSH key auth.
       """
       self.config = config
       self.settings = self.config.settings()
       self.username = "root"
       self.sysname = sysname
       if sysname is None:
           raise cexception.CobblerException("enchant_failed","no system name specified")
       self.profile = ''
       self.password = password
 
   def call(self,cmd):
       """
       Invoke something over SSH.
       """
       print "ssh -> %s" % cmd
       self.ssh.sendline(cmd)
       self.ssh.prompt()

   def run(self):
       """
       Replace the OS of a remote system.
       """
       koan = os.path.basename(self.settings.koan_path)
       sys  = self.config.systems().find(self.sysname)
       if sys is None:
           raise cexceptions.CobblerException("enchant_failed","system not in cobbler database")
       pro  = self.config.profiles().find(sys.profile)
       self.profile = pro.name
       if pro is None:
           raise cexceptions.CobblerException("enchant_failed","no such profile for system (%s): %s" % (self.sysname, self.profile))
       where_is_koan = os.path.join(self.settings.webdir,os.path.basename(koan))
       if not os.path.exists(where_is_koan):
           raise cexceptions.CobblerException("enchant_failed","koan is missing")

       try:
           ssh = self.ssh = pxssh.pxssh()
           if not ssh.login(self.sysname, self.username, self.password):
               raise cexceptions.CobblerException("enchant_failed","SSH login denied")
           else:
               self.call("wget http://%s/cobbler/%s" % (self.settings.server, koan))
               # nodeps allows installation on Centos 4 with python 2.3
               # RHEL4 won't work due to urlgrabber
               self.call("rpm -Uvh %s --force --nodeps" % koan)
               self.call("koan --replace-self --profile=%s --server=%s" % (self.profile, self.settings.server))
               #self.call("/sbin/reboot")
               return True
       except:
           traceback.print_exc()
           raise cexceptions.CobblerException("enchant_failed","exception")
       return False # shouldn't be here