Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_backup.py @ 2af8b9c9

History | View | Annotate | Download (4.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2010, 2011, 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
"""Backup related commands"""
22

    
23
# pylint: disable=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
from ganeti import qlang
34

    
35

    
36
_LIST_DEF_FIELDS = ["node", "export"]
37

    
38

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

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

48
  """
49
  selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
50

    
51
  qfilter = qlang.MakeSimpleFilter("node", opts.nodes)
52

    
53
  cl = GetClient()
54

    
55
  return GenericList(constants.QR_EXPORT, selected_fields, None, opts.units,
56
                     opts.separator, not opts.no_headers,
57
                     verbose=opts.verbose, qfilter=qfilter, cl=cl)
58

    
59

    
60
def ListExportFields(opts, args):
61
  """List export fields.
62

63
  @param opts: the command line options selected by the user
64
  @type args: list
65
  @param args: fields to list, or empty for all
66
  @rtype: int
67
  @return: the desired exit code
68

69
  """
70
  cl = GetClient()
71

    
72
  return GenericListFields(constants.QR_EXPORT, args, opts.separator,
73
                           not opts.no_headers, cl=cl)
74

    
75

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

79
  @param opts: the command line options selected by the user
80
  @type args: list
81
  @param args: should contain only one element, the name
82
      of the instance to be exported
83
  @rtype: int
84
  @return: the desired exit code
85

86
  """
87
  ignore_remove_failures = opts.ignore_remove_failures
88

    
89
  if not opts.node:
90
    raise errors.OpPrereqError("Target node must be specified",
91
                               errors.ECODE_INVAL)
92

    
93
  op = opcodes.OpBackupExport(instance_name=args[0],
94
                              target_node=opts.node,
95
                              compress=opts.compress,
96
                              shutdown=opts.shutdown,
97
                              shutdown_timeout=opts.shutdown_timeout,
98
                              remove_instance=opts.remove_instance,
99
                              ignore_remove_failures=ignore_remove_failures)
100

    
101
  SubmitOrSend(op, opts)
102
  return 0
103

    
104

    
105
def ImportInstance(opts, args):
106
  """Add an instance to the cluster.
107

108
  This is just a wrapper over GenericInstanceCreate.
109

110
  """
111
  return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
112

    
113

    
114
def RemoveExport(opts, args):
115
  """Remove an export from the cluster.
116

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

124
  """
125
  op = opcodes.OpBackupRemove(instance_name=args[0])
126

    
127
  SubmitOrSend(op, opts)
128
  return 0
129

    
130

    
131
# this is defined separately due to readability only
132
import_opts = [
133
  IDENTIFY_DEFAULTS_OPT,
134
  SRC_DIR_OPT,
135
  SRC_NODE_OPT,
136
  COMPRESS_OPT,
137
  IGNORE_IPOLICY_OPT,
138
  ]
139

    
140

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

    
168

    
169
def Main():
170
  return GenericMain(commands)