Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-backup @ 2f79bd34

History | View | Annotate | Download (7.8 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2006, 2007 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

    
22
# pylint: disable-msg=W0401,W0614
23
# W0401: Wildcard import ganeti.cli
24
# W0614: Unused import %s from wildcard import (since we need cli)
25

    
26
import sys
27
from optparse import make_option
28

    
29
from ganeti.cli import *
30
from ganeti import opcodes
31
from ganeti import constants
32

    
33

    
34
_VALUE_TRUE = "true"
35

    
36
def PrintExportList(opts, args):
37
  """Prints a list of all the exported system images.
38

    
39
  Args:
40
   opts - class with options as members (should be empty)
41
   args - should be empty
42

    
43
  Returns:
44
    nothing
45

    
46
  """
47
  exports = GetClient().QueryExports(opts.nodes)
48
  for node in exports:
49
    ToStdout("Node: %s", node)
50
    ToStdout("Exports:")
51
    if isinstance(exports[node], list):
52
      for instance_name in exports[node]:
53
        ToStdout("\t%s", instance_name)
54
    else:
55
      ToStdout("  Could not get exports list")
56

    
57

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

    
61
  Args:
62
   opts - class with options as members
63
   args - list with a single element, the instance name
64

    
65
  Returns:
66
    1 in case of error, 0 otherwise
67

    
68
  """
69
  op = opcodes.OpExportInstance(instance_name=args[0],
70
                                target_node=opts.node,
71
                                shutdown=opts.shutdown)
72

    
73
  SubmitOpCode(op)
74

    
75

    
76
def ImportInstance(opts, args):
77
  """Add an instance to the cluster.
78

    
79
  Args:
80
   opts - class with options as members
81
   args - list with a single element, the new instance name
82
  Opts used:
83
   memory - amount of memory to allocate to instance (MiB)
84
   size - amount of disk space to allocate to instance (MiB)
85
   os - which OS to run on instance
86
   node - node to run new instance on
87
   src_node - node containing the export
88
   src_dir - directory on the old node with the export in it
89

    
90
  Returns:
91
    1 in case of error, 0 otherwise
92

    
93
  """
94
  instance = args[0]
95

    
96
  (pnode, snode) = SplitNodeOption(opts.node)
97

    
98
  hypervisor = None
99
  hvparams = {}
100
  if opts.hypervisor:
101
    hypervisor, hvparams = opts.hypervisor
102

    
103
  ValidateBeParams(opts.beparams)
104

    
105
  op = opcodes.OpCreateInstance(instance_name=instance,
106
                                disk_size=opts.size, swap_size=opts.swap,
107
                                disk_template=opts.disk_template,
108
                                mode=constants.INSTANCE_IMPORT,
109
                                pnode=pnode, snode=snode,
110
                                ip_check=opts.ip_check,
111
                                ip=opts.ip, bridge=opts.bridge, start=False,
112
                                src_node=opts.src_node, src_path=opts.src_dir,
113
                                wait_for_sync=opts.wait_for_sync, mac="auto",
114
                                file_storage_dir=opts.file_storage_dir,
115
                                file_driver=opts.file_driver,
116
                                iallocator=opts.iallocator,
117
                                hypervisor=hypervisor,
118
                                hvparams=hvparams,
119
                                beparams=opts.beparams)
120

    
121
  SubmitOpCode(op)
122
  return 0
123

    
124

    
125
def RemoveExport(opts, args):
126
  """Remove an export from the cluster.
127

    
128
  Args:
129
   opts - class with options as members
130
   args - list with a single element, the exported instance to remove
131
  Opts used:
132

    
133
  Returns:
134
    1 in case of error, 0 otherwise
135

    
136
  """
137
  instance = args[0]
138
  op = opcodes.OpRemoveExport(instance_name=args[0])
139

    
140
  SubmitOpCode(op)
141
  return 0
142

    
143

    
144
# this is defined separately due to readability only
145
import_opts = [
146
  DEBUG_OPT,
147
  make_option("-n", "--node", dest="node",
148
              help="Target node and optional secondary node",
149
              metavar="<pnode>[:<snode>]"),
150
  cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
151
             " a suffix is used",
152
             default=20 * 1024, type="unit", metavar="<size>"),
153
  cli_option("--swap-size", dest="swap", help="Swap size",
154
             default=4 * 1024, type="unit", metavar="<size>"),
155
  keyval_option("-B", "--backend", dest="beparams",
156
                type="keyval", default={},
157
                help="Backend parameters"),
158
  make_option("-t", "--disk-template", dest="disk_template",
159
              help="Custom disk setup (diskless, file, plain, drbd)",
160
              default=None, metavar="TEMPL"),
161
  make_option("-i", "--ip", dest="ip",
162
              help="IP address ('none' [default], 'auto', or specify address)",
163
              default='none', type="string", metavar="<ADDRESS>"),
164
  make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
165
              action="store_false", help="Don't wait for sync (DANGEROUS!)"),
166
  make_option("-b", "--bridge", dest="bridge",
167
              help="Bridge to connect this instance to",
168
              default=None, metavar="<bridge>"),
169
  make_option("--src-node", dest="src_node", help="Source node",
170
              metavar="<node>"),
171
  make_option("--src-dir", dest="src_dir", help="Source directory",
172
              metavar="<dir>"),
173
  make_option("--no-ip-check", dest="ip_check", default=True,
174
              action="store_false", help="Don't check that the instance's IP"
175
              " is alive"),
176
  make_option("--iallocator", metavar="<NAME>",
177
              help="Select nodes for the instance automatically using the"
178
              " <NAME> iallocator plugin", default=None, type="string"),
179
  make_option("--file-storage-dir", dest="file_storage_dir",
180
              help="Relative path under default cluster-wide file storage dir"
181
              " to store file-based disks", default=None,
182
              metavar="<DIR>"),
183
  make_option("--file-driver", dest="file_driver", help="Driver to use"
184
              " for image files", default="loop", metavar="<DRIVER>"),
185
  ikv_option("-H", "--hypervisor", dest="hypervisor",
186
              help="Hypervisor and hypervisor options, in the format"
187
              " hypervisor:option=value,option=value,...", default=None,
188
              type="identkeyval"),
189
  ]
190

    
191
commands = {
192
  'list': (PrintExportList, ARGS_NONE,
193
           [DEBUG_OPT,
194
            make_option("--node", dest="nodes", default=[], action="append",
195
                        help="List only backups stored on this node"
196
                             " (can be used multiple times)"),
197
            ],
198
           "", "Lists instance exports available in the ganeti cluster"),
199
  'export': (ExportInstance, ARGS_ONE,
200
             [DEBUG_OPT, FORCE_OPT,
201
              make_option("-n", "--node", dest="node", help="Target node",
202
                          metavar="<node>"),
203
              make_option("","--noshutdown", dest="shutdown",
204
                          action="store_false", default=True,
205
                          help="Don't shutdown the instance (unsafe)"), ],
206
             "-n <target_node> [opts...] <name>",
207
             "Exports an instance to an image"),
208
  'import': (ImportInstance, ARGS_ONE, import_opts,
209
             ("[...] -t disk-type -n node[:secondary-node]"
210
              " --src-node node --src-dir dir"
211
              " <name>"),
212
             "Imports an instance from an exported image"),
213
  'remove': (RemoveExport, ARGS_ONE,
214
             [DEBUG_OPT],
215
             "<name>",
216
             "Remove exports of named instance from the filesystem."),
217
  }
218

    
219
if __name__ == '__main__':
220
  sys.exit(GenericMain(commands))