Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
3eb8a702
Commit
3eb8a702
authored
Dec 12, 2016
by
yumaojun03
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
为模型生成fake数据
parent
806d38bb
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
222 additions
and
16 deletions
+222
-16
__init__.py
apps/ops/models/__init__.py
+1
-0
ansible.py
apps/ops/models/ansible.py
+68
-9
cron.py
apps/ops/models/cron.py
+29
-2
sudo.py
apps/ops/models/sudo.py
+119
-4
utils.py
apps/ops/models/utils.py
+5
-1
No files found.
apps/ops/models/__init__.py
View file @
3eb8a702
from
ansible
import
*
from
cron
import
*
from
sudo
import
*
from
utils
import
*
apps/ops/models/ansible.py
View file @
3eb8a702
...
...
@@ -30,21 +30,21 @@ class Tasker(models.Model):
return
self
.
hosts
.
split
(
','
)
@classmethod
def
generate_fake
(
cls
,
count
=
10
0
):
def
generate_fake
(
cls
,
count
=
2
0
):
from
random
import
seed
from
uuid
import
uuid4
import
forgery_py
from
django.db
import
IntegrityError
seed
()
for
i
in
range
(
count
):
group
=
cls
(
name
=
forgery_py
.
name
.
full_name
(
),
comment
=
forgery_py
.
lorem_ipsum
.
sentenc
e
(),
created_by
=
'Fake'
)
tasker
=
cls
(
uuid
=
str
(
uuid4
()
),
name
=
forgery_py
.
name
.
full_nam
e
(),
)
try
:
group
.
save
()
logger
.
debug
(
'Generate fake
asset group:
%
s'
%
group
.
name
)
except
IntegrityError
:
print
(
'Error
continue'
)
tasker
.
save
()
logger
.
debug
(
'Generate fake
tasker:
%
s'
%
tasker
.
name
)
except
Exception
as
e
:
print
(
'Error
:
%
s, continue...'
%
e
.
message
)
continue
...
...
@@ -59,6 +59,25 @@ class AnsiblePlay(models.Model):
def
to_dict
(
self
):
return
{
"uuid"
:
self
.
uuid
,
"name"
:
self
.
name
}
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
,
choice
from
uuid
import
uuid4
import
forgery_py
seed
()
for
i
in
range
(
count
):
play
=
cls
(
uuid
=
str
(
uuid4
()),
name
=
forgery_py
.
name
.
full_name
(),
)
try
:
play
.
tasker
=
choice
(
Tasker
.
objects
.
all
())
play
.
save
()
logger
.
debug
(
'Generate fake play:
%
s'
%
play
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
AnsibleTask
(
models
.
Model
):
play
=
models
.
ForeignKey
(
AnsiblePlay
,
related_name
=
'tasks'
,
blank
=
True
,
null
=
True
)
...
...
@@ -77,6 +96,25 @@ class AnsibleTask(models.Model):
def
success
(
self
):
pass
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
,
choice
from
uuid
import
uuid4
import
forgery_py
seed
()
for
i
in
range
(
count
):
task
=
cls
(
uuid
=
str
(
uuid4
()),
name
=
forgery_py
.
name
.
full_name
(),
)
try
:
task
.
play
=
choice
(
AnsiblePlay
.
objects
.
all
())
task
.
save
()
logger
.
debug
(
'Generate fake play:
%
s'
%
task
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
AnsibleHostResult
(
models
.
Model
):
task
=
models
.
ForeignKey
(
AnsibleTask
,
related_name
=
'host_results'
,
blank
=
True
,
null
=
True
)
...
...
@@ -230,6 +268,27 @@ class AnsibleHostResult(models.Model):
except
Exception
as
e
:
return
{
"msg"
:
"deal with ping data failed,
%
s"
%
e
.
message
,
"data"
:
None
}
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
,
choice
import
forgery_py
seed
()
for
i
in
range
(
count
):
result
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
success
=
forgery_py
.
lorem_ipsum
.
sentence
(),
failed
=
forgery_py
.
lorem_ipsum
.
sentence
(),
skipped
=
forgery_py
.
lorem_ipsum
.
sentence
(),
unreachable
=
forgery_py
.
lorem_ipsum
.
sentence
(),
no_host
=
forgery_py
.
lorem_ipsum
.
sentence
(),
)
try
:
result
.
task
=
choice
(
AnsibleTask
.
objects
.
all
())
result
.
save
()
logger
.
debug
(
'Generate fake HostResult:
%
s'
%
result
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
...
...
apps/ops/models/cron.py
View file @
3eb8a702
# ~*~ coding: utf-8 ~*~
from
__future__
import
unicode_literals
,
absolute_import
import
logging
from
django.db
import
models
from
assets.models
import
Asset
from
django.utils.translation
import
ugettext_lazy
as
_
logger
=
logging
.
getLogger
(
__name__
)
__all__
=
[
"CronTable"
]
...
...
@@ -31,4 +35,27 @@ class CronTable(models.Model):
@property
def
describe
(
self
):
return
"http://docs.ansible.com/ansible/cron_module.html"
\ No newline at end of file
return
"http://docs.ansible.com/ansible/cron_module.html"
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
,
choice
import
forgery_py
seed
()
for
i
in
range
(
count
):
cron
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
month
=
str
(
choice
(
range
(
1
,
13
))),
weekday
=
str
(
choice
(
range
(
0
,
7
))),
day
=
str
(
choice
(
range
(
1
,
32
))),
hour
=
str
(
choice
(
range
(
0
,
24
))),
minute
=
str
(
choice
(
range
(
0
,
60
))),
job
=
forgery_py
.
lorem_ipsum
.
sentence
(),
user
=
forgery_py
.
name
.
first_name
(),
)
try
:
cron
.
save
()
logger
.
debug
(
'Generate fake cron:
%
s'
%
cron
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
\ No newline at end of file
apps/ops/models/sudo.py
View file @
3eb8a702
# ~*~ coding: utf-8 ~*~
from
__future__
import
unicode_literals
,
absolute_import
import
logging
from
jinja2
import
Template
from
django.db
import
models
from
django.utils
import
timezone
from
assets.models
import
Asset
,
AssetGroup
from
django.utils.translation
import
ugettext_lazy
as
_
logger
=
logging
.
getLogger
(
__name__
)
__all__
=
[
"HostAlia"
,
"UserAlia"
,
"CmdAlia"
,
"RunasAlia"
,
"Privilege"
,
"Extra_conf"
,
"Sudo"
]
...
...
@@ -17,6 +22,23 @@ class HostAlia(models.Model):
def
__unicode__
(
self
):
return
self
.
name
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
import
forgery_py
seed
()
for
i
in
range
(
count
):
hostA
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
host_items
=
forgery_py
.
lorem_ipsum
.
sentence
(),
)
try
:
hostA
.
save
()
logger
.
debug
(
'Generate fake host alia:
%
s'
%
hostA
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
UserAlia
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
null
=
True
,
unique
=
True
,
verbose_name
=
_
(
'User_Alias'
))
...
...
@@ -25,6 +47,23 @@ class UserAlia(models.Model):
def
__unicode__
(
self
):
return
self
.
name
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
import
forgery_py
seed
()
for
i
in
range
(
count
):
userA
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
user_items
=
forgery_py
.
lorem_ipsum
.
sentence
(),
)
try
:
userA
.
save
()
logger
.
debug
(
'Generate fake host alia:
%
s'
%
userA
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
CmdAlia
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
null
=
True
,
unique
=
True
,
verbose_name
=
_
(
'Command_Alias'
))
...
...
@@ -33,6 +72,23 @@ class CmdAlia(models.Model):
def
__unicode__
(
self
):
return
self
.
name
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
import
forgery_py
seed
()
for
i
in
range
(
count
):
cmdA
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
cmd_items
=
forgery_py
.
lorem_ipsum
.
sentence
(),
)
try
:
cmdA
.
save
()
logger
.
debug
(
'Generate fake command alia:
%
s'
%
cmdA
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
RunasAlia
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
null
=
True
,
unique
=
True
,
verbose_name
=
_
(
'Runas_Alias'
))
...
...
@@ -41,6 +97,23 @@ class RunasAlia(models.Model):
def
__unicode__
(
self
):
return
self
.
name
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
import
forgery_py
seed
()
for
i
in
range
(
count
):
runas
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
runas_items
=
forgery_py
.
lorem_ipsum
.
sentence
(),
)
try
:
runas
.
save
()
logger
.
debug
(
'Generate fake RunAs alia:
%
s'
%
runas
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
Privilege
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
128
,
unique
=
True
,
verbose_name
=
_
(
'Name'
))
...
...
@@ -61,6 +134,27 @@ class Privilege(models.Model):
def
to_tuple
(
self
):
return
self
.
user
.
name
,
self
.
host
.
name
,
self
.
runas
.
name
,
self
.
command
.
name
,
self
.
nopassword
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
,
choice
import
forgery_py
seed
()
for
i
in
range
(
count
):
pri
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
comment
=
forgery_py
.
lorem_ipsum
.
sentence
(),
)
try
:
pri
.
user
=
choice
(
UserAlia
.
objects
.
all
())
pri
.
host
=
choice
(
HostAlia
.
objects
.
all
())
pri
.
runas
=
choice
(
RunasAlia
.
objects
.
all
())
pri
.
command
=
choice
(
CmdAlia
.
objects
.
all
())
pri
.
save
()
logger
.
debug
(
'Generate fake privileges:
%
s'
%
pri
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
class
Extra_conf
(
models
.
Model
):
line
=
models
.
TextField
(
blank
=
True
,
null
=
True
,
verbose_name
=
_
(
'Extra_Item'
),
...
...
@@ -80,8 +174,10 @@ class Sudo(models.Model):
name
=
models
.
CharField
(
max_length
=
128
,
unique
=
True
,
verbose_name
=
_
(
'Name'
),
help_text
=
_
(
'Name for this sudo'
))
created_by
=
models
.
CharField
(
max_length
=
32
,
blank
=
True
,
verbose_name
=
_
(
'Created by'
),
help_text
=
_
(
'The create time of this sudo'
))
created_time
=
models
.
DateTimeField
(
verbose_name
=
_
(
'Created Time'
),
default
=
timezone
.
now
(),
help_text
=
_
(
'The create time of this sudo'
))
modify_time
=
models
.
DateTimeField
(
auto_now
=
True
,
verbose_name
=
_
(
'Modify Time'
),
help_text
=
_
(
'The recent modify time of this sudo'
))
assets
=
models
.
ManyToManyField
(
Asset
,
blank
=
True
,
related_name
=
'sudos'
)
asset_groups
=
models
.
ManyToManyField
(
AssetGroup
,
blank
=
True
,
related_name
=
'sudos'
)
extra_lines
=
models
.
ManyToManyField
(
Extra_conf
,
related_name
=
'sudos'
,
blank
=
True
)
...
...
@@ -204,4 +300,23 @@ root ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
"""
\ No newline at end of file
"""
@classmethod
def
generate_fake
(
cls
,
count
=
20
):
from
random
import
seed
,
choice
import
forgery_py
seed
()
for
i
in
range
(
count
):
sudo
=
cls
(
name
=
forgery_py
.
name
.
full_name
(),
created_time
=
timezone
.
now
()
)
try
:
sudo
.
save
()
sudo
.
privilege_items
=
[
choice
(
Privilege
.
objects
.
all
())]
sudo
.
save
()
logger
.
debug
(
'Generate fake cron:
%
s'
%
sudo
.
name
)
except
Exception
as
e
:
print
(
'Error:
%
s, continue...'
%
e
.
message
)
continue
\ No newline at end of file
apps/ops/models/utils.py
View file @
3eb8a702
...
...
@@ -5,7 +5,10 @@ from ansible import *
from
cron
import
*
from
sudo
import
*
__all__
=
[
"generate_fake"
]
def
generate_fake
():
for
cls
in
(
AssetGroup
,
IDC
,
AdminUser
,
SystemUser
,
Asset
):
for
cls
in
(
Tasker
,
AnsiblePlay
,
AnsibleTask
,
AnsibleHostResult
,
CronTable
,
HostAlia
,
UserAlia
,
CmdAlia
,
RunasAlia
,
Privilege
,
Sudo
):
cls
.
generate_fake
()
\ No newline at end of file
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