From b22d13c5b290a1407521971bb0b37b1888b7f98b Mon Sep 17 00:00:00 2001 From: hassegawa Date: Tue, 25 Jan 2022 17:43:20 -0300 Subject: [PATCH 1/6] add .env OCI_DISPLAY_NAME --- .env.example | 3 +++ index.php | 1 + src/OciApi.php | 2 +- src/OciConfig.php | 4 ++++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index fa1d569..5f1facf 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,9 @@ OCI_USER_ID=ocid1.user.oc1..aaaaaaaax72***kd3q OCI_TENANCY_ID=ocid1.tenancy.oc1..aaaaaaaakpx***qmpa OCI_KEY_FINGERPRINT=b3:a5:90:***:b0:8d:1c +# Optional. If not set, default value 'instance-' . date('Ymd-Hi'). +OCI_DISPLAY_NAME= + # absolute path (including directories) or direct public accessible URL OCI_PRIVATE_KEY_FILENAME="/path/to/oracleidentitycloudservice_***-07-14-10-35.pem" OCI_SUBNET_ID=ocid1.subnet.oc1.eu-frankfurt-1.aaaaaaaaahbb***faq diff --git a/index.php b/index.php index 34f3366..d414c97 100644 --- a/index.php +++ b/index.php @@ -27,6 +27,7 @@ getenv('OCI_USER_ID'), getenv('OCI_TENANCY_ID'), getenv('OCI_KEY_FINGERPRINT'), + getenv('OCI_DISPLAY_NAME'), getenv('OCI_PRIVATE_KEY_FILENAME'), getenv('OCI_AVAILABILITY_DOMAIN') ?: null, // null or '' or 'jYtI:PHX-AD-1' or ['jYtI:PHX-AD-1','jYtI:PHX-AD-2'] getenv('OCI_SUBNET_ID'), diff --git a/src/OciApi.php b/src/OciApi.php index 4644e5e..c8bdd7a 100644 --- a/src/OciApi.php +++ b/src/OciApi.php @@ -37,7 +37,7 @@ public function createInstance( string $availabilityDomain ): array { - $displayName = 'instance-' . date('Ymd-Hi'); + $displayName = "{$config->displayName}"; $body = <<tenancyId = $tenancyId; $this->keyFingerPrint = $keyFingerPrint; $this->privateKeyFilename = $privateKeyFilename; + $this->displayName = $displayName; $this->availabilityDomains = $availabilityDomains; $this->subnetId = $subnetId; $this->imageId = $imageId; From aae59cb37f6e8aa9a88272b06bd53ace445cc292 Mon Sep 17 00:00:00 2001 From: hassegawa Date: Tue, 25 Jan 2022 21:47:35 -0300 Subject: [PATCH 2/6] 'instance-' . date('Ymd-Hi') location --- index.php | 2 +- src/OciConfig.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index d414c97..7f30264 100644 --- a/index.php +++ b/index.php @@ -27,7 +27,7 @@ getenv('OCI_USER_ID'), getenv('OCI_TENANCY_ID'), getenv('OCI_KEY_FINGERPRINT'), - getenv('OCI_DISPLAY_NAME'), + getenv('OCI_DISPLAY_NAME') ?: 'instance-' . date('Ymd-Hi'), getenv('OCI_PRIVATE_KEY_FILENAME'), getenv('OCI_AVAILABILITY_DOMAIN') ?: null, // null or '' or 'jYtI:PHX-AD-1' or ['jYtI:PHX-AD-1','jYtI:PHX-AD-2'] getenv('OCI_SUBNET_ID'), diff --git a/src/OciConfig.php b/src/OciConfig.php index 4a6a71c..4a9ad63 100644 --- a/src/OciConfig.php +++ b/src/OciConfig.php @@ -47,7 +47,7 @@ public function __construct( string $tenancyId, string $keyFingerPrint, string $privateKeyFilename, - string $displayName = 'instance-' . date('Ymd-Hi'), + string $displayName, $availabilityDomains, string $subnetId, string $imageId, From 290f3613eda75ea771c4fc9b0fa0a8b7f7ed43af Mon Sep 17 00:00:00 2001 From: hassegawa Date: Tue, 25 Jan 2022 22:55:27 -0300 Subject: [PATCH 3/6] parameter position --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index 7f30264..d071447 100644 --- a/index.php +++ b/index.php @@ -27,8 +27,8 @@ getenv('OCI_USER_ID'), getenv('OCI_TENANCY_ID'), getenv('OCI_KEY_FINGERPRINT'), - getenv('OCI_DISPLAY_NAME') ?: 'instance-' . date('Ymd-Hi'), getenv('OCI_PRIVATE_KEY_FILENAME'), + getenv('OCI_DISPLAY_NAME') ?: 'instance-' . date('Ymd-Hi'), getenv('OCI_AVAILABILITY_DOMAIN') ?: null, // null or '' or 'jYtI:PHX-AD-1' or ['jYtI:PHX-AD-1','jYtI:PHX-AD-2'] getenv('OCI_SUBNET_ID'), getenv('OCI_IMAGE_ID'), From 4eb053a31291d6fea400ba267e883e984b14b85b Mon Sep 17 00:00:00 2001 From: hassegawa Date: Wed, 26 Jan 2022 22:27:07 -0300 Subject: [PATCH 4/6] Dockerfile e github action --- .github/workflows/docker-image.yml | 45 ++++++++++++++++++++++++++++++ Dockerfile | 27 ++++++++++++++++++ DockerfilePerformance | 33 ++++++++++++++++++++++ README_docker.md | 27 ++++++++++++++++++ run.sh | 15 ++++++++++ 5 files changed, 147 insertions(+) create mode 100644 .github/workflows/docker-image.yml create mode 100644 Dockerfile create mode 100644 DockerfilePerformance create mode 100644 README_docker.md create mode 100644 run.sh diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..5175cb8 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,45 @@ +name: Docker Image CI + +on: + create + +jobs: + push_to_registry: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@master + with: + platforms: arm64,amd64 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@master + + - name: Log in to Docker Hub + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ secrets.DOCKER_NAMESPACE }}/${{ secrets.DOCKER_REPOSITORY }} + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + builder: ${{ steps.buildx.outputs.name }} + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..09cefca --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM ubuntu:20.04 + +ARG DEBIAN_FRONTEND=noninteractive + +COPY . /app/oci-arm-host-capacity + +RUN apt update && apt install -y git php-cli php-curl php-xml jq nano unzip curl && \ + mkdir /compose && \ + useradd -r -s /bin/false ubuntu; \ + groupadd docker; \ + usermod -aG docker ubuntu; \ + cd /compose && \ + curl -sS https://getcomposer.org/installer -o composer-setup.php && \ + HASH=`curl -sS https://composer.github.io/installer.sig` && \ + php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \ + php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \ + cd /app/oci-arm-host-capacity && \ + composer update && \ + composer install && \ + chown ubuntu:ubuntu /app/oci-arm-host-capacity + +WORKDIR /app/oci-arm-host-capacity + +USER ubuntu + +COPY run.sh run.sh +CMD ./run.sh \ No newline at end of file diff --git a/DockerfilePerformance b/DockerfilePerformance new file mode 100644 index 0000000..e1b5aad --- /dev/null +++ b/DockerfilePerformance @@ -0,0 +1,33 @@ +FROM ubuntu:20.04 + +ARG DEBIAN_FRONTEND=noninteractive + +COPY . /app/oci-arm-host-capacity + +# first build to generate this layer +RUN apt update && apt install -y git php-cli php-curl php-xml jq nano unzip curl && \ + mkdir /compose && \ + useradd -r -s /bin/false ubuntu; \ + groupadd docker; \ + usermod -aG docker ubuntu; + +# SECOND build to generate this layer, uncomment after first build and build again +# RUN cd /compose && \ +# curl -sS https://getcomposer.org/installer -o composer-setup.php && \ +# HASH=`curl -sS https://composer.github.io/installer.sig` && \ +# php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \ +# php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \ +# mkdir /app + +# THIRT build to generate this layer, uncommnet after second build and build again +# RUN cd /app/oci-arm-host-capacity && \ +# composer update && \ +# composer install && \ +# chown ubuntu:ubuntu /app/oci-arm-host-capacity + +WORKDIR /app/oci-arm-host-capacity + +USER ubuntu + +COPY run.sh run.sh +CMD ./run.sh \ No newline at end of file diff --git a/README_docker.md b/README_docker.md new file mode 100644 index 0000000..6c8fbfe --- /dev/null +++ b/README_docker.md @@ -0,0 +1,27 @@ +# oracle-tf +### [source and config](https://github.com/hitrov/oci-arm-host-capacity) + +* build Dockerfile + * $ docker login + * $ docker buildx build --platform linux/arm64,linux/amd64 -t DOCKER/IMAGENAME:IMAGETAG -o type=registry . + + +* Build local whith DockerfilePerformance, edit DockerfilePerformance to first image + * $ docker login + * Fisrt and Second___: docker buildx build --platform linux/arm64,linux/amd64 -f DockerfilePerformance -t DOCKER/IMAGENAME:IMAGETAG -o type=image . + * Thirt______________: docker buildx build --platform linux/arm64,linux/amd64 -f DockerfilePerformance -t DOCKER/IMAGENAME:IMAGETAG -o type=registry . + +* run + * $ docker create --name machine DOCKER/IMAGENAME:IMAGETAG + * $ docker cp your-private-file.pem machine:/app/oci-arm-host-capacity + * $ docker cp .env machine:/app/oci-arm-host-capacity + * $ docker start machine + + +* Github action - .github/workflows/docker-image.yml + * add secret - github.com > project > settings > Secrets > Actions + * DOCKER_NAMESPACE + * DOCKER_PASSWORD + * create a token on user settings + * DOCKER_REPOSITORY + * DOCKER_USERNAME diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..1f740ba --- /dev/null +++ b/run.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +CODE=$(php ./index.php | jq -r '.code'); + + +while [ "$CODE" != "LimitExceeded" ] +do + REQ=$(php ./index.php); + + CODE=$(echo "${REQ}"| jq -r '.code') + MSG=$(echo "${REQ}"| jq -r '.message') + + echo $(date +%F_%H-%M-%S) - $CODE - $MSG; + sleep 10m; +done \ No newline at end of file From 9954f4a5add9e8ba9b169ecb2d8d82ed4470e811 Mon Sep 17 00:00:00 2001 From: hassegawa Date: Thu, 3 Feb 2022 14:05:35 -0300 Subject: [PATCH 5/6] ubuntu user owner run.sh --- Dockerfile | 6 +++++- DockerfilePerformance | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 09cefca..45d7a09 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,11 @@ RUN apt update && apt install -y git php-cli php-curl php-xml jq nano unzip curl WORKDIR /app/oci-arm-host-capacity -USER ubuntu COPY run.sh run.sh +RUN chmod +x run.sh +RUN chown ubuntu:ubuntu run.sh + +USER ubuntu + CMD ./run.sh \ No newline at end of file diff --git a/DockerfilePerformance b/DockerfilePerformance index e1b5aad..c89c7b9 100644 --- a/DockerfilePerformance +++ b/DockerfilePerformance @@ -27,7 +27,10 @@ RUN apt update && apt install -y git php-cli php-curl php-xml jq nano unzip curl WORKDIR /app/oci-arm-host-capacity +COPY run.sh run.sh +RUN chmod +x run.sh +RUN chown ubuntu:ubuntu run.sh + USER ubuntu -COPY run.sh run.sh CMD ./run.sh \ No newline at end of file From aacef4df734057559f993795cf269634144d376e Mon Sep 17 00:00:00 2001 From: hassegawa Date: Sat, 19 Feb 2022 00:22:14 -0300 Subject: [PATCH 6/6] RETRY_DELAY_TIME --- Dockerfile | 2 ++ DockerfilePerformance | 2 ++ README_docker.md | 23 ++++++++++++++++------- run.sh | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 45d7a09..02f2ac2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,8 @@ FROM ubuntu:20.04 ARG DEBIAN_FRONTEND=noninteractive +ENV RETRY_DELAY_TIME 10 + COPY . /app/oci-arm-host-capacity RUN apt update && apt install -y git php-cli php-curl php-xml jq nano unzip curl && \ diff --git a/DockerfilePerformance b/DockerfilePerformance index c89c7b9..6be0372 100644 --- a/DockerfilePerformance +++ b/DockerfilePerformance @@ -2,6 +2,8 @@ FROM ubuntu:20.04 ARG DEBIAN_FRONTEND=noninteractive +ENV RETRY_DELAY_TIME 10 + COPY . /app/oci-arm-host-capacity # first build to generate this layer diff --git a/README_docker.md b/README_docker.md index 6c8fbfe..f3d744d 100644 --- a/README_docker.md +++ b/README_docker.md @@ -6,19 +6,28 @@ * $ docker buildx build --platform linux/arm64,linux/amd64 -t DOCKER/IMAGENAME:IMAGETAG -o type=registry . -* Build local whith DockerfilePerformance, edit DockerfilePerformance to first image +### Build local whith DockerfilePerformance, edit DockerfilePerformance to first image * $ docker login * Fisrt and Second___: docker buildx build --platform linux/arm64,linux/amd64 -f DockerfilePerformance -t DOCKER/IMAGENAME:IMAGETAG -o type=image . * Thirt______________: docker buildx build --platform linux/arm64,linux/amd64 -f DockerfilePerformance -t DOCKER/IMAGENAME:IMAGETAG -o type=registry . -* run - * $ docker create --name machine DOCKER/IMAGENAME:IMAGETAG - * $ docker cp your-private-file.pem machine:/app/oci-arm-host-capacity - * $ docker cp .env machine:/app/oci-arm-host-capacity - * $ docker start machine +### run + * default RETRY_DELAY_TIME 10 minutes + * $ docker run --rm -d \ + -v $(pwd)/.env:/app/oci-arm-host-capacity/.env \ + -v $(pwd)/key.pem:/app/oci-arm-host-capacity/key.pem \ + hassegawa/machine + or + + * to set RETRY_DELAY_TIME = 5 minutes + * $ docker run --rm -d \ + -e RETRY_DELAY_TIME=5 + -v $(pwd)/.env:/app/oci-arm-host-capacity/.env \ + -v $(pwd)/key.pem:/app/oci-arm-host-capacity/key.pem \ + hassegawa/machine -* Github action - .github/workflows/docker-image.yml +### Github action - .github/workflows/docker-image.yml * add secret - github.com > project > settings > Secrets > Actions * DOCKER_NAMESPACE * DOCKER_PASSWORD diff --git a/run.sh b/run.sh index 1f740ba..084fe33 100644 --- a/run.sh +++ b/run.sh @@ -11,5 +11,5 @@ do MSG=$(echo "${REQ}"| jq -r '.message') echo $(date +%F_%H-%M-%S) - $CODE - $MSG; - sleep 10m; + sleep ${RETRY_DELAY_TIME}m; done \ No newline at end of file