Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-backup @ 7260cfbe

History | View | Annotate | Download (4.6 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
"""Backup related commands"""
22

    
23
# pylint: disable-msg=W0401,W0614,C0103
24
# W0401: Wildcard import ganeti.cli
25
# W0614: Unused import %s from wildcard import (since we need cli)
26
# C0103: Invalid name gnt-backup
27

    
28
import sys
29

    
30
from ganeti.cli import *
31
from ganeti import opcodes
32
from ganeti import constants
33
from ganeti import errors
34
from ganeti import utils
35

    
36

    
37
_VALUE_TRUE = "true"
38

    
39

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

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

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

    
63

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

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

    
74
  """
75
  op = opcodes.OpExportInstance(instance_name=args[0],
76
                                target_node=opts.node,
77
                                shutdown=opts.shutdown,
78
                                shutdown_timeout=opts.shutdown_timeout)
79

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

    
102
def ImportInstance(opts, args):
103
  """Add an instance to the cluster.
104

    
105
  This is just a wrapper over GenericInstanceCreate.
106

    
107
  """
108
  return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
109

    
110

    
111
def RemoveExport(opts, args):
112
  """Remove an export from the cluster.
113

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

    
121
  """
122
  instance = args[0]
123
  op = opcodes.OpRemoveExport(instance_name=args[0])
124

    
125
  SubmitOpCode(op)
126
  return 0
127

    
128

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

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

    
169

    
170
if __name__ == '__main__':
171
  sys.exit(GenericMain(commands))