Lines Matching full:server

6 """This module provides functions to manage servers in server database
10 Create a server with given role, with status primary.
13 Delete a server from the database.
17 Modify a server's role, status, note, or attribute:
18 1. Add role to a server. If the server is in primary status, proper actions
20 2. Delete a role from a server. If the server is in primary status, proper
22 3. Change status of a server. If the server is changed from or to primary
24 or disable each role of the server.
25 4. Change note of a server. Note is a field you can add description about
26 the server.
27 5. Change/delete attribute of a server. Attribute can be used to store
28 information about a server. For example, the max_processes count for a
42 from autotest_lib.frontend.server import models as server_models
47 def _add_role(server, role, action): argument
48 """Add a role to the server.
50 @param server: An object of server_models.Server.
51 @param role: Role to be added to the server.
58 if role in server.get_role_names():
60 'Server %s already has role %s.' % (server.hostname, role))
62 # Verify server
63 if not server_manager_utils.check_server(server.hostname, role):
65 'Server %s is not ready for role %s.' % (server.hostname, role))
68 server.status == server_models.Server.STATUS.PRIMARY):
69 servers = server_models.Server.objects.filter(
70 roles__role=role, status=server_models.Server.STATUS.PRIMARY)
73 'Role %s must be unique. Server %s already has role %s.' %
76 server_models.ServerRole.objects.create(server=server, role=role)
78 # If needed, apply actions to enable the role for the server.
79 server_manager_actions.try_execute(server, [role], enable=True,
82 print('Role %s is added to server %s.' % (role, server.hostname))
85 def _delete_role(server, role, action=False): argument
86 """Delete a role from the server.
88 @param server: An object of server_models.Server.
89 @param role: Role to be deleted from the server.
96 if role not in server.get_role_names():
98 'Server %s does not have role %s.' % (server.hostname, role))
100 if server.status == server_models.Server.STATUS.PRIMARY:
101 server_manager_utils.warn_missing_role(role, server)
103 # Apply actions to disable the role for the server before the role is
104 # removed from the server.
105 server_manager_actions.try_execute(server, [role], enable=False,
108 print('Deleting role %s from server %s...' % (role, server.hostname))
109 server.roles.get(role=role).delete()
111 # Apply actions to disable the role for the server after the role is
112 # removed from the server.
113 server_manager_actions.try_execute(server, [role], enable=False,
116 if (not server.get_role_names() and
117 server.status == server_models.Server.STATUS.PRIMARY):
118 print ('Server %s has no role.')
120 print('Role %s is deleted from server %s.' % (role, server.hostname))
123 def _change_status(server, status, action): argument
124 """Change the status of the server.
126 @param server: An object of server_models.Server.
127 @param status: New status of the server.
134 if server.status == status:
136 'Server %s already has status of %s.' %
137 (server.hostname, status))
138 if (not server.roles.all() and
139 status == server_models.Server.STATUS.PRIMARY):
141 'Server %s has no role associated. Server must have a role to '
142 'be in status primary.' % server.hostname)
144 # Abort the action if the server's status will be changed to primary and
145 # the Autotest instance already has another server running an unique role.
146 # For example, a scheduler server is already running, and a repair_required
147 # server with role scheduler should not be changed to status primary.
148 unique_roles = server.roles.filter(
150 if unique_roles and status == server_models.Server.STATUS.PRIMARY:
152 servers = server_models.Server.objects.filter(
154 status=server_models.Server.STATUS.PRIMARY)
157 'Role %s must be unique. Server %s already has the '
160 # Post a warning if the server's status will be changed from primary to
161 # other value and the server is running a unique role across database, e.g.
163 if server.status == server_models.Server.STATUS.PRIMARY:
164 for role in server.get_role_names():
165 server_manager_utils.warn_missing_role(role, server)
167 enable = status == server_models.Server.STATUS.PRIMARY
168 server_manager_actions.try_execute(server, server.get_role_names(),
172 prev_status = server.status
173 server.status = status
174 server.save()
176 # Apply actions to enable/disable roles of the server after the status is
178 server_manager_actions.try_execute(server, server.get_role_names(),
183 print('Status of server %s is changed from %s to %s. Affected roles: %s' %
184 (server.hostname, prev_status, status,
185 ', '.join(server.get_role_names())))
190 """Create a new server.
192 The status of new server will always be primary.
194 @param hostname: hostname of the server.
195 @param role: role of the new server, default to None.
196 @param note: notes about the server, default to None.
198 @return: A Server object that contains the server information.
201 server = server_models.Server.objects.create(
202 hostname=hostname, status=server_models.Server.STATUS.PRIMARY,
204 server_models.ServerRole.objects.create(server=server, role=role)
205 return server
209 def delete(hostname, server=None): argument
210 """Delete given server from server database.
212 @param hostname: hostname of the server to be deleted.
213 @param server: Server object from database query, this argument should be
216 @raise ServerActionError: If delete server action failed, e.g., server is
219 print('Deleting server %s from server database.' % hostname)
222 server.status == server_models.Server.STATUS.PRIMARY):
223 print('Server %s is in status primary, need to disable its '
225 for role in server.roles.all():
226 _delete_role(server, role.role)
228 server.delete()
229 print('Server %s is deleted from server database.' % hostname)
234 attribute=None, value=None, action=False, server=None): argument
235 """Modify given server with specified actions.
237 @param hostname: hostname of the server to be modified.
238 @param role: Role to be added to the server.
239 @param status: Modify server status.
240 @param delete: True to delete given role from the server, default to False.
241 @param note: Note of the server.
242 @param attribute: Name of an attribute of the server.
243 @param value: Value of an attribute of the server.
246 @param server: Server object from database query, this argument should be
255 _add_role(server, role, action)
257 _delete_role(server, role, action)
260 _change_status(server, status, action)
263 server.note = note
264 server.save()
267 server_manager_utils.change_attribute(server, attribute, value)
269 server_manager_utils.delete_attribute(server, attribute)
271 return server