root / snf-cyclades-app / synnefo / logic / management / commands / server-modify.py @ 8c911970
History | View | Annotate | Download (5.6 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
|
36 |
from django.db import transaction |
37 |
from django.core.management.base import CommandError |
38 |
from snf_django.management.commands import SynnefoCommand |
39 |
from synnefo.management.common import (get_vm, get_flavor, convert_api_faults, |
40 |
wait_server_task) |
41 |
from snf_django.management.utils import parse_bool |
42 |
from synnefo.logic import servers |
43 |
|
44 |
|
45 |
ACTIONS = ["start", "stop", "reboot_hard", "reboot_soft"] |
46 |
|
47 |
|
48 |
class Command(SynnefoCommand): |
49 |
args = "<server ID>"
|
50 |
help = "Modify a server."
|
51 |
|
52 |
option_list = SynnefoCommand.option_list + ( |
53 |
make_option( |
54 |
'--name',
|
55 |
dest='name',
|
56 |
metavar='NAME',
|
57 |
help="Rename server."),
|
58 |
make_option( |
59 |
'--owner',
|
60 |
dest='owner',
|
61 |
metavar='USER_UUID',
|
62 |
help="Change ownership of server. Value must be a user UUID"),
|
63 |
make_option( |
64 |
"--suspended",
|
65 |
dest="suspended",
|
66 |
default=None,
|
67 |
choices=["True", "False"], |
68 |
metavar="True|False",
|
69 |
help="Mark a server as suspended/non-suspended."),
|
70 |
make_option( |
71 |
"--flavor",
|
72 |
dest="flavor",
|
73 |
metavar="FLAVOR_ID",
|
74 |
help="Resize a server by modifying its flavor. The new flavor"
|
75 |
" must have the same disk size and disk template."),
|
76 |
make_option( |
77 |
"--action",
|
78 |
dest="action",
|
79 |
choices=ACTIONS, |
80 |
metavar="|".join(ACTIONS),
|
81 |
help="Perform one of the allowed actions."),
|
82 |
make_option( |
83 |
"--wait",
|
84 |
dest="wait",
|
85 |
default="True",
|
86 |
choices=["True", "False"], |
87 |
metavar="True|False",
|
88 |
help="Wait for Ganeti jobs to complete."),
|
89 |
) |
90 |
|
91 |
@transaction.commit_on_success
|
92 |
@convert_api_faults
|
93 |
def handle(self, *args, **options): |
94 |
if len(args) != 1: |
95 |
raise CommandError("Please provide a server ID") |
96 |
|
97 |
server = get_vm(args[0], for_update=True) |
98 |
|
99 |
new_name = options.get("name", None) |
100 |
if new_name is not None: |
101 |
old_name = server.name |
102 |
server = servers.rename(server, new_name) |
103 |
self.stdout.write("Renamed server '%s' from '%s' to '%s'\n" % |
104 |
(server, old_name, new_name)) |
105 |
|
106 |
suspended = options.get("suspended", None) |
107 |
if suspended is not None: |
108 |
suspended = parse_bool(suspended) |
109 |
server.suspended = suspended |
110 |
server.save() |
111 |
self.stdout.write("Set server '%s' as suspended=%s\n" % |
112 |
(server, suspended)) |
113 |
|
114 |
new_owner = options.get('owner')
|
115 |
if new_owner is not None: |
116 |
if "@" in new_owner: |
117 |
raise CommandError("Invalid owner UUID.") |
118 |
old_owner = server.userid |
119 |
server.userid = new_owner |
120 |
server.save() |
121 |
msg = "Changed the owner of server '%s' from '%s' to '%s'.\n"
|
122 |
self.stdout.write(msg % (server, old_owner, new_owner))
|
123 |
|
124 |
wait = parse_bool(options["wait"])
|
125 |
new_flavor_id = options.get("flavor")
|
126 |
if new_flavor_id is not None: |
127 |
new_flavor = get_flavor(new_flavor_id) |
128 |
old_flavor = server.flavor |
129 |
msg = "Resizing server '%s' from flavor '%s' to '%s'.\n"
|
130 |
self.stdout.write(msg % (server, old_flavor, new_flavor))
|
131 |
server = servers.resize(server, new_flavor) |
132 |
wait_server_task(server, wait, stdout=self.stdout)
|
133 |
|
134 |
action = options.get("action")
|
135 |
if action is not None: |
136 |
if action == "start": |
137 |
server = servers.start(server) |
138 |
elif action == "stop": |
139 |
server = servers.stop(server) |
140 |
elif action == "reboot_hard": |
141 |
server = servers.reboot(server, reboot_type="HARD")
|
142 |
elif action == "reboot_stof": |
143 |
server = servers.reboot(server, reboot_type="SOFT")
|
144 |
else:
|
145 |
raise CommandError("Unknown action.") |
146 |
wait_server_task(server, wait, stdout=self.stdout)
|