Package osh :: Package command :: Module installosh
[frames] | no frames]

Source Code for Module osh.command.installosh

  1  # osh 
  2  # Copyright (C) 2005 Jack Orenstein <jao@geophile.com> 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 17   
 18  """C{installosh -c CLUSTER [-d INSTALL_DIRECTORY] [OSH_CONFIG_FILE]} 
 19   
 20  Installs osh on C{CLUSTER}. If C{-d} is not specified, then 
 21  installation is to the standard site-packages directory 
 22  (e.g. /usr/lib/python2.4/site-packages). Otherwise, installation goes 
 23  to C{INSTALL_DIRECTORY}. (C{INSTALL_DIRECTORY} replaces the entire path, not just 
 24  the C{/usr} or C{/usr/lib} part of the path.)  On each node of the 
 25  cluster, a C{.oshrc} files is created in the home directory of the 
 26  user specified for C{CLUSTER}. This file is copied from 
 27  C{OSH_CONFIG_FILE}, if specified. Otherwise, C{~/.oshrc} is copied. 
 28   
 29  Not available through the API. 
 30  """ 
 31   
 32  import os 
 33  import sys 
 34  import threading 
 35   
 36  import osh.core 
 37  import osh.config 
 38  import osh.spawn 
 39  import progtrack 
 40   
 41   
 42  _DISTRIBUTION_FILENAME = 'osh-' + osh.config.OSH_VERSION + '.tar.gz' 
 43  _CONFIG_FILE = '.oshrc' 
 44  _PACKAGE_HOME = '/usr/share/osh' 
 45   
 46  Spawn = osh.spawn.Spawn 
 47  SpawnSSH = osh.spawn.SpawnSSH 
 48  collect_lines = osh.spawn.collect_lines 
 49   
50 -def _scp(user, host, source, target):
51 err_lines = [] 52 scp_command = 'scp %s %s@%s:%s' % (source, user, host, target) 53 Spawn(scp_command, 54 None, 55 None, 56 collect_lines(err_lines)).run() 57 if len(err_lines) > 0: 58 raise Exception(' '.join(err_lines))
59
60 -def _ssh(user, host, command):
61 out_lines = [] 62 SpawnSSH(user, 63 host, 64 command, 65 None, 66 collect_lines(out_lines), 67 None).run() 68 return out_lines
69 70 # CLI
71 -def _installosh():
72 return _InstallOsh()
73
74 -class _InstallOsh(osh.core.RemoteOp):
75 76 _install_dir = None 77 _config_file = None 78 _host_counter = None 79 _ui = None 80 81 # object interface 82
83 - def __init__(self):
84 osh.core.RemoteOp.__init__(self, 'c:d:', (0, 1))
85 86 87 # BaseOp interface 88
89 - def doc(self):
90 return __doc__
91
92 - def setup(self):
93 osh.core.RemoteOp.setup(self) 94 self._install_dir = self.args().string_arg('-d') 95 if self.args().has_next(): 96 self._config_file = self.args().next_string() 97 else: 98 self._config_file = '%s/%s' % (os.environ['HOME'], _CONFIG_FILE) 99 self._host_counter = 0 100 self._ui = progtrack.ProgressTrackingUI('installosh') 101 self._ui.add_column('host', 25) 102 self._ui.add_column(['create', 'osh dir'], 10) 103 self._ui.add_column(['copy', 'package'], 10) 104 self._ui.add_column(['copy', 'installer'], 10) 105 self._ui.add_column(['run', 'installer'], 10) 106 self._ui.add_column(['copy', 'conf file'], 10) 107 for host in self.hosts(): 108 self._ui.add_row(host.name)
109 110 # generator interface 111
112 - def execute(self):
113 self._ui.start() 114 try: 115 osh.core.RemoteOp.execute(self) 116 finally: 117 self._ui.stop()
118 119 120 # RemoteOp interface 121
122 - def action(self, host):
123 stage = 0 124 try: 125 # Remove existing /usr/share/osh directory 126 stage += 1 127 _ssh(self.user(), 128 host.address, 129 '"rm -rf %s"' % _PACKAGE_HOME) 130 # Create directory /usr/share/osh 131 _ssh(self.user(), 132 host.address, 133 '"mkdir %s"' % _PACKAGE_HOME) 134 self._ui.ok(host.name, stage) 135 # Copy package to /usr/share/osh 136 stage += 1 137 _scp(self.user(), 138 host.address, 139 '%s/%s' % (_PACKAGE_HOME, _DISTRIBUTION_FILENAME), 140 _PACKAGE_HOME) 141 self._ui.ok(host.name, stage) 142 # Copy remoteinstall script to /usr/share/osh 143 stage += 1 144 _scp(self.user(), 145 host.address, 146 '%s/remoteinstall' % _PACKAGE_HOME, 147 _PACKAGE_HOME) 148 self._ui.ok(host.name, stage) 149 # Run remoteinstall script 150 stage += 1 151 if self._install_dir: 152 _ssh(self.user(), 153 host.address, 154 '"%s/remoteinstall %s %s"' % (_PACKAGE_HOME, _PACKAGE_HOME, self._install_dir)) 155 else: 156 _ssh(self.user(), 157 host.address, 158 '"%s/remoteinstall %s"' % (_PACKAGE_HOME, _PACKAGE_HOME)) 159 self._ui.ok(host.name, stage) 160 # Copy config file to home directory unless one already exists 161 stage += 1 162 ls_oshrc = _ssh(self.user(), 163 host.address, 164 '"ls /%s/%s"' % (self.user(), _CONFIG_FILE)) 165 if len(ls_oshrc) == 0: 166 _scp(self.user(), 167 host.address, 168 self._config_file, 169 '~') 170 self._ui.ok(host.name, stage) 171 except: 172 (exc_type, exc_value, exc_traceback) = sys.exc_info() 173 message = str(exc_value).strip() 174 self._ui.error(host.name, stage, message)
175