0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Diagram as Codeに挑戦して構成図をかいてみた(導入編)

Posted at

はじめに

最近色々な記事を見てて参考にしてる公式ドキュメント等の記載がなく探すことがままよくあると感じるものぐさの初心者エンジニアです。
今日はいつも書いてる構成図をコード管理できる便利なDiagrams an Codeに挑戦してみました。

今回の記事は導入編ですので、実践編と応用編は別途投稿を考えております。

公式ドキュメント

導入

最終的な導入環境

  • WIndows11 Pro
  • WSL環境
  • Ubuntu v22.04.1
  • Python 3.10.6
  • pip 22.3.1
  • graphviz version 2.43.

導入手順

言語周りをインストール

pip, pipenvどちらかでインストール
※今回はpipを利用

# お約束のアップデート
sudo apt update
sudo apt upgrade

# pip周りインストール
sudo apt install python3-pip

# 各バージョン確認
python3 --version
pip --version

Diagramsの導入作業

diagramsインストール

pip install diagrams

実行テスト

公式サンプルコード

# diagram.py
from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB

with Diagram("Web Service", show=False):
    ELB("lb") >> EC2("web") >> RDS("userdb")

エラー発生

ubuntu@PC-01:~$ python3 diagram.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/graphviz/backend/execute.py", line 81, in run_check
    proc = subprocess.run(cmd, **kwargs)
  File "/usr/lib/python3.10/subprocess.py", line 501, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.10/subprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1845, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: PosixPath('dot')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ubuntu/diagram.py", line 7, in <module>
    with Diagram("Web Service", show=False):
  File "/usr/local/lib/python3.10/dist-packages/diagrams/__init__.py", line 158, in __exit__
    self.render()
  File "/usr/local/lib/python3.10/dist-packages/diagrams/__init__.py", line 192, in render
    self.dot.render(format=self.outformat, view=self.show, quiet=True)
  File "/usr/local/lib/python3.10/dist-packages/graphviz/_tools.py", line 171, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/graphviz/rendering.py", line 122, in render
    rendered = self._render(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/graphviz/_tools.py", line 171, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/graphviz/backend/rendering.py", line 324, in render
    execute.run_check(cmd,
  File "/usr/local/lib/python3.10/dist-packages/graphviz/backend/execute.py", line 84, in run_check
    raise ExecutableNotFound(cmd) from e
graphviz.backend.execute.ExecutableNotFound: failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH

どうやらdotがないみたいなので確認

ubuntu@PC-01:~$ dot -V
Command 'dot' not found, but can be installed with:
sudo apt install graphviz

graphvizがないのでインストール

sudo apt install graphviz

ubuntu@PC-01:~$ dot -V
dot - graphviz version 2.43.0 (0)

改めてdiagram.pyを実行

実行すると無事画像の作成に成功

ubuntu@PC-01:~$ python3 diagram.py
ubuntu@PC-01:~$
ubuntu@PC-01:~$ ls
diagram.py  web_service.png

wslのubuntu os用ディレクトリにアクセスして画像を確認

web_service.png

無事画像が出力されていることを確認
次回はこのツールでいくつか構成図のパターンを作ってみようと思います!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?