Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-backup @ b03efa30

History | View | Annotate | Download (7.7 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
import sys
23
from optparse import make_option
24

    
25
from ganeti.cli import *
26
from ganeti import cmdlib
27
from ganeti import opcodes
28
from ganeti import constants
29

    
30

    
31
_VALUE_TRUE = "true"
32

    
33
def PrintExportList(opts, args):
34
  """Prints a list of all the exported system images.
35

    
36
  Args:
37
   opts - class with options as members (should be empty)
38
   args - should be empty
39

    
40
  Returns:
41
    nothing
42

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

    
54

    
55
def ExportInstance(opts, args):
56
  """Export an instance to an image in the cluster.
57

    
58
  Args:
59
   opts - class with options as members
60
   args - list with a single element, the instance name
61

    
62
  Returns:
63
    1 in case of error, 0 otherwise
64

    
65
  """
66
  op = opcodes.OpExportInstance(instance_name=args[0],
67
                                target_node=opts.node,
68
                                shutdown=opts.shutdown)
69

    
70
  SubmitOpCode(op)
71

    
72

    
73
def ImportInstance(opts, args):
74
  """Add an instance to the cluster.
75

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

    
87
  Returns:
88
    1 in case of error, 0 otherwise
89

    
90
  """
91
  instance = args[0]
92

    
93
  (pnode, snode) = SplitNodeOption(opts.node)
94

    
95
  hypervisor = None
96
  hvparams = {}
97
  if opts.hypervisor:
98
    hypervisor, hvparams = opts.hypervisor
99

    
100
  ValidateBeParams(opts.beparams)
101

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

    
118
  SubmitOpCode(op)
119
  return 0
120

    
121

    
122
def RemoveExport(opts, args):
123
  """Remove an export from the cluster.
124

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

    
130
  Returns:
131
    1 in case of error, 0 otherwise
132

    
133
  """
134
  instance = args[0]
135
  op = opcodes.OpRemoveExport(instance_name=args[0])
136

    
137
  SubmitOpCode(op)
138
  return 0
139

    
140

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

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

    
216
if __name__ == '__main__':
217
  sys.exit(GenericMain(commands))