diff --git a/.github/workflows/precommit.yaml b/.github/workflows/precommit.yaml new file mode 100644 index 0000000..18e7498 --- /dev/null +++ b/.github/workflows/precommit.yaml @@ -0,0 +1,14 @@ +name: pre-commit + +on: + pull_request: + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v3.0.1 \ No newline at end of file diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..f7a1270 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,38 @@ +name: Release + +on: + push: + +permissions: + contents: write + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up QEMU + uses: docker/setup-qemu-action@master + with: + platforms: all + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@master + - name: Install earthly + uses: earthly/actions-setup@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + version: "latest" + use-cache: true + - name: Run Release target + run: earthly --ci --output +release + - name: Release + uses: softprops/action-gh-release@v2 + if: startsWith(github.ref, 'refs/tags/') + with: + files: build/* \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..44af965 --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +bin/* +.netrc + +# custom arg files +*.arg + +# env files +*.env + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + + +vendor +!vendor/**/zz_generated.* + +# editor and IDE paraphernalia +.idea +*.swp +*.swo +*~ +a + +# cred file for docker build +.netrc +_build +.scannerwork +.fossa.yml +.DS_Store +.temp +local + +outputs/ +build/* +*_local* +*__debug_bin* +*.vscode* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..1d44399 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,29 @@ +# install pre-commit on your system and then +# run pre-commit install in this repository. +# You can by pass commit hooks with: +# git commit -n +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: check-yaml + stages: [commit] + - id: check-json + stages: [commit] + - id: check-toml + stages: [commit] + - id: check-merge-conflict + stages: [commit] + - id: check-case-conflict + stages: [commit] + - id: detect-private-key + stages: [commit] + - repo: https://github.com/crate-ci/typos + rev: v1.25.0 + hooks: + - id: typos + - repo: https://github.com/compilerla/conventional-pre-commit + rev: v3.4.0 + hooks: + - id: conventional-pre-commit + stages: [commit-msg] diff --git a/Earthfile b/Earthfile new file mode 100644 index 0000000..941e08e --- /dev/null +++ b/Earthfile @@ -0,0 +1,93 @@ +VERSION 0.8 + +ARG --global SPECTRO_PUB_REPO=us-docker.pkg.dev/palette-images +ARG --global UBUNTU_IMAGE=${SPECTRO_PUB_REPO}/third-party/ubuntu:22.04 +FROM ${UBUNTU_IMAGE} + +release: + BUILD +package-tar \ + --PLATFORM=linux \ + --ARCH=amd64 \ + --ARCH=arm64 + + BUILD +palette-agent \ + --PLATFORM=linux \ + --ARCH=amd64 \ + --ARCH=arm64 + + BUILD +install-script + +ubuntu: + FROM ${UBUNTU_IMAGE} + RUN apt-get update && apt-get install -y systemctl gettext-base + COPY PE_VERSION PE_VERSION + +stylus-image: + ARG PLATFORM=linux + ARG ARCH=amd64 + ARG STYLUS_IMAGE + FROM --platform=$PLATFORM/$ARCH $STYLUS_IMAGE + SAVE ARTIFACT --keep-own ./* + +palette-agent: + FROM +ubuntu + + ARG VERSION=$(head -n 1 PE_VERSION) + ARG PLATFORM=linux + ARG ARCH=amd64 + ARG STYLUS_IMAGE=${SPECTRO_PUB_REPO}/edge/stylus-agent-mode-${PLATFORM}-${ARCH}:${VERSION} + + WORKDIR /workdir + COPY (+stylus-image/opt/spectrocloud/bin/palette-agent --PLATFORM=${PLATFORM} --ARCH=${ARCH} --STYLUS_IMAGE=${STYLUS_IMAGE}) /workdir/ + RUN chmod +x /workdir/palette-agent + + SAVE ARTIFACT --keep-own /workdir/palette-agent AS LOCAL ./build/palette-agent-${PLATFORM}-${ARCH} + +package-tar: + FROM +ubuntu + + ARG VERSION=$(head -n 1 PE_VERSION) + ARG PLATFORM=linux + ARG ARCH=amd64 + ARG STYLUS_IMAGE=${SPECTRO_PUB_REPO}/edge/stylus-agent-mode-${PLATFORM}-${ARCH}:${VERSION} + ARG TAR_NAME=agent-mode-${PLATFORM}-${ARCH} + + WORKDIR /workdir/var/lib/spectro + COPY (+stylus-image/ --PLATFORM=${PLATFORM} --ARCH=${ARCH} --STYLUS_IMAGE=${STYLUS_IMAGE}) /workdir/var/lib/spectro/stylus + + COPY package/tar/spectro-init.service /etc/systemd/system/spectro-init.service + COPY package/tar/spectro-init.sh /workdir/var/lib/spectro/spectro-init.sh + RUN mkdir -p /workdir/etc/systemd/system + RUN cp -rfv /workdir/var/lib/spectro/stylus/etc/systemd/system/spectro* /etc/systemd/system/ + RUN cp -rfv /etc/systemd/system/spectro* /workdir/etc/systemd/system/ + + RUN for service in /etc/systemd/system/spectro*; do systemctl enable "$(basename "$service")"; done + + RUN mkdir -p /workdir/etc/systemd/system/multi-user.target.wants + RUN cp -rfv /etc/systemd/system/multi-user.target.wants/spectro* /workdir/etc/systemd/system/multi-user.target.wants/ + RUN mkdir -p /workdir/etc/systemd/system/local-fs.target.wants + RUN cp -rfv /etc/systemd/system/local-fs.target.wants/spectro* /workdir/etc/systemd/system/local-fs.target.wants/ + RUN mkdir -p /workdir/etc/systemd/system/sysinit.target.wants + RUN cp -rfv /etc/systemd/system/sysinit.target.wants/spectro* /workdir/etc/systemd/system/sysinit.target.wants/ + + RUN tar -cvf /${TAR_NAME}.tar -C /workdir . + + SAVE ARTIFACT /${TAR_NAME}.tar AS LOCAL ./build/ + +install-script: + FROM +ubuntu + + ARG VERSION=$(head -n 1 PE_VERSION) + ARG IMAGE_REPO=${SPECTRO_PUB_REPO}/edge + ARG AGENT_URL_PREFIX=https://github.com/spectrocloud/agent-mode/releases/latest/download + + ENV PE_VERSION=${VERSION} + ENV IMAGE_REPO=${IMAGE_REPO} + ENV AGENT_URL_PREFIX=${AGENT_URL_PREFIX} + + WORKDIR /workdir + COPY install.sh.tmpl /workdir/install.sh.tmpl + RUN envsubst '${PE_VERSION} ${IMAGE_REPO} ${AGENT_URL_PREFIX}' < /workdir/install.sh.tmpl > /workdir/install.sh + RUN chmod +x /workdir/install.sh + + SAVE ARTIFACT --keep-own /workdir/install.sh AS LOCAL ./build/install.sh diff --git a/PE_VERSION b/PE_VERSION new file mode 100644 index 0000000..83cc6d3 --- /dev/null +++ b/PE_VERSION @@ -0,0 +1 @@ +v4.5.0-rc4 \ No newline at end of file diff --git a/README.md b/README.md index 5aa7637..fb3666d 100644 --- a/README.md +++ b/README.md @@ -1 +1,25 @@ -# agent-mode \ No newline at end of file +# agent-mode + +## Quick Start + +```bash +curl https://github.com/spectrocloud/agent-mode/releases/latest/download/install.sh | bash +``` + +## Usage + +### specify userdata + +Userdata can be a URL or a file path. + +```bash +curl https://github.com/spectrocloud/agent-mode/releases/latest/download/install.sh | USERDATA=https://xxx/userdata bash +# or +curl https://github.com/spectrocloud/agent-mode/releases/latest/download/install.sh | USERDATA=/path/to/userdata bash +``` + +### specify palette version + +```bash +curl https://github.com/spectrocloud/agent-mode/releases/latest/download/install.sh | VERSION=v4.5.0 bash +``` diff --git a/install.sh.tmpl b/install.sh.tmpl new file mode 100644 index 0000000..8ebaa57 --- /dev/null +++ b/install.sh.tmpl @@ -0,0 +1,32 @@ +#!/bin/bash +set -e +# set -x + +# Get the platform architecture +ARCH=$(uname -m) +VERSION=${PE_VERSION} + +# Check the architecture and print the appropriate message +if [ "$ARCH" == "x86_64" ]; then + ARCH=amd64 +elif [ "$ARCH" == "aarch64" ] || [ "$ARCH" == "arm64" ]; then + ARCH=arm64 +else + echo "Unsupported architecture: $ARCH" +fi + +IMAGE=${IMAGE_REPO}/stylus-agent-mode-linux-${ARCH}:${VERSION} +URL=${AGENT_URL_PREFIX}/palette-agent-${VERSION}-linux-${ARCH} + +# Download edge-agent +curl -v -L $URL -o palette-agent +chmod +x palette-agent + +# Install with palette-agent +if [ -z "$USER_DATA" ]; then + # Install without user data + ./palette-agent install --source $IMAGE +else + # Install with user data + ./palette-agent install --source $IMAGE --config "$USER_DATA" +fi diff --git a/package/tar/install.sh b/package/tar/install.sh new file mode 100755 index 0000000..27041d6 --- /dev/null +++ b/package/tar/install.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -e +# set -x # Uncomment for debugging + +unpack() { + TAR_FILE=$1 + tar -xvf $TAR_FILE -C / +} + +unpack $1 diff --git a/package/tar/spectro-init.service b/package/tar/spectro-init.service new file mode 100644 index 0000000..ec1a4b3 --- /dev/null +++ b/package/tar/spectro-init.service @@ -0,0 +1,14 @@ +[Unit] +Description=Spectro Initialization +Before=local-fs.target spectro-palette-agent-bootstrap.service + +[Service] +ExecStart=/var/lib/spectro/spectro-init.sh +User=root +Group=root +Restart=on-failure +RemainAfterExit=yes +RestartSec=5s + +[Install] +WantedBy=local-fs.target diff --git a/package/tar/spectro-init.sh b/package/tar/spectro-init.sh new file mode 100755 index 0000000..d5374bf --- /dev/null +++ b/package/tar/spectro-init.sh @@ -0,0 +1,28 @@ +#!/bin/bash +set -ex +if [ -f /etc/spectro/environment ]; then + . /etc/spectro/environment +fi + +SENTINEL_FILE="${STYLUS_ROOT}/opt/spectrocloud/state/setup_done" + +if [ -f ${SENTINEL_FILE} ]; then + echo "Setup already done" + exit +fi + +export PATH=$PATH:/var/lib/spectro/stylus/opt/spectrocloud/bin +palette-agent install --source dir:/var/lib/spectro/stylus --config /var/lib/spectro/userdata + +# reload the environment on first boot +if [ -f /etc/spectro/environment ]; then + . /etc/spectro/environment +fi + +touch ${SENTINEL_FILE} +systemctl daemon-reload +systemctl restart spectro-palette-agent-start || true +systemctl restart spectro-palette-agent-initramfs || true +systemctl restart spectro-palette-agent-boot || true +systemctl restart spectro-palette-agent-network || true +systemctl restart spectro-palette-agent-bootstrap || true