無.Flac's Blog

無.Flac

1Panel推送SSL证书到腾讯云

180
2024-11-26

构建二合一 Cli 镜像

在 1Panel 侧边栏 容器 -> 镜像 中选择 构建镜像 ,名称填写 aliyun-tccli ,编辑内容如下:

FROM alpine:latest

RUN if curl -s https://cloudflare.com/cdn-cgi/trace | grep -q 'loc=CN' || [ $? -ne 0 ]; then \

        sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories; \

        pip_conf="https://pypi.tuna.tsinghua.edu.cn/simple"; \

    else \

        pip_conf="https://pypi.org/simple"; \

    fi \

    && apk add --no-cache python3 py3-pip jq wget curl \

    && pip config set global.index-url $pip_conf

RUN wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz \

    && tar -xvzf aliyun-cli-linux-latest-amd64.tgz \

    && rm aliyun-cli-linux-latest-amd64.tgz \

    && mv aliyun /usr/local/bin/ \

    && mkdir -p /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

RUN python3 -m venv /opt/venv \

    && . /opt/venv/bin/activate \

    && pip install --no-cache-dir tccli

ENV PATH="/opt/venv/bin:$PATH"

ENV ALIBABA_CLOUD_PROFILE=AkProfile

ENV ALIBABA_CLOUD_IGNORE_PROFILE=TRUE

ENV ALIBABA_CLOUD_REGION_ID=cn-hangzhou

RUN echo '#!/bin/sh' > /entrypoint.sh && \

    echo '' >> /entrypoint.sh && \

    echo 'CLOUD_PROVIDER="$1"' >> /entrypoint.sh && \

    echo 'SERVICE_TYPE="$2"' >> /entrypoint.sh && \

    echo 'DOMAIN_NAME="${DomainName}"' >> /entrypoint.sh && \

    echo '' >> /entrypoint.sh && \

    echo 'if [ -z "$CLOUD_PROVIDER" ] || [ -z "$SERVICE_TYPE" ] || [ -z "$DOMAIN_NAME" ]; then' >> /entrypoint.sh && \

    echo '  echo "错误: 缺少必要的参数。请提供 CLOUD_PROVIDER, SERVICE_TYPE 和 DOMAIN_NAME 参数。"' >> /entrypoint.sh && \

    echo '  exit 1' >> /entrypoint.sh && \

    echo 'fi' >> /entrypoint.sh && \

    echo '' >> /entrypoint.sh && \

    echo 'if [ "$CLOUD_PROVIDER" = "aliyun" ]; then' >> /entrypoint.sh && \

    echo '  if [ "$SERVICE_TYPE" != "cdn" ] && [ "$SERVICE_TYPE" != "dcdn" ]; then' >> /entrypoint.sh && \

    echo '    echo "错误: 对于阿里云,支持的服务类型只有 cdn 和 dcdn。"' >> /entrypoint.sh && \

    echo '    exit 1' >> /entrypoint.sh && \

    echo '  fi' >> /entrypoint.sh && \

    echo '  SSLPub=$(cat /ssl/fullchain.pem)' >> /entrypoint.sh && \

    echo '  SSLPri=$(cat /ssl/privkey.pem)' >> /entrypoint.sh && \

    echo '  if [ "$SERVICE_TYPE" = "cdn" ]; then' >> /entrypoint.sh && \

    echo '    echo "同步证书到阿里云 CDN..."' >> /entrypoint.sh && \

    echo '    aliyun cdn SetCdnDomainSSLCertificate --DomainName "$DOMAIN_NAME" --CertType upload --SSLProtocol on --SSLPub="$SSLPub" --SSLPri="$SSLPri"' >> /entrypoint.sh && \

    echo '    if [ $? -eq 0 ]; then' >> /entrypoint.sh && \

    echo '      echo "阿里云 CDN 证书同步成功。"' >> /entrypoint.sh && \

    echo '    else' >> /entrypoint.sh && \

    echo '      echo "阿里云 CDN 证书同步失败。"' >> /entrypoint.sh && \

    echo '      exit 1' >> /entrypoint.sh && \

    echo '    fi' >> /entrypoint.sh && \

    echo '  elif [ "$SERVICE_TYPE" = "dcdn" ]; then' >> /entrypoint.sh && \

    echo '    echo "同步证书到阿里云 DCDN..."' >> /entrypoint.sh && \

    echo '    aliyun dcdn SetDcdnDomainSSLCertificate --DomainName "$DOMAIN_NAME" --CertType upload --SSLProtocol on --SSLPub="$SSLPub" --SSLPri="$SSLPri"' >> /entrypoint.sh && \

    echo '    if [ $? -eq 0 ]; then' >> /entrypoint.sh && \

    echo '      echo "阿里云 DCDN 证书同步成功。"' >> /entrypoint.sh && \

    echo '    else' >> /entrypoint.sh && \

    echo '      echo "阿里云 DCDN 证书同步失败。"' >> /entrypoint.sh && \

    echo '      exit 1' >> /entrypoint.sh && \

    echo '    fi' >> /entrypoint.sh && \

    echo '  fi' >> /entrypoint.sh && \

    echo '' >> /entrypoint.sh && \

    echo 'elif [ "$CLOUD_PROVIDER" = "tencent" ]; then' >> /entrypoint.sh && \

    echo '  if [ "$SERVICE_TYPE" != "cdn" ] && [ "$SERVICE_TYPE" != "eo" ]; then' >> /entrypoint.sh && \

    echo '    echo "错误: 对于腾讯云,支持的服务类型只有 cdn 和 eo。"' >> /entrypoint.sh && \

    echo '    exit 1' >> /entrypoint.sh && \

    echo '  fi' >> /entrypoint.sh && \

    echo '  CertificatePublicKey=$(cat /ssl/fullchain.pem)' >> /entrypoint.sh && \

    echo '  CertificatePrivateKey=$(cat /ssl/privkey.pem)' >> /entrypoint.sh && \

    echo '  resp=$(tccli ssl UploadCertificate --cli-unfold-argument --CertificatePublicKey "$CertificatePublicKey" --CertificatePrivateKey "$CertificatePrivateKey")' >> /entrypoint.sh && \

    echo "  CertificateId=\$(echo \$resp | egrep -o '\"CertificateId\": \"[^\"]+\"' | cut -d'\"' -f4)" >> /entrypoint.sh && \

    echo '' >> /entrypoint.sh && \

    echo '  if [ "$SERVICE_TYPE" = "cdn" ] || [ "$SERVICE_TYPE" = "ecdn" ]; then' >> /entrypoint.sh && \

    echo '    echo "同步证书到腾讯云 CDN/ECDN..."' >> /entrypoint.sh && \

    echo '    tccli ssl DeployCertificateInstance --cli-unfold-argument --CertificateId "$CertificateId" --InstanceIdList "$DOMAIN_NAME" --ResourceType cdn --Status 1' >> /entrypoint.sh && \

    echo '    if [ $? -eq 0 ]; then' >> /entrypoint.sh && \

    echo '      echo "腾讯云 CDN/ECDN 证书同步成功。"' >> /entrypoint.sh && \

    echo '    else' >> /entrypoint.sh && \

    echo '      echo "腾讯云 CDN/ECDN 证书同步失败。"' >> /entrypoint.sh && \

    echo '      exit 1' >> /entrypoint.sh && \

    echo '    fi' >> /entrypoint.sh && \

    echo '  elif [ "$SERVICE_TYPE" = "eo" ]; then' >> /entrypoint.sh && \

    echo '    echo "同步证书到腾讯云 EO..."' >> /entrypoint.sh && \

    echo '    tccli ssl DeployCertificateInstance --cli-unfold-argument --CertificateId "$CertificateId" --InstanceIdList "$DOMAIN_NAME" --ResourceType eo --Status 1' >> /entrypoint.sh && \

    echo '    if [ $? -eq 0 ]; then' >> /entrypoint.sh && \

    echo '      echo "腾讯云 EO 证书同步成功。"' >> /entrypoint.sh && \

    echo '    else' >> /entrypoint.sh && \

    echo '      echo "腾讯云 EO 证书同步失败。"' >> /entrypoint.sh && \

    echo '      exit 1' >> /entrypoint.sh && \

    echo '    fi' >> /entrypoint.sh && \

    echo '  fi' >> /entrypoint.sh && \

    echo '' >> /entrypoint.sh && \

    echo 'else' >> /entrypoint.sh && \

    echo '  echo "错误: 不支持的云服务提供商 $CLOUD_PROVIDER。"' >> /entrypoint.sh && \

    echo '  exit 1' >> /entrypoint.sh && \

    echo 'fi' >> /entrypoint.sh

RUN chmod +x /entrypoint.sh

CMD ["/entrypoint.sh"]

运行终端代码

在申请证书后执行脚本

docker run --rm --name aliyun-tccli \
    -e TENCENTCLOUD_SECRET_ID= \
    -e TENCENTCLOUD_SECRET_KEY= \
    -e DomainName=domain.com \
    -v /root/ssl:/ssl \
    aliyun-tccli "/entrypoint.sh" "tencent" "cdn"

docker run --rm --name aliyun-tccli \
    -e TENCENTCLOUD_SECRET_ID= \
    -e TENCENTCLOUD_SECRET_KEY= \
    -e DomainName=domain.com \
    -v /root/ssl/mimo:/ssl \
    aliyun-tccli "/entrypoint.sh" "tencent" "cdn"