ksaitoの日記

日々試したことの覚え書き

プロキシの設定する

プロキシ環境下では、プロキシサーバを設定しないとインターネットに接続できません。 環境変数に設定する場所は、/etc/environment/etc/profile.dだったり、$HOMEの各種シェルの.profileと、いろいろな場所で設定できます。 yumaptといったコマンド毎にも設定場所があり、正しく設定されているか非常に不安になります。

さらに、各種仮想環境やコンテナ環境で、設定場所や反映されるタイミングがいろいろなので整理が必要です。

下記の方針を墨守します。

  • GitにコミットするDockerfileや設定ファイルにプロキシサーバを直書きしない
  • プロキシの設定をシンプルに保ち、(最終的には)ansibleのplay bookにする
  • 一度に沢山考えすぎない…

OS

設定は、/etc/environmentにすることで利用するログインシェルによらず、OS全体で設定できる。

docker build

docker buildでは、--build-arg環境変数を参照して設定する。

makeを使った場合、下記で実行環境に設定されたプロキシ設定を利用できる。

$ grep BUILD_OPTS Makefile
BUILD_OPTS=--build-arg http_proxy=$(shell echo $$http_proxy) --build-arg https_proxy=$(shell echo $$https_proxy) --build-arg no_proxy=$(shell echo $$no_proxy)
        docker build -t $(NAME) $(BUILD_OPTS) .
$ make -nB
docker build -t docker-bp --build-arg http_proxy=http://proxy.co.jp:8080 --build-arg https_proxy=http://proxy.co.jp:8080 --build-arg no_proxy= .
touch build
$

続きは、また今度…

以上

systemdのサービスをansible-playbookのhandlersでうまく記述する方法

ansible-playbookのhandlersでserviceを使ってサービスをリスタート出来ますが、systemdのサービスを再起動しようとしてもエラーになります。

systemdは、設定変更時にsystemctl daemin-reloadを実行してからサービスを再起動する必要があるためです。

下記の例は、systemdで起動するdocker daemonの例です。

handlersに、shellを使ってsystemctl daemin-reloadするハンドラとserviceを使ってリスタートするハンドラを定義します。

notifyで、2つのハンドラを順番に呼び出すことで正しくサービスを再起動してくれます。

tasks:
    - block:
        - name: create docker daemon systemd directory
          file:
            path: "/etc/systemd/system/docker.service.d"
            state: directory
            owner: root
            group: root
            mode: "u=rwx,g=rx,o=rx"
        - name: setup docker daemon proxy
          template:
            src: "{{ item }}.j2"
            dest: "/etc/systemd/system/docker.service.d/{{ item }}"
            owner: root
            group: root
            mode: "u=rw,g=r,o=r"
          notify:
            - daemon reload
            - restart docker
          with_items:
            - 50-http-proxy.conf
      when: http_proxy != ""

  handlers:
    - name: daemon reload
      shell: systemctl daemon-reload
    - name: restart docker
      service: name=docker state=restarted

docker runすると/bin/bash: /bin/bash: cannot execute binary fileが出る場合の対処

コンテナのデバッグで下記のコマンドをよく使います。

$ docker run -it --rm <イメージ> /bin/sh

zabbixのオフシャルイメージの幾つかで下記のようなエラーがでてしばらく悩みました。

$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest /bin/sh
/bin/sh: /bin/sh: cannot execute binary file
$

次のように実行することでエラーを回避出来ます。

$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest -i
bash-4.3#

原因と再現手順

オフシャルイメージのDockerfileを見ると、ENTRYPOINTCMDは、下記のように書かれています。

$ tail -3 Dockerfile
ENTRYPOINT ["/bin/bash"]

CMD ["/run_zabbix_component.sh", "agentd", "none"]
$

5. CMD and ENTRYPOINT better togetherENTRYPOINTCMDを併用することでコンテナ化したバイナリのアプリケーションをENTRYPOINTに指定して、CMDにデフォルトの振る舞いに必要なオプションを指定するというベストプラクティスが紹介されています。

今回のケースでは、ENTRYPOINT/bin/bashが指定されていることで引数に指定した/bin/bashシェルスクリプトと想定して実行しようとしたためにエラーとなりました。

再現してみると下記のようなことが発生していることになります。-iオプションは、bashの対話的な起動オプションでCMDに指定されたシェルを無効にするためのワークアラウンドです。

$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest /bin/bash
/bin/bash: /bin/bash: cannot execute binary file
$ docker run -it --rm zabbix/zabbix-agent:alpine-3.2-latest -i
bash-4.3# /bin/bash /bin/bash
/bin/bash: /bin/bash: cannot execute binary file
bash-4.3#

ENTRYPOINTにシェル以外のバイナリが指定されているコンテナの場合は、下記のような別解で対応します。

$ docker run --entrypoint /bin/sh -it --rm zabbix/zabbix-agent:alpine-3.2-latest
/var/lib/zabbix #

これで、Dockerfileの設定にかかわらず内容を確認できるようになりました。

s3cmd syncでシンボリックリンクのコピー

s3cmd syncで下記のようなシンボリックリンクをコピーすると無限に深いディレクトリを作り続けます。(何れどこかでエラーになると思いますが…)

$ ls -l .fcpcache
lrwxr-xr-x@ 1 user  staff  1  3 11 16:40 .fcpcache -> .
$

Dockerコンテナでdebconfのdialogを使う

Dockerコンテナでdialogを使ってインタラクティブに設定をするための設定です。

コンテナからdebconf実行

特別何もしないとtextで入力を求められます。

sudo docker run -it --rm -e TZ=Asia/Tokyo -e LANG=ja_JP.UTF-8 ubuntu:16.10 bash
root@b1af411dee00:/mnt# dpkg-reconfigure debconf
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
Configuring debconf
-------------------

設定に debconf
を用いるパッケージは、共通のルック&フィールを用います。どの種類のユーザインターフェイスを用いるかを選んでください。

「ダイアログ」は全画面の文字ベースのインターフェイスです。「readline」はより伝統的なプレーンテキストのインターフェイスです�
��「gnome」と「kde」は近代的な X のインターフェイスで、それぞれのデスクトップに適しています (ほかの X
環境で利用することもできます)。「エディタ」を用いるとあなたの好きなテキストエディタを用いることができます。「非対話的」�
�選ぶとまったく質問をしなくなります。

  1. ダイアログ  2. Readline  3. Gnome  4. Kde  5. エディタ  6. 非対話的
利用するインターフェイス: 

whiptailをインストールして再実行

whiptailは、newtというテキストベースのウィンドウツールです。

これがないので、利用できるtextが選択されたのですね。

再実行するとおなじみのダイアログが表示されました。

root@b1af411dee00:/mnt# apt-get install whiptail
root@b1af411dee00:/mnt# dpkg-reconfigure debconf
  ┌────────────────────────────────────────────────────────────────────────────┤ Configuring debconf ├────────────────────────────────────────────────────────────────────────────┐  
  │ 設定に debconf                                                                                                                                                                │  
  │ を用いるパッケージは、共通のルック&フィールを用います。どの種類のユーザインターフェイスを用いるかを選んでください<E3>                                                        │  
  │ <80><82>                                                                                                                                                                      │  
  │                                                                                                                                                                               │  
  │ 「ダイアログ」は全画面の文字ベースのインターフェイスです。「readline」はより伝統的なプレーンテキストのインターフェイ<E3><82>                                                  │  
  │ <B9>です。「gnome」と「kde」は近代的な X のインターフェイスで、それぞれのデスクトップに適しています (ほかの X                                                                 │  
  │ 環境で利用することもできます)。「エディタ」を用いるとあなたの好きなテキストエディタを用いることができます。「非対話                                                           │  
  │ 的」を選ぶとまったく質問をしなくなります。                                                                                                                                    │  
  │                                                                                                                                                                               │  
  │ 利用するインターフェイス:                                                                                                                                                     │  
  │                                                                                                                                                                               │  
  │                                                                                  ダイアログ                                                                                   │  
  │                                                                                  Readline                                                                                     │  
  │                                                                                  Gnome                                                                                        │  
  │                                                                                  Kde                                                                                          │  
  │                                                                                  エディタ                                                                                     │  
  │                                                                                  非対話的                                                                                     │  
  │                                                                                                                                                                               │  
  │                                                                                                                                                                               │  
  │                                                     <了解>                                                       <取消>                                                       │  
  │                                                                                                                                                                               │  
  └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘  

後続のダイアログです。debconfは、ソフトウェアをパッケージして柔軟に設定/再設定をするための非常に優れた仕組みなので使わない手はないですね。

                   ┌──────────────────────────────────────────────────────────┤ Configuring debconf ├───────────────────────────────────────────────────────────┐                    
                   │ あなたが答えたい質問のうち、最低の優先度のものを選択してください。                                                                         │                    
                   │  - 「重要」は、ユーザが介在しないとシステムを破壊しかねないような項目用です。                                                              │                    
                   │    あなたが初心者か、あるいは急いでいるのであればこれを選んでください。                                                                    │                    
                   │  - 「高」は、適切なデフォルトの回答がないような項目用です。                                                                                │                    
                   │  - 「中」は、適切なデフォルトの回答があるような普通の項目用です。                                                                          │                    
                   │  - 「低」は、ほとんどの場合にデフォルトの回答でかまわないような、ささいな項目用です。                                                      │                    
                   │                                                                                                                                            │                    
                   │                                                                                                                                            │                    
                   │ 注意: ここで何を選択しても、以前に行った質問は dpkg-reconfigure プログラムを使用して表示できます。                                         │                    
                   │                                                                                                                                            │                    
                   │ より低い優先度の質問を無視:                                                                                                                │                    
                   │                                                                                                                                            │                    
                   │                                                                    重要                                                                    │                    
                   │                                                                    高                                                                      │                    
                   │                                                                    中                                                                      │                    
                   │                                                                    低                                                                      │                    
                   │                                                                                                                                            │                    
                   │                                                                                                                                            │                    
                   │                                         <了解>                                           <取消>                                            │                    
                   │                                                                                                                                            │                    
                   └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘      

GNU screenの日本語環境でncursesを使うと表示が乱れる

GNU screenの日本語環境でncursesを使うと表示が乱れます。

環境

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial
$ screen -v
Screen version 4.03.01 (GNU) 28-Jun-15
$ echo $LANG
ja_JP.UTF-8
$ 

表示が乱れる

screenを起動してncursesのサンプルを動かすと表示が乱れます。

サンプルは、こちらのコードを使わせていただきました。

$ screen
$ ./curwin1
                            ┌─────────────────────┐
     │   Move the window   │
   │ with the arrow keys │
 │     or HOME/END     │
│                    │
│  Press 'q' to quit  │
└─────────────────────┘

GNU screenの設定

下記の設定を`.screenrcに追加します。

$ tail -1 ~/.screenrc 
cjkwidth off
$

screenを再起動すると、きれいに表示されるようになりました。

日本語表示/入力とも問題ないようです。今のところ副作用はないです。

$ screen
$ ./curwin1

                            ┌─────────────────────┐
                            │   Move the window   │
                            │ with the arrow keys │
                            │     or HOME/END     │
                            │                     │
                            │  Press 'q' to quit  │
                            └─────────────────────┘

dockerでgnu screenを使う

dockerコンテナでGNU screenを使えるようにしてみました。

環境

試した環境は、以下の通りです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:    16.04
Codename:   denial
$ docker --version
Docker version 1.12.2, build bb80604
$ sudo docker run -it -d ubuntu:16.04 bash
$

screenのインストールと起動

コンテナに接続してscreenを起動するとttyがないので起動できません。

$ sudo docker exec -it ecstatic_williams bash
root@d82a18f67359:/# apt update && apt install -y screen
root@d82a18f67359:/# screen
Must be connected to a terminal.
root@d82a18f67359:/# tty
not a tty
root@d82a18f67359:/# 

ttyを設定

ngettyパッケージでgettyttyを設定します。

root@d82a18f67359:/# apt install -y ngetty
root@d82a18f67359:/# useradd -m test
root@d82a18f67359:/# passwd test
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@d82a18f67359:/# getty tty 

Ubuntu 16.04.1 LTS d82a18f67359 tty

d82a18f67359 login: test
Password: 
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-45-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

test@d82a18f67359:~$ tty
/dev/tty
test@d82a18f67359:~$ 

ttyが設定されたのでscreenも無事起動しました。

test@d82a18f67359:~$ screen
Screen version 4.03.01 (GNU) 28-Jun-15

Copyright (c) 2010 Juergen Weigert, Sadrul Habib Chowdhury
Copyright (c) 2008, 2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib Chowdhury
Copyright (c) 1993-2002, 2003, 2005, 2006, 2007 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either
version 3, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, see http://www.gnu.org/licenses/, or contact Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA.

Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to screen-devel@gnu.org


Capabilities:
+copy +remote-detach +power-detach +multi-attach +multi-user +font +color-256 +utf8 +rxvt +builtin-telnet