gnt-backup 4.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

Iustin Pop's avatar
Iustin Pop committed
21
"""Backup related commands"""
22

23
# pylint: disable-msg=W0401,W0613,W0614,C0103
24
# W0401: Wildcard import ganeti.cli
25
# W0613: Unused argument, since all functions follow the same API
26
# W0614: Unused import %s from wildcard import (since we need cli)
Iustin Pop's avatar
Iustin Pop committed
27
# C0103: Invalid name gnt-backup
28

29
30
31
32
33
import sys

from ganeti.cli import *
from ganeti import opcodes
from ganeti import constants
34
35
from ganeti import errors
from ganeti import utils
36

37

38
39
_VALUE_TRUE = "true"

40

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

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

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


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

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

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

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

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

Iustin Pop's avatar
Iustin Pop committed
105
  This is just a wrapper over GenericInstanceCreate.
106
107

  """
Iustin Pop's avatar
Iustin Pop committed
108
  return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
109
110


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

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

  """
  op = opcodes.OpRemoveExport(instance_name=args[0])

  SubmitOpCode(op)
  return 0


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

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

168

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