120 lines
2.2 KiB
JavaScript
120 lines
2.2 KiB
JavaScript
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;
|
|
}
|
|
}
|