Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
94
Merge Requests
10
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7a1f12cb
authored
May 27, 2021
by
Máhonfai Bálint
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update mass create to log created VMs and handle errors
parent
6f4187b1
Pipeline
#1442
passed with stage
in 0 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
17 deletions
+23
-17
circle/vm/management/commands/mass_create_vms.py
+20
-4
circle/vm/models/instance.py
+3
-13
No files found.
circle/vm/management/commands/mass_create_vms.py
View file @
7a1f12cb
...
...
@@ -14,8 +14,9 @@
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from
django.contrib.auth.models
import
User
from
django.core.management.base
import
BaseCommand
from
vm.models
import
Instance
,
InstanceTemplate
...
...
@@ -32,11 +33,26 @@ class Command(BaseCommand):
with
open
(
options
[
'users'
])
as
f
:
users
=
f
.
read
()
.
splitlines
()
missing_users
=
Instance
.
mass_create_for_users
(
template
,
users
,
options
[
'admin'
],
options
[
'operator'
]
missing_users
,
error_users
=
[],
[]
for
user_id
in
users
:
try
:
Instance
.
create_for_user
(
template
,
user_id
,
options
[
'admin'
],
options
[
'operator'
]
)
self
.
stdout
.
write
(
'Created VM for user:
%
s'
%
user_id
)
except
User
.
DoesNotExist
:
missing_users
.
append
(
user_id
)
except
:
error_users
.
append
(
user_id
)
if
len
(
missing_users
)
>
0
:
self
.
stdout
.
write
(
'These users do not exist:'
)
self
.
stdout
.
write
(
'
\n
These users do not exist:'
)
for
user
in
missing_users
:
self
.
stdout
.
write
(
user
)
if
len
(
error_users
)
>
0
:
self
.
stdout
.
write
(
'
\n
Failed to create VM for these users:'
)
for
user
in
error_users
:
self
.
stdout
.
write
(
user
)
if
len
(
missing_users
)
+
len
(
error_users
)
==
0
:
self
.
stdout
.
write
(
'
\n
Created all VMs successfully.'
)
circle/vm/models/instance.py
View file @
7a1f12cb
...
...
@@ -443,20 +443,12 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
for
cps
in
customized_params
]
@classmethod
def
mass_create_for_users
(
cls
,
template
,
users
,
admin
=
None
,
operator
=
None
,
**
kwargs
):
def
create_for_user
(
cls
,
template
,
user_id
,
admin
=
None
,
operator
=
None
,
**
kwargs
):
"""
Create and deploy an instance of a template for each user
in a list of users. Returns the user IDs of missing users.
Create and deploy an instance of a template for a given user.
"""
user_instances
=
[]
missing_users
=
[]
for
user_id
in
users
:
try
:
user_instances
.
append
(
User
.
objects
.
get
(
profile__org_id
=
user_id
))
except
User
.
DoesNotExist
:
missing_users
.
append
(
user_id
)
user
=
User
.
objects
.
get
(
profile__org_id
=
user_id
)
for
user
in
user_instances
:
instance
=
cls
.
create_from_template
(
template
,
user
,
**
kwargs
)
if
admin
:
instance
.
set_level
(
User
.
objects
.
get
(
username
=
admin
),
'owner'
)
...
...
@@ -464,8 +456,6 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
instance
.
set_level
(
User
.
objects
.
get
(
username
=
operator
),
'operator'
)
instance
.
deploy
(
user
=
user
)
return
missing_users
def
clean
(
self
,
*
args
,
**
kwargs
):
self
.
time_of_suspend
,
self
.
time_of_delete
=
self
.
get_renew_times
()
super
(
Instance
,
self
)
.
clean
(
*
args
,
**
kwargs
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment