Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-backup @ 8bb66978

History | View | Annotate | Download (7.3 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
def PrintExportList(opts, args):
32
  """Prints a list of all the exported system images.
33

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

    
38
  Returns:
39
    nothing
40

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

    
52

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

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

    
60
  Returns:
61
    1 in case of error, 0 otherwise
62

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

    
68
  SubmitOpCode(op)
69

    
70

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

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

    
85
  Returns:
86
    1 in case of error, 0 otherwise
87

    
88
  """
89
  instance = args[0]
90

    
91
  (pnode, snode) = SplitNodeOption(opts.node)
92

    
93
  op = opcodes.OpCreateInstance(instance_name=instance, mem_size=opts.mem,
94
                                disk_size=opts.size, swap_size=opts.swap,
95
                                disk_template=opts.disk_template,
96
                                mode=constants.INSTANCE_IMPORT,
97
                                pnode=pnode, snode=snode,
98
                                vcpus=opts.vcpus, ip_check=opts.ip_check,
99
                                ip=opts.ip, bridge=opts.bridge, start=False,
100
                                src_node=opts.src_node, src_path=opts.src_dir,
101
                                wait_for_sync=opts.wait_for_sync, mac="auto",
102
                                file_storage_dir=opts.file_storage_dir,
103
                                file_driver=opts.file_driver,
104
                                iallocator=opts.iallocator)
105
  SubmitOpCode(op)
106
  return 0
107

    
108

    
109
def RemoveExport(opts, args):
110
  """Remove an export from the cluster.
111

    
112
  Args:
113
   opts - class with options as members
114
   args - list with a single element, the exported instance to remove
115
  Opts used:
116

    
117
  Returns:
118
    1 in case of error, 0 otherwise
119

    
120
  """
121
  instance = args[0]
122
  op = opcodes.OpRemoveExport(instance_name=args[0])
123

    
124
  SubmitOpCode(op)
125
  return 0
126

    
127

    
128
# this is defined separately due to readability only
129
import_opts = [
130
  DEBUG_OPT,
131
  make_option("-n", "--node", dest="node",
132
              help="Target node and optional secondary node",
133
              metavar="<pnode>[:<snode>]"),
134
  cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
135
             " a suffix is used",
136
             default=20 * 1024, type="unit", metavar="<size>"),
137
  cli_option("--swap-size", dest="swap", help="Swap size",
138
             default=4 * 1024, type="unit", metavar="<size>"),
139
  cli_option("-m", "--memory", dest="mem", help="Memory size",
140
             default=128, type="unit", metavar="<mem>"),
141
  make_option("-p", "--cpu", dest="vcpus", help="Number of virtual CPUs",
142
              default=1, type="int", metavar="<PROC>"),
143
  make_option("-t", "--disk-template", dest="disk_template",
144
              help="Custom disk setup (diskless, file, plain, drbd)",
145
              default=None, metavar="TEMPL"),
146
  make_option("-i", "--ip", dest="ip",
147
              help="IP address ('none' [default], 'auto', or specify address)",
148
              default='none', type="string", metavar="<ADDRESS>"),
149
  make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
150
              action="store_false", help="Don't wait for sync (DANGEROUS!)"),
151
  make_option("-b", "--bridge", dest="bridge",
152
              help="Bridge to connect this instance to",
153
              default=None, metavar="<bridge>"),
154
  make_option("--src-node", dest="src_node", help="Source node",
155
              metavar="<node>"),
156
  make_option("--src-dir", dest="src_dir", help="Source directory",
157
              metavar="<dir>"),
158
  make_option("--no-ip-check", dest="ip_check", default=True,
159
              action="store_false", help="Don't check that the instance's IP"
160
              " is alive"),
161
  make_option("--iallocator", metavar="<NAME>",
162
              help="Select nodes for the instance automatically using the"
163
              " <NAME> iallocator plugin", default=None, type="string"),
164
  make_option("--file-storage-dir", dest="file_storage_dir",
165
              help="Relative path under default cluster-wide file storage dir"
166
              " to store file-based disks", default=None,
167
              metavar="<DIR>"),
168
  make_option("--file-driver", dest="file_driver", help="Driver to use"
169
              " for image files", default="loop", metavar="<DRIVER>"),
170
  ]
171

    
172
commands = {
173
  'list': (PrintExportList, ARGS_NONE,
174
           [DEBUG_OPT,
175
            make_option("--node", dest="nodes", default=[], action="append",
176
                        help="List only backups stored on this node"
177
                             " (can be used multiple times)"),
178
            ],
179
           "", "Lists instance exports available in the ganeti cluster"),
180
  'export': (ExportInstance, ARGS_ONE,
181
             [DEBUG_OPT, FORCE_OPT,
182
              make_option("-n", "--node", dest="node", help="Target node",
183
                          metavar="<node>"),
184
              make_option("","--noshutdown", dest="shutdown",
185
                          action="store_false", default=True,
186
                          help="Don't shutdown the instance (unsafe)"), ],
187
             "-n <target_node> [opts...] <name>",
188
             "Exports an instance to an image"),
189
  'import': (ImportInstance, ARGS_ONE, import_opts,
190
             ("[...] -t disk-type -n node[:secondary-node]"
191
              " --src-node node --src-dir dir"
192
              " <name>"),
193
             "Imports an instance from an exported image"),
194
  'remove': (RemoveExport, ARGS_ONE,
195
             [DEBUG_OPT],
196
             "<name>",
197
             "Remove exports of named instance from the filesystem."),
198
  }
199

    
200
if __name__ == '__main__':
201
  sys.exit(GenericMain(commands))