Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-backup @ d77490c5

History | View | Annotate | Download (4.4 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2006, 2007 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
# pylint: disable-msg=W0401,W0614
23
# W0401: Wildcard import ganeti.cli
24
# W0614: Unused import %s from wildcard import (since we need cli)
25

    
26
import sys
27

    
28
from ganeti.cli import *
29
from ganeti import opcodes
30
from ganeti import constants
31
from ganeti import errors
32
from ganeti import utils
33

    
34

    
35
_VALUE_TRUE = "true"
36

    
37

    
38
def PrintExportList(opts, args):
39
  """Prints a list of all the exported system images.
40

    
41
  @param opts: the command line options selected by the user
42
  @type args: list
43
  @param args: should be an empty list
44
  @rtype: int
45
  @return: the desired exit code
46

    
47
  """
48
  exports = GetClient().QueryExports(opts.nodes, False)
49
  retcode = 0
50
  for node in exports:
51
    ToStdout("Node: %s", node)
52
    ToStdout("Exports:")
53
    if isinstance(exports[node], list):
54
      for instance_name in exports[node]:
55
        ToStdout("\t%s", instance_name)
56
    else:
57
      ToStdout("  Could not get exports list")
58
      retcode = 1
59
  return retcode
60

    
61

    
62
def ExportInstance(opts, args):
63
  """Export an instance to an image in the cluster.
64

    
65
  @param opts: the command line options selected by the user
66
  @type args: list
67
  @param args: should contain only one element, the name
68
      of the instance to be exported
69
  @rtype: int
70
  @return: the desired exit code
71

    
72
  """
73
  op = opcodes.OpExportInstance(instance_name=args[0],
74
                                target_node=opts.node,
75
                                shutdown=opts.shutdown)
76

    
77
  fin_resu, dlist = SubmitOpCode(op)
78
  if not isinstance(dlist, list):
79
    ToStderr("Cannot parse execution results")
80
    return 1
81
  tot_dsk = len(dlist)
82
  # TODO: handle diskless instances
83
  if dlist.count(False) == 0:
84
    # all OK
85
    rcode = 0
86
  elif dlist.count(True) == 0:
87
    ToStderr("Error: No disks were backed up successfully."
88
             " The export doesn't have any valid data,"
89
             " it is recommended to retry the operation.")
90
    rcode = 1
91
  else:
92
    ToStderr("Partial export failure: %d disks backed up, %d disks failed.",
93
             dlist.count(True), dlist.count(False))
94
    rcode = 2
95
  if not fin_resu:
96
    rcode = 1
97
  return rcode
98

    
99
def ImportInstance(opts, args):
100
  """Add an instance to the cluster.
101

    
102
  This is just a wrapper over GenericInstanceCreate.
103

    
104
  """
105
  return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
106

    
107

    
108
def RemoveExport(opts, args):
109
  """Remove an export from the cluster.
110

    
111
  @param opts: the command line options selected by the user
112
  @type args: list
113
  @param args: should contain only one element, the name of the
114
      instance whose backup should be removed
115
  @rtype: int
116
  @return: the desired exit code
117

    
118
  """
119
  instance = args[0]
120
  op = opcodes.OpRemoveExport(instance_name=args[0])
121

    
122
  SubmitOpCode(op)
123
  return 0
124

    
125

    
126
# this is defined separately due to readability only
127
import_opts = [
128
  NODE_PLACEMENT_OPT,
129
  BACKEND_OPT,
130
  DISK_TEMPLATE_OPT,
131
  DISK_OPT,
132
  OS_SIZE_OPT,
133
  NET_OPT,
134
  NONICS_OPT,
135
  NWSYNC_OPT,
136
  SRC_DIR_OPT,
137
  SRC_NODE_OPT,
138
  NOIPCHECK_OPT,
139
  IALLOCATOR_OPT,
140
  FILESTORE_DIR_OPT,
141
  FILESTORE_DRIVER_OPT,
142
  HYPERVISOR_OPT,
143
  SUBMIT_OPT,
144
  ]
145

    
146
commands = {
147
  'list': (
148
    PrintExportList, ARGS_NONE,
149
    [NODE_LIST_OPT],
150
    "", "Lists instance exports available in the ganeti cluster"),
151
  'export': (
152
    ExportInstance, ARGS_ONE_INSTANCE,
153
    [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT],
154
    "-n <target_node> [opts...] <name>",
155
    "Exports an instance to an image"),
156
  'import': (
157
    ImportInstance, ARGS_ONE_INSTANCE, import_opts,
158
    "[...] -t disk-type -n node[:secondary-node] <name>",
159
    "Imports an instance from an exported image"),
160
  'remove': (
161
    RemoveExport, [ArgUnknown(min=1, max=1)], [],
162
    "<name>", "Remove exports of named instance from the filesystem."),
163
  }
164

    
165

    
166
if __name__ == '__main__':
167
  sys.exit(GenericMain(commands))