This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
# https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions
|
||||||
|
name: Build Images and Deploy
|
||||||
|
run-name: ${{ gitea.actor }} is building new PROD images and redeploying the existing stack 🚀
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
# not working right now https://github.com/actions/runner/issues/2324
|
||||||
|
# paths-ignore:
|
||||||
|
# - **.yml
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
env:
|
||||||
|
STACK_NAME: hashex
|
||||||
|
DOT_ENV: ${{ secrets.PROD_ENV }}
|
||||||
|
PORTAINER_TOKEN: ${{ vars.PORTAINER_TOKEN }}
|
||||||
|
PORTAINER_API_URL: https://portainer.dev.nervesocket.com/api
|
||||||
|
ENDPOINT_NAME: "mini" #sometimes "primary"
|
||||||
|
IMAGE_TAG: "reg.dev.nervesocket.com/hashex:latest"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Update-PROD-Stack:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
# if: contains(github.event.pull_request.head.ref, 'init-stack')
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: main
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v1
|
||||||
|
|
||||||
|
- name: Build and push PROD Docker image
|
||||||
|
run: |
|
||||||
|
echo $DOT_ENV | base64 -d > .env
|
||||||
|
docker buildx build --push -f Dockerfile -t $IMAGE_TAG .
|
||||||
|
|
||||||
|
- name: Get the endpoint ID
|
||||||
|
# Usually ID is 1, but you can get it from the API. Only skip this if you are VERY sure.
|
||||||
|
run: |
|
||||||
|
ENDPOINT_ID=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" "$PORTAINER_API_URL/endpoints" | jq -r ".[] | select(.Name==\"$ENDPOINT_NAME\") | .Id")
|
||||||
|
echo "ENDPOINT_ID=$ENDPOINT_ID" >> $GITHUB_ENV
|
||||||
|
echo "Got stack Endpoint ID: $ENDPOINT_ID"
|
||||||
|
|
||||||
|
- name: Fetch stack ID from Portainer
|
||||||
|
run: |
|
||||||
|
STACK_ID=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks" | jq -r ".[] | select(.Name==\"$STACK_NAME\" and .EndpointId==$ENDPOINT_ID) | .Id")
|
||||||
|
|
||||||
|
echo "STACK_ID=$STACK_ID" >> $GITHUB_ENV
|
||||||
|
echo "Got stack ID: $STACK_ID matched with Endpoint ID: $ENDPOINT_ID"
|
||||||
|
|
||||||
|
- name: Fetch Stack
|
||||||
|
run: |
|
||||||
|
# Get the stack details (including env vars)
|
||||||
|
STACK_DETAILS=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks/$STACK_ID")
|
||||||
|
|
||||||
|
# Extract environment variables from the stack
|
||||||
|
echo "$STACK_DETAILS" | jq -r '.Env' > stack_env.json
|
||||||
|
|
||||||
|
echo "Existing stack environment variables:"
|
||||||
|
cat stack_env.json
|
||||||
|
|
||||||
|
- name: Redeploy stack in Portainer
|
||||||
|
run: |
|
||||||
|
# Read stack file content
|
||||||
|
STACK_FILE_CONTENT=$(echo "$(<prod-compose.yml )")
|
||||||
|
|
||||||
|
# Read existing environment variables from the fetched stack
|
||||||
|
ENV_VARS=$(cat stack_env.json)
|
||||||
|
|
||||||
|
# Prepare JSON payload with environment variables
|
||||||
|
JSON_PAYLOAD=$(jq -n --arg stackFileContent "$STACK_FILE_CONTENT" --argjson pullImage true --argjson env "$ENV_VARS" \
|
||||||
|
'{stackFileContent: $stackFileContent, pullImage: $pullImage, env: $env}')
|
||||||
|
|
||||||
|
echo "About to push the following JSON payload:"
|
||||||
|
echo $JSON_PAYLOAD
|
||||||
|
|
||||||
|
# Update stack in Portainer (this redeploys it)
|
||||||
|
DEPLOY_RESPONSE=$(curl -X PUT "$PORTAINER_API_URL/stacks/$STACK_ID?endpointId=$ENDPOINT_ID" \
|
||||||
|
-H "X-API-Key: $PORTAINER_TOKEN" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
--data "$JSON_PAYLOAD")
|
||||||
|
|
||||||
|
echo "Redeployed stack in Portainer. Response:"
|
||||||
|
echo $DEPLOY_RESPONSE
|
||||||
|
|
||||||
|
- name: Status check
|
||||||
|
run: |
|
||||||
|
echo "📋 This job's status is ${{ job.status }}. Make sure you delete the init file to avoid issues."
|
||||||
+109
-6
@@ -85,6 +85,80 @@ async function handleRequest(req, res) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// ── Debug endpoint ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
if (url === '/api/debug/test-device' && method === 'POST') {
|
||||||
|
const { host, port } = body;
|
||||||
|
if (!host) {
|
||||||
|
return jsonErr(res, 'Host is required', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
const devicePort = port ? parseInt(port, 10) : 49153;
|
||||||
|
|
||||||
|
console.log(`[DWM] Debug test for ${host}:${devicePort}`);
|
||||||
|
|
||||||
|
const results = {};
|
||||||
|
|
||||||
|
// Test 1: HTTP connection to setup.xml
|
||||||
|
try {
|
||||||
|
const setupUrl = `http://${host}:${devicePort}/setup.xml`;
|
||||||
|
console.log(`[DWM] Testing setup.xml URL: ${setupUrl}`);
|
||||||
|
const response = await axios.get(setupUrl, {
|
||||||
|
timeout: 5000,
|
||||||
|
httpAgent: NO_KEEPALIVE
|
||||||
|
});
|
||||||
|
results.setupXml = {
|
||||||
|
status: response.status,
|
||||||
|
dataLength: response.data.length,
|
||||||
|
success: response.status === 200
|
||||||
|
};
|
||||||
|
console.log(`[DWM] setup.xml response:`, results.setupXml);
|
||||||
|
} catch (err) {
|
||||||
|
results.setupXml = {
|
||||||
|
success: false,
|
||||||
|
error: err.message,
|
||||||
|
code: err.code
|
||||||
|
};
|
||||||
|
console.log(`[DWM] setup.xml failed:`, err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: Try to get device info
|
||||||
|
try {
|
||||||
|
console.log(`[DWM] Testing getDeviceInfo`);
|
||||||
|
const deviceInfo = await wemo.getDeviceInfo(host, devicePort);
|
||||||
|
results.deviceInfo = {
|
||||||
|
success: true,
|
||||||
|
data: deviceInfo
|
||||||
|
};
|
||||||
|
console.log(`[DWM] getDeviceInfo successful:`, deviceInfo);
|
||||||
|
} catch (err) {
|
||||||
|
results.deviceInfo = {
|
||||||
|
success: false,
|
||||||
|
error: err.message
|
||||||
|
};
|
||||||
|
console.log(`[DWM] getDeviceInfo failed:`, err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 3: Try to get binary state
|
||||||
|
try {
|
||||||
|
console.log(`[DWM] Testing getBinaryState`);
|
||||||
|
const state = await wemo.getBinaryState(host, devicePort);
|
||||||
|
results.binaryState = {
|
||||||
|
success: true,
|
||||||
|
state: state
|
||||||
|
};
|
||||||
|
console.log(`[DWM] getBinaryState successful:`, state);
|
||||||
|
} catch (err) {
|
||||||
|
results.binaryState = {
|
||||||
|
success: false,
|
||||||
|
error: err.message
|
||||||
|
};
|
||||||
|
console.log(`[DWM] getBinaryState failed:`, err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return json(res, results);
|
||||||
|
}
|
||||||
|
|
||||||
// ── Devices ────────────────────────────────────────────────────────────
|
// ── Devices ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
if (url === '/api/devices' && method === 'GET') {
|
if (url === '/api/devices' && method === 'GET') {
|
||||||
@@ -114,18 +188,47 @@ async function handleRequest(req, res) {
|
|||||||
const devicePort = port ? parseInt(port, 10) : 49153;
|
const devicePort = port ? parseInt(port, 10) : 49153;
|
||||||
const manualEntry = { host, port: devicePort };
|
const manualEntry = { host, port: devicePort };
|
||||||
|
|
||||||
|
console.log(`[DWM] Attempting to add device manually: ${host}:${devicePort}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to discover this specific device
|
// First try direct device info fetch
|
||||||
const devices = await wemo.discoverDevices(5000, [manualEntry]);
|
let device = null;
|
||||||
if (devices.length > 0) {
|
try {
|
||||||
|
console.log(`[DWM] Trying direct device info fetch for ${host}:${devicePort}`);
|
||||||
|
device = await wemo.getDeviceInfo(host, devicePort);
|
||||||
|
if (device) {
|
||||||
|
device.host = host;
|
||||||
|
device.port = devicePort;
|
||||||
|
device.manual = true; // Mark as manually added
|
||||||
|
console.log(`[DWM] Direct fetch successful:`, device);
|
||||||
|
}
|
||||||
|
} catch (directErr) {
|
||||||
|
console.log(`[DWM] Direct fetch failed:`, directErr.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If direct fetch failed, try discovery with manual entry
|
||||||
|
if (!device) {
|
||||||
|
console.log(`[DWM] Trying discovery with manual entry`);
|
||||||
|
const devices = await wemo.discoverDevices(5000, [manualEntry]);
|
||||||
|
if (devices.length > 0) {
|
||||||
|
device = devices[0];
|
||||||
|
device.manual = true; // Mark as manually added
|
||||||
|
console.log(`[DWM] Discovery successful:`, device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device) {
|
||||||
// Add to existing devices
|
// Add to existing devices
|
||||||
const allDevices = [...store.getDevices(), ...devices];
|
const allDevices = [...store.getDevices(), ...[device]];
|
||||||
store.saveDevices(allDevices);
|
store.saveDevices(allDevices);
|
||||||
return json(res, devices[0], 201);
|
console.log(`[DWM] Device added successfully: ${device.friendlyName || device.host}`);
|
||||||
|
return json(res, device, 201);
|
||||||
} else {
|
} else {
|
||||||
return jsonErr(res, 'No Wemo device found at this address', 404);
|
console.log(`[DWM] No device found at ${host}:${devicePort}`);
|
||||||
|
return jsonErr(res, `No Wemo device found at ${host}:${devicePort}. Check IP address and port.`, 404);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log(`[DWM] Error adding device:`, err);
|
||||||
return jsonErr(res, `Failed to connect: ${err.message}`, 500);
|
return jsonErr(res, `Failed to connect: ${err.message}`, 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user