Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-backup @ dd4b1106

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

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

    
37
  Returns:
38
    nothing
39

    
40
  """
41

    
42
  op = opcodes.OpQueryExports(nodes=opts.nodes)
43
  exports = SubmitOpCode(op)
44
  for node in exports:
45
    print ("Node: %s" % node)
46
    print ("Exports:")
47
    for instance_name in exports[node]:
48
      print ("\t%s" % instance_name)
49

    
50

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

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

    
58
  Returns:
59
    1 in case of error, 0 otherwise
60

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

    
66
  SubmitOpCode(op)
67

    
68
def ImportInstance(opts, args):
69
  """Add an instance to the cluster.
70

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

    
82
  Returns:
83
    1 in case of error, 0 otherwise
84

    
85
  """
86

    
87
  instance = args[0]
88

    
89
  op = opcodes.OpCreateInstance(instance_name=instance, mem_size=opts.mem,
90
                                disk_size=opts.size, swap_size=opts.swap,
91
                                disk_template=opts.disk_template,
92
                                mode=constants.INSTANCE_IMPORT, pnode=opts.node,
93
                                snode=opts.snode, vcpus=opts.vcpus,
94
                                ip=opts.ip, bridge=opts.bridge, start=False,
95
                                src_node=opts.src_node, src_path=opts.src_dir,
96
                                wait_for_sync=opts.wait_for_sync)
97
  SubmitOpCode(op)
98
  return 0
99

    
100

    
101
# options used in more than one cmd
102
node_opt = make_option("-n", "--node", dest="node", help="Target node",
103
                       metavar="<node>")
104
force_opt = make_option("-f", "--force", dest="force", action="store_true",
105
                        default=False, help="Force the operation")
106

    
107
# this is defined separately due to readability only
108
import_opts = [
109
  DEBUG_OPT,
110
  node_opt,
111
  cli_option("-s", "--os-size", dest="size", help="Disk size",
112
             default=20 * 1024, type="unit", metavar="<size>"),
113
  cli_option("--swap-size", dest="swap", help="Swap size",
114
             default=4 * 1024, type="unit", metavar="<size>"),
115
  cli_option("-m", "--memory", dest="mem", help="Memory size",
116
             default=128, type="unit", metavar="<mem>"),
117
  make_option("-p", "--cpu", dest="vcpus", help="Number of virtual CPUs",
118
              default=1, type="int", metavar="<PROC>"),
119
  make_option("-t", "--disk-template", dest="disk_template",
120
              help="Custom disk setup (diskless, plain, local_raid1 or"
121
              " remote_raid1)", default=None, metavar="TEMPL"),
122
  make_option("-i", "--ip", dest="ip",
123
              help="IP address ('none' [default], 'auto', or specify address)",
124
              default='none', type="string", metavar="<ADDRESS>"),
125
  make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
126
              action="store_false", help="Don't wait for sync (DANGEROUS!)"),
127
  make_option("--secondary-node", dest="snode",
128
              help="Secondary node for remote_raid1 disk layout",
129
              metavar="<node>"),
130
  make_option("-b", "--bridge", dest="bridge",
131
              help="Bridge to connect this instance to",
132
              default=None, metavar="<bridge>"),
133
  make_option("--src-node", dest="src_node", help="Source node",
134
              metavar="<node>"),
135
  make_option("--src-dir", dest="src_dir", help="Source directory",
136
              metavar="<dir>"),
137
  ]
138

    
139

    
140
commands = {
141
  'list': (PrintExportList, ARGS_NONE,
142
           [DEBUG_OPT,
143
            make_option("--nodes", dest="nodes", default=[], action="append",
144
                        help="List only backups stored on these nodes"),
145
            ],
146
           "", "Lists instance exports available in the ganeti cluster"),
147
  'export': (ExportInstance, ARGS_ONE,
148
             [node_opt, DEBUG_OPT, force_opt,
149
              make_option("","--noshutdown", dest="shutdown",
150
                          action="store_false", default=True,
151
                          help="Don't shutdown the instance (unsafe)"), ],
152
             "[opts...] <name>",
153
             "Exports an instance to an image"),
154
  'import': (ImportInstance, ARGS_ONE, import_opts, "[opts...] <name>",
155
             "Imports an instance from an exported image"),
156
  }
157

    
158
if __name__ == '__main__':
159
  retcode = GenericMain(commands)
160
  sys.exit(retcode)