From 126857fab0cce1ecd40cd71df3dc268c2fc3397f Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 19:43:50 +0800 Subject: [PATCH 01/17] Create demo.yml --- .github/workflows/demo.yml | 78 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 .github/workflows/demo.yml diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml new file mode 100644 index 0000000..104f58e --- /dev/null +++ b/.github/workflows/demo.yml @@ -0,0 +1,78 @@ +name: demo-reset + +# Controls when the workflow will run +on: +# schedule: +# - cron: "0 * * * *" + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') +# OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') +# NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s) + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + reset: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + - name: Configure AWS credentials from Test account + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-2 + + - name: Get old instance and snapshot name + run: | + OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') + OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') + + - name: create-instances-from-snapshot + run: | + NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s) + aws lightsail create-instances-from-snapshot \ + --instance-snapshot-name $OLD_INSTANCE_SNAPSHOT_NAME \ + --instance-names $NEW_INSTANCE_NAME \ + --availability-zone us-west-2a \ + --bundle-id large_2_0 + + - name: wait for new instance running + run: | + TIMEOUT=$(($(date +%s)+600)) + while [ $(date +%s) -gt $TIMEOUT ] \ + do \ + NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name $OLD_INSTANCE_NAME | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') \ + if [ $NEW_INSTANCE_STATE == running ] \ + then \ + echo "New instance is running now" \ + break \ + fi \ + done + + - name: Put instance public ports + run: | + aws lightsail put-instance-public-ports \ + --port-infos fromPort=0,toPort=65535,protocol=all \ + --instance-name $NEW_INSTANCE_NAME + + - name: Attach static ip + run: | + aws lightsail attach-static-ip \ + --static-ip-name CasaOS-Demo-IP \ + --instance-name $NEW_INSTANCE_NAME + + - name: Delete old instance + run: | + aws lightsail delete-instance \ + --instance-name $OLD_INSTANCE_NAME + + From 76c8bade7f3c9d83561756a619245440d06c4ceb Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 19:50:30 +0800 Subject: [PATCH 02/17] Update demo.yml --- .github/workflows/demo.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 104f58e..4e09af9 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -29,16 +29,16 @@ jobs: with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: us-east-2 + aws-region: us-east-2a - name: Get old instance and snapshot name run: | - OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') - OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') + export OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') + export OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') - name: create-instances-from-snapshot run: | - NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s) + export NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s) aws lightsail create-instances-from-snapshot \ --instance-snapshot-name $OLD_INSTANCE_SNAPSHOT_NAME \ --instance-names $NEW_INSTANCE_NAME \ From 7194792c3fdbe5f126586b62114d22bf8a0cf9ca Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 19:51:36 +0800 Subject: [PATCH 03/17] Update demo.yml --- .github/workflows/demo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 4e09af9..2f38a0f 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -29,7 +29,7 @@ jobs: with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: us-east-2a + aws-region: us-east-2 - name: Get old instance and snapshot name run: | From ec5bc922e77dfc47dc507b1fa3f0fb64120fbffb Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 20:07:44 +0800 Subject: [PATCH 04/17] Update demo.yml --- .github/workflows/demo.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 2f38a0f..b300cdd 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -33,12 +33,12 @@ jobs: - name: Get old instance and snapshot name run: | - export OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') - export OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g') + echo "OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV + echo "OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV - name: create-instances-from-snapshot run: | - export NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s) + echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV aws lightsail create-instances-from-snapshot \ --instance-snapshot-name $OLD_INSTANCE_SNAPSHOT_NAME \ --instance-names $NEW_INSTANCE_NAME \ From 0fac461783e5b5ac1b908b352598d92a460d3476 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 20:11:32 +0800 Subject: [PATCH 05/17] Update demo.yml --- .github/workflows/demo.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index b300cdd..91a2e20 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -40,8 +40,8 @@ jobs: run: | echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV aws lightsail create-instances-from-snapshot \ - --instance-snapshot-name $OLD_INSTANCE_SNAPSHOT_NAME \ - --instance-names $NEW_INSTANCE_NAME \ + --instance-snapshot-name ${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} \ + --instance-names ${{ env.NEW_INSTANCE_NAME }} \ --availability-zone us-west-2a \ --bundle-id large_2_0 @@ -50,7 +50,7 @@ jobs: TIMEOUT=$(($(date +%s)+600)) while [ $(date +%s) -gt $TIMEOUT ] \ do \ - NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name $OLD_INSTANCE_NAME | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') \ + NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') \ if [ $NEW_INSTANCE_STATE == running ] \ then \ echo "New instance is running now" \ @@ -62,17 +62,17 @@ jobs: run: | aws lightsail put-instance-public-ports \ --port-infos fromPort=0,toPort=65535,protocol=all \ - --instance-name $NEW_INSTANCE_NAME + --instance-name ${{ env.NEW_INSTANCE_NAME }} - name: Attach static ip run: | aws lightsail attach-static-ip \ --static-ip-name CasaOS-Demo-IP \ - --instance-name $NEW_INSTANCE_NAME + --instance-name ${{ env.NEW_INSTANCE_NAME }} - name: Delete old instance run: | aws lightsail delete-instance \ - --instance-name $OLD_INSTANCE_NAME + --instance-name ${{ env.OLD_INSTANCE_NAME }} From c67191d8c20b37f9fbf06b0a4dd3e97de5f0e371 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 20:45:36 +0800 Subject: [PATCH 06/17] Update demo.yml --- .github/workflows/demo.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 91a2e20..8c91ce9 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -39,6 +39,7 @@ jobs: - name: create-instances-from-snapshot run: | echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV + echo ${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} aws lightsail create-instances-from-snapshot \ --instance-snapshot-name ${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} \ --instance-names ${{ env.NEW_INSTANCE_NAME }} \ From 91087b534143e2071f2f71e91ed89c872d95f0b0 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:01:40 +0800 Subject: [PATCH 07/17] Update demo.yml --- .github/workflows/demo.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 8c91ce9..644d4c5 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -34,19 +34,20 @@ jobs: - name: Get old instance and snapshot name run: | echo "OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV + echo "OLD_INSTANCE_SNAPSHOT_NAME=${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} echo "OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV + echo "OLD_INSTANCE_NAME=${{ env.OLD_INSTANCE_NAME }} - - name: create-instances-from-snapshot + - name: Create instances from snapshot run: | echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV - echo ${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} aws lightsail create-instances-from-snapshot \ --instance-snapshot-name ${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} \ --instance-names ${{ env.NEW_INSTANCE_NAME }} \ --availability-zone us-west-2a \ --bundle-id large_2_0 - - name: wait for new instance running + - name: Wait for new instance running run: | TIMEOUT=$(($(date +%s)+600)) while [ $(date +%s) -gt $TIMEOUT ] \ From f66b67b0c93fcfc7772699be9a76bfc96396b0b5 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:05:52 +0800 Subject: [PATCH 08/17] Update demo.yml --- .github/workflows/demo.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 644d4c5..bb20db8 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -31,16 +31,14 @@ jobs: aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: us-east-2 - - name: Get old instance and snapshot name + - name: Get old instance and snapshot name, create new instance name run: | echo "OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV - echo "OLD_INSTANCE_SNAPSHOT_NAME=${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} echo "OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV - echo "OLD_INSTANCE_NAME=${{ env.OLD_INSTANCE_NAME }} - + echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV + - name: Create instances from snapshot run: | - echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV aws lightsail create-instances-from-snapshot \ --instance-snapshot-name ${{ env.OLD_INSTANCE_SNAPSHOT_NAME }} \ --instance-names ${{ env.NEW_INSTANCE_NAME }} \ From f02337b83b0d8099fa310e5892566feace1a104f Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:09:18 +0800 Subject: [PATCH 09/17] Update demo.yml --- .github/workflows/demo.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index bb20db8..ee7b160 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -36,6 +36,12 @@ jobs: echo "OLD_INSTANCE_SNAPSHOT_NAME=$(aws lightsail get-instance-snapshots | grep '"name": "CasaOS-Demo-Snapshot-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV echo "OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV + + - name: Check GitHub envs + run: | + echo "OLD_INSTANCE_SNAPSHOT_NAME=${{ env.OLD_INSTANCE_SNAPSHOT_NAME }}" + echo "OLD_INSTANCE_NAME=${{ env.OLD_INSTANCE_NAME }}" + echo "NEW_INSTANCE_NAME=${{ env.NEW_INSTANCE_NAME }}" - name: Create instances from snapshot run: | From 7e2a5d553c5194a01c229e97d02c2c968dcf7bc3 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:14:34 +0800 Subject: [PATCH 10/17] Update demo.yml --- .github/workflows/demo.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index ee7b160..3196258 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -29,7 +29,7 @@ jobs: with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: us-east-2 + aws-region: us-west-2 - name: Get old instance and snapshot name, create new instance name run: | @@ -37,12 +37,6 @@ jobs: echo "OLD_INSTANCE_NAME=$(aws lightsail get-instances | grep '"name": "CasaOS-Demo-[0-9]' | sed 's/ //g' | sed 's/"//g' | sed 's/,//g' | sed 's/name://g')" >> $GITHUB_ENV echo "NEW_INSTANCE_NAME=CasaOS-Demo-$(date +%s)" >> $GITHUB_ENV - - name: Check GitHub envs - run: | - echo "OLD_INSTANCE_SNAPSHOT_NAME=${{ env.OLD_INSTANCE_SNAPSHOT_NAME }}" - echo "OLD_INSTANCE_NAME=${{ env.OLD_INSTANCE_NAME }}" - echo "NEW_INSTANCE_NAME=${{ env.NEW_INSTANCE_NAME }}" - - name: Create instances from snapshot run: | aws lightsail create-instances-from-snapshot \ From 99db197a3911910206e0aff359c565b0831071f8 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:31:17 +0800 Subject: [PATCH 11/17] Update demo.yml --- .github/workflows/demo.yml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 3196258..a496eff 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -48,14 +48,13 @@ jobs: - name: Wait for new instance running run: | TIMEOUT=$(($(date +%s)+600)) - while [ $(date +%s) -gt $TIMEOUT ] \ - do \ - NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') \ - if [ $NEW_INSTANCE_STATE == running ] \ - then \ - echo "New instance is running now" \ - break \ - fi \ + while [ $TIMEOUT -gt $(date +%s) ] + do + NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') + if [ $NEW_INSTANCE_STATE == running ] + echo "New instance is running now" + break + fi done - name: Put instance public ports From 65cc1f47522b528fae694fd0675461a4e94ce0a0 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:36:44 +0800 Subject: [PATCH 12/17] Update demo.yml --- .github/workflows/demo.yml | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index a496eff..d7eb300 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -45,17 +45,20 @@ jobs: --availability-zone us-west-2a \ --bundle-id large_2_0 +# - name: Wait for new instance running +# run: | +# TIMEOUT=$(($(date +%s)+600)) +# while [ $TIMEOUT -gt $(date +%s) ] +# do +# NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') +# if [ $NEW_INSTANCE_STATE == running ] +# echo "New instance is running now" +# break +# fi +# done + - name: Wait for new instance running - run: | - TIMEOUT=$(($(date +%s)+600)) - while [ $TIMEOUT -gt $(date +%s) ] - do - NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') - if [ $NEW_INSTANCE_STATE == running ] - echo "New instance is running now" - break - fi - done + run: sleep 10s - name: Put instance public ports run: | From eb03a3e6c76b7d849268bc5a34597eb5eaf9778e Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:46:34 +0800 Subject: [PATCH 13/17] Update demo.yml --- .github/workflows/demo.yml | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index d7eb300..59f0d70 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -45,20 +45,21 @@ jobs: --availability-zone us-west-2a \ --bundle-id large_2_0 -# - name: Wait for new instance running -# run: | -# TIMEOUT=$(($(date +%s)+600)) -# while [ $TIMEOUT -gt $(date +%s) ] -# do -# NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') -# if [ $NEW_INSTANCE_STATE == running ] -# echo "New instance is running now" -# break -# fi -# done - - name: Wait for new instance running - run: sleep 10s + run: | + TIMEOUT=$(($(date +%s)+600)) + while [ $TIMEOUT -gt $(date +%s) ] + do + NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') + if [ $NEW_INSTANCE_STATE == running ] + then + echo "New instance is running now" + break + fi + done + +# - name: Wait for new instance running +# run: sleep 10s - name: Put instance public ports run: | From d22cc7d3f6b8638790e7177a6f1067b83776ca40 Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:49:23 +0800 Subject: [PATCH 14/17] Update demo.yml --- .github/workflows/demo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 59f0d70..3a90a0d 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -50,7 +50,7 @@ jobs: TIMEOUT=$(($(date +%s)+600)) while [ $TIMEOUT -gt $(date +%s) ] do - NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.OLD_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') + NEW_INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name ${{ env.NEW_INSTANCE_NAME }} | grep '"name":' | sed 's/ //g' | sed 's/"//g' | sed 's/name://g') if [ $NEW_INSTANCE_STATE == running ] then echo "New instance is running now" From 3f5595e7940cabb71b8deb2f21f08f5de8aeceac Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:54:02 +0800 Subject: [PATCH 15/17] Update demo.yml --- .github/workflows/demo.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 3a90a0d..a0c9893 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -1,9 +1,9 @@ -name: demo-reset +name: Demo Reset # Controls when the workflow will run on: -# schedule: -# - cron: "0 * * * *" + schedule: + - cron: "0 * * * *" # Allows you to run this workflow manually from the Actions tab workflow_dispatch: @@ -57,9 +57,7 @@ jobs: break fi done - -# - name: Wait for new instance running -# run: sleep 10s + sleep 10s - name: Put instance public ports run: | From 46e9458e82a2419a733ca4b260fad1b7427b6e8e Mon Sep 17 00:00:00 2001 From: John Guan Date: Wed, 1 Dec 2021 21:58:57 +0800 Subject: [PATCH 16/17] Update demo.yml --- .github/workflows/demo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index a0c9893..989d336 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -54,10 +54,10 @@ jobs: if [ $NEW_INSTANCE_STATE == running ] then echo "New instance is running now" + sleep 10s break fi done - sleep 10s - name: Put instance public ports run: | From 0bb3c92335d2b6d636c1ad89cedd0f77b89a9ebf Mon Sep 17 00:00:00 2001 From: xuc2 Date: Fri, 3 Dec 2021 16:48:07 +0800 Subject: [PATCH 17/17] format code and fix wrong spell --- model/system.go | 2 -- pkg/github/github.go | 1 - pkg/upnp/device.go | 4 +-- pkg/upnp/gateway.go | 8 ++--- pkg/upnp/gateway_test.go | 2 +- pkg/upnp/mapping.go | 53 +++++++++++++++------------------ pkg/upnp/upnp.go | 7 ++--- pkg/upnp/upnp_test.go | 2 +- pkg/utils/loger/log.go | 2 +- pkg/utils/random/random_test.go | 4 +-- route/v1/ddns.go | 2 +- route/v1/docker.go | 6 ++-- route/v1/system.go | 2 +- service/app.go | 10 ++++--- service/ddns.go | 19 ++++++------ service/ddns/common.go | 1 - service/docker.go | 10 +++---- service/docker_base/model.go | 2 -- service/model/o_ddns.go | 12 ++++---- service/service.go | 2 +- service/task.go | 3 +- service/zerotier.go | 36 +++++++++++----------- 22 files changed, 90 insertions(+), 100 deletions(-) diff --git a/model/system.go b/model/system.go index c52acae..8b53790 100644 --- a/model/system.go +++ b/model/system.go @@ -1,3 +1 @@ package model - - diff --git a/pkg/github/github.go b/pkg/github/github.go index ed007c1..6ef88ea 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -6,7 +6,6 @@ import ( "golang.org/x/oauth2" ) - func GetGithubClient() *github.Client { ctx := context.Background() ts := oauth2.StaticTokenSource( diff --git a/pkg/upnp/device.go b/pkg/upnp/device.go index d0b9e9d..1791e9c 100644 --- a/pkg/upnp/device.go +++ b/pkg/upnp/device.go @@ -7,7 +7,7 @@ import ( "strings" ) -func GetCtrlUrl(host,device string) string { +func GetCtrlUrl(host, device string) string { request := ctrlUrlRequest(host, device) response, _ := http.DefaultClient.Do(request) resultBody, _ := ioutil.ReadAll(response.Body) @@ -86,4 +86,4 @@ func resolve(resultStr string) string { } } return controlURL -} \ No newline at end of file +} diff --git a/pkg/upnp/gateway.go b/pkg/upnp/gateway.go index 257b549..b3307a5 100644 --- a/pkg/upnp/gateway.go +++ b/pkg/upnp/gateway.go @@ -22,21 +22,21 @@ func send() (string, error) { "ST: urn:schemas-upnp-org:service:WANIPConnection:1\r\n" + "MAN: \"ssdp:discover\"\r\n" + "MX: 3\r\n\r\n" var conn *net.UDPConn - remotAddr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900") + remoteAddr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900") if err != nil { return "", errors.New("组播地址格式不正确") } - locaAddr, err := net.ResolveUDPAddr("udp", ip_helper2.GetLoclIp()+":") + localAddr, err := net.ResolveUDPAddr("udp", ip_helper2.GetLoclIp()+":") if err != nil { return "", errors.New("本地ip地址格式不正确") } - conn, err = net.ListenUDP("udp", locaAddr) + conn, err = net.ListenUDP("udp", localAddr) defer conn.Close() if err != nil { return "", errors.New("监听udp出错") } - _, err = conn.WriteToUDP([]byte(str), remotAddr) + _, err = conn.WriteToUDP([]byte(str), remoteAddr) if err != nil { return "", errors.New("发送msg到组播地址出错") } diff --git a/pkg/upnp/gateway_test.go b/pkg/upnp/gateway_test.go index 2fc42cb..9fde2b3 100644 --- a/pkg/upnp/gateway_test.go +++ b/pkg/upnp/gateway_test.go @@ -5,4 +5,4 @@ import "testing" func TestGateway(t *testing.T) { Gateway() -} \ No newline at end of file +} diff --git a/pkg/upnp/mapping.go b/pkg/upnp/mapping.go index 5bb2509..70c007d 100644 --- a/pkg/upnp/mapping.go +++ b/pkg/upnp/mapping.go @@ -2,40 +2,37 @@ package upnp import ( "bytes" - "github.com/pkg/errors" "net/http" "strconv" "strings" + + loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger" + "github.com/pkg/errors" ) // ////添加一个端口映射 -func (n *Upnp)AddPortMapping(localPort, remotePort int, protocol string) (err error) { - defer func(err error) { +func (n *Upnp) AddPortMapping(localPort, remotePort int, protocol string) (err error) { + defer func() { if errTemp := recover(); errTemp != nil { - //log.Println("upnp模块报错了", errTemp) - err = errTemp.(error) + loger2.NewOLoger().Error("upnp模块报错了", errTemp) } - }(err) - if issuccess := addSend(localPort, remotePort, protocol,n.GatewayHost, n.CtrlUrl,n.LocalHost); issuccess { + }() + + if isSuccess := addSend(localPort, remotePort, protocol, n.GatewayHost, n.CtrlUrl, n.LocalHost); isSuccess { return nil } else { return errors.New("添加一个端口映射失败") } - return } -func addSend(localPort, remotePort int, protocol, host, ctrUrl,localHost string) bool { - request := addRequest(localPort, remotePort, protocol, host, ctrUrl,localHost) +func addSend(localPort, remotePort int, protocol, host, ctrUrl, localHost string) bool { + request := addRequest(localPort, remotePort, protocol, host, ctrUrl, localHost) response, _ := http.DefaultClient.Do(request) defer response.Body.Close() //resultBody, _ := ioutil.ReadAll(response.Body) //fmt.Println(string(resultBody)) - if response.StatusCode == 200 { - return true - } - - return false + return response.StatusCode == 200 } type Node struct { @@ -45,7 +42,7 @@ type Node struct { Child []Node } -func addRequest(localPort, remotePort int, protocol string, gatewayHost, ctlUrl,localHost string) *http.Request { +func addRequest(localPort, remotePort int, protocol string, gatewayHost, ctlUrl, localHost string) *http.Request { //请求头 header := http.Header{} header.Set("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2") @@ -109,27 +106,25 @@ func (n *Node) BuildXML() string { return buf.String() } -func (n *Upnp)DelPortMapping(remotePort int, protocol string) bool { - issuccess := delSendSend(remotePort, protocol,n.GatewayHost,n.CtrlUrl) - if issuccess { +func (n *Upnp) DelPortMapping(remotePort int, protocol string) bool { + isSuccess := delSendSend(remotePort, protocol, n.GatewayHost, n.CtrlUrl) + if isSuccess { //this.MappingPort.delMapping(remotePort, protocol) //fmt.Println("删除了一个端口映射: remote:", remotePort) } - return issuccess + return isSuccess } -func delSendSend(remotePort int, protocol,host,ctlUrl string) bool { - delrequest := delbuildRequest(remotePort, protocol,host,ctlUrl) +func delSendSend(remotePort int, protocol, host, ctlUrl string) bool { + delrequest := delbuildRequest(remotePort, protocol, host, ctlUrl) response, _ := http.DefaultClient.Do(delrequest) //resultBody, _ := ioutil.ReadAll(response.Body) defer response.Body.Close() - if response.StatusCode == 200 { - // log.Println(string(resultBody)) - return true - } - return false + + return response.StatusCode == 200 } -func delbuildRequest(remotePort int, protocol,host,ctlUrl string) *http.Request { + +func delbuildRequest(remotePort int, protocol, host, ctlUrl string) *http.Request { //请求头 header := http.Header{} header.Set("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2") @@ -160,4 +155,4 @@ func delbuildRequest(remotePort int, protocol,host,ctlUrl string) *http.Request request.Header = header request.Header.Set("Content-Length", strconv.Itoa(len([]byte(bodyStr)))) return request -} \ No newline at end of file +} diff --git a/pkg/upnp/upnp.go b/pkg/upnp/upnp.go index 3b5e013..8f78c9c 100644 --- a/pkg/upnp/upnp.go +++ b/pkg/upnp/upnp.go @@ -7,10 +7,10 @@ import ( type Upnp struct { LocalHost string `json:"local_host"` - GatewayName string `json:"gateway_name"` //网关名称 - GatewayHost string `json:"gateway_host"` //网关ip和端口 + GatewayName string `json:"gateway_name"` //网关名称 + GatewayHost string `json:"gateway_host"` //网关ip和端口 DeviceDescUrl string `json:"device_desc_url"` //设备描述url - CtrlUrl string `json:"ctrl_url"` //控制请求url + CtrlUrl string `json:"ctrl_url"` //控制请求url } func Testaaa() { @@ -23,4 +23,3 @@ func Testaaa() { fmt.Println("gateway ip address: ", upnpMan.Gateway.Host) } } - diff --git a/pkg/upnp/upnp_test.go b/pkg/upnp/upnp_test.go index e7a1e5f..aa68666 100644 --- a/pkg/upnp/upnp_test.go +++ b/pkg/upnp/upnp_test.go @@ -6,4 +6,4 @@ import ( func TestTestaaa(t *testing.T) { (Testaaa()) -} \ No newline at end of file +} diff --git a/pkg/utils/loger/log.go b/pkg/utils/loger/log.go index 8e7028e..11e779f 100644 --- a/pkg/utils/loger/log.go +++ b/pkg/utils/loger/log.go @@ -6,7 +6,7 @@ import ( "os" "path/filepath" "runtime" - + "github.com/IceWhaleTech/CasaOS/pkg/config" file2 "github.com/IceWhaleTech/CasaOS/pkg/utils/file" ) diff --git a/pkg/utils/random/random_test.go b/pkg/utils/random/random_test.go index a9b5d31..47d801d 100644 --- a/pkg/utils/random/random_test.go +++ b/pkg/utils/random/random_test.go @@ -6,5 +6,5 @@ import ( ) func TestRandomString(t *testing.T) { - fmt.Println(RandomString(6,true)) -} \ No newline at end of file + fmt.Println(RandomString(6, true)) +} diff --git a/route/v1/ddns.go b/route/v1/ddns.go index 133f3f0..f7ad219 100644 --- a/route/v1/ddns.go +++ b/route/v1/ddns.go @@ -64,7 +64,7 @@ func DDNSAddConfig(c *gin.Context) { }) return } - var m model2.DDNSUpdataDBModel + var m model2.DDNSUpdateDBModel c.Bind(&m) if err := service.MyService.DDNS().SaveConfig(m); err != nil { c.JSON(http.StatusOK, diff --git a/route/v1/docker.go b/route/v1/docker.go index 59001c0..84cd31f 100644 --- a/route/v1/docker.go +++ b/route/v1/docker.go @@ -268,11 +268,11 @@ func InstallApp(c *gin.Context) { rely.ContainerId = mysqlContainerId rely.CustomId = mid rely.ContainerCustomId = id - var msqlConfig model2.MysqlConfigs + var mysqlConfig model2.MysqlConfigs //结构体转换 - copier.Copy(&msqlConfig, &mc) - rely.Config = msqlConfig + copier.Copy(&mysqlConfig, &mc) + rely.Config = mysqlConfig service.MyService.Rely().Create(rely) relyMap["mysql"] = mid diff --git a/route/v1/system.go b/route/v1/system.go index 69505eb..c81e3fe 100644 --- a/route/v1/system.go +++ b/route/v1/system.go @@ -102,7 +102,7 @@ func GetSystemConfigDebug(c *gin.Context) { array := service.MyService.System().GetSystemConfigDebug() disk := service.MyService.ZiMa().GetDiskInfo() - array = append(array, fmt.Sprintf("disk,totle:%v,used:%v,UsedPercent:%v", disk.Total>>20, disk.Used>>20, disk.UsedPercent)) + array = append(array, fmt.Sprintf("disk,total:%v,used:%v,UsedPercent:%v", disk.Total>>20, disk.Used>>20, disk.UsedPercent)) c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS), Data: array}) } diff --git a/service/app.go b/service/app.go index c7f0659..1b1a974 100644 --- a/service/app.go +++ b/service/app.go @@ -68,8 +68,7 @@ func (a *appStruct) GetMyList(index, size int, position bool) *[]model2.MyAppLis for _, container := range containers { if lMap[container.ID] != nil && container.Labels["origin"] != "system" { - var m model2.AppListDBModel - m = lMap[container.ID].(model2.AppListDBModel) + m := lMap[container.ID].(model2.AppListDBModel) if len(m.Label) == 0 { m.Label = m.Title } @@ -129,8 +128,7 @@ func (a *appStruct) GetSystemAppList() *[]model2.MyAppList { for _, container := range containers { if lMap[container.ID] != nil { - var m model2.AppListDBModel - m = lMap[container.ID].(model2.AppListDBModel) + m := lMap[container.ID].(model2.AppListDBModel) if len(m.Label) == 0 { m.Label = m.Title } @@ -194,6 +192,10 @@ func (a *appStruct) GetSimpleContainerInfo(name string) (types.Container, error) filters := filters.NewArgs() filters.Add("name", name) containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: filters}) + if err != nil { + return types.Container{}, err + } + if len(containers) > 0 { return containers[0], nil } diff --git a/service/ddns.go b/service/ddns.go index fddc8e4..c55bcb4 100644 --- a/service/ddns.go +++ b/service/ddns.go @@ -1,12 +1,13 @@ package service import ( + "os/exec" + ip_helper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/ip_helper" loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger" "github.com/IceWhaleTech/CasaOS/service/ddns" "github.com/IceWhaleTech/CasaOS/service/model" "gorm.io/gorm" - "os/exec" ) type ddnsStruct struct { @@ -20,17 +21,15 @@ type DDNSService interface { GetConfigList() *[]model.DDNSList DeleteConfig(id uint) bool GetType(name string) (uint, string) - SaveConfig(model model.DDNSUpdataDBModel) error + SaveConfig(model model.DDNSUpdateDBModel) error } //判断当前添加的是否存在 func (d *ddnsStruct) IsExis(t int, domain string, host string) bool { var count int64 d.db.Table(model.DDNSLISTTABLENAME).Where("type=? AND domain=? AND host=?", t, domain, host).Count(&count) - if count > 0 { - return true - } - return false + + return count > 0 } //前台获取已配置的ddns列表 @@ -41,7 +40,7 @@ func (d *ddnsStruct) GetConfigList() *[]model.DDNSList { } func (d *ddnsStruct) DeleteConfig(id uint) bool { - d.db.Delete(&model.DDNSUpdataDBModel{Id: id}) + d.db.Delete(&model.DDNSUpdateDBModel{Id: id}) return true } @@ -66,12 +65,12 @@ func (d *ddnsStruct) GetType(name string) (uint, string) { } //保存配置到数据库 -func (d *ddnsStruct) GetDockerRootDir(model model.DDNSUpdataDBModel) error { +func (d *ddnsStruct) GetDockerRootDir(model model.DDNSUpdateDBModel) error { return d.db.Create(&model).Error } //保存配置到数据库 -func (d *ddnsStruct) SaveConfig(model model.DDNSUpdataDBModel) error { +func (d *ddnsStruct) SaveConfig(model model.DDNSUpdateDBModel) error { return d.db.Create(&model).Error } @@ -87,7 +86,7 @@ func chackPing(b chan bool, url string) { } //更新列表 -func UpdataDDNSList(db *gorm.DB) { +func UpdateDDNSList(db *gorm.DB) { var s []model.DDNSCoreList db.Table(model.DDNSLISTTABLENAME).Select("o_ddns_type.name as name,o_ddns_type.api_host as api_host,o_ddns.id,`host`,domain,user_name,`password`,`key`,secret,type").Joins("left join o_ddns_type on o_ddns.type=o_ddns_type.id").Scan(&s) for _, item := range s { diff --git a/service/ddns/common.go b/service/ddns/common.go index 9d26ee0..2ace08c 100644 --- a/service/ddns/common.go +++ b/service/ddns/common.go @@ -31,4 +31,3 @@ func SetOauth(request *http.Request, value string) { func SetXFilter(request *http.Request, value string) { request.Header.Set("X-Filter", value) } - diff --git a/service/docker.go b/service/docker.go index a9b8302..1884732 100644 --- a/service/docker.go +++ b/service/docker.go @@ -107,7 +107,7 @@ func (ds *dockerService) GetNetWorkNameByNetWorkID(id string) (string, error) { defer cli.Close() filter := filters.NewArgs() filter.Add("id", id) - d, err := cli.NetworkList(context.Background(), types.NetworkListOptions{filter}) + d, err := cli.NetworkList(context.Background(), types.NetworkListOptions{Filters: filter}) if err == nil && len(d) > 0 { return d[0].Name, nil } @@ -158,7 +158,7 @@ func DockerEx() { importResponse.Close() println(string(response)) if string(response) != "response" { - fmt.Println("expected response to contain 'response', got %s", string(response)) + fmt.Printf("expected response to contain 'response', got %s", string(response)) } } @@ -187,7 +187,6 @@ func (ds *dockerService) DockerImageInfo(image string) { if err != nil { fmt.Print(err) } - } func MsqlExec(container string) error { @@ -266,6 +265,8 @@ func DockerLogs() { if err != nil { log.Fatal(err) } + defer i.Close() + hdr := make([]byte, 8) for { _, err := i.Read(hdr) @@ -284,7 +285,6 @@ func DockerLogs() { _, err = i.Read(dat) fmt.Fprint(w, string(dat)) } - defer i.Close() } //正式内容 @@ -787,7 +787,7 @@ func (ds *dockerService) DockerNetworkModelList() []types.NetworkResource { networks, _ := cli.NetworkList(context.Background(), types.NetworkListOptions{}) return networks } -func NewDcokerService(log loger2.OLog) DockerService { +func NewDockerService(log loger2.OLog) DockerService { return &dockerService{rootDir: command2.ExecResultStr(`source ./shell/helper.sh ;GetDockerRootDir`), log: log} } diff --git a/service/docker_base/model.go b/service/docker_base/model.go index 33dceed..3413577 100644 --- a/service/docker_base/model.go +++ b/service/docker_base/model.go @@ -7,5 +7,3 @@ type MysqlConfig struct { DataBasePassword string `json:"data_base_password"` DataBaseDB string `json:"data_base_db"` } - - diff --git a/service/model/o_ddns.go b/service/model/o_ddns.go index 28b9486..d669a90 100644 --- a/service/model/o_ddns.go +++ b/service/model/o_ddns.go @@ -2,11 +2,11 @@ package model import "time" -func (p *DDNSUpdataDBModel) TableName() string { +func (p *DDNSUpdateDBModel) TableName() string { return "o_ddns" } -type DDNSUpdataDBModel struct { +type DDNSUpdateDBModel struct { Id uint `gorm:"column:id;primary_key" json:"id"` Ipv4 string `gorm:"-"` Ipv6 string `gorm:"-"` @@ -17,8 +17,8 @@ type DDNSUpdataDBModel struct { Secret string `json:"secret" form:"secret"` UserName string `json:"user_name" form:"user_name"` Password string `json:"password" form:"password"` - CreatedAt time.Time `gorm:"<-:create" json:"created_at"` - UpdatedAt time.Time `gorm:"<-:create;<-:update" json:"updated_at"` + CreatedAt time.Time `gorm:"<-:create" json:"created_at"` + UpdatedAt time.Time `gorm:"<-:create;<-:update" json:"updated_at"` } const DDNSLISTTABLENAME = "o_ddns" @@ -39,9 +39,9 @@ type DDNSList struct { //定时任务使用 type DDNSCoreList struct { - Id uint `gorm:"column:id;primary_key" json:"id"` + Id uint `gorm:"column:id;primary_key" json:"id"` Domain string `json:"domain" form:"domain"` - Name string `json:"domain" form:"name"` + Name string `json:"name" form:"name"` Type uint `json:"type"` Key string `json:"key"` Message string `json:"message"` diff --git a/service/service.go b/service/service.go index d187bea..13f68ce 100644 --- a/service/service.go +++ b/service/service.go @@ -35,7 +35,7 @@ func NewService(db *gorm.DB, log loger2.OLog) Repository { app: NewAppService(db, log), ddns: NewDDNSService(db, log), user: NewUserService(), - docker: NewDcokerService(log), + docker: NewDockerService(log), //redis: NewRedisService(rp), zerotier: NewZeroTierService(), zima: NewZiMaService(), diff --git a/service/task.go b/service/task.go index 713fafd..4da9079 100644 --- a/service/task.go +++ b/service/task.go @@ -2,6 +2,8 @@ package service import ( json2 "encoding/json" + "strconv" + "github.com/IceWhaleTech/CasaOS/pkg/config" httper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/httper" loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger" @@ -9,7 +11,6 @@ import ( "github.com/IceWhaleTech/CasaOS/types" "github.com/tidwall/gjson" "gorm.io/gorm" - "strconv" ) type TaskService interface { diff --git a/service/zerotier.go b/service/zerotier.go index 26e994e..e62c33a 100644 --- a/service/zerotier.go +++ b/service/zerotier.go @@ -34,20 +34,20 @@ type ZeroTierService interface { DeleteNetwork(token, id string) interface{} GetJoinNetworks() string } -type zerotierstruct struct { +type zerotierStruct struct { } var client http.Client -func (c *zerotierstruct) ZeroTierJoinNetwork(networkId string) { +func (c *zerotierStruct) ZeroTierJoinNetwork(networkId string) { command2.OnlyExec(`zerotier-cli join ` + networkId) } -func (c *zerotierstruct) ZeroTierLeaveNetwork(networkId string) { +func (c *zerotierStruct) ZeroTierLeaveNetwork(networkId string) { command2.OnlyExec(`zerotier-cli leave ` + networkId) } //登录并获取token -func (c *zerotierstruct) GetToken(username, pwd string) string { +func (c *zerotierStruct) GetToken(username, pwd string) string { if len(config.ZeroTierInfo.Token) > 0 { return config.ZeroTierInfo.Token } else { @@ -55,7 +55,7 @@ func (c *zerotierstruct) GetToken(username, pwd string) string { } } -func (c *zerotierstruct) ZeroTierRegister(email, lastName, firstName, password string) string { +func (c *zerotierStruct) ZeroTierRegister(email, lastName, firstName, password string) string { url := "https://accounts.zerotier.com/auth/realms/zerotier/protocol/openid-connect/registrations?client_id=zt-central&redirect_uri=https%3A%2F%2Fmy.zerotier.com%2Fapi%2F_auth%2Foidc%2Fcallback&response_type=code&scope=openid+profile+email+offline_access&state=state" @@ -210,7 +210,7 @@ func ZeroTierGet(url string, cookies []*http.Cookie, t uint8) (action string, c } //模拟提交表单 -func ZeroTierPost(str bytes.Buffer, action string, cookes []*http.Cookie, isLogin bool) (url, errInfo string, err error) { +func ZeroTierPost(str bytes.Buffer, action string, cookies []*http.Cookie, isLogin bool) (url, errInfo string, err error) { req, err := http.NewRequest(http.MethodPost, action, strings.NewReader(str.String())) if err != nil { return "", "", errors.New("newrequest error") @@ -219,7 +219,7 @@ func ZeroTierPost(str bytes.Buffer, action string, cookes []*http.Cookie, isLogi req.Header.Set(k, v) } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - for _, cookie := range cookes { + for _, cookie := range cookies { req.AddCookie(cookie) } res, err := client.Do(req) @@ -273,62 +273,62 @@ func ZeroTierPost(str bytes.Buffer, action string, cookes []*http.Cookie, isLogi } //获取zerotile网络列表和本地用户已加入的网络 -func (c *zerotierstruct) ZeroTierNetworkList(token string) (interface{}, []string) { +func (c *zerotierStruct) ZeroTierNetworkList(token string) (interface{}, []string) { url := "https://my.zerotier.com/api/network" return zerotier.GetData(url, token), command2.ExecResultStrArray(`zerotier-cli listnetworks | awk 'NR>1 {print $3} {line=$0}'`) } // get network info -func (c *zerotierstruct) ZeroTierGetInfo(token, id string) (interface{}, []string) { +func (c *zerotierStruct) ZeroTierGetInfo(token, id string) (interface{}, []string) { url := "https://my.zerotier.com/api/network/" + id info := zerotier.GetData(url, token) return info, command2.ExecResultStrArray(`zerotier-cli listnetworks | awk 'NR>1 {print $3} {line=$0}'`) } //get status -func (c *zerotierstruct) ZeroTierGetStatus(token string) interface{} { +func (c *zerotierStruct) ZeroTierGetStatus(token string) interface{} { url := "https://my.zerotier.com/api/v1/status" info := zerotier.GetData(url, token) return info } -func (c *zerotierstruct) EditNetwork(token string, data string, id string) interface{} { +func (c *zerotierStruct) EditNetwork(token string, data string, id string) interface{} { url := "https://my.zerotier.com/api/v1/network/" + id info := zerotier.PostData(url, token, data) return info } -func (c *zerotierstruct) EditNetworkMember(token string, data string, id, mId string) interface{} { +func (c *zerotierStruct) EditNetworkMember(token string, data string, id, mId string) interface{} { url := "https://my.zerotier.com/api/v1/network/" + id + "/member/" + mId info := zerotier.PostData(url, token, data) return info } -func (c *zerotierstruct) MemberList(token string, id string) interface{} { +func (c *zerotierStruct) MemberList(token string, id string) interface{} { url := "https://my.zerotier.com/api/v1/network/" + id + "/member" info := zerotier.GetData(url, token) return info } -func (c *zerotierstruct) DeleteMember(token string, id, mId string) interface{} { +func (c *zerotierStruct) DeleteMember(token string, id, mId string) interface{} { url := "https://my.zerotier.com/api/v1/network/" + id + "/member/" + mId info := zerotier.DeleteMember(url, token) return info } -func (c *zerotierstruct) DeleteNetwork(token, id string) interface{} { +func (c *zerotierStruct) DeleteNetwork(token, id string) interface{} { url := "https://my.zerotier.com/api/v1/network/" + id info := zerotier.DeleteMember(url, token) return info } -func (c *zerotierstruct) CreateNetwork(token string) interface{} { +func (c *zerotierStruct) CreateNetwork(token string) interface{} { url := "https://my.zerotier.com/api/v1/network" info := zerotier.PostData(url, token, "{}") return info } -func (c *zerotierstruct) GetJoinNetworks() string { +func (c *zerotierStruct) GetJoinNetworks() string { json := command2.ExecResultStr("source " + config.AppInfo.ProjectPath + "/shell/helper.sh ;GetLocalJoinNetworks") return json } @@ -339,5 +339,5 @@ func NewZeroTierService() ZeroTierService { return http.ErrUseLastResponse //禁止重定向 }, } - return &zerotierstruct{} + return &zerotierStruct{} }