Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_backup.py @ 02266fe0

History | View | Annotate | Download (4.5 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2010 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,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)
27
# C0103: Invalid name gnt-backup
28

    
29
from ganeti.cli import *
30
from ganeti import opcodes
31
from ganeti import constants
32
from ganeti import errors
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
  ignore_remove_failures = opts.ignore_remove_failures
74

    
75
  if not opts.node:
76
    raise errors.OpPrereqError("Target node must be specified",
77
                               errors.ECODE_INVAL)
78

    
79
  op = opcodes.OpExportInstance(instance_name=args[0],
80
                                target_node=opts.node,
81
                                shutdown=opts.shutdown,
82
                                shutdown_timeout=opts.shutdown_timeout,
83
                                remove_instance=opts.remove_instance,
84
                                ignore_remove_failures=ignore_remove_failures)
85

    
86
  SubmitOpCode(op, opts=opts)
87
  return 0
88

    
89

    
90
def ImportInstance(opts, args):
91
  """Add an instance to the cluster.
92

93
  This is just a wrapper over GenericInstanceCreate.
94

95
  """
96
  return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
97

    
98

    
99
def RemoveExport(opts, args):
100
  """Remove an export from the cluster.
101

102
  @param opts: the command line options selected by the user
103
  @type args: list
104
  @param args: should contain only one element, the name of the
105
      instance whose backup should be removed
106
  @rtype: int
107
  @return: the desired exit code
108

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

    
112
  SubmitOpCode(op, opts=opts)
113
  return 0
114

    
115

    
116
# this is defined separately due to readability only
117
import_opts = [
118
  BACKEND_OPT,
119
  DISK_OPT,
120
  DISK_TEMPLATE_OPT,
121
  FILESTORE_DIR_OPT,
122
  FILESTORE_DRIVER_OPT,
123
  HYPERVISOR_OPT,
124
  IALLOCATOR_OPT,
125
  IDENTIFY_DEFAULTS_OPT,
126
  NET_OPT,
127
  NODE_PLACEMENT_OPT,
128
  NOIPCHECK_OPT,
129
  NONAMECHECK_OPT,
130
  NONICS_OPT,
131
  NWSYNC_OPT,
132
  OSPARAMS_OPT,
133
  OS_SIZE_OPT,
134
  SRC_DIR_OPT,
135
  SRC_NODE_OPT,
136
  SUBMIT_OPT,
137
  DRY_RUN_OPT,
138
  PRIORITY_OPT,
139
  ]
140

    
141

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

    
163

    
164
def Main():
165
  return GenericMain(commands)