Initial commit
This commit is contained in:
commit
0ab44a0f29
119
portainer.js
Normal file
119
portainer.js
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
var portainerURL, portainerEndpoint, portainerUser, portainerPass, portainerBearer;
|
||||||
|
|
||||||
|
function portainerInit(url, endpoint, user, password)
|
||||||
|
{
|
||||||
|
portainerURL = url;
|
||||||
|
portainerEndpoint = endpoint;
|
||||||
|
portainerUser = user;
|
||||||
|
portainerPass = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function portainerAuth()
|
||||||
|
{
|
||||||
|
if (portainerBearer)
|
||||||
|
{
|
||||||
|
return portainerBearer;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
let req = new Request(portainerURL + "/api/auth",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {"Accept":"application/json"},
|
||||||
|
body: JSON.stringify({"Username": portainerUser,"Password": portainerPass})
|
||||||
|
});
|
||||||
|
|
||||||
|
let resp = await fetch(req);
|
||||||
|
let json = await resp.json();
|
||||||
|
if (json.jwt)
|
||||||
|
{
|
||||||
|
portainerBearer = json.jwt;
|
||||||
|
}
|
||||||
|
return json.jwt || null;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function portainerIsRunning(container)
|
||||||
|
{
|
||||||
|
let bearer = await portainerAuth();
|
||||||
|
if (bearer == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
let req = new Request(portainerURL + "/api/endpoints/" + portainerEndpoint + "/docker/containers/" + container + "/json",
|
||||||
|
{
|
||||||
|
headers: {"Authorization": "Bearer " + bearer}
|
||||||
|
});
|
||||||
|
|
||||||
|
let resp = await fetch(req);
|
||||||
|
json = await resp.json();
|
||||||
|
return json.State.Running;
|
||||||
|
}
|
||||||
|
catch(e)
|
||||||
|
{
|
||||||
|
console.log(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function portainerStart(container)
|
||||||
|
{
|
||||||
|
let bearer = await portainerAuth();
|
||||||
|
if (bearer == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
let req = new Request(portainerURL + "/api/endpoints/" + portainerEndpoint + "/docker/containers/" + container + "/start",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {"Authorization": "Bearer " + bearer}
|
||||||
|
});
|
||||||
|
|
||||||
|
let resp = await fetch(req);
|
||||||
|
// there is no response ...
|
||||||
|
return resp.status < 300;
|
||||||
|
}
|
||||||
|
catch(e)
|
||||||
|
{
|
||||||
|
console.log(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function portainerStop(container)
|
||||||
|
{
|
||||||
|
let bearer = await portainerAuth();
|
||||||
|
if (bearer == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
let req = new Request(portainerURL + "/api/endpoints/" + portainerEndpoint + "/docker/containers/" + container + "/stop",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {"Authorization": "Bearer " + bearer}
|
||||||
|
});
|
||||||
|
|
||||||
|
let resp = await fetch(req);
|
||||||
|
// there is no response ...
|
||||||
|
return resp.status < 300;
|
||||||
|
}
|
||||||
|
catch(e)
|
||||||
|
{
|
||||||
|
console.log(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user