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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
27 deletions
+33
-27
circle/vm/management/commands/mass_create_vms.py
+21
-5
circle/vm/models/instance.py
+12
-22
No files found.
circle/vm/management/commands/mass_create_vms.py
View file @
7a1f12cb
...
@@ -14,8 +14,9 @@
...
@@ -14,8 +14,9 @@
#
#
# You should have received a copy of the GNU General Public License along
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
# 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
django.core.management.base
import
BaseCommand
from
vm.models
import
Instance
,
InstanceTemplate
from
vm.models
import
Instance
,
InstanceTemplate
...
@@ -32,11 +33,26 @@ class Command(BaseCommand):
...
@@ -32,11 +33,26 @@ class Command(BaseCommand):
with
open
(
options
[
'users'
])
as
f
:
with
open
(
options
[
'users'
])
as
f
:
users
=
f
.
read
()
.
splitlines
()
users
=
f
.
read
()
.
splitlines
()
missing_users
=
Instance
.
mass_create_for_users
(
missing_users
,
error_users
=
[],
[]
template
,
users
,
options
[
'admin'
],
options
[
'operator'
]
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
:
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
:
for
user
in
missing_users
:
self
.
stdout
.
write
(
user
)
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,28 +443,18 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
...
@@ -443,28 +443,18 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
for
cps
in
customized_params
]
for
cps
in
customized_params
]
@classmethod
@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
Create and deploy an instance of a template for a given user.
in a list of users. Returns the user IDs of missing users.
"""
"""
user
=
User
.
objects
.
get
(
profile__org_id
=
user_id
)
user_instances
=
[]
missing_users
=
[]
instance
=
cls
.
create_from_template
(
template
,
user
,
**
kwargs
)
for
user_id
in
users
:
if
admin
:
try
:
instance
.
set_level
(
User
.
objects
.
get
(
username
=
admin
),
'owner'
)
user_instances
.
append
(
User
.
objects
.
get
(
profile__org_id
=
user_id
))
if
operator
:
except
User
.
DoesNotExist
:
instance
.
set_level
(
User
.
objects
.
get
(
username
=
operator
),
'operator'
)
missing_users
.
append
(
user_id
)
instance
.
deploy
(
user
=
user
)
for
user
in
user_instances
:
instance
=
cls
.
create_from_template
(
template
,
user
,
**
kwargs
)
if
admin
:
instance
.
set_level
(
User
.
objects
.
get
(
username
=
admin
),
'owner'
)
if
operator
:
instance
.
set_level
(
User
.
objects
.
get
(
username
=
operator
),
'operator'
)
instance
.
deploy
(
user
=
user
)
return
missing_users
def
clean
(
self
,
*
args
,
**
kwargs
):
def
clean
(
self
,
*
args
,
**
kwargs
):
self
.
time_of_suspend
,
self
.
time_of_delete
=
self
.
get_renew_times
()
self
.
time_of_suspend
,
self
.
time_of_delete
=
self
.
get_renew_times
()
...
...
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