-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_start_server.lua
More file actions
78 lines (70 loc) · 2.82 KB
/
auto_start_server.lua
File metadata and controls
78 lines (70 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- (c) 2013 Flexiant Ltd
-- Released under the Apache 2.0 Licence - see LICENCE for details
function auto_start_server(p)
if(p == nil) then
return {
ref = "auto_start_server",
name = "Auto Start Server",
description = "Automatically start newly created servers if customer has AUTO_START_SERVERS Customer Key Set to 'yes'",
priority = 0,
triggerType = "POST_JOB_STATE_CHANGE",
triggerOptions = {"SUCCESSFUL"},
api = "TRIGGER",
version = 1,
}
end
if(p.input:getJobType() == new("JobType","SHUTDOWN_SERVER")) then
print('========== AUTO START SERVER TRIGGER ==========')
local serverId = p.input:getItemUUID()
local customerUUID = p.input:getCustomerUUID()
local customerCheck = checkCustomerKey(customerUUID, "AUTO_START_SERVERS")
local serverCheck =
if(customerCheck.success) then
local userToken = getUserToken(customerUUID)
userAPI:setSessionUser(userToken)
print('Starting up server: ' .. serverId)
local startServer = userAPI:changeServerStatus(serverId, new("ServerStatus","RUNNING"), true, nil, nil)
else
print("Customer key not set.")
end
print('========== AUTO START SERVER TRIGGER COMPLETE ==========')
end
return { exitState = "SUCCESS" }
end
function getUserToken(customerUUID)
local searchFilter = new("SearchFilter")
local filterCondition1 = new("FilterCondition")
filterCondition1:setField('resourceuuid')
filterCondition1:setValue({customerUUID})
filterCondition1:setCondition(new("Condition","IS_EQUAL_TO"))
searchFilter:addCondition(filterCondition1)
local customer = adminAPI:listResources(searchFilter,nil,new("ResourceType","CUSTOMER"))
local userEmail = customer:getList():get(0):getUsers():get(0):getEmail()
return userEmail .. "/" .. customer:getList():get(0):getResourceUUID()
end
function checkCustomerKey(customerUUID, resourceKeyName)
local searchFilter = new("SearchFilter")
local filterCondition1 = new("FilterCondition")
filterCondition1:setField('resourceuuid')
filterCondition1:setValue({customerUUID})
filterCondition1:setCondition(new("Condition","IS_EQUAL_TO"))
local filterCondition2 = new("FilterCondition")
filterCondition2:setField('resourcekey.name')
filterCondition2:setValue({resourceKeyName})
filterCondition2:setCondition(new("Condition","IS_EQUAL_TO"))
searchFilter:addCondition(filterCondition1)
searchFilter:addCondition(filterCondition2)
local customer = adminAPI:listResources(searchFilter,nil,new("ResourceType","CUSTOMER"))
if(customer:getList():size() == 1) then
for i = 0, customer:getList():get(0):getResourceKey():size() - 1, 1 do
if(customer:getList():get(0):getResourceKey():get(i):getName() == resourceKeyName) then
return {success = true, keyValue = customer:getList():get(0):getResourceKey():get(i):getValue() }
end
end
else
return {success = false}
end
end
function register()
return {"auto_start_server"}
end