Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_backup.py @ 57dc299a

History | View | Annotate | Download (4.2 kB)

1 02266fe0 Michael Hanselmann
#
2 dd4b1106 Iustin Pop
#
3 dd4b1106 Iustin Pop
4 4ff922a2 Iustin Pop
# Copyright (C) 2006, 2007, 2010, 2011 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 dd4b1106 Iustin Pop
34 7c0d6283 Michael Hanselmann
35 dd4b1106 Iustin Pop
def PrintExportList(opts, args):
36 dd4b1106 Iustin Pop
  """Prints a list of all the exported system images.
37 dd4b1106 Iustin Pop

38 48de3413 Iustin Pop
  @param opts: the command line options selected by the user
39 48de3413 Iustin Pop
  @type args: list
40 48de3413 Iustin Pop
  @param args: should be an empty list
41 48de3413 Iustin Pop
  @rtype: int
42 48de3413 Iustin Pop
  @return: the desired exit code
43 dd4b1106 Iustin Pop

44 dd4b1106 Iustin Pop
  """
45 77921a95 Iustin Pop
  exports = GetClient().QueryExports(opts.nodes, False)
46 d70b3058 Iustin Pop
  retcode = 0
47 dd4b1106 Iustin Pop
  for node in exports:
48 3a24c527 Iustin Pop
    ToStdout("Node: %s", node)
49 3a24c527 Iustin Pop
    ToStdout("Exports:")
50 461f0538 Guido Trotter
    if isinstance(exports[node], list):
51 461f0538 Guido Trotter
      for instance_name in exports[node]:
52 3a24c527 Iustin Pop
        ToStdout("\t%s", instance_name)
53 461f0538 Guido Trotter
    else:
54 3a24c527 Iustin Pop
      ToStdout("  Could not get exports list")
55 d70b3058 Iustin Pop
      retcode = 1
56 d70b3058 Iustin Pop
  return retcode
57 dd4b1106 Iustin Pop
58 dd4b1106 Iustin Pop
59 dd4b1106 Iustin Pop
def ExportInstance(opts, args):
60 dd4b1106 Iustin Pop
  """Export an instance to an image in the cluster.
61 dd4b1106 Iustin Pop

62 48de3413 Iustin Pop
  @param opts: the command line options selected by the user
63 48de3413 Iustin Pop
  @type args: list
64 48de3413 Iustin Pop
  @param args: should contain only one element, the name
65 48de3413 Iustin Pop
      of the instance to be exported
66 48de3413 Iustin Pop
  @rtype: int
67 48de3413 Iustin Pop
  @return: the desired exit code
68 dd4b1106 Iustin Pop

69 dd4b1106 Iustin Pop
  """
70 8d8d650c Michael Hanselmann
  ignore_remove_failures = opts.ignore_remove_failures
71 8d8d650c Michael Hanselmann
72 bc696589 Michael Hanselmann
  if not opts.node:
73 5ec4b9d2 Michael Hanselmann
    raise errors.OpPrereqError("Target node must be specified",
74 5ec4b9d2 Michael Hanselmann
                               errors.ECODE_INVAL)
75 bc696589 Michael Hanselmann
76 4ff922a2 Iustin Pop
  op = opcodes.OpBackupExport(instance_name=args[0],
77 4ff922a2 Iustin Pop
                              target_node=opts.node,
78 4ff922a2 Iustin Pop
                              shutdown=opts.shutdown,
79 4ff922a2 Iustin Pop
                              shutdown_timeout=opts.shutdown_timeout,
80 4ff922a2 Iustin Pop
                              remove_instance=opts.remove_instance,
81 4ff922a2 Iustin Pop
                              ignore_remove_failures=ignore_remove_failures)
82 dd4b1106 Iustin Pop
83 44247302 Iustin Pop
  SubmitOpCode(op, opts=opts)
84 44247302 Iustin Pop
  return 0
85 60d49723 Michael Hanselmann
86 8d8d650c Michael Hanselmann
87 dd4b1106 Iustin Pop
def ImportInstance(opts, args):
88 dd4b1106 Iustin Pop
  """Add an instance to the cluster.
89 dd4b1106 Iustin Pop

90 d77490c5 Iustin Pop
  This is just a wrapper over GenericInstanceCreate.
91 dd4b1106 Iustin Pop

92 dd4b1106 Iustin Pop
  """
93 d77490c5 Iustin Pop
  return GenericInstanceCreate(constants.INSTANCE_IMPORT, opts, args)
94 dd4b1106 Iustin Pop
95 dd4b1106 Iustin Pop
96 9ac99fda Guido Trotter
def RemoveExport(opts, args):
97 9ac99fda Guido Trotter
  """Remove an export from the cluster.
98 9ac99fda Guido Trotter

99 48de3413 Iustin Pop
  @param opts: the command line options selected by the user
100 48de3413 Iustin Pop
  @type args: list
101 48de3413 Iustin Pop
  @param args: should contain only one element, the name of the
102 48de3413 Iustin Pop
      instance whose backup should be removed
103 48de3413 Iustin Pop
  @rtype: int
104 48de3413 Iustin Pop
  @return: the desired exit code
105 9ac99fda Guido Trotter

106 9ac99fda Guido Trotter
  """
107 ca5890ad Iustin Pop
  op = opcodes.OpBackupRemove(instance_name=args[0])
108 9ac99fda Guido Trotter
109 400ca2f7 Iustin Pop
  SubmitOpCode(op, opts=opts)
110 9ac99fda Guido Trotter
  return 0
111 9ac99fda Guido Trotter
112 9ac99fda Guido Trotter
113 dd4b1106 Iustin Pop
# this is defined separately due to readability only
114 dd4b1106 Iustin Pop
import_opts = [
115 e588764d Iustin Pop
  IDENTIFY_DEFAULTS_OPT,
116 df62e5db Iustin Pop
  SRC_DIR_OPT,
117 df62e5db Iustin Pop
  SRC_NODE_OPT,
118 2a84b7d3 René Nussbaumer
  IGNORE_IPOLICY_OPT,
119 dd4b1106 Iustin Pop
  ]
120 dd4b1106 Iustin Pop
121 8d8d650c Michael Hanselmann
122 dd4b1106 Iustin Pop
commands = {
123 6ea815cf Iustin Pop
  'list': (
124 6ea815cf Iustin Pop
    PrintExportList, ARGS_NONE,
125 064c21f8 Iustin Pop
    [NODE_LIST_OPT],
126 6ea815cf Iustin Pop
    "", "Lists instance exports available in the ganeti cluster"),
127 6ea815cf Iustin Pop
  'export': (
128 6ea815cf Iustin Pop
    ExportInstance, ARGS_ONE_INSTANCE,
129 8d8d650c Michael Hanselmann
    [FORCE_OPT, SINGLE_NODE_OPT, NOSHUTDOWN_OPT, SHUTDOWN_TIMEOUT_OPT,
130 aa06f8c6 Michael Hanselmann
     REMOVE_INSTANCE_OPT, IGNORE_REMOVE_FAILURES_OPT, DRY_RUN_OPT,
131 aa06f8c6 Michael Hanselmann
     PRIORITY_OPT],
132 6ea815cf Iustin Pop
    "-n <target_node> [opts...] <name>",
133 6ea815cf Iustin Pop
    "Exports an instance to an image"),
134 6ea815cf Iustin Pop
  'import': (
135 eb28ecf6 Guido Trotter
    ImportInstance, ARGS_ONE_INSTANCE, COMMON_CREATE_OPTS + import_opts,
136 6ea815cf Iustin Pop
    "[...] -t disk-type -n node[:secondary-node] <name>",
137 6ea815cf Iustin Pop
    "Imports an instance from an exported image"),
138 6ea815cf Iustin Pop
  'remove': (
139 aa06f8c6 Michael Hanselmann
    RemoveExport, [ArgUnknown(min=1, max=1)], [DRY_RUN_OPT, PRIORITY_OPT],
140 6ea815cf Iustin Pop
    "<name>", "Remove exports of named instance from the filesystem."),
141 dd4b1106 Iustin Pop
  }
142 dd4b1106 Iustin Pop
143 a8005e17 Michael Hanselmann
144 02266fe0 Michael Hanselmann
def Main():
145 02266fe0 Michael Hanselmann
  return GenericMain(commands)