to set break-point inside kolla container's code and to use pdb outside of it

https://docs.openstack.org/kolla-ansible/queens/contributor/kolla-for-openstack-development.html

에는 다음과 같은 내용이 적혀있다.

this url says following:

![image.png](https://qiita-image-store.s3.amazonaws.com/0/12587/803db686-4db1-d81a-844b-6450010b5e94.png)

그럼, 해보자
then, let's try it.

container image 안에는 remote-pdb와 socat등이 이미 설치되어 있었다.
I found that remote-pdb and socat are already installed.

```bash
[root@tkim-rocky4 opt]# docker exec -ti horizon /bin/bash
(horizon)[root@tkim-rocky4 /]# pip freeze | grep pdb
remote-pdb==1.2.0
(horizon)[root@tkim-rocky4 /]# which socat
/usr/bin/socat
```

nova-api등은 번거로우므로 horizon의 코드에 breakpoint를 걸어보았다. 적당히 로그인 하는 폼을 보여주는 파일에 break point 작성했다.

for *-apis are sort of cumbersome, I chose horizon service to test. I added a couple of lines to create a break point at 
``/var/lib/kolla/venv/lib/python2.7/site-packages/openstack_auth/forms.py`` 



```python
class Login(django_auth_forms.AuthenticationForm):
    ....
    use_required_attribute = False
    region = forms.ChoiceField(label=_("Region"), required=False)
    username = forms.CharField(
        label=_("User Name"),
        widget=forms.TextInput(attrs={"autofocus": "autofocus"}))
    password = forms.CharField(label=_("Password"),
                               widget=forms.PasswordInput(render_value=False))

    # from here
    from remote_pdb import RemotePdb
    RemotePdb('127.0.0.1', 4444).set_trace()
    # to here

    def __init__(self, *args, **kwargs):
        super(Login, self).__init__(*args, **kwargs)
        ...
```


저장하고 container를 재시작
saved file and restarted the container.


```
[root@tkim-rocky4 opt]# docker restart horizon
horizon
[root@tkim-rocky4 opt]#
```

브라우저로 dashboard에 접속하면 페이지가 waiting 상태로 열리지 않는다. http://{가상머신 IP}/

tried to open dashboard url http://{virtual machine IP}/ with a internet browser but it keeps 'waiting' status. 

이때 가상머신 안에서 ``socat readline tcp:127.0.0.1:4444``을 실행하면 pdb 프롬프트가 뜬다.
( 가상머신 안에서 컨테이너의 포트에 직접 접근이 가능한 것은 현재 Linux뿐이다. Mac과 Windows의 docker container는 제약이 있어서 안된다. )

if you run ``socat readline tcp:127.0.0.1:4444``  on the virtual machine, you can see pdb prompt.
( use Linux to access container with port directly. 'docker for Mac' and 'docker for Windows' lags for some constraints. )


![image.png](https://qiita-image-store.s3.amazonaws.com/0/12587/b7b1671d-933d-4622-ca95-6300a5b7b832.png)

``where`` 커맨드로 어디에서 멈추었는 지 확인하면 forms.py에 멈추어 있는 것을 확인할 수 있다.
``where`` command shows where instruction point locates now.

```
-> res = instance.__dict__[self.name] = self.func(instance)
  /var/lib/kolla/venv/lib/python2.7/site-packages/django/urls/resolvers.py(398)urlconf_module()
-> return import_module(self.urlconf_name)
  /usr/lib64/python2.7/importlib/__init__.py(37)import_module()
-> __import__(name)
  /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_dashboard/urls.py(53)<module>()
-> urlpatterns.append(url(r'^auth/', include(u)))
  /var/lib/kolla/venv/lib/python2.7/site-packages/django/conf/urls/__init__.py(50)include()
-> urlconf_module = import_module(urlconf_module)
  /usr/lib64/python2.7/importlib/__init__.py(37)import_module()
-> __import__(name)
  /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_auth/urls.py(17)<module>()
-> from openstack_auth import views
  /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_auth/views.py(33)<module>()
-> from openstack_auth import forms
  /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_auth/forms.py(31)<module>()
-> class Login(django_auth_forms.AuthenticationForm):
> /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_auth/forms.py(61)Login()
-> def __init__(self, *args, **kwargs):
(Pdb)
```

자, 컨테이너 내부의 디버깅을 컨테이너 바깥에서 pdb를 사용해서 할 수 있다.
Now, you know how to debug container's code outside of it.


필요없거나 원래환경으로 되돌리려면, 작성된 instance를 지우고, kolla-ansible을 초기화 하고 다시 환경을 만든다.
if you want to reset the environment, delete instances created before and use ``kolla-ansible`` command.

```
$ nova delete demo1

$ kolla-ansible destroy --yes-i-really-really-mean-it

$ cd /opt && kolla-ansible -i all-in-one bootstrap-servers && kolla-ansible -i all-in-one prechecks && kolla-ansible -i all-in-one deploy && kolla-ansible post-deploy && source /etc/kolla/admin-openrc.sh && source /usr/share/kolla-ansible/init-runonce
```

다음에는 pycharm IDE를 설치해 docker 서버 리모트 디버깅을 해보자.
next time, I'd like to talk about pycharm IDE remote debugging with docker server connection.