Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
memri
plugins
DEPRECATED plugin-template
Commits
18716d59
Commit
18716d59
authored
Jun 20, 2021
by
Alp Deniz Ogut
Browse files
Use plugin_id instead of plugin item
parent
e8845267
Pipeline
#2723
failed with stage
Changes
3
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
plugin_template/plugin_flow/plugin_flow.py
View file @
18716d59
...
...
@@ -21,11 +21,10 @@ logging.basicConfig(format='%(asctime)s [%(levelname)s] - %(message)s')
# Requires the plugin class to have a "run" method.
class
PluginFlow
:
def
__init__
(
self
,
client
,
run_id
=
None
,
plugin
=
None
):
def
__init__
(
self
,
client
,
run_id
=
None
,
plugin
_id
=
None
):
self
.
client
=
client
self
.
run_id
=
run_id
self
.
plugin
=
plugin
self
.
plugin_id
=
plugin
.
id
if
plugin
else
None
self
.
plugin_id
=
plugin_id
self
.
_setup_schema
()
self
.
initialized
()
...
...
@@ -52,18 +51,18 @@ class PluginFlow:
def
get_account_from_plugin
(
self
,
service
=
None
):
# Update plugin
self
.
plugin
=
self
.
client
.
get
(
self
.
plugin_id
)
plugin
=
self
.
client
.
get
(
self
.
plugin_id
)
# get its connected accounts
account_edges
=
self
.
plugin
.
get_edges
(
'account'
)
account_edges
=
plugin
.
get_edges
(
'account'
)
# if multiple accounts are used
if
len
(
account_edges
)
>
1
and
service
:
for
account_edge
in
account_edges
:
account
=
account_edge
.
traverse
(
self
.
plugin
)
account
=
account_edge
.
traverse
(
plugin
)
if
account
.
service
==
service
:
return
account
# assumes there is only one account
elif
len
(
account_edges
)
==
1
:
return
account_edges
[
0
].
traverse
(
self
.
plugin
)
return
account_edges
[
0
].
traverse
(
plugin
)
def
ask_user_for_accounts
(
self
,
service
,
view
,
oauth_url
=
None
):
# start userActionNeeded flow
...
...
@@ -100,8 +99,10 @@ class PluginFlow:
# Save accounts as an edge to the plugin
self
.
client
.
create
(
account
)
logging
.
warning
(
f
"ACCOUNT created:
{
account
.
__dict__
}
"
)
self
.
plugin
.
add_edge
(
'account'
,
account
)
self
.
plugin
.
update
(
self
.
client
)
# add the account to the plugin item
plugin
=
self
.
client
.
get
(
self
.
plugin_id
)
plugin
.
add_edge
(
'account'
,
account
)
plugin
.
update
(
self
.
client
)
# =======================================
# ---------- USER CLIENT METHODS -------------
...
...
@@ -111,17 +112,17 @@ class PluginFlow:
self
.
client
.
create
(
plugin
)
# set in instance
self
.
plugin_id
=
plugin
.
id
self
.
plugin
=
plugin
# TODO: Add an edge from the plugin to the desired CVUStoredDefinitions
# start_plugin.view = createLoginCVUs()
return
plugin
.
id
return
plugin
def
trigger_plugin
(
self
,
interval
=
None
):
starter
=
StartPlugin
(
targetItemId
=
self
.
plugin_id
,
container
=
self
.
plugin
.
container
,
state
=
RUN_IDLE
,
interval
=
interval
)
plugin
=
self
.
client
.
get
(
self
.
plugin_id
)
starter
=
StartPlugin
(
targetItemId
=
self
.
plugin_id
,
container
=
plugin
.
container
,
state
=
RUN_IDLE
,
interval
=
interval
)
# add cvus here
self
.
client
.
create
(
starter
)
self
.
run_id
=
starter
.
id
print
(
f
"Started plugin
{
self
.
plugin
.
name
}
-
{
self
.
plugin_id
}
and run id
{
self
.
run_id
}
"
)
print
(
f
"Started plugin
{
plugin
.
name
}
-
{
self
.
plugin_id
}
and run id
{
self
.
run_id
}
"
)
def
terminate_run
(
self
):
self
.
_set_run_vars
({
'interval'
:
None
})
...
...
@@ -139,26 +140,26 @@ class PluginFlow:
def
initialized
(
self
):
logging
.
warning
(
"PLUGIN run is initialized"
)
self
.
state
=
RUN_INITIALIZED
if
self
.
run_id
and
self
.
plugin
:
if
self
.
run_id
:
self
.
_set_run_vars
({
'state'
:
RUN_INITIALIZED
})
def
started
(
self
):
logging
.
warning
(
"PLUGIN run is started"
)
self
.
state
=
RUN_STARTED
if
self
.
run_id
and
self
.
plugin
:
if
self
.
run_id
:
self
.
_set_run_vars
({
'state'
:
RUN_STARTED
})
def
failed
(
self
,
error
):
logging
.
error
(
f
"PLUGIN run is failed:
{
error
}
"
)
print
(
"Exception while running plugin:"
,
error
)
self
.
state
=
RUN_FAILED
if
self
.
run_id
and
self
.
plugin
:
if
self
.
run_id
:
self
.
_set_run_vars
({
'state'
:
RUN_FAILED
,
'message'
:
str
(
error
)})
def
completed
(
self
):
logging
.
warning
(
"PLUGIN run is completed"
)
self
.
state
=
RUN_COMPLETED
if
self
.
run_id
and
self
.
plugin
:
if
self
.
run_id
:
self
.
_set_run_vars
({
'state'
:
RUN_COMPLETED
})
def
complete_user_action
(
self
):
...
...
plugin_template/plugin_template.py
View file @
18716d59
...
...
@@ -11,18 +11,14 @@ APP_SECRET = ""
class
PluginTemplate
(
PluginFlow
):
def
__init__
(
self
,
client
,
plugin
=
None
,
start_plugi
n_id
=
None
):
super
().
__init__
(
client
=
client
,
plugin
=
plugin
,
run_id
=
start_plugi
n_id
)
def
__init__
(
self
,
client
,
plugin
_id
=
None
,
ru
n_id
=
None
):
super
().
__init__
(
client
=
client
,
plugin
_id
=
plugin
_id
,
run_id
=
ru
n_id
)
self
.
dummy_service
=
ServiceAPI
()
# activate plugin flow
if
plugin
and
start_plugi
n_id
:
if
plugin
_id
and
ru
n_id
:
# init login process
# self.start_auth_oauth()
self
.
start_auth_userpass_and_two_factor
()
if
self
.
dummy_service
.
authenticated
:
logging
.
warning
(
"Authtentication success"
)
else
:
logging
.
warning
(
"Authtentication failure"
)
def
run
(
self
):
...
...
@@ -59,17 +55,17 @@ class PluginTemplate(PluginFlow):
result
=
self
.
dummy_service
.
login
(
account
.
identifier
,
account
.
secret
)
if
result
and
result
[
'success'
]
==
True
:
if
result
[
'2fa_required'
]:
logging
.
warning
(
"Auth
t
entication requires 2FA"
)
logging
.
warning
(
"Authentication requires 2FA"
)
for
_
in
range
(
NUM_LOGIN_TRIES
):
account
=
self
.
ask_user_for_accounts
(
SERVICE_NAME
,
'2fa-view'
)
result
=
self
.
dummy_service
.
complete_2fa
(
account
.
code
)
if
result
[
'success'
]:
logging
.
warning
(
"Auth
t
entication 2FA success"
)
logging
.
warning
(
"Authentication 2FA success"
)
return
True
# set error message for UI
self
.
set_account_vars
({
'errorMessage'
:
'Incorrect 2fa code'
},
service
=
SERVICE_NAME
)
else
:
logging
.
warning
(
"Auth
t
entication success"
)
logging
.
warning
(
"Authentication success"
)
return
True
# set error message for UI
self
.
set_account_vars
({
'errorMessage'
:
'Incorrect credentials'
},
service
=
SERVICE_NAME
)
...
...
scripts/main.py
View file @
18716d59
...
...
@@ -10,13 +10,9 @@ def main(pod_url=None, database_key=None, owner_key=None, plugin_item=None, star
# setup client
client
=
PodClient
(
url
=
pod_url
,
database_key
=
database_key
,
owner_key
=
owner_key
,
auth_json
=
auth_json
)
# Get plugin item from the POD.
# expand? we need credential edges. Also move into PluginFlow after getting plugin_id only.
plugin
=
Plugin
.
from_json
(
json
.
loads
(
plugin_item
))
# ----- PLUGIN STARTS ------
plugin
=
PluginTemplate
(
client
,
plugin
,
start_plugin_id
)
plugin
=
PluginTemplate
(
client
,
plugin
_id
=
plugin_item
[
'id'
],
run_id
=
start_plugin_id
)
try
:
plugin
.
start
()
except
Exception
as
e
:
...
...
@@ -41,8 +37,8 @@ def parse_environment_variables():
print
(
f
"Missing environment variable:
{
v
}
. Exiting..."
)
exit
()
plugin_data
[
k
]
=
os
.
environ
[
v
]
# parse auth json
if
k
==
'auth_json'
and
plugin_data
[
k
]:
# parse
plugin and
auth json
if
k
in
[
'plugin_item'
,
'auth_json'
]
and
plugin_data
[
k
]:
plugin_data
[
k
]
=
json
.
loads
(
plugin_data
[
k
])
return
plugin_data
...
...
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