Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib / common.py @ 37dc17e3

History | View | Annotate | Download (2.7 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Common functions used by multiple logical units."""
23

    
24
from ganeti import errors
25
from ganeti import locking
26
from ganeti import utils
27

    
28

    
29
def _ExpandItemName(fn, name, kind):
30
  """Expand an item name.
31

32
  @param fn: the function to use for expansion
33
  @param name: requested item name
34
  @param kind: text description ('Node' or 'Instance')
35
  @return: the resolved (full) name
36
  @raise errors.OpPrereqError: if the item is not found
37

38
  """
39
  full_name = fn(name)
40
  if full_name is None:
41
    raise errors.OpPrereqError("%s '%s' not known" % (kind, name),
42
                               errors.ECODE_NOENT)
43
  return full_name
44

    
45

    
46
def _ExpandInstanceName(cfg, name):
47
  """Wrapper over L{_ExpandItemName} for instance."""
48
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
49

    
50

    
51
def _ExpandNodeName(cfg, name):
52
  """Wrapper over L{_ExpandItemName} for nodes."""
53
  return _ExpandItemName(cfg.ExpandNodeName, name, "Node")
54

    
55

    
56
def _ShareAll():
57
  """Returns a dict declaring all lock levels shared.
58

59
  """
60
  return dict.fromkeys(locking.LEVELS, 1)
61

    
62

    
63
def _CheckNodeGroupInstances(cfg, group_uuid, owned_instances):
64
  """Checks if the instances in a node group are still correct.
65

66
  @type cfg: L{config.ConfigWriter}
67
  @param cfg: The cluster configuration
68
  @type group_uuid: string
69
  @param group_uuid: Node group UUID
70
  @type owned_instances: set or frozenset
71
  @param owned_instances: List of currently owned instances
72

73
  """
74
  wanted_instances = cfg.GetNodeGroupInstances(group_uuid)
75
  if owned_instances != wanted_instances:
76
    raise errors.OpPrereqError("Instances in node group '%s' changed since"
77
                               " locks were acquired, wanted '%s', have '%s';"
78
                               " retry the operation" %
79
                               (group_uuid,
80
                                utils.CommaJoin(wanted_instances),
81
                                utils.CommaJoin(owned_instances)),
82
                               errors.ECODE_STATE)
83

    
84
  return wanted_instances