LoginSignup
0
0

More than 1 year has passed since last update.

OpenFaaSでmydns.jpのIPアドレス更新を行うfunctionを作成する

Last updated at Posted at 2022-01-10

前提

OpenFaaS環境の構築は終わっているものとする

faas-cliでfunctionイメージの雛形を作成する

faas-cli template pull
faas-cli new mydns-update --lang dockerfile

Folder: mydns-update created.
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|


Function created in folder: mydns-update
Stack file written: mydns-update.yml

認証情報用Secret作成

公式ではfaas-cli経由で作成とあるが、gatewayやloginを指定する必要があり煩雑なためkubectlを使用して作成

kubectl create secret generic mydns-username --from-literal mydns-username="(username)" -n openfaas-fn
kubectl create secret generic mydns-password --from-literal mydns-password="(password)" -n openfaas-fn

コマンド作成

実行するdocker imageはalpineなのでbashはないので、ashを指定する。

/mydns-update/start.sh
#!/bin/ash

USER=`cat /var/openfaas/secrets/mydns-username`
PASS=`cat /var/openfaas/secrets/mydns-password`

wget -O- "http://$USER:$PASS@www.mydns.jp/login.html"

Dockerfile編集

mydns-update/Dockerfile
FROM ghcr.io/openfaas/classic-watchdog:0.2.0 as watchdog

FROM alpine:3.12

RUN mkdir -p /home/app

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

+ ADD start.sh /home/app/start.sh
+ RUN chmod +x /home/app/start.sh

# Add non root user
RUN addgroup -S app && adduser app -S -G app
RUN chown -R app /home/app

WORKDIR /home/app

USER app

# Populate example here - i.e. "cat", "sha512sum" or "node index.js"
- ENV fprocess="cat"
+ ENV fprocess="/home/app/start.sh"
# Set to true to see request in function logs
ENV write_debug="false"

EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]

イメージ作成&push

faas-cli build -f mydns-update.yml
docker login
docker tag mydns-update username/mydns-update
docker push username/mydns-update

function定義

image.png

実行結果

INVOKEボタンを押して実行

Connecting to www.mydns.jp (81.4.101.244:80)
writing to stdout
<html>
<head>
<title>Free Dynamic DNS (DDNS) for Home Server and VPS etc  | MyDNS.JP</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<LINK href="./site.css" rel=stylesheet type=text/css>

</head>
<BODY BGCOLOR="#FFFFFF"
      TEXT="#304040"
      leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"
>
Login and IP address notify OK.<BR>
login_status = 1.<BR>
<BR>
<DT>MASTERID :</DT><DD>mydns??????</DD>
<DT>REMOTE ADDRESS:</DT><DD>xxx.102.yyy.zzz</DD>
<DT>ACCESS DAYTIME:</DT><DD>2022/01/10 13:50:44 UTC</DD>
<DT>SERVER ADDRESS:</DT><DD>81.4.101.244</DD>
<BR>

</body>
</html>
-                    100% |********************************|   618  0:00:00 ETA
written to stdout

感想

Secret作成・参照するのではなく、実行時に与えるべき

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0