Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_backup.py @ 8ca22fef

History | View | Annotate | Download (5 kB)

1 02266fe0 Michael Hanselmann
#
2 dd4b1106 Iustin Pop
#
3 dd4b1106 Iustin Pop
4 5ba392f9 Iustin Pop
# Copyright (C) 2006, 2007, 2010, 2011, 2013 Google Inc.
5 dd4b1106 Iustin Pop
#
6 dd4b1106 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 dd4b1106 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 dd4b1106 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 dd4b1106 Iustin Pop
# (at your option) any later version.
10 dd4b1106 Iustin Pop
#
11 dd4b1106 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 dd4b1106 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 dd4b1106 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 dd4b1106 Iustin Pop
# General Public License for more details.
15 dd4b1106 Iustin Pop
#
16 dd4b1106 Iustin Pop
# You should have received a copy of the GNU General Public License
17 dd4b1106 Iustin Pop
# along with this program; if not, write to the Free Software
18 dd4b1106 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 dd4b1106 Iustin Pop
# 02110-1301, USA.
20 dd4b1106 Iustin Pop
21 7260cfbe Iustin Pop
"""Backup related commands"""
22 dd4b1106 Iustin Pop
23 b459a848 Andrea Spadaccini
# pylint: disable=W0401,W0613,W0614,C0103
24 2f79bd34 Iustin Pop
# W0401: Wildcard import ganeti.cli
25 2d54e29c Iustin Pop
# W0613: Unused argument, since all functions follow the same API
26 2f79bd34 Iustin Pop
# W0614: Unused import %s from wildcard import (since we need cli)
27 7260cfbe Iustin Pop
# C0103: Invalid name gnt-backup
28 2f79bd34 Iustin Pop
29 dd4b1106 Iustin Pop
from ganeti.cli import *
30 dd4b1106 Iustin Pop
from ganeti import opcodes
31 dd4b1106 Iustin Pop
from ganeti import constants
32 bc696589 Michael Hanselmann
from ganeti import errors
33 0fdf247d Michael Hanselmann
from ganeti import qlang
34 0fdf247d Michael Hanselmann
35 0fdf247d Michael Hanselmann
36 0fdf247d Michael Hanselmann
_LIST_DEF_FIELDS = ["node", "export"]
37 dd4b1106 Iustin Pop
38 7c0d6283 Michael Hanselmann
39 dd4b1106 Iustin Pop
def PrintExportList(opts, args):
40 dd4b1106 Iustin Pop
  """Prints a list of all the exported system images.
41 dd4b1106 Iustin Pop

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

48 dd4b1106 Iustin Pop
  """
49 0fdf247d Michael Hanselmann
  selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
50 0fdf247d Michael Hanselmann
51 0fdf247d Michael Hanselmann
  qfilter = qlang.MakeSimpleFilter("node", opts.nodes)
52 0fdf247d Michael Hanselmann
53 5ba392f9 Iustin Pop
  cl = GetClient(query=True)
54 5ba392f9 Iustin Pop
55 0fdf247d Michael Hanselmann
  return GenericList(constants.QR_EXPORT, selected_fields, None, opts.units,
56 0fdf247d Michael Hanselmann
                     opts.separator, not opts.no_headers,
57 5ba392f9 Iustin Pop
                     verbose=opts.verbose, qfilter=qfilter, cl=cl)
58 0fdf247d Michael Hanselmann
59 0fdf247d Michael Hanselmann
60 0fdf247d Michael Hanselmann
def ListExportFields(opts, args):
61 0fdf247d Michael Hanselmann
  """List export fields.
62 0fdf247d Michael Hanselmann

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

69 0fdf247d Michael Hanselmann
  """
70 5ba392f9 Iustin Pop
  cl = GetClient(query=True)
71 5ba392f9 Iustin Pop
72 0fdf247d Michael Hanselmann
  return GenericListFields(constants.QR_EXPORT, args, opts.separator,
73 5ba392f9 Iustin Pop
                           not opts.no_headers, cl=cl)
74 dd4b1106 Iustin Pop
75 dd4b1106 Iustin Pop
76 dd4b1106 Iustin Pop
def ExportInstance(opts, args):
77 dd4b1106 Iustin Pop
  """Export an instance to an image in the cluster.
78 dd4b1106 Iustin Pop

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

86 dd4b1106 Iustin Pop
  """
87 8d8d650c Michael Hanselmann
  ignore_remove_failures = opts.ignore_remove_failures
88 8d8d650c Michael Hanselmann
89 bc696589 Michael Hanselmann
  if not opts.node:
90 5ec4b9d2 Michael Hanselmann
    raise errors.OpPrereqError("Target node must be specified",
91 5ec4b9d2 Michael Hanselmann
                               errors.ECODE_INVAL)
92 bc696589 Michael Hanselmann
93 4ff922a2 Iustin Pop
  op = opcodes.OpBackupExport(instance_name=args[0],
94 4ff922a2 Iustin Pop
                              target_node=opts.node,
95 0a6e6e6d Thomas Thrainer
                              compress=opts.compress,
96 4ff922a2 Iustin Pop
                              shutdown=opts.shutdown,
97 4ff922a2 Iustin Pop
                              shutdown_timeout=opts.shutdown_timeout,
98 4ff922a2 Iustin Pop
                              remove_instance=opts.remove_instance,
99 4ff922a2 Iustin Pop
                              ignore_remove_failures=ignore_remove_failures)
100 dd4b1106 Iustin Pop
101 f5c0c206 Michael Hanselmann
  SubmitOrSend(op, opts)
102 44247302 Iustin Pop
  return 0
103 60d49723 Michael Hanselmann
104 8d8d650c Michael Hanselmann
105 dd4b1106 Iustin Pop
def ImportInstance(opts, args):
106 dd4b1106 Iustin Pop
  """Add an instance to the cluster.
107 dd4b1106 Iustin Pop

108 d77490c5 Iustin Pop
  This is just a wrapper over GenericInstanceCreate.
109 dd4b1106 Iustin Pop

110 dd4b1106 Iustin Pop
  """
111 d77490c5 Iustin Pop
  return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
112 dd4b1106 Iustin Pop
113 dd4b1106 Iustin Pop
114 9ac99fda Guido Trotter
def RemoveExport(opts, args):
115 9ac99fda Guido Trotter
  """Remove an export from the cluster.
116 9ac99fda Guido Trotter

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

124 9ac99fda Guido Trotter
  """
125 ca5890ad Iustin Pop
  op = opcodes.OpBackupRemove(instance_name=args[0])
126 9ac99fda Guido Trotter
127 f5c0c206 Michael Hanselmann
  SubmitOrSend(op, opts)
128 9ac99fda Guido Trotter
  return 0
129 9ac99fda Guido Trotter
130 9ac99fda Guido Trotter
131 dd4b1106 Iustin Pop
# this is defined separately due to readability only
132 dd4b1106 Iustin Pop
import_opts = [
133 e588764d Iustin Pop
  IDENTIFY_DEFAULTS_OPT,
134 df62e5db Iustin Pop
  SRC_DIR_OPT,
135 df62e5db Iustin Pop
  SRC_NODE_OPT,
136 8ca22fef Thomas Thrainer
  COMPRESS_OPT,
137 2a84b7d3 René Nussbaumer
  IGNORE_IPOLICY_OPT,
138 dd4b1106 Iustin Pop
  ]
139 dd4b1106 Iustin Pop
140 8d8d650c Michael Hanselmann
141 dd4b1106 Iustin Pop
commands = {
142 3ccb3a64 Michael Hanselmann
  "list": (
143 6ea815cf Iustin Pop
    PrintExportList, ARGS_NONE,
144 0fdf247d Michael Hanselmann
    [NODE_LIST_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, VERBOSE_OPT],
145 6ea815cf Iustin Pop
    "", "Lists instance exports available in the ganeti cluster"),
146 0fdf247d Michael Hanselmann
  "list-fields": (
147 0fdf247d Michael Hanselmann
    ListExportFields, [ArgUnknown()],
148 0fdf247d Michael Hanselmann
    [NOHDR_OPT, SEP_OPT],
149 0fdf247d Michael Hanselmann
    "[fields...]",
150 0fdf247d Michael Hanselmann
    "Lists all available fields for exports"),
151 3ccb3a64 Michael Hanselmann
  "export": (
152 6ea815cf Iustin Pop
    ExportInstance, ARGS_ONE_INSTANCE,
153 0a6e6e6d Thomas Thrainer
    [FORCE_OPT, SINGLE_NODE_OPT, COMPRESS_OPT, NOSHUTDOWN_OPT,
154 0a6e6e6d Thomas Thrainer
     SHUTDOWN_TIMEOUT_OPT, REMOVE_INSTANCE_OPT, IGNORE_REMOVE_FAILURES_OPT,
155 0a6e6e6d Thomas Thrainer
     DRY_RUN_OPT, PRIORITY_OPT] + SUBMIT_OPTS,
156 6ea815cf Iustin Pop
    "-n <target_node> [opts...] <name>",
157 6ea815cf Iustin Pop
    "Exports an instance to an image"),
158 3ccb3a64 Michael Hanselmann
  "import": (
159 eb28ecf6 Guido Trotter
    ImportInstance, ARGS_ONE_INSTANCE, COMMON_CREATE_OPTS + import_opts,
160 6ea815cf Iustin Pop
    "[...] -t disk-type -n node[:secondary-node] <name>",
161 6ea815cf Iustin Pop
    "Imports an instance from an exported image"),
162 3ccb3a64 Michael Hanselmann
  "remove": (
163 f5c0c206 Michael Hanselmann
    RemoveExport, [ArgUnknown(min=1, max=1)],
164 d6cd74dd Klaus Aehlig
    [DRY_RUN_OPT, PRIORITY_OPT] + SUBMIT_OPTS,
165 6ea815cf Iustin Pop
    "<name>", "Remove exports of named instance from the filesystem."),
166 dd4b1106 Iustin Pop
  }
167 dd4b1106 Iustin Pop
168 a8005e17 Michael Hanselmann
169 02266fe0 Michael Hanselmann
def Main():
170 02266fe0 Michael Hanselmann
  return GenericMain(commands)